blob: c0fbb6c306b2c4e6f8e9c5c8ce4e6385a88dcd5d [file] [log] [blame]
Joe Thornber991d9fa2011-10-31 20:21:18 +00001/*
Joe Thornbere49e5822012-07-27 15:08:16 +01002 * Copyright (C) 2011-2012 Red Hat UK.
Joe Thornber991d9fa2011-10-31 20:21:18 +00003 *
4 * This file is released under the GPL.
5 */
6
7#include "dm-thin-metadata.h"
Mike Snitzer4f81a412012-10-12 21:02:13 +01008#include "dm-bio-prison.h"
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01009#include "dm.h"
Joe Thornber991d9fa2011-10-31 20:21:18 +000010
11#include <linux/device-mapper.h>
12#include <linux/dm-io.h>
13#include <linux/dm-kcopyd.h>
Manuel Schölling0f30af92014-05-22 22:42:37 +020014#include <linux/jiffies.h>
Mike Snitzer604ea902014-10-09 18:43:25 -040015#include <linux/log2.h>
Joe Thornber991d9fa2011-10-31 20:21:18 +000016#include <linux/list.h>
Mike Snitzerc140e1c2014-03-20 21:17:14 -040017#include <linux/rculist.h>
Joe Thornber991d9fa2011-10-31 20:21:18 +000018#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/slab.h>
Joe Thornberac4c3f32014-10-10 16:42:10 +010021#include <linux/sort.h>
Mike Snitzer67324ea2014-03-21 18:33:41 -040022#include <linux/rbtree.h>
Joe Thornber991d9fa2011-10-31 20:21:18 +000023
24#define DM_MSG_PREFIX "thin"
25
26/*
27 * Tunable constants
28 */
Alasdair G Kergon7768ed32012-07-27 15:07:57 +010029#define ENDIO_HOOK_POOL_SIZE 1024
Joe Thornber991d9fa2011-10-31 20:21:18 +000030#define MAPPING_POOL_SIZE 1024
Joe Thornber905e51b2012-03-28 18:41:27 +010031#define COMMIT_PERIOD HZ
Mike Snitzer80c57892014-05-20 13:38:33 -040032#define NO_SPACE_TIMEOUT_SECS 60
33
34static unsigned no_space_timeout_secs = NO_SPACE_TIMEOUT_SECS;
Joe Thornber991d9fa2011-10-31 20:21:18 +000035
Mikulas Patockadf5d2e92013-03-01 22:45:49 +000036DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
37 "A percentage of time allocated for copy on write");
38
Joe Thornber991d9fa2011-10-31 20:21:18 +000039/*
40 * The block size of the device holding pool data must be
41 * between 64KB and 1GB.
42 */
43#define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
44#define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
45
46/*
Joe Thornber991d9fa2011-10-31 20:21:18 +000047 * Device id is restricted to 24 bits.
48 */
49#define MAX_DEV_ID ((1 << 24) - 1)
50
51/*
52 * How do we handle breaking sharing of data blocks?
53 * =================================================
54 *
55 * We use a standard copy-on-write btree to store the mappings for the
56 * devices (note I'm talking about copy-on-write of the metadata here, not
57 * the data). When you take an internal snapshot you clone the root node
58 * of the origin btree. After this there is no concept of an origin or a
59 * snapshot. They are just two device trees that happen to point to the
60 * same data blocks.
61 *
62 * When we get a write in we decide if it's to a shared data block using
63 * some timestamp magic. If it is, we have to break sharing.
64 *
65 * Let's say we write to a shared block in what was the origin. The
66 * steps are:
67 *
68 * i) plug io further to this physical block. (see bio_prison code).
69 *
70 * ii) quiesce any read io to that shared data block. Obviously
Mike Snitzer44feb382012-10-12 21:02:10 +010071 * including all devices that share this block. (see dm_deferred_set code)
Joe Thornber991d9fa2011-10-31 20:21:18 +000072 *
73 * iii) copy the data block to a newly allocate block. This step can be
74 * missed out if the io covers the block. (schedule_copy).
75 *
76 * iv) insert the new mapping into the origin's btree
Joe Thornberfe878f32012-03-28 18:41:24 +010077 * (process_prepared_mapping). This act of inserting breaks some
Joe Thornber991d9fa2011-10-31 20:21:18 +000078 * sharing of btree nodes between the two devices. Breaking sharing only
79 * effects the btree of that specific device. Btrees for the other
80 * devices that share the block never change. The btree for the origin
81 * device as it was after the last commit is untouched, ie. we're using
82 * persistent data structures in the functional programming sense.
83 *
84 * v) unplug io to this physical block, including the io that triggered
85 * the breaking of sharing.
86 *
87 * Steps (ii) and (iii) occur in parallel.
88 *
89 * The metadata _doesn't_ need to be committed before the io continues. We
90 * get away with this because the io is always written to a _new_ block.
91 * If there's a crash, then:
92 *
93 * - The origin mapping will point to the old origin block (the shared
94 * one). This will contain the data as it was before the io that triggered
95 * the breaking of sharing came in.
96 *
97 * - The snap mapping still points to the old block. As it would after
98 * the commit.
99 *
100 * The downside of this scheme is the timestamp magic isn't perfect, and
101 * will continue to think that data block in the snapshot device is shared
102 * even after the write to the origin has broken sharing. I suspect data
103 * blocks will typically be shared by many different devices, so we're
104 * breaking sharing n + 1 times, rather than n, where n is the number of
105 * devices that reference this data block. At the moment I think the
106 * benefits far, far outweigh the disadvantages.
107 */
108
109/*----------------------------------------------------------------*/
110
111/*
Joe Thornber991d9fa2011-10-31 20:21:18 +0000112 * Key building.
113 */
114static void build_data_key(struct dm_thin_device *td,
Mike Snitzer44feb382012-10-12 21:02:10 +0100115 dm_block_t b, struct dm_cell_key *key)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000116{
117 key->virtual = 0;
118 key->dev = dm_thin_dev_id(td);
Joe Thornber5f274d82014-09-17 10:17:39 +0100119 key->block_begin = b;
120 key->block_end = b + 1ULL;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000121}
122
123static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
Mike Snitzer44feb382012-10-12 21:02:10 +0100124 struct dm_cell_key *key)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000125{
126 key->virtual = 1;
127 key->dev = dm_thin_dev_id(td);
Joe Thornber5f274d82014-09-17 10:17:39 +0100128 key->block_begin = b;
129 key->block_end = b + 1ULL;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000130}
131
132/*----------------------------------------------------------------*/
133
Joe Thornber7d327fe2014-10-06 15:45:59 +0100134#define THROTTLE_THRESHOLD (1 * HZ)
135
136struct throttle {
137 struct rw_semaphore lock;
138 unsigned long threshold;
139 bool throttle_applied;
140};
141
142static void throttle_init(struct throttle *t)
143{
144 init_rwsem(&t->lock);
145 t->throttle_applied = false;
146}
147
148static void throttle_work_start(struct throttle *t)
149{
150 t->threshold = jiffies + THROTTLE_THRESHOLD;
151}
152
153static void throttle_work_update(struct throttle *t)
154{
155 if (!t->throttle_applied && jiffies > t->threshold) {
156 down_write(&t->lock);
157 t->throttle_applied = true;
158 }
159}
160
161static void throttle_work_complete(struct throttle *t)
162{
163 if (t->throttle_applied) {
164 t->throttle_applied = false;
165 up_write(&t->lock);
166 }
167}
168
169static void throttle_lock(struct throttle *t)
170{
171 down_read(&t->lock);
172}
173
174static void throttle_unlock(struct throttle *t)
175{
176 up_read(&t->lock);
177}
178
179/*----------------------------------------------------------------*/
180
Joe Thornber991d9fa2011-10-31 20:21:18 +0000181/*
182 * A pool device ties together a metadata device and a data device. It
183 * also provides the interface for creating and destroying internal
184 * devices.
185 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100186struct dm_thin_new_mapping;
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100187
Joe Thornbere49e5822012-07-27 15:08:16 +0100188/*
Joe Thornber3e1a0692014-03-03 16:03:26 +0000189 * The pool runs in 4 modes. Ordered in degraded order for comparisons.
Joe Thornbere49e5822012-07-27 15:08:16 +0100190 */
191enum pool_mode {
192 PM_WRITE, /* metadata may be changed */
Joe Thornber3e1a0692014-03-03 16:03:26 +0000193 PM_OUT_OF_DATA_SPACE, /* metadata may be changed, though data may not be allocated */
Joe Thornbere49e5822012-07-27 15:08:16 +0100194 PM_READ_ONLY, /* metadata may not be changed */
195 PM_FAIL, /* all I/O fails */
196};
197
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100198struct pool_features {
Joe Thornbere49e5822012-07-27 15:08:16 +0100199 enum pool_mode mode;
200
Mike Snitzer9bc142d2012-09-26 23:45:46 +0100201 bool zero_new_blocks:1;
202 bool discard_enabled:1;
203 bool discard_passdown:1;
Mike Snitzer787a996c2013-12-06 16:21:43 -0500204 bool error_if_no_space:1;
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100205};
206
Joe Thornbere49e5822012-07-27 15:08:16 +0100207struct thin_c;
208typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio);
Joe Thornbera374bb22014-10-10 13:43:14 +0100209typedef void (*process_cell_fn)(struct thin_c *tc, struct dm_bio_prison_cell *cell);
Joe Thornbere49e5822012-07-27 15:08:16 +0100210typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m);
211
Joe Thornberac4c3f32014-10-10 16:42:10 +0100212#define CELL_SORT_ARRAY_SIZE 8192
213
Joe Thornber991d9fa2011-10-31 20:21:18 +0000214struct pool {
215 struct list_head list;
216 struct dm_target *ti; /* Only set if a pool target is bound */
217
218 struct mapped_device *pool_md;
219 struct block_device *md_dev;
220 struct dm_pool_metadata *pmd;
221
Joe Thornber991d9fa2011-10-31 20:21:18 +0000222 dm_block_t low_water_blocks;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100223 uint32_t sectors_per_block;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100224 int sectors_per_block_shift;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000225
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100226 struct pool_features pf;
Joe Thornber88a66212013-12-04 20:16:12 -0500227 bool low_water_triggered:1; /* A dm event has been sent */
Mike Snitzer80e96c52014-11-07 15:09:46 -0500228 bool suspended:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000229
Mike Snitzer44feb382012-10-12 21:02:10 +0100230 struct dm_bio_prison *prison;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000231 struct dm_kcopyd_client *copier;
232
233 struct workqueue_struct *wq;
Joe Thornber7d327fe2014-10-06 15:45:59 +0100234 struct throttle throttle;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000235 struct work_struct worker;
Joe Thornber905e51b2012-03-28 18:41:27 +0100236 struct delayed_work waker;
Joe Thornber85ad643b2014-05-09 15:59:38 +0100237 struct delayed_work no_space_timeout;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000238
Joe Thornber905e51b2012-03-28 18:41:27 +0100239 unsigned long last_commit_jiffies;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100240 unsigned ref_count;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000241
242 spinlock_t lock;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000243 struct bio_list deferred_flush_bios;
244 struct list_head prepared_mappings;
Joe Thornber104655f2012-03-28 18:41:28 +0100245 struct list_head prepared_discards;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400246 struct list_head active_thins;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000247
Mike Snitzer44feb382012-10-12 21:02:10 +0100248 struct dm_deferred_set *shared_read_ds;
249 struct dm_deferred_set *all_io_ds;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000250
Mike Snitzera24c2562012-06-03 00:30:00 +0100251 struct dm_thin_new_mapping *next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000252 mempool_t *mapping_pool;
Joe Thornbere49e5822012-07-27 15:08:16 +0100253
254 process_bio_fn process_bio;
255 process_bio_fn process_discard;
256
Joe Thornbera374bb22014-10-10 13:43:14 +0100257 process_cell_fn process_cell;
258 process_cell_fn process_discard_cell;
259
Joe Thornbere49e5822012-07-27 15:08:16 +0100260 process_mapping_fn process_prepared_mapping;
261 process_mapping_fn process_prepared_discard;
Joe Thornberac4c3f32014-10-10 16:42:10 +0100262
263 struct dm_bio_prison_cell *cell_sort_array[CELL_SORT_ARRAY_SIZE];
Joe Thornber991d9fa2011-10-31 20:21:18 +0000264};
265
Joe Thornbere49e5822012-07-27 15:08:16 +0100266static enum pool_mode get_pool_mode(struct pool *pool);
Joe Thornberb5330652013-12-04 19:51:33 -0500267static void metadata_operation_failed(struct pool *pool, const char *op, int r);
Joe Thornbere49e5822012-07-27 15:08:16 +0100268
Joe Thornber991d9fa2011-10-31 20:21:18 +0000269/*
270 * Target context for a pool.
271 */
272struct pool_c {
273 struct dm_target *ti;
274 struct pool *pool;
275 struct dm_dev *data_dev;
276 struct dm_dev *metadata_dev;
277 struct dm_target_callbacks callbacks;
278
279 dm_block_t low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +0100280 struct pool_features requested_pf; /* Features requested during table load */
281 struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000282};
283
284/*
285 * Target context for a thin.
286 */
287struct thin_c {
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400288 struct list_head list;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000289 struct dm_dev *pool_dev;
Joe Thornber2dd9c252012-03-28 18:41:28 +0100290 struct dm_dev *origin_dev;
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100291 sector_t origin_size;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000292 dm_thin_id dev_id;
293
294 struct pool *pool;
295 struct dm_thin_device *td;
Mike Snitzer583024d2014-10-28 20:58:45 -0400296 struct mapped_device *thin_md;
297
Joe Thornber738211f2014-03-03 15:52:28 +0000298 bool requeue_mode:1;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400299 spinlock_t lock;
Joe Thornbera374bb22014-10-10 13:43:14 +0100300 struct list_head deferred_cells;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400301 struct bio_list deferred_bio_list;
302 struct bio_list retry_on_resume_list;
Mike Snitzer67324ea2014-03-21 18:33:41 -0400303 struct rb_root sort_bio_list; /* sorted list of deferred bios */
Joe Thornberb10ebd32014-04-08 11:29:01 +0100304
305 /*
306 * Ensures the thin is not destroyed until the worker has finished
307 * iterating the active_thins list.
308 */
309 atomic_t refcount;
310 struct completion can_destroy;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000311};
312
313/*----------------------------------------------------------------*/
314
Joe Thornber025b9682013-03-01 22:45:50 +0000315/*
316 * wake_worker() is used when new work is queued and when pool_resume is
317 * ready to continue deferred IO processing.
318 */
319static void wake_worker(struct pool *pool)
320{
321 queue_work(pool->wq, &pool->worker);
322}
323
324/*----------------------------------------------------------------*/
325
Joe Thornber6beca5e2013-03-01 22:45:50 +0000326static int bio_detain(struct pool *pool, struct dm_cell_key *key, struct bio *bio,
327 struct dm_bio_prison_cell **cell_result)
328{
329 int r;
330 struct dm_bio_prison_cell *cell_prealloc;
331
332 /*
333 * Allocate a cell from the prison's mempool.
334 * This might block but it can't fail.
335 */
336 cell_prealloc = dm_bio_prison_alloc_cell(pool->prison, GFP_NOIO);
337
338 r = dm_bio_detain(pool->prison, key, bio, cell_prealloc, cell_result);
339 if (r)
340 /*
341 * We reused an old cell; we can get rid of
342 * the new one.
343 */
344 dm_bio_prison_free_cell(pool->prison, cell_prealloc);
345
346 return r;
347}
348
349static void cell_release(struct pool *pool,
350 struct dm_bio_prison_cell *cell,
351 struct bio_list *bios)
352{
353 dm_cell_release(pool->prison, cell, bios);
354 dm_bio_prison_free_cell(pool->prison, cell);
355}
356
Joe Thornber2d759a42014-10-10 15:27:16 +0100357static void cell_visit_release(struct pool *pool,
358 void (*fn)(void *, struct dm_bio_prison_cell *),
359 void *context,
360 struct dm_bio_prison_cell *cell)
361{
362 dm_cell_visit_release(pool->prison, fn, context, cell);
363 dm_bio_prison_free_cell(pool->prison, cell);
364}
365
Joe Thornber6beca5e2013-03-01 22:45:50 +0000366static void cell_release_no_holder(struct pool *pool,
367 struct dm_bio_prison_cell *cell,
368 struct bio_list *bios)
369{
370 dm_cell_release_no_holder(pool->prison, cell, bios);
371 dm_bio_prison_free_cell(pool->prison, cell);
372}
373
Mike Snitzeraf918052014-05-22 14:32:51 -0400374static void cell_error_with_code(struct pool *pool,
375 struct dm_bio_prison_cell *cell, int error_code)
Joe Thornber6beca5e2013-03-01 22:45:50 +0000376{
Mike Snitzeraf918052014-05-22 14:32:51 -0400377 dm_cell_error(pool->prison, cell, error_code);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000378 dm_bio_prison_free_cell(pool->prison, cell);
379}
380
Mike Snitzeraf918052014-05-22 14:32:51 -0400381static void cell_error(struct pool *pool, struct dm_bio_prison_cell *cell)
382{
383 cell_error_with_code(pool, cell, -EIO);
384}
385
Joe Thornbera374bb22014-10-10 13:43:14 +0100386static void cell_success(struct pool *pool, struct dm_bio_prison_cell *cell)
387{
388 cell_error_with_code(pool, cell, 0);
389}
390
391static void cell_requeue(struct pool *pool, struct dm_bio_prison_cell *cell)
392{
393 cell_error_with_code(pool, cell, DM_ENDIO_REQUEUE);
394}
395
Joe Thornber6beca5e2013-03-01 22:45:50 +0000396/*----------------------------------------------------------------*/
397
Joe Thornber991d9fa2011-10-31 20:21:18 +0000398/*
399 * A global list of pools that uses a struct mapped_device as a key.
400 */
401static struct dm_thin_pool_table {
402 struct mutex mutex;
403 struct list_head pools;
404} dm_thin_pool_table;
405
406static void pool_table_init(void)
407{
408 mutex_init(&dm_thin_pool_table.mutex);
409 INIT_LIST_HEAD(&dm_thin_pool_table.pools);
410}
411
412static void __pool_table_insert(struct pool *pool)
413{
414 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
415 list_add(&pool->list, &dm_thin_pool_table.pools);
416}
417
418static void __pool_table_remove(struct pool *pool)
419{
420 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
421 list_del(&pool->list);
422}
423
424static struct pool *__pool_table_lookup(struct mapped_device *md)
425{
426 struct pool *pool = NULL, *tmp;
427
428 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
429
430 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
431 if (tmp->pool_md == md) {
432 pool = tmp;
433 break;
434 }
435 }
436
437 return pool;
438}
439
440static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
441{
442 struct pool *pool = NULL, *tmp;
443
444 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
445
446 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
447 if (tmp->md_dev == md_dev) {
448 pool = tmp;
449 break;
450 }
451 }
452
453 return pool;
454}
455
456/*----------------------------------------------------------------*/
457
Mike Snitzera24c2562012-06-03 00:30:00 +0100458struct dm_thin_endio_hook {
Joe Thornbereb2aa482012-03-28 18:41:28 +0100459 struct thin_c *tc;
Mike Snitzer44feb382012-10-12 21:02:10 +0100460 struct dm_deferred_entry *shared_read_entry;
461 struct dm_deferred_entry *all_io_entry;
Mike Snitzera24c2562012-06-03 00:30:00 +0100462 struct dm_thin_new_mapping *overwrite_mapping;
Mike Snitzer67324ea2014-03-21 18:33:41 -0400463 struct rb_node rb_node;
Joe Thornbereb2aa482012-03-28 18:41:28 +0100464};
465
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400466static void __merge_bio_list(struct bio_list *bios, struct bio_list *master)
467{
468 bio_list_merge(bios, master);
469 bio_list_init(master);
470}
471
472static void error_bio_list(struct bio_list *bios, int error)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000473{
474 struct bio *bio;
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400475
476 while ((bio = bio_list_pop(bios)))
477 bio_endio(bio, error);
478}
479
480static void error_thin_bio_list(struct thin_c *tc, struct bio_list *master, int error)
481{
Joe Thornber991d9fa2011-10-31 20:21:18 +0000482 struct bio_list bios;
Joe Thornber18adc572014-03-03 15:46:42 +0000483 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000484
485 bio_list_init(&bios);
Joe Thornber18adc572014-03-03 15:46:42 +0000486
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400487 spin_lock_irqsave(&tc->lock, flags);
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400488 __merge_bio_list(&bios, master);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400489 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000490
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400491 error_bio_list(&bios, error);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000492}
493
Joe Thornbera374bb22014-10-10 13:43:14 +0100494static void requeue_deferred_cells(struct thin_c *tc)
495{
496 struct pool *pool = tc->pool;
497 unsigned long flags;
498 struct list_head cells;
499 struct dm_bio_prison_cell *cell, *tmp;
500
501 INIT_LIST_HEAD(&cells);
502
503 spin_lock_irqsave(&tc->lock, flags);
504 list_splice_init(&tc->deferred_cells, &cells);
505 spin_unlock_irqrestore(&tc->lock, flags);
506
507 list_for_each_entry_safe(cell, tmp, &cells, user_list)
508 cell_requeue(pool, cell);
509}
510
Joe Thornber991d9fa2011-10-31 20:21:18 +0000511static void requeue_io(struct thin_c *tc)
512{
Joe Thornber3e1a0692014-03-03 16:03:26 +0000513 struct bio_list bios;
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400514 unsigned long flags;
Joe Thornber3e1a0692014-03-03 16:03:26 +0000515
516 bio_list_init(&bios);
517
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400518 spin_lock_irqsave(&tc->lock, flags);
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400519 __merge_bio_list(&bios, &tc->deferred_bio_list);
520 __merge_bio_list(&bios, &tc->retry_on_resume_list);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400521 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber3e1a0692014-03-03 16:03:26 +0000522
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400523 error_bio_list(&bios, DM_ENDIO_REQUEUE);
524 requeue_deferred_cells(tc);
Joe Thornber3e1a0692014-03-03 16:03:26 +0000525}
526
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400527static void error_retry_list(struct pool *pool)
528{
529 struct thin_c *tc;
530
531 rcu_read_lock();
532 list_for_each_entry_rcu(tc, &pool->active_thins, list)
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400533 error_thin_bio_list(tc, &tc->retry_on_resume_list, -EIO);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400534 rcu_read_unlock();
535}
536
Joe Thornber991d9fa2011-10-31 20:21:18 +0000537/*
538 * This section of code contains the logic for processing a thin device's IO.
539 * Much of the code depends on pool object resources (lists, workqueues, etc)
540 * but most is exclusively called from the thin target rather than the thin-pool
541 * target.
542 */
543
Mike Snitzer58f77a22013-03-01 22:45:45 +0000544static bool block_size_is_power_of_two(struct pool *pool)
545{
546 return pool->sectors_per_block_shift >= 0;
547}
548
Joe Thornber991d9fa2011-10-31 20:21:18 +0000549static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
550{
Mike Snitzer58f77a22013-03-01 22:45:45 +0000551 struct pool *pool = tc->pool;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700552 sector_t block_nr = bio->bi_iter.bi_sector;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100553
Mike Snitzer58f77a22013-03-01 22:45:45 +0000554 if (block_size_is_power_of_two(pool))
555 block_nr >>= pool->sectors_per_block_shift;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100556 else
Mike Snitzer58f77a22013-03-01 22:45:45 +0000557 (void) sector_div(block_nr, pool->sectors_per_block);
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100558
559 return block_nr;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000560}
561
562static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
563{
564 struct pool *pool = tc->pool;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700565 sector_t bi_sector = bio->bi_iter.bi_sector;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000566
567 bio->bi_bdev = tc->pool_dev->bdev;
Mike Snitzer58f77a22013-03-01 22:45:45 +0000568 if (block_size_is_power_of_two(pool))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700569 bio->bi_iter.bi_sector =
570 (block << pool->sectors_per_block_shift) |
571 (bi_sector & (pool->sectors_per_block - 1));
Mike Snitzer58f77a22013-03-01 22:45:45 +0000572 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700573 bio->bi_iter.bi_sector = (block * pool->sectors_per_block) +
Mike Snitzer58f77a22013-03-01 22:45:45 +0000574 sector_div(bi_sector, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000575}
576
Joe Thornber2dd9c252012-03-28 18:41:28 +0100577static void remap_to_origin(struct thin_c *tc, struct bio *bio)
578{
579 bio->bi_bdev = tc->origin_dev->bdev;
580}
581
Joe Thornber4afdd682012-07-27 15:08:14 +0100582static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
583{
584 return (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) &&
585 dm_thin_changed_this_transaction(tc->td);
586}
587
Joe Thornbere8088072012-12-21 20:23:31 +0000588static void inc_all_io_entry(struct pool *pool, struct bio *bio)
589{
590 struct dm_thin_endio_hook *h;
591
592 if (bio->bi_rw & REQ_DISCARD)
593 return;
594
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000595 h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbere8088072012-12-21 20:23:31 +0000596 h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
597}
598
Joe Thornber2dd9c252012-03-28 18:41:28 +0100599static void issue(struct thin_c *tc, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000600{
601 struct pool *pool = tc->pool;
602 unsigned long flags;
603
Joe Thornbere49e5822012-07-27 15:08:16 +0100604 if (!bio_triggers_commit(tc, bio)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000605 generic_make_request(bio);
Joe Thornbere49e5822012-07-27 15:08:16 +0100606 return;
607 }
608
609 /*
610 * Complete bio with an error if earlier I/O caused changes to
611 * the metadata that can't be committed e.g, due to I/O errors
612 * on the metadata device.
613 */
614 if (dm_thin_aborted_changes(tc->td)) {
615 bio_io_error(bio);
616 return;
617 }
618
619 /*
620 * Batch together any bios that trigger commits and then issue a
621 * single commit for them in process_deferred_bios().
622 */
623 spin_lock_irqsave(&pool->lock, flags);
624 bio_list_add(&pool->deferred_flush_bios, bio);
625 spin_unlock_irqrestore(&pool->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000626}
627
Joe Thornber2dd9c252012-03-28 18:41:28 +0100628static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
629{
630 remap_to_origin(tc, bio);
631 issue(tc, bio);
632}
633
634static void remap_and_issue(struct thin_c *tc, struct bio *bio,
635 dm_block_t block)
636{
637 remap(tc, bio, block);
638 issue(tc, bio);
639}
640
Joe Thornber991d9fa2011-10-31 20:21:18 +0000641/*----------------------------------------------------------------*/
642
643/*
644 * Bio endio functions.
645 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100646struct dm_thin_new_mapping {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000647 struct list_head list;
648
Mike Snitzer7f214662013-12-17 13:43:31 -0500649 bool pass_discard:1;
650 bool definitely_not_shared:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000651
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100652 /*
653 * Track quiescing, copying and zeroing preparation actions. When this
654 * counter hits zero the block is prepared and can be inserted into the
655 * btree.
656 */
657 atomic_t prepare_actions;
658
Mike Snitzer7f214662013-12-17 13:43:31 -0500659 int err;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000660 struct thin_c *tc;
661 dm_block_t virt_block;
662 dm_block_t data_block;
Mike Snitzera24c2562012-06-03 00:30:00 +0100663 struct dm_bio_prison_cell *cell, *cell2;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000664
665 /*
666 * If the bio covers the whole area of a block then we can avoid
667 * zeroing or copying. Instead this bio is hooked. The bio will
668 * still be in the cell, so care has to be taken to avoid issuing
669 * the bio twice.
670 */
671 struct bio *bio;
672 bio_end_io_t *saved_bi_end_io;
673};
674
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100675static void __complete_mapping_preparation(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000676{
677 struct pool *pool = m->tc->pool;
678
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100679 if (atomic_dec_and_test(&m->prepare_actions)) {
Mike Snitzerdaec3382013-12-11 14:01:20 -0500680 list_add_tail(&m->list, &pool->prepared_mappings);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000681 wake_worker(pool);
682 }
683}
684
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100685static void complete_mapping_preparation(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000686{
687 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000688 struct pool *pool = m->tc->pool;
689
Joe Thornber991d9fa2011-10-31 20:21:18 +0000690 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100691 __complete_mapping_preparation(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000692 spin_unlock_irqrestore(&pool->lock, flags);
693}
694
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100695static void copy_complete(int read_err, unsigned long write_err, void *context)
696{
697 struct dm_thin_new_mapping *m = context;
698
699 m->err = read_err || write_err ? -EIO : 0;
700 complete_mapping_preparation(m);
701}
702
Joe Thornber991d9fa2011-10-31 20:21:18 +0000703static void overwrite_endio(struct bio *bio, int err)
704{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000705 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Mike Snitzera24c2562012-06-03 00:30:00 +0100706 struct dm_thin_new_mapping *m = h->overwrite_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000707
Mike Snitzer8b908f82015-05-13 17:53:13 -0400708 bio->bi_end_io = m->saved_bi_end_io;
709
Joe Thornber991d9fa2011-10-31 20:21:18 +0000710 m->err = err;
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100711 complete_mapping_preparation(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000712}
713
Joe Thornber991d9fa2011-10-31 20:21:18 +0000714/*----------------------------------------------------------------*/
715
716/*
717 * Workqueue.
718 */
719
720/*
721 * Prepared mapping jobs.
722 */
723
724/*
Joe Thornber2d759a42014-10-10 15:27:16 +0100725 * This sends the bios in the cell, except the original holder, back
726 * to the deferred_bios list.
Joe Thornber991d9fa2011-10-31 20:21:18 +0000727 */
Joe Thornberf286ba02012-12-21 20:23:33 +0000728static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000729{
Joe Thornber991d9fa2011-10-31 20:21:18 +0000730 struct pool *pool = tc->pool;
731 unsigned long flags;
732
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400733 spin_lock_irqsave(&tc->lock, flags);
734 cell_release_no_holder(pool, cell, &tc->deferred_bio_list);
735 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000736
737 wake_worker(pool);
738}
739
Joe Thornbera374bb22014-10-10 13:43:14 +0100740static void thin_defer_bio(struct thin_c *tc, struct bio *bio);
741
Joe Thornber2d759a42014-10-10 15:27:16 +0100742struct remap_info {
743 struct thin_c *tc;
744 struct bio_list defer_bios;
745 struct bio_list issue_bios;
746};
747
748static void __inc_remap_and_issue_cell(void *context,
749 struct dm_bio_prison_cell *cell)
750{
751 struct remap_info *info = context;
752 struct bio *bio;
753
754 while ((bio = bio_list_pop(&cell->bios))) {
755 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA))
756 bio_list_add(&info->defer_bios, bio);
757 else {
758 inc_all_io_entry(info->tc->pool, bio);
759
760 /*
761 * We can't issue the bios with the bio prison lock
762 * held, so we add them to a list to issue on
763 * return from this function.
764 */
765 bio_list_add(&info->issue_bios, bio);
766 }
767 }
768}
769
Joe Thornbera374bb22014-10-10 13:43:14 +0100770static void inc_remap_and_issue_cell(struct thin_c *tc,
771 struct dm_bio_prison_cell *cell,
772 dm_block_t block)
773{
774 struct bio *bio;
Joe Thornber2d759a42014-10-10 15:27:16 +0100775 struct remap_info info;
Joe Thornbera374bb22014-10-10 13:43:14 +0100776
Joe Thornber2d759a42014-10-10 15:27:16 +0100777 info.tc = tc;
778 bio_list_init(&info.defer_bios);
779 bio_list_init(&info.issue_bios);
Joe Thornbera374bb22014-10-10 13:43:14 +0100780
Joe Thornber2d759a42014-10-10 15:27:16 +0100781 /*
782 * We have to be careful to inc any bios we're about to issue
783 * before the cell is released, and avoid a race with new bios
784 * being added to the cell.
785 */
786 cell_visit_release(tc->pool, __inc_remap_and_issue_cell,
787 &info, cell);
788
789 while ((bio = bio_list_pop(&info.defer_bios)))
790 thin_defer_bio(tc, bio);
791
792 while ((bio = bio_list_pop(&info.issue_bios)))
793 remap_and_issue(info.tc, bio, block);
Joe Thornbera374bb22014-10-10 13:43:14 +0100794}
795
Joe Thornbere49e5822012-07-27 15:08:16 +0100796static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
797{
Joe Thornber6beca5e2013-03-01 22:45:50 +0000798 cell_error(m->tc->pool, m->cell);
Joe Thornbere49e5822012-07-27 15:08:16 +0100799 list_del(&m->list);
800 mempool_free(m, m->tc->pool->mapping_pool);
801}
Joe Thornber025b9682013-03-01 22:45:50 +0000802
Mike Snitzera24c2562012-06-03 00:30:00 +0100803static void process_prepared_mapping(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000804{
805 struct thin_c *tc = m->tc;
Joe Thornber6beca5e2013-03-01 22:45:50 +0000806 struct pool *pool = tc->pool;
Mike Snitzer8b908f82015-05-13 17:53:13 -0400807 struct bio *bio = m->bio;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000808 int r;
809
Joe Thornber991d9fa2011-10-31 20:21:18 +0000810 if (m->err) {
Joe Thornber6beca5e2013-03-01 22:45:50 +0000811 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100812 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000813 }
814
815 /*
816 * Commit the prepared block into the mapping btree.
817 * Any I/O for this block arriving after this point will get
818 * remapped to it directly.
819 */
820 r = dm_thin_insert_block(tc->td, m->virt_block, m->data_block);
821 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -0500822 metadata_operation_failed(pool, "dm_thin_insert_block", r);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000823 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100824 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000825 }
826
827 /*
828 * Release any bios held while the block was being provisioned.
829 * If we are processing a write bio that completely covers the block,
830 * we already processed it so can ignore it now when processing
831 * the bios in the cell.
832 */
833 if (bio) {
Joe Thornber2d759a42014-10-10 15:27:16 +0100834 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000835 bio_endio(bio, 0);
Joe Thornber2d759a42014-10-10 15:27:16 +0100836 } else {
837 inc_all_io_entry(tc->pool, m->cell->holder);
838 remap_and_issue(tc, m->cell->holder, m->data_block);
839 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
840 }
Joe Thornber991d9fa2011-10-31 20:21:18 +0000841
Joe Thornber905386f2012-07-27 15:08:05 +0100842out:
Joe Thornber991d9fa2011-10-31 20:21:18 +0000843 list_del(&m->list);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000844 mempool_free(m, pool->mapping_pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000845}
846
Joe Thornbere49e5822012-07-27 15:08:16 +0100847static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
Joe Thornber104655f2012-03-28 18:41:28 +0100848{
Joe Thornber104655f2012-03-28 18:41:28 +0100849 struct thin_c *tc = m->tc;
850
Joe Thornbere49e5822012-07-27 15:08:16 +0100851 bio_io_error(m->bio);
Joe Thornberf286ba02012-12-21 20:23:33 +0000852 cell_defer_no_holder(tc, m->cell);
853 cell_defer_no_holder(tc, m->cell2);
Joe Thornbere49e5822012-07-27 15:08:16 +0100854 mempool_free(m, tc->pool->mapping_pool);
855}
Joe Thornber104655f2012-03-28 18:41:28 +0100856
Joe Thornbere49e5822012-07-27 15:08:16 +0100857static void process_prepared_discard_passdown(struct dm_thin_new_mapping *m)
858{
859 struct thin_c *tc = m->tc;
860
Joe Thornbere8088072012-12-21 20:23:31 +0000861 inc_all_io_entry(tc->pool, m->bio);
Joe Thornberf286ba02012-12-21 20:23:33 +0000862 cell_defer_no_holder(tc, m->cell);
863 cell_defer_no_holder(tc, m->cell2);
Joe Thornbere8088072012-12-21 20:23:31 +0000864
Joe Thornber104655f2012-03-28 18:41:28 +0100865 if (m->pass_discard)
Joe Thornber19fa1a62013-12-17 12:09:40 -0500866 if (m->definitely_not_shared)
867 remap_and_issue(tc, m->bio, m->data_block);
868 else {
869 bool used = false;
870 if (dm_pool_block_is_used(tc->pool->pmd, m->data_block, &used) || used)
871 bio_endio(m->bio, 0);
872 else
873 remap_and_issue(tc, m->bio, m->data_block);
874 }
Joe Thornber104655f2012-03-28 18:41:28 +0100875 else
876 bio_endio(m->bio, 0);
877
Joe Thornber104655f2012-03-28 18:41:28 +0100878 mempool_free(m, tc->pool->mapping_pool);
879}
880
Joe Thornbere49e5822012-07-27 15:08:16 +0100881static void process_prepared_discard(struct dm_thin_new_mapping *m)
882{
883 int r;
884 struct thin_c *tc = m->tc;
885
886 r = dm_thin_remove_block(tc->td, m->virt_block);
887 if (r)
Mike Snitzerc3977412012-12-21 20:23:34 +0000888 DMERR_LIMIT("dm_thin_remove_block() failed");
Joe Thornbere49e5822012-07-27 15:08:16 +0100889
890 process_prepared_discard_passdown(m);
891}
892
Joe Thornber104655f2012-03-28 18:41:28 +0100893static void process_prepared(struct pool *pool, struct list_head *head,
Joe Thornbere49e5822012-07-27 15:08:16 +0100894 process_mapping_fn *fn)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000895{
896 unsigned long flags;
897 struct list_head maps;
Mike Snitzera24c2562012-06-03 00:30:00 +0100898 struct dm_thin_new_mapping *m, *tmp;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000899
900 INIT_LIST_HEAD(&maps);
901 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +0100902 list_splice_init(head, &maps);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000903 spin_unlock_irqrestore(&pool->lock, flags);
904
905 list_for_each_entry_safe(m, tmp, &maps, list)
Joe Thornbere49e5822012-07-27 15:08:16 +0100906 (*fn)(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000907}
908
909/*
910 * Deferred bio jobs.
911 */
Joe Thornber104655f2012-03-28 18:41:28 +0100912static int io_overlaps_block(struct pool *pool, struct bio *bio)
913{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700914 return bio->bi_iter.bi_size ==
915 (pool->sectors_per_block << SECTOR_SHIFT);
Joe Thornber104655f2012-03-28 18:41:28 +0100916}
917
Joe Thornber991d9fa2011-10-31 20:21:18 +0000918static int io_overwrites_block(struct pool *pool, struct bio *bio)
919{
Joe Thornber104655f2012-03-28 18:41:28 +0100920 return (bio_data_dir(bio) == WRITE) &&
921 io_overlaps_block(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000922}
923
924static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
925 bio_end_io_t *fn)
926{
927 *save = bio->bi_end_io;
928 bio->bi_end_io = fn;
929}
930
931static int ensure_next_mapping(struct pool *pool)
932{
933 if (pool->next_mapping)
934 return 0;
935
936 pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
937
938 return pool->next_mapping ? 0 : -ENOMEM;
939}
940
Mike Snitzera24c2562012-06-03 00:30:00 +0100941static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000942{
Mike Snitzer16961b02013-12-17 13:19:11 -0500943 struct dm_thin_new_mapping *m = pool->next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000944
945 BUG_ON(!pool->next_mapping);
946
Mike Snitzer16961b02013-12-17 13:19:11 -0500947 memset(m, 0, sizeof(struct dm_thin_new_mapping));
948 INIT_LIST_HEAD(&m->list);
949 m->bio = NULL;
950
Joe Thornber991d9fa2011-10-31 20:21:18 +0000951 pool->next_mapping = NULL;
952
Mike Snitzer16961b02013-12-17 13:19:11 -0500953 return m;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000954}
955
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100956static void ll_zero(struct thin_c *tc, struct dm_thin_new_mapping *m,
957 sector_t begin, sector_t end)
958{
959 int r;
960 struct dm_io_region to;
961
962 to.bdev = tc->pool_dev->bdev;
963 to.sector = begin;
964 to.count = end - begin;
965
966 r = dm_kcopyd_zero(tc->pool->copier, 1, &to, 0, copy_complete, m);
967 if (r < 0) {
968 DMERR_LIMIT("dm_kcopyd_zero() failed");
969 copy_complete(1, 1, m);
970 }
971}
972
Mike Snitzer452d7a62014-10-09 19:20:21 -0400973static void remap_and_issue_overwrite(struct thin_c *tc, struct bio *bio,
974 dm_block_t data_block,
975 struct dm_thin_new_mapping *m)
976{
977 struct pool *pool = tc->pool;
978 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
979
980 h->overwrite_mapping = m;
981 m->bio = bio;
982 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
983 inc_all_io_entry(pool, bio);
984 remap_and_issue(tc, bio, data_block);
985}
986
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100987/*
988 * A partial copy also needs to zero the uncopied region.
989 */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000990static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
Joe Thornber2dd9c252012-03-28 18:41:28 +0100991 struct dm_dev *origin, dm_block_t data_origin,
992 dm_block_t data_dest,
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100993 struct dm_bio_prison_cell *cell, struct bio *bio,
994 sector_t len)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000995{
996 int r;
997 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +0100998 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000999
Joe Thornber991d9fa2011-10-31 20:21:18 +00001000 m->tc = tc;
1001 m->virt_block = virt_block;
1002 m->data_block = data_dest;
1003 m->cell = cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001004
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001005 /*
1006 * quiesce action + copy action + an extra reference held for the
1007 * duration of this function (we may need to inc later for a
1008 * partial zero).
1009 */
1010 atomic_set(&m->prepare_actions, 3);
1011
Mike Snitzer44feb382012-10-12 21:02:10 +01001012 if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001013 complete_mapping_preparation(m); /* already quiesced */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001014
1015 /*
1016 * IO to pool_dev remaps to the pool target's data_dev.
1017 *
1018 * If the whole block of data is being overwritten, we can issue the
1019 * bio immediately. Otherwise we use kcopyd to clone the data first.
1020 */
Mike Snitzer452d7a62014-10-09 19:20:21 -04001021 if (io_overwrites_block(pool, bio))
1022 remap_and_issue_overwrite(tc, bio, data_dest, m);
1023 else {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001024 struct dm_io_region from, to;
1025
Joe Thornber2dd9c252012-03-28 18:41:28 +01001026 from.bdev = origin->bdev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001027 from.sector = data_origin * pool->sectors_per_block;
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001028 from.count = len;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001029
1030 to.bdev = tc->pool_dev->bdev;
1031 to.sector = data_dest * pool->sectors_per_block;
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001032 to.count = len;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001033
1034 r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
1035 0, copy_complete, m);
1036 if (r < 0) {
Mike Snitzerc3977412012-12-21 20:23:34 +00001037 DMERR_LIMIT("dm_kcopyd_copy() failed");
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001038 copy_complete(1, 1, m);
1039
1040 /*
1041 * We allow the zero to be issued, to simplify the
1042 * error path. Otherwise we'd need to start
1043 * worrying about decrementing the prepare_actions
1044 * counter.
1045 */
1046 }
1047
1048 /*
1049 * Do we need to zero a tail region?
1050 */
1051 if (len < pool->sectors_per_block && pool->pf.zero_new_blocks) {
1052 atomic_inc(&m->prepare_actions);
1053 ll_zero(tc, m,
1054 data_dest * pool->sectors_per_block + len,
1055 (data_dest + 1) * pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001056 }
1057 }
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001058
1059 complete_mapping_preparation(m); /* drop our ref */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001060}
1061
Joe Thornber2dd9c252012-03-28 18:41:28 +01001062static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
1063 dm_block_t data_origin, dm_block_t data_dest,
Mike Snitzera24c2562012-06-03 00:30:00 +01001064 struct dm_bio_prison_cell *cell, struct bio *bio)
Joe Thornber2dd9c252012-03-28 18:41:28 +01001065{
1066 schedule_copy(tc, virt_block, tc->pool_dev,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001067 data_origin, data_dest, cell, bio,
1068 tc->pool->sectors_per_block);
Joe Thornber2dd9c252012-03-28 18:41:28 +01001069}
1070
Joe Thornber991d9fa2011-10-31 20:21:18 +00001071static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
Mike Snitzera24c2562012-06-03 00:30:00 +01001072 dm_block_t data_block, struct dm_bio_prison_cell *cell,
Joe Thornber991d9fa2011-10-31 20:21:18 +00001073 struct bio *bio)
1074{
1075 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +01001076 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001077
Joe Thornber50f3c3e2014-06-13 13:57:09 +01001078 atomic_set(&m->prepare_actions, 1); /* no need to quiesce */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001079 m->tc = tc;
1080 m->virt_block = virt_block;
1081 m->data_block = data_block;
1082 m->cell = cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001083
1084 /*
1085 * If the whole block of data is being overwritten or we are not
1086 * zeroing pre-existing data, we can issue the bio immediately.
1087 * Otherwise we use kcopyd to zero the data first.
1088 */
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001089 if (!pool->pf.zero_new_blocks)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001090 process_prepared_mapping(m);
1091
Mike Snitzer452d7a62014-10-09 19:20:21 -04001092 else if (io_overwrites_block(pool, bio))
1093 remap_and_issue_overwrite(tc, bio, data_block, m);
Mike Snitzera24c2562012-06-03 00:30:00 +01001094
Mike Snitzer452d7a62014-10-09 19:20:21 -04001095 else
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001096 ll_zero(tc, m,
1097 data_block * pool->sectors_per_block,
1098 (data_block + 1) * pool->sectors_per_block);
1099}
Joe Thornber991d9fa2011-10-31 20:21:18 +00001100
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001101static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
1102 dm_block_t data_dest,
1103 struct dm_bio_prison_cell *cell, struct bio *bio)
1104{
1105 struct pool *pool = tc->pool;
1106 sector_t virt_block_begin = virt_block * pool->sectors_per_block;
1107 sector_t virt_block_end = (virt_block + 1) * pool->sectors_per_block;
1108
1109 if (virt_block_end <= tc->origin_size)
1110 schedule_copy(tc, virt_block, tc->origin_dev,
1111 virt_block, data_dest, cell, bio,
1112 pool->sectors_per_block);
1113
1114 else if (virt_block_begin < tc->origin_size)
1115 schedule_copy(tc, virt_block, tc->origin_dev,
1116 virt_block, data_dest, cell, bio,
1117 tc->origin_size - virt_block_begin);
1118
1119 else
1120 schedule_zero(tc, virt_block, data_dest, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001121}
1122
Joe Thornber2c43fd22014-12-11 11:12:19 +00001123static void set_pool_mode(struct pool *pool, enum pool_mode new_mode);
1124
1125static void check_for_space(struct pool *pool)
1126{
1127 int r;
1128 dm_block_t nr_free;
1129
1130 if (get_pool_mode(pool) != PM_OUT_OF_DATA_SPACE)
1131 return;
1132
1133 r = dm_pool_get_free_block_count(pool->pmd, &nr_free);
1134 if (r)
1135 return;
1136
1137 if (nr_free)
1138 set_pool_mode(pool, PM_WRITE);
1139}
1140
Joe Thornbere49e5822012-07-27 15:08:16 +01001141/*
1142 * A non-zero return indicates read_only or fail_io mode.
1143 * Many callers don't care about the return value.
1144 */
Joe Thornber020cc3b2013-12-04 15:05:36 -05001145static int commit(struct pool *pool)
Joe Thornbere49e5822012-07-27 15:08:16 +01001146{
1147 int r;
1148
Joe Thornber8d07e8a2014-05-06 16:28:14 +01001149 if (get_pool_mode(pool) >= PM_READ_ONLY)
Joe Thornbere49e5822012-07-27 15:08:16 +01001150 return -EINVAL;
1151
Joe Thornber020cc3b2013-12-04 15:05:36 -05001152 r = dm_pool_commit_metadata(pool->pmd);
Joe Thornberb5330652013-12-04 19:51:33 -05001153 if (r)
1154 metadata_operation_failed(pool, "dm_pool_commit_metadata", r);
Joe Thornber2c43fd22014-12-11 11:12:19 +00001155 else
1156 check_for_space(pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01001157
1158 return r;
1159}
1160
Joe Thornber88a66212013-12-04 20:16:12 -05001161static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks)
1162{
1163 unsigned long flags;
1164
1165 if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
1166 DMWARN("%s: reached low water mark for data device: sending event.",
1167 dm_device_name(pool->pool_md));
1168 spin_lock_irqsave(&pool->lock, flags);
1169 pool->low_water_triggered = true;
1170 spin_unlock_irqrestore(&pool->lock, flags);
1171 dm_table_event(pool->ti->table);
1172 }
1173}
1174
Joe Thornber991d9fa2011-10-31 20:21:18 +00001175static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
1176{
1177 int r;
1178 dm_block_t free_blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001179 struct pool *pool = tc->pool;
1180
Joe Thornber3e1a0692014-03-03 16:03:26 +00001181 if (WARN_ON(get_pool_mode(pool) != PM_WRITE))
Joe Thornber8d30abf2013-12-04 19:16:11 -05001182 return -EINVAL;
1183
Joe Thornber991d9fa2011-10-31 20:21:18 +00001184 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
Joe Thornberb5330652013-12-04 19:51:33 -05001185 if (r) {
1186 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001187 return r;
Joe Thornberb5330652013-12-04 19:51:33 -05001188 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001189
Joe Thornber88a66212013-12-04 20:16:12 -05001190 check_low_water_mark(pool, free_blocks);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001191
1192 if (!free_blocks) {
Mike Snitzer94563ba2013-08-22 09:56:18 -04001193 /*
1194 * Try to commit to see if that will free up some
1195 * more space.
1196 */
Joe Thornber020cc3b2013-12-04 15:05:36 -05001197 r = commit(pool);
1198 if (r)
1199 return r;
Mike Snitzer94563ba2013-08-22 09:56:18 -04001200
1201 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
Joe Thornberb5330652013-12-04 19:51:33 -05001202 if (r) {
1203 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
Mike Snitzer94563ba2013-08-22 09:56:18 -04001204 return r;
Joe Thornberb5330652013-12-04 19:51:33 -05001205 }
Mike Snitzer94563ba2013-08-22 09:56:18 -04001206
Mike Snitzer94563ba2013-08-22 09:56:18 -04001207 if (!free_blocks) {
Joe Thornber3e1a0692014-03-03 16:03:26 +00001208 set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001209 return -ENOSPC;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001210 }
1211 }
1212
1213 r = dm_pool_alloc_data_block(pool->pmd, result);
Mike Snitzer4a02b342013-12-03 12:20:57 -05001214 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05001215 metadata_operation_failed(pool, "dm_pool_alloc_data_block", r);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001216 return r;
Mike Snitzer4a02b342013-12-03 12:20:57 -05001217 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001218
1219 return 0;
1220}
1221
1222/*
1223 * If we have run out of space, queue bios until the device is
1224 * resumed, presumably after having been reloaded with more space.
1225 */
1226static void retry_on_resume(struct bio *bio)
1227{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001228 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01001229 struct thin_c *tc = h->tc;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001230 unsigned long flags;
1231
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001232 spin_lock_irqsave(&tc->lock, flags);
1233 bio_list_add(&tc->retry_on_resume_list, bio);
1234 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001235}
1236
Mike Snitzeraf918052014-05-22 14:32:51 -04001237static int should_error_unserviceable_bio(struct pool *pool)
Joe Thornber3e1a0692014-03-03 16:03:26 +00001238{
1239 enum pool_mode m = get_pool_mode(pool);
1240
1241 switch (m) {
1242 case PM_WRITE:
1243 /* Shouldn't get here */
1244 DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
Mike Snitzeraf918052014-05-22 14:32:51 -04001245 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001246
1247 case PM_OUT_OF_DATA_SPACE:
Mike Snitzeraf918052014-05-22 14:32:51 -04001248 return pool->pf.error_if_no_space ? -ENOSPC : 0;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001249
1250 case PM_READ_ONLY:
1251 case PM_FAIL:
Mike Snitzeraf918052014-05-22 14:32:51 -04001252 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001253 default:
1254 /* Shouldn't get here */
1255 DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
Mike Snitzeraf918052014-05-22 14:32:51 -04001256 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001257 }
1258}
1259
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001260static void handle_unserviceable_bio(struct pool *pool, struct bio *bio)
1261{
Mike Snitzeraf918052014-05-22 14:32:51 -04001262 int error = should_error_unserviceable_bio(pool);
1263
1264 if (error)
1265 bio_endio(bio, error);
Mike Snitzer6d162022013-12-20 18:09:02 -05001266 else
1267 retry_on_resume(bio);
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001268}
1269
Mike Snitzer399cadd2013-12-05 16:03:33 -05001270static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001271{
1272 struct bio *bio;
1273 struct bio_list bios;
Mike Snitzeraf918052014-05-22 14:32:51 -04001274 int error;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001275
Mike Snitzeraf918052014-05-22 14:32:51 -04001276 error = should_error_unserviceable_bio(pool);
1277 if (error) {
1278 cell_error_with_code(pool, cell, error);
Joe Thornber3e1a0692014-03-03 16:03:26 +00001279 return;
1280 }
1281
Joe Thornber991d9fa2011-10-31 20:21:18 +00001282 bio_list_init(&bios);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001283 cell_release(pool, cell, &bios);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001284
Mike Snitzer9d094ee2014-10-19 08:23:09 -04001285 while ((bio = bio_list_pop(&bios)))
1286 retry_on_resume(bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001287}
1288
Joe Thornbera374bb22014-10-10 13:43:14 +01001289static void process_discard_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber104655f2012-03-28 18:41:28 +01001290{
1291 int r;
Joe Thornbera374bb22014-10-10 13:43:14 +01001292 struct bio *bio = cell->holder;
Joe Thornber104655f2012-03-28 18:41:28 +01001293 struct pool *pool = tc->pool;
Joe Thornbera374bb22014-10-10 13:43:14 +01001294 struct dm_bio_prison_cell *cell2;
1295 struct dm_cell_key key2;
Joe Thornber104655f2012-03-28 18:41:28 +01001296 dm_block_t block = get_bio_block(tc, bio);
1297 struct dm_thin_lookup_result lookup_result;
Mike Snitzera24c2562012-06-03 00:30:00 +01001298 struct dm_thin_new_mapping *m;
Joe Thornber104655f2012-03-28 18:41:28 +01001299
Joe Thornbera374bb22014-10-10 13:43:14 +01001300 if (tc->requeue_mode) {
1301 cell_requeue(pool, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001302 return;
Joe Thornbera374bb22014-10-10 13:43:14 +01001303 }
Joe Thornber104655f2012-03-28 18:41:28 +01001304
1305 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1306 switch (r) {
1307 case 0:
1308 /*
1309 * Check nobody is fiddling with this pool block. This can
1310 * happen if someone's in the process of breaking sharing
1311 * on this block.
1312 */
1313 build_data_key(tc->td, lookup_result.block, &key2);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001314 if (bio_detain(tc->pool, &key2, bio, &cell2)) {
Joe Thornberf286ba02012-12-21 20:23:33 +00001315 cell_defer_no_holder(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001316 break;
1317 }
1318
1319 if (io_overlaps_block(pool, bio)) {
1320 /*
1321 * IO may still be going to the destination block. We must
1322 * quiesce before we can do the removal.
1323 */
1324 m = get_next_mapping(pool);
1325 m->tc = tc;
Joe Thornber19fa1a62013-12-17 12:09:40 -05001326 m->pass_discard = pool->pf.discard_passdown;
1327 m->definitely_not_shared = !lookup_result.shared;
Joe Thornber104655f2012-03-28 18:41:28 +01001328 m->virt_block = block;
1329 m->data_block = lookup_result.block;
1330 m->cell = cell;
1331 m->cell2 = cell2;
Joe Thornber104655f2012-03-28 18:41:28 +01001332 m->bio = bio;
1333
Joe Thornber7a7e97c2014-09-12 11:34:01 +01001334 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
1335 pool->process_prepared_discard(m);
1336
Joe Thornber104655f2012-03-28 18:41:28 +01001337 } else {
Joe Thornbere8088072012-12-21 20:23:31 +00001338 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001339 cell_defer_no_holder(tc, cell);
1340 cell_defer_no_holder(tc, cell2);
Joe Thornbere8088072012-12-21 20:23:31 +00001341
Joe Thornber104655f2012-03-28 18:41:28 +01001342 /*
Mikulas Patocka49296302012-07-27 15:08:03 +01001343 * The DM core makes sure that the discard doesn't span
1344 * a block boundary. So we submit the discard of a
1345 * partial block appropriately.
Joe Thornber104655f2012-03-28 18:41:28 +01001346 */
Mikulas Patocka650d2a02012-07-20 14:25:05 +01001347 if ((!lookup_result.shared) && pool->pf.discard_passdown)
1348 remap_and_issue(tc, bio, lookup_result.block);
1349 else
1350 bio_endio(bio, 0);
Joe Thornber104655f2012-03-28 18:41:28 +01001351 }
1352 break;
1353
1354 case -ENODATA:
1355 /*
1356 * It isn't provisioned, just forget it.
1357 */
Joe Thornberf286ba02012-12-21 20:23:33 +00001358 cell_defer_no_holder(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001359 bio_endio(bio, 0);
1360 break;
1361
1362 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001363 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1364 __func__, r);
Joe Thornberf286ba02012-12-21 20:23:33 +00001365 cell_defer_no_holder(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001366 bio_io_error(bio);
1367 break;
1368 }
1369}
1370
Joe Thornbera374bb22014-10-10 13:43:14 +01001371static void process_discard_bio(struct thin_c *tc, struct bio *bio)
1372{
1373 struct dm_bio_prison_cell *cell;
1374 struct dm_cell_key key;
1375 dm_block_t block = get_bio_block(tc, bio);
1376
1377 build_virtual_key(tc->td, block, &key);
1378 if (bio_detain(tc->pool, &key, bio, &cell))
1379 return;
1380
1381 process_discard_cell(tc, cell);
1382}
1383
Joe Thornber991d9fa2011-10-31 20:21:18 +00001384static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzer44feb382012-10-12 21:02:10 +01001385 struct dm_cell_key *key,
Joe Thornber991d9fa2011-10-31 20:21:18 +00001386 struct dm_thin_lookup_result *lookup_result,
Mike Snitzera24c2562012-06-03 00:30:00 +01001387 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001388{
1389 int r;
1390 dm_block_t data_block;
Mike Snitzerd6fc2042013-08-21 17:40:11 -04001391 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001392
1393 r = alloc_data_block(tc, &data_block);
1394 switch (r) {
1395 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001396 schedule_internal_copy(tc, block, lookup_result->block,
1397 data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001398 break;
1399
1400 case -ENOSPC:
Mike Snitzer399cadd2013-12-05 16:03:33 -05001401 retry_bios_on_resume(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001402 break;
1403
1404 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001405 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1406 __func__, r);
Mike Snitzerd6fc2042013-08-21 17:40:11 -04001407 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001408 break;
1409 }
1410}
1411
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001412static void __remap_and_issue_shared_cell(void *context,
1413 struct dm_bio_prison_cell *cell)
1414{
1415 struct remap_info *info = context;
1416 struct bio *bio;
1417
1418 while ((bio = bio_list_pop(&cell->bios))) {
1419 if ((bio_data_dir(bio) == WRITE) ||
1420 (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)))
1421 bio_list_add(&info->defer_bios, bio);
1422 else {
1423 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));;
1424
1425 h->shared_read_entry = dm_deferred_entry_inc(info->tc->pool->shared_read_ds);
1426 inc_all_io_entry(info->tc->pool, bio);
1427 bio_list_add(&info->issue_bios, bio);
1428 }
1429 }
1430}
1431
1432static void remap_and_issue_shared_cell(struct thin_c *tc,
1433 struct dm_bio_prison_cell *cell,
1434 dm_block_t block)
1435{
1436 struct bio *bio;
1437 struct remap_info info;
1438
1439 info.tc = tc;
1440 bio_list_init(&info.defer_bios);
1441 bio_list_init(&info.issue_bios);
1442
1443 cell_visit_release(tc->pool, __remap_and_issue_shared_cell,
1444 &info, cell);
1445
1446 while ((bio = bio_list_pop(&info.defer_bios)))
1447 thin_defer_bio(tc, bio);
1448
1449 while ((bio = bio_list_pop(&info.issue_bios)))
1450 remap_and_issue(tc, bio, block);
1451}
1452
Joe Thornber991d9fa2011-10-31 20:21:18 +00001453static void process_shared_bio(struct thin_c *tc, struct bio *bio,
1454 dm_block_t block,
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001455 struct dm_thin_lookup_result *lookup_result,
1456 struct dm_bio_prison_cell *virt_cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001457{
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001458 struct dm_bio_prison_cell *data_cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001459 struct pool *pool = tc->pool;
Mike Snitzer44feb382012-10-12 21:02:10 +01001460 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001461
1462 /*
1463 * If cell is already occupied, then sharing is already in the process
1464 * of being broken so we have nothing further to do here.
1465 */
1466 build_data_key(tc->td, lookup_result->block, &key);
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001467 if (bio_detain(pool, &key, bio, &data_cell)) {
1468 cell_defer_no_holder(tc, virt_cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001469 return;
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001470 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001471
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001472 if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size) {
1473 break_sharing(tc, bio, block, &key, lookup_result, data_cell);
1474 cell_defer_no_holder(tc, virt_cell);
1475 } else {
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001476 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornber991d9fa2011-10-31 20:21:18 +00001477
Mike Snitzer44feb382012-10-12 21:02:10 +01001478 h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
Joe Thornbere8088072012-12-21 20:23:31 +00001479 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001480 remap_and_issue(tc, bio, lookup_result->block);
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001481
1482 remap_and_issue_shared_cell(tc, data_cell, lookup_result->block);
1483 remap_and_issue_shared_cell(tc, virt_cell, lookup_result->block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001484 }
1485}
1486
1487static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzera24c2562012-06-03 00:30:00 +01001488 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001489{
1490 int r;
1491 dm_block_t data_block;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001492 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001493
1494 /*
1495 * Remap empty bios (flushes) immediately, without provisioning.
1496 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07001497 if (!bio->bi_iter.bi_size) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001498 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001499 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001500
Joe Thornber991d9fa2011-10-31 20:21:18 +00001501 remap_and_issue(tc, bio, 0);
1502 return;
1503 }
1504
1505 /*
1506 * Fill read bios with zeroes and complete them immediately.
1507 */
1508 if (bio_data_dir(bio) == READ) {
1509 zero_fill_bio(bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001510 cell_defer_no_holder(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001511 bio_endio(bio, 0);
1512 return;
1513 }
1514
1515 r = alloc_data_block(tc, &data_block);
1516 switch (r) {
1517 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001518 if (tc->origin_dev)
1519 schedule_external_copy(tc, block, data_block, cell, bio);
1520 else
1521 schedule_zero(tc, block, data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001522 break;
1523
1524 case -ENOSPC:
Mike Snitzer399cadd2013-12-05 16:03:33 -05001525 retry_bios_on_resume(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001526 break;
1527
1528 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001529 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1530 __func__, r);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001531 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001532 break;
1533 }
1534}
1535
Joe Thornbera374bb22014-10-10 13:43:14 +01001536static void process_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001537{
1538 int r;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001539 struct pool *pool = tc->pool;
Joe Thornbera374bb22014-10-10 13:43:14 +01001540 struct bio *bio = cell->holder;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001541 dm_block_t block = get_bio_block(tc, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001542 struct dm_thin_lookup_result lookup_result;
1543
Joe Thornbera374bb22014-10-10 13:43:14 +01001544 if (tc->requeue_mode) {
1545 cell_requeue(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001546 return;
Joe Thornbera374bb22014-10-10 13:43:14 +01001547 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001548
1549 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1550 switch (r) {
1551 case 0:
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001552 if (lookup_result.shared)
1553 process_shared_bio(tc, bio, block, &lookup_result, cell);
1554 else {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001555 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001556 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbera374bb22014-10-10 13:43:14 +01001557 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001558 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001559 break;
1560
1561 case -ENODATA:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001562 if (bio_data_dir(bio) == READ && tc->origin_dev) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001563 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001564 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001565
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001566 if (bio_end_sector(bio) <= tc->origin_size)
1567 remap_to_origin_and_issue(tc, bio);
1568
1569 else if (bio->bi_iter.bi_sector < tc->origin_size) {
1570 zero_fill_bio(bio);
1571 bio->bi_iter.bi_size = (tc->origin_size - bio->bi_iter.bi_sector) << SECTOR_SHIFT;
1572 remap_to_origin_and_issue(tc, bio);
1573
1574 } else {
1575 zero_fill_bio(bio);
1576 bio_endio(bio, 0);
1577 }
Joe Thornber2dd9c252012-03-28 18:41:28 +01001578 } else
1579 provision_block(tc, bio, block, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001580 break;
1581
1582 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001583 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1584 __func__, r);
Joe Thornberf286ba02012-12-21 20:23:33 +00001585 cell_defer_no_holder(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001586 bio_io_error(bio);
1587 break;
1588 }
1589}
1590
Joe Thornbera374bb22014-10-10 13:43:14 +01001591static void process_bio(struct thin_c *tc, struct bio *bio)
1592{
1593 struct pool *pool = tc->pool;
1594 dm_block_t block = get_bio_block(tc, bio);
1595 struct dm_bio_prison_cell *cell;
1596 struct dm_cell_key key;
1597
1598 /*
1599 * If cell is already occupied, then the block is already
1600 * being provisioned so we have nothing further to do here.
1601 */
1602 build_virtual_key(tc->td, block, &key);
1603 if (bio_detain(pool, &key, bio, &cell))
1604 return;
1605
1606 process_cell(tc, cell);
1607}
1608
1609static void __process_bio_read_only(struct thin_c *tc, struct bio *bio,
1610 struct dm_bio_prison_cell *cell)
Joe Thornbere49e5822012-07-27 15:08:16 +01001611{
1612 int r;
1613 int rw = bio_data_dir(bio);
1614 dm_block_t block = get_bio_block(tc, bio);
1615 struct dm_thin_lookup_result lookup_result;
1616
1617 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1618 switch (r) {
1619 case 0:
Joe Thornbera374bb22014-10-10 13:43:14 +01001620 if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size) {
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001621 handle_unserviceable_bio(tc->pool, bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01001622 if (cell)
1623 cell_defer_no_holder(tc, cell);
1624 } else {
Joe Thornbere8088072012-12-21 20:23:31 +00001625 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001626 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbera374bb22014-10-10 13:43:14 +01001627 if (cell)
1628 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001629 }
Joe Thornbere49e5822012-07-27 15:08:16 +01001630 break;
1631
1632 case -ENODATA:
Joe Thornbera374bb22014-10-10 13:43:14 +01001633 if (cell)
1634 cell_defer_no_holder(tc, cell);
Joe Thornbere49e5822012-07-27 15:08:16 +01001635 if (rw != READ) {
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001636 handle_unserviceable_bio(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001637 break;
1638 }
1639
1640 if (tc->origin_dev) {
Joe Thornbere8088072012-12-21 20:23:31 +00001641 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001642 remap_to_origin_and_issue(tc, bio);
1643 break;
1644 }
1645
1646 zero_fill_bio(bio);
1647 bio_endio(bio, 0);
1648 break;
1649
1650 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001651 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1652 __func__, r);
Joe Thornbera374bb22014-10-10 13:43:14 +01001653 if (cell)
1654 cell_defer_no_holder(tc, cell);
Joe Thornbere49e5822012-07-27 15:08:16 +01001655 bio_io_error(bio);
1656 break;
1657 }
1658}
1659
Joe Thornbera374bb22014-10-10 13:43:14 +01001660static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
1661{
1662 __process_bio_read_only(tc, bio, NULL);
1663}
1664
1665static void process_cell_read_only(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1666{
1667 __process_bio_read_only(tc, cell->holder, cell);
1668}
1669
Joe Thornber3e1a0692014-03-03 16:03:26 +00001670static void process_bio_success(struct thin_c *tc, struct bio *bio)
1671{
1672 bio_endio(bio, 0);
1673}
1674
Joe Thornbere49e5822012-07-27 15:08:16 +01001675static void process_bio_fail(struct thin_c *tc, struct bio *bio)
1676{
1677 bio_io_error(bio);
1678}
1679
Joe Thornbera374bb22014-10-10 13:43:14 +01001680static void process_cell_success(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1681{
1682 cell_success(tc->pool, cell);
1683}
1684
1685static void process_cell_fail(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1686{
1687 cell_error(tc->pool, cell);
1688}
1689
Joe Thornberac8c3f32013-05-10 14:37:21 +01001690/*
1691 * FIXME: should we also commit due to size of transaction, measured in
1692 * metadata blocks?
1693 */
Joe Thornber905e51b2012-03-28 18:41:27 +01001694static int need_commit_due_to_time(struct pool *pool)
1695{
Manuel Schölling0f30af92014-05-22 22:42:37 +02001696 return !time_in_range(jiffies, pool->last_commit_jiffies,
1697 pool->last_commit_jiffies + COMMIT_PERIOD);
Joe Thornber905e51b2012-03-28 18:41:27 +01001698}
1699
Mike Snitzer67324ea2014-03-21 18:33:41 -04001700#define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node)
1701#define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook))
1702
1703static void __thin_bio_rb_add(struct thin_c *tc, struct bio *bio)
1704{
1705 struct rb_node **rbp, *parent;
1706 struct dm_thin_endio_hook *pbd;
1707 sector_t bi_sector = bio->bi_iter.bi_sector;
1708
1709 rbp = &tc->sort_bio_list.rb_node;
1710 parent = NULL;
1711 while (*rbp) {
1712 parent = *rbp;
1713 pbd = thin_pbd(parent);
1714
1715 if (bi_sector < thin_bio(pbd)->bi_iter.bi_sector)
1716 rbp = &(*rbp)->rb_left;
1717 else
1718 rbp = &(*rbp)->rb_right;
1719 }
1720
1721 pbd = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1722 rb_link_node(&pbd->rb_node, parent, rbp);
1723 rb_insert_color(&pbd->rb_node, &tc->sort_bio_list);
1724}
1725
1726static void __extract_sorted_bios(struct thin_c *tc)
1727{
1728 struct rb_node *node;
1729 struct dm_thin_endio_hook *pbd;
1730 struct bio *bio;
1731
1732 for (node = rb_first(&tc->sort_bio_list); node; node = rb_next(node)) {
1733 pbd = thin_pbd(node);
1734 bio = thin_bio(pbd);
1735
1736 bio_list_add(&tc->deferred_bio_list, bio);
1737 rb_erase(&pbd->rb_node, &tc->sort_bio_list);
1738 }
1739
1740 WARN_ON(!RB_EMPTY_ROOT(&tc->sort_bio_list));
1741}
1742
1743static void __sort_thin_deferred_bios(struct thin_c *tc)
1744{
1745 struct bio *bio;
1746 struct bio_list bios;
1747
1748 bio_list_init(&bios);
1749 bio_list_merge(&bios, &tc->deferred_bio_list);
1750 bio_list_init(&tc->deferred_bio_list);
1751
1752 /* Sort deferred_bio_list using rb-tree */
1753 while ((bio = bio_list_pop(&bios)))
1754 __thin_bio_rb_add(tc, bio);
1755
1756 /*
1757 * Transfer the sorted bios in sort_bio_list back to
1758 * deferred_bio_list to allow lockless submission of
1759 * all bios.
1760 */
1761 __extract_sorted_bios(tc);
1762}
1763
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001764static void process_thin_deferred_bios(struct thin_c *tc)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001765{
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001766 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001767 unsigned long flags;
1768 struct bio *bio;
1769 struct bio_list bios;
Mike Snitzer67324ea2014-03-21 18:33:41 -04001770 struct blk_plug plug;
Joe Thornber8a01a6a2014-10-06 15:28:30 +01001771 unsigned count = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001772
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001773 if (tc->requeue_mode) {
Mike Snitzer42d6a8c2014-10-19 07:52:44 -04001774 error_thin_bio_list(tc, &tc->deferred_bio_list, DM_ENDIO_REQUEUE);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001775 return;
1776 }
1777
Joe Thornber991d9fa2011-10-31 20:21:18 +00001778 bio_list_init(&bios);
1779
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001780 spin_lock_irqsave(&tc->lock, flags);
Mike Snitzer67324ea2014-03-21 18:33:41 -04001781
1782 if (bio_list_empty(&tc->deferred_bio_list)) {
1783 spin_unlock_irqrestore(&tc->lock, flags);
1784 return;
1785 }
1786
1787 __sort_thin_deferred_bios(tc);
1788
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001789 bio_list_merge(&bios, &tc->deferred_bio_list);
1790 bio_list_init(&tc->deferred_bio_list);
Mike Snitzer67324ea2014-03-21 18:33:41 -04001791
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001792 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001793
Mike Snitzer67324ea2014-03-21 18:33:41 -04001794 blk_start_plug(&plug);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001795 while ((bio = bio_list_pop(&bios))) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001796 /*
1797 * If we've got no free new_mapping structs, and processing
1798 * this bio might require one, we pause until there are some
1799 * prepared mappings to process.
1800 */
1801 if (ensure_next_mapping(pool)) {
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001802 spin_lock_irqsave(&tc->lock, flags);
1803 bio_list_add(&tc->deferred_bio_list, bio);
1804 bio_list_merge(&tc->deferred_bio_list, &bios);
1805 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001806 break;
1807 }
Joe Thornber104655f2012-03-28 18:41:28 +01001808
1809 if (bio->bi_rw & REQ_DISCARD)
Joe Thornbere49e5822012-07-27 15:08:16 +01001810 pool->process_discard(tc, bio);
Joe Thornber104655f2012-03-28 18:41:28 +01001811 else
Joe Thornbere49e5822012-07-27 15:08:16 +01001812 pool->process_bio(tc, bio);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01001813
1814 if ((count++ & 127) == 0) {
Joe Thornber7d327fe2014-10-06 15:45:59 +01001815 throttle_work_update(&pool->throttle);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01001816 dm_pool_issue_prefetches(pool->pmd);
1817 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001818 }
Mike Snitzer67324ea2014-03-21 18:33:41 -04001819 blk_finish_plug(&plug);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001820}
1821
Joe Thornberac4c3f32014-10-10 16:42:10 +01001822static int cmp_cells(const void *lhs, const void *rhs)
1823{
1824 struct dm_bio_prison_cell *lhs_cell = *((struct dm_bio_prison_cell **) lhs);
1825 struct dm_bio_prison_cell *rhs_cell = *((struct dm_bio_prison_cell **) rhs);
1826
1827 BUG_ON(!lhs_cell->holder);
1828 BUG_ON(!rhs_cell->holder);
1829
1830 if (lhs_cell->holder->bi_iter.bi_sector < rhs_cell->holder->bi_iter.bi_sector)
1831 return -1;
1832
1833 if (lhs_cell->holder->bi_iter.bi_sector > rhs_cell->holder->bi_iter.bi_sector)
1834 return 1;
1835
1836 return 0;
1837}
1838
1839static unsigned sort_cells(struct pool *pool, struct list_head *cells)
1840{
1841 unsigned count = 0;
1842 struct dm_bio_prison_cell *cell, *tmp;
1843
1844 list_for_each_entry_safe(cell, tmp, cells, user_list) {
1845 if (count >= CELL_SORT_ARRAY_SIZE)
1846 break;
1847
1848 pool->cell_sort_array[count++] = cell;
1849 list_del(&cell->user_list);
1850 }
1851
1852 sort(pool->cell_sort_array, count, sizeof(cell), cmp_cells, NULL);
1853
1854 return count;
1855}
1856
Joe Thornbera374bb22014-10-10 13:43:14 +01001857static void process_thin_deferred_cells(struct thin_c *tc)
1858{
1859 struct pool *pool = tc->pool;
1860 unsigned long flags;
1861 struct list_head cells;
Joe Thornberac4c3f32014-10-10 16:42:10 +01001862 struct dm_bio_prison_cell *cell;
1863 unsigned i, j, count;
Joe Thornbera374bb22014-10-10 13:43:14 +01001864
1865 INIT_LIST_HEAD(&cells);
1866
1867 spin_lock_irqsave(&tc->lock, flags);
1868 list_splice_init(&tc->deferred_cells, &cells);
1869 spin_unlock_irqrestore(&tc->lock, flags);
1870
1871 if (list_empty(&cells))
1872 return;
1873
Joe Thornberac4c3f32014-10-10 16:42:10 +01001874 do {
1875 count = sort_cells(tc->pool, &cells);
Joe Thornbera374bb22014-10-10 13:43:14 +01001876
Joe Thornberac4c3f32014-10-10 16:42:10 +01001877 for (i = 0; i < count; i++) {
1878 cell = pool->cell_sort_array[i];
1879 BUG_ON(!cell->holder);
1880
1881 /*
1882 * If we've got no free new_mapping structs, and processing
1883 * this bio might require one, we pause until there are some
1884 * prepared mappings to process.
1885 */
1886 if (ensure_next_mapping(pool)) {
1887 for (j = i; j < count; j++)
1888 list_add(&pool->cell_sort_array[j]->user_list, &cells);
1889
1890 spin_lock_irqsave(&tc->lock, flags);
1891 list_splice(&cells, &tc->deferred_cells);
1892 spin_unlock_irqrestore(&tc->lock, flags);
1893 return;
1894 }
1895
1896 if (cell->holder->bi_rw & REQ_DISCARD)
1897 pool->process_discard_cell(tc, cell);
1898 else
1899 pool->process_cell(tc, cell);
Joe Thornbera374bb22014-10-10 13:43:14 +01001900 }
Joe Thornberac4c3f32014-10-10 16:42:10 +01001901 } while (!list_empty(&cells));
Joe Thornbera374bb22014-10-10 13:43:14 +01001902}
1903
Joe Thornberb10ebd32014-04-08 11:29:01 +01001904static void thin_get(struct thin_c *tc);
1905static void thin_put(struct thin_c *tc);
1906
1907/*
1908 * We can't hold rcu_read_lock() around code that can block. So we
1909 * find a thin with the rcu lock held; bump a refcount; then drop
1910 * the lock.
1911 */
1912static struct thin_c *get_first_thin(struct pool *pool)
1913{
1914 struct thin_c *tc = NULL;
1915
1916 rcu_read_lock();
1917 if (!list_empty(&pool->active_thins)) {
1918 tc = list_entry_rcu(pool->active_thins.next, struct thin_c, list);
1919 thin_get(tc);
1920 }
1921 rcu_read_unlock();
1922
1923 return tc;
1924}
1925
1926static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc)
1927{
1928 struct thin_c *old_tc = tc;
1929
1930 rcu_read_lock();
1931 list_for_each_entry_continue_rcu(tc, &pool->active_thins, list) {
1932 thin_get(tc);
1933 thin_put(old_tc);
1934 rcu_read_unlock();
1935 return tc;
1936 }
1937 thin_put(old_tc);
1938 rcu_read_unlock();
1939
1940 return NULL;
1941}
1942
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001943static void process_deferred_bios(struct pool *pool)
1944{
1945 unsigned long flags;
1946 struct bio *bio;
1947 struct bio_list bios;
1948 struct thin_c *tc;
1949
Joe Thornberb10ebd32014-04-08 11:29:01 +01001950 tc = get_first_thin(pool);
1951 while (tc) {
Joe Thornbera374bb22014-10-10 13:43:14 +01001952 process_thin_deferred_cells(tc);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001953 process_thin_deferred_bios(tc);
Joe Thornberb10ebd32014-04-08 11:29:01 +01001954 tc = get_next_thin(pool, tc);
1955 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001956
1957 /*
1958 * If there are any deferred flush bios, we must commit
1959 * the metadata before issuing them.
1960 */
1961 bio_list_init(&bios);
1962 spin_lock_irqsave(&pool->lock, flags);
1963 bio_list_merge(&bios, &pool->deferred_flush_bios);
1964 bio_list_init(&pool->deferred_flush_bios);
1965 spin_unlock_irqrestore(&pool->lock, flags);
1966
Mike Snitzer4d1662a2014-02-06 06:08:56 -05001967 if (bio_list_empty(&bios) &&
1968 !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool)))
Joe Thornber991d9fa2011-10-31 20:21:18 +00001969 return;
1970
Joe Thornber020cc3b2013-12-04 15:05:36 -05001971 if (commit(pool)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001972 while ((bio = bio_list_pop(&bios)))
1973 bio_io_error(bio);
1974 return;
1975 }
Joe Thornber905e51b2012-03-28 18:41:27 +01001976 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001977
1978 while ((bio = bio_list_pop(&bios)))
1979 generic_make_request(bio);
1980}
1981
1982static void do_worker(struct work_struct *ws)
1983{
1984 struct pool *pool = container_of(ws, struct pool, worker);
1985
Joe Thornber7d327fe2014-10-06 15:45:59 +01001986 throttle_work_start(&pool->throttle);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01001987 dm_pool_issue_prefetches(pool->pmd);
Joe Thornber7d327fe2014-10-06 15:45:59 +01001988 throttle_work_update(&pool->throttle);
Joe Thornbere49e5822012-07-27 15:08:16 +01001989 process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
Joe Thornber7d327fe2014-10-06 15:45:59 +01001990 throttle_work_update(&pool->throttle);
Joe Thornbere49e5822012-07-27 15:08:16 +01001991 process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
Joe Thornber7d327fe2014-10-06 15:45:59 +01001992 throttle_work_update(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001993 process_deferred_bios(pool);
Joe Thornber7d327fe2014-10-06 15:45:59 +01001994 throttle_work_complete(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001995}
1996
Joe Thornber905e51b2012-03-28 18:41:27 +01001997/*
1998 * We want to commit periodically so that not too much
1999 * unwritten data builds up.
2000 */
2001static void do_waker(struct work_struct *ws)
2002{
2003 struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
2004 wake_worker(pool);
2005 queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
2006}
2007
Joe Thornber85ad643b2014-05-09 15:59:38 +01002008/*
2009 * We're holding onto IO to allow userland time to react. After the
2010 * timeout either the pool will have been resized (and thus back in
2011 * PM_WRITE mode), or we degrade to PM_READ_ONLY and start erroring IO.
2012 */
2013static void do_no_space_timeout(struct work_struct *ws)
2014{
2015 struct pool *pool = container_of(to_delayed_work(ws), struct pool,
2016 no_space_timeout);
2017
2018 if (get_pool_mode(pool) == PM_OUT_OF_DATA_SPACE && !pool->pf.error_if_no_space)
2019 set_pool_mode(pool, PM_READ_ONLY);
2020}
2021
Joe Thornber991d9fa2011-10-31 20:21:18 +00002022/*----------------------------------------------------------------*/
2023
Joe Thornbere7a3e872014-05-13 16:14:14 -04002024struct pool_work {
Joe Thornber738211f2014-03-03 15:52:28 +00002025 struct work_struct worker;
Joe Thornbere7a3e872014-05-13 16:14:14 -04002026 struct completion complete;
Joe Thornber738211f2014-03-03 15:52:28 +00002027};
2028
Joe Thornbere7a3e872014-05-13 16:14:14 -04002029static struct pool_work *to_pool_work(struct work_struct *ws)
Joe Thornber738211f2014-03-03 15:52:28 +00002030{
Joe Thornbere7a3e872014-05-13 16:14:14 -04002031 return container_of(ws, struct pool_work, worker);
2032}
2033
2034static void pool_work_complete(struct pool_work *pw)
2035{
2036 complete(&pw->complete);
2037}
2038
2039static void pool_work_wait(struct pool_work *pw, struct pool *pool,
2040 void (*fn)(struct work_struct *))
2041{
2042 INIT_WORK_ONSTACK(&pw->worker, fn);
2043 init_completion(&pw->complete);
2044 queue_work(pool->wq, &pw->worker);
2045 wait_for_completion(&pw->complete);
2046}
2047
2048/*----------------------------------------------------------------*/
2049
2050struct noflush_work {
2051 struct pool_work pw;
2052 struct thin_c *tc;
2053};
2054
2055static struct noflush_work *to_noflush(struct work_struct *ws)
2056{
2057 return container_of(to_pool_work(ws), struct noflush_work, pw);
Joe Thornber738211f2014-03-03 15:52:28 +00002058}
2059
2060static void do_noflush_start(struct work_struct *ws)
2061{
Joe Thornbere7a3e872014-05-13 16:14:14 -04002062 struct noflush_work *w = to_noflush(ws);
Joe Thornber738211f2014-03-03 15:52:28 +00002063 w->tc->requeue_mode = true;
2064 requeue_io(w->tc);
Joe Thornbere7a3e872014-05-13 16:14:14 -04002065 pool_work_complete(&w->pw);
Joe Thornber738211f2014-03-03 15:52:28 +00002066}
2067
2068static void do_noflush_stop(struct work_struct *ws)
2069{
Joe Thornbere7a3e872014-05-13 16:14:14 -04002070 struct noflush_work *w = to_noflush(ws);
Joe Thornber738211f2014-03-03 15:52:28 +00002071 w->tc->requeue_mode = false;
Joe Thornbere7a3e872014-05-13 16:14:14 -04002072 pool_work_complete(&w->pw);
Joe Thornber738211f2014-03-03 15:52:28 +00002073}
2074
2075static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *))
2076{
2077 struct noflush_work w;
2078
Joe Thornber738211f2014-03-03 15:52:28 +00002079 w.tc = tc;
Joe Thornbere7a3e872014-05-13 16:14:14 -04002080 pool_work_wait(&w.pw, tc->pool, fn);
Joe Thornber738211f2014-03-03 15:52:28 +00002081}
2082
2083/*----------------------------------------------------------------*/
2084
Joe Thornbere49e5822012-07-27 15:08:16 +01002085static enum pool_mode get_pool_mode(struct pool *pool)
2086{
2087 return pool->pf.mode;
2088}
2089
Joe Thornber3e1a0692014-03-03 16:03:26 +00002090static void notify_of_pool_mode_change(struct pool *pool, const char *new_mode)
2091{
2092 dm_table_event(pool->ti->table);
2093 DMINFO("%s: switching pool to %s mode",
2094 dm_device_name(pool->pool_md), new_mode);
2095}
2096
Mike Snitzer8b64e882013-12-20 14:27:28 -05002097static void set_pool_mode(struct pool *pool, enum pool_mode new_mode)
Joe Thornbere49e5822012-07-27 15:08:16 +01002098{
Mike Snitzercdc2b412014-02-14 18:10:55 -05002099 struct pool_c *pt = pool->ti->private;
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002100 bool needs_check = dm_pool_metadata_needs_check(pool->pmd);
2101 enum pool_mode old_mode = get_pool_mode(pool);
Mike Snitzer80c57892014-05-20 13:38:33 -04002102 unsigned long no_space_timeout = ACCESS_ONCE(no_space_timeout_secs) * HZ;
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002103
2104 /*
2105 * Never allow the pool to transition to PM_WRITE mode if user
2106 * intervention is required to verify metadata and data consistency.
2107 */
2108 if (new_mode == PM_WRITE && needs_check) {
2109 DMERR("%s: unable to switch pool to write mode until repaired.",
2110 dm_device_name(pool->pool_md));
2111 if (old_mode != new_mode)
2112 new_mode = old_mode;
2113 else
2114 new_mode = PM_READ_ONLY;
2115 }
2116 /*
2117 * If we were in PM_FAIL mode, rollback of metadata failed. We're
2118 * not going to recover without a thin_repair. So we never let the
2119 * pool move out of the old mode.
2120 */
2121 if (old_mode == PM_FAIL)
2122 new_mode = old_mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002123
Mike Snitzer8b64e882013-12-20 14:27:28 -05002124 switch (new_mode) {
Joe Thornbere49e5822012-07-27 15:08:16 +01002125 case PM_FAIL:
Mike Snitzer8b64e882013-12-20 14:27:28 -05002126 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00002127 notify_of_pool_mode_change(pool, "failure");
Joe Thornber5383ef32013-12-04 16:30:01 -05002128 dm_pool_metadata_read_only(pool->pmd);
Joe Thornbere49e5822012-07-27 15:08:16 +01002129 pool->process_bio = process_bio_fail;
2130 pool->process_discard = process_bio_fail;
Joe Thornbera374bb22014-10-10 13:43:14 +01002131 pool->process_cell = process_cell_fail;
2132 pool->process_discard_cell = process_cell_fail;
Joe Thornbere49e5822012-07-27 15:08:16 +01002133 pool->process_prepared_mapping = process_prepared_mapping_fail;
2134 pool->process_prepared_discard = process_prepared_discard_fail;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002135
2136 error_retry_list(pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01002137 break;
2138
2139 case PM_READ_ONLY:
Mike Snitzer8b64e882013-12-20 14:27:28 -05002140 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00002141 notify_of_pool_mode_change(pool, "read-only");
2142 dm_pool_metadata_read_only(pool->pmd);
2143 pool->process_bio = process_bio_read_only;
2144 pool->process_discard = process_bio_success;
Joe Thornbera374bb22014-10-10 13:43:14 +01002145 pool->process_cell = process_cell_read_only;
2146 pool->process_discard_cell = process_cell_success;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002147 pool->process_prepared_mapping = process_prepared_mapping_fail;
2148 pool->process_prepared_discard = process_prepared_discard_passdown;
2149
2150 error_retry_list(pool);
2151 break;
2152
2153 case PM_OUT_OF_DATA_SPACE:
2154 /*
2155 * Ideally we'd never hit this state; the low water mark
2156 * would trigger userland to extend the pool before we
2157 * completely run out of data space. However, many small
2158 * IOs to unprovisioned space can consume data space at an
2159 * alarming rate. Adjust your low water mark if you're
2160 * frequently seeing this mode.
2161 */
2162 if (old_mode != new_mode)
2163 notify_of_pool_mode_change(pool, "out-of-data-space");
2164 pool->process_bio = process_bio_read_only;
Joe Thornbera374bb22014-10-10 13:43:14 +01002165 pool->process_discard = process_discard_bio;
2166 pool->process_cell = process_cell_read_only;
2167 pool->process_discard_cell = process_discard_cell;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002168 pool->process_prepared_mapping = process_prepared_mapping;
Joe Thornber45ec9bd2014-12-10 17:06:57 +00002169 pool->process_prepared_discard = process_prepared_discard;
Joe Thornber85ad643b2014-05-09 15:59:38 +01002170
Mike Snitzer80c57892014-05-20 13:38:33 -04002171 if (!pool->pf.error_if_no_space && no_space_timeout)
2172 queue_delayed_work(pool->wq, &pool->no_space_timeout, no_space_timeout);
Joe Thornbere49e5822012-07-27 15:08:16 +01002173 break;
2174
2175 case PM_WRITE:
Mike Snitzer8b64e882013-12-20 14:27:28 -05002176 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00002177 notify_of_pool_mode_change(pool, "write");
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002178 dm_pool_metadata_read_write(pool->pmd);
Joe Thornbere49e5822012-07-27 15:08:16 +01002179 pool->process_bio = process_bio;
Joe Thornbera374bb22014-10-10 13:43:14 +01002180 pool->process_discard = process_discard_bio;
2181 pool->process_cell = process_cell;
2182 pool->process_discard_cell = process_discard_cell;
Joe Thornbere49e5822012-07-27 15:08:16 +01002183 pool->process_prepared_mapping = process_prepared_mapping;
2184 pool->process_prepared_discard = process_prepared_discard;
2185 break;
2186 }
Mike Snitzer8b64e882013-12-20 14:27:28 -05002187
2188 pool->pf.mode = new_mode;
Mike Snitzercdc2b412014-02-14 18:10:55 -05002189 /*
2190 * The pool mode may have changed, sync it so bind_control_target()
2191 * doesn't cause an unexpected mode transition on resume.
2192 */
2193 pt->adjusted_pf.mode = new_mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002194}
2195
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002196static void abort_transaction(struct pool *pool)
2197{
2198 const char *dev_name = dm_device_name(pool->pool_md);
2199
2200 DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
2201 if (dm_pool_abort_metadata(pool->pmd)) {
2202 DMERR("%s: failed to abort metadata transaction", dev_name);
2203 set_pool_mode(pool, PM_FAIL);
2204 }
2205
2206 if (dm_pool_metadata_set_needs_check(pool->pmd)) {
2207 DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
2208 set_pool_mode(pool, PM_FAIL);
2209 }
2210}
2211
Joe Thornberb5330652013-12-04 19:51:33 -05002212static void metadata_operation_failed(struct pool *pool, const char *op, int r)
2213{
2214 DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
2215 dm_device_name(pool->pool_md), op, r);
2216
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002217 abort_transaction(pool);
Joe Thornberb5330652013-12-04 19:51:33 -05002218 set_pool_mode(pool, PM_READ_ONLY);
2219}
2220
Joe Thornbere49e5822012-07-27 15:08:16 +01002221/*----------------------------------------------------------------*/
2222
Joe Thornber991d9fa2011-10-31 20:21:18 +00002223/*
2224 * Mapping functions.
2225 */
2226
2227/*
2228 * Called only while mapping a thin bio to hand it over to the workqueue.
2229 */
2230static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
2231{
2232 unsigned long flags;
2233 struct pool *pool = tc->pool;
2234
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002235 spin_lock_irqsave(&tc->lock, flags);
2236 bio_list_add(&tc->deferred_bio_list, bio);
2237 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002238
2239 wake_worker(pool);
2240}
2241
Joe Thornber7d327fe2014-10-06 15:45:59 +01002242static void thin_defer_bio_with_throttle(struct thin_c *tc, struct bio *bio)
2243{
2244 struct pool *pool = tc->pool;
2245
2246 throttle_lock(&pool->throttle);
2247 thin_defer_bio(tc, bio);
2248 throttle_unlock(&pool->throttle);
2249}
2250
Joe Thornbera374bb22014-10-10 13:43:14 +01002251static void thin_defer_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
2252{
2253 unsigned long flags;
2254 struct pool *pool = tc->pool;
2255
2256 throttle_lock(&pool->throttle);
2257 spin_lock_irqsave(&tc->lock, flags);
2258 list_add_tail(&cell->user_list, &tc->deferred_cells);
2259 spin_unlock_irqrestore(&tc->lock, flags);
2260 throttle_unlock(&pool->throttle);
2261
2262 wake_worker(pool);
2263}
2264
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002265static void thin_hook_bio(struct thin_c *tc, struct bio *bio)
Joe Thornbereb2aa482012-03-28 18:41:28 +01002266{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002267 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01002268
2269 h->tc = tc;
2270 h->shared_read_entry = NULL;
Joe Thornbere8088072012-12-21 20:23:31 +00002271 h->all_io_entry = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01002272 h->overwrite_mapping = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01002273}
2274
Joe Thornber991d9fa2011-10-31 20:21:18 +00002275/*
2276 * Non-blocking function called from the thin target's map function.
2277 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002278static int thin_bio_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002279{
2280 int r;
2281 struct thin_c *tc = ti->private;
2282 dm_block_t block = get_bio_block(tc, bio);
2283 struct dm_thin_device *td = tc->td;
2284 struct dm_thin_lookup_result result;
Joe Thornbera374bb22014-10-10 13:43:14 +01002285 struct dm_bio_prison_cell *virt_cell, *data_cell;
Joe Thornbere8088072012-12-21 20:23:31 +00002286 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002287
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002288 thin_hook_bio(tc, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01002289
Joe Thornber738211f2014-03-03 15:52:28 +00002290 if (tc->requeue_mode) {
2291 bio_endio(bio, DM_ENDIO_REQUEUE);
2292 return DM_MAPIO_SUBMITTED;
2293 }
2294
Joe Thornbere49e5822012-07-27 15:08:16 +01002295 if (get_pool_mode(tc->pool) == PM_FAIL) {
2296 bio_io_error(bio);
2297 return DM_MAPIO_SUBMITTED;
2298 }
2299
Joe Thornber104655f2012-03-28 18:41:28 +01002300 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) {
Joe Thornber7d327fe2014-10-06 15:45:59 +01002301 thin_defer_bio_with_throttle(tc, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002302 return DM_MAPIO_SUBMITTED;
2303 }
2304
Joe Thornberc822ed92014-10-10 09:41:09 +01002305 /*
2306 * We must hold the virtual cell before doing the lookup, otherwise
2307 * there's a race with discard.
2308 */
2309 build_virtual_key(tc->td, block, &key);
Joe Thornbera374bb22014-10-10 13:43:14 +01002310 if (bio_detain(tc->pool, &key, bio, &virt_cell))
Joe Thornberc822ed92014-10-10 09:41:09 +01002311 return DM_MAPIO_SUBMITTED;
2312
Joe Thornber991d9fa2011-10-31 20:21:18 +00002313 r = dm_thin_find_block(td, block, 0, &result);
2314
2315 /*
2316 * Note that we defer readahead too.
2317 */
2318 switch (r) {
2319 case 0:
2320 if (unlikely(result.shared)) {
2321 /*
2322 * We have a race condition here between the
2323 * result.shared value returned by the lookup and
2324 * snapshot creation, which may cause new
2325 * sharing.
2326 *
2327 * To avoid this always quiesce the origin before
2328 * taking the snap. You want to do this anyway to
2329 * ensure a consistent application view
2330 * (i.e. lockfs).
2331 *
2332 * More distant ancestors are irrelevant. The
2333 * shared flag will be set in their case.
2334 */
Joe Thornbera374bb22014-10-10 13:43:14 +01002335 thin_defer_cell(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002336 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002337 }
Joe Thornbere8088072012-12-21 20:23:31 +00002338
Joe Thornbere8088072012-12-21 20:23:31 +00002339 build_data_key(tc->td, result.block, &key);
Joe Thornbera374bb22014-10-10 13:43:14 +01002340 if (bio_detain(tc->pool, &key, bio, &data_cell)) {
2341 cell_defer_no_holder(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002342 return DM_MAPIO_SUBMITTED;
2343 }
2344
2345 inc_all_io_entry(tc->pool, bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01002346 cell_defer_no_holder(tc, data_cell);
2347 cell_defer_no_holder(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002348
2349 remap(tc, bio, result.block);
2350 return DM_MAPIO_REMAPPED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002351
2352 case -ENODATA:
Joe Thornbere49e5822012-07-27 15:08:16 +01002353 case -EWOULDBLOCK:
Joe Thornbera374bb22014-10-10 13:43:14 +01002354 thin_defer_cell(tc, virt_cell);
Joe Thornber2aab3852012-12-21 20:23:33 +00002355 return DM_MAPIO_SUBMITTED;
Joe Thornbere49e5822012-07-27 15:08:16 +01002356
2357 default:
2358 /*
2359 * Must always call bio_io_error on failure.
2360 * dm_thin_find_block can fail with -EINVAL if the
2361 * pool is switched to fail-io mode.
2362 */
2363 bio_io_error(bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01002364 cell_defer_no_holder(tc, virt_cell);
Joe Thornber2aab3852012-12-21 20:23:33 +00002365 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002366 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002367}
2368
2369static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
2370{
Joe Thornber991d9fa2011-10-31 20:21:18 +00002371 struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
Mike Snitzer760fe672014-03-20 08:36:47 -04002372 struct request_queue *q;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002373
Mike Snitzer760fe672014-03-20 08:36:47 -04002374 if (get_pool_mode(pt->pool) == PM_OUT_OF_DATA_SPACE)
2375 return 1;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002376
Mike Snitzer760fe672014-03-20 08:36:47 -04002377 q = bdev_get_queue(pt->data_dev->bdev);
2378 return bdi_congested(&q->backing_dev_info, bdi_bits);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002379}
2380
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002381static void requeue_bios(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002382{
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002383 unsigned long flags;
2384 struct thin_c *tc;
2385
2386 rcu_read_lock();
2387 list_for_each_entry_rcu(tc, &pool->active_thins, list) {
2388 spin_lock_irqsave(&tc->lock, flags);
2389 bio_list_merge(&tc->deferred_bio_list, &tc->retry_on_resume_list);
2390 bio_list_init(&tc->retry_on_resume_list);
2391 spin_unlock_irqrestore(&tc->lock, flags);
2392 }
2393 rcu_read_unlock();
Joe Thornber991d9fa2011-10-31 20:21:18 +00002394}
2395
2396/*----------------------------------------------------------------
2397 * Binding of control targets to a pool object
2398 *--------------------------------------------------------------*/
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002399static bool data_dev_supports_discard(struct pool_c *pt)
2400{
2401 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
2402
2403 return q && blk_queue_discard(q);
2404}
2405
Joe Thornber58051b92013-03-20 17:21:25 +00002406static bool is_factor(sector_t block_size, uint32_t n)
2407{
2408 return !sector_div(block_size, n);
2409}
2410
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002411/*
2412 * If discard_passdown was enabled verify that the data device
Mike Snitzer0424caa2012-09-26 23:45:47 +01002413 * supports discards. Disable discard_passdown if not.
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002414 */
Mike Snitzer0424caa2012-09-26 23:45:47 +01002415static void disable_passdown_if_not_supported(struct pool_c *pt)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002416{
Mike Snitzer0424caa2012-09-26 23:45:47 +01002417 struct pool *pool = pt->pool;
2418 struct block_device *data_bdev = pt->data_dev->bdev;
2419 struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
2420 sector_t block_size = pool->sectors_per_block << SECTOR_SHIFT;
2421 const char *reason = NULL;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002422 char buf[BDEVNAME_SIZE];
2423
Mike Snitzer0424caa2012-09-26 23:45:47 +01002424 if (!pt->adjusted_pf.discard_passdown)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002425 return;
2426
Mike Snitzer0424caa2012-09-26 23:45:47 +01002427 if (!data_dev_supports_discard(pt))
2428 reason = "discard unsupported";
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002429
Mike Snitzer0424caa2012-09-26 23:45:47 +01002430 else if (data_limits->max_discard_sectors < pool->sectors_per_block)
2431 reason = "max discard sectors smaller than a block";
2432
2433 else if (data_limits->discard_granularity > block_size)
2434 reason = "discard granularity larger than a block";
2435
Joe Thornber58051b92013-03-20 17:21:25 +00002436 else if (!is_factor(block_size, data_limits->discard_granularity))
Mike Snitzer0424caa2012-09-26 23:45:47 +01002437 reason = "discard granularity not a factor of block size";
2438
2439 if (reason) {
2440 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
2441 pt->adjusted_pf.discard_passdown = false;
2442 }
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002443}
2444
Joe Thornber991d9fa2011-10-31 20:21:18 +00002445static int bind_control_target(struct pool *pool, struct dm_target *ti)
2446{
2447 struct pool_c *pt = ti->private;
2448
Joe Thornbere49e5822012-07-27 15:08:16 +01002449 /*
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002450 * We want to make sure that a pool in PM_FAIL mode is never upgraded.
Joe Thornbere49e5822012-07-27 15:08:16 +01002451 */
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002452 enum pool_mode old_mode = get_pool_mode(pool);
Mike Snitzer0424caa2012-09-26 23:45:47 +01002453 enum pool_mode new_mode = pt->adjusted_pf.mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002454
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002455 /*
Mike Snitzer8b64e882013-12-20 14:27:28 -05002456 * Don't change the pool's mode until set_pool_mode() below.
2457 * Otherwise the pool's process_* function pointers may
2458 * not match the desired pool mode.
2459 */
2460 pt->adjusted_pf.mode = old_mode;
2461
2462 pool->ti = ti;
2463 pool->pf = pt->adjusted_pf;
2464 pool->low_water_blocks = pt->low_water_blocks;
2465
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002466 set_pool_mode(pool, new_mode);
Mike Snitzerf4026932012-05-19 01:01:01 +01002467
Joe Thornber991d9fa2011-10-31 20:21:18 +00002468 return 0;
2469}
2470
2471static void unbind_control_target(struct pool *pool, struct dm_target *ti)
2472{
2473 if (pool->ti == ti)
2474 pool->ti = NULL;
2475}
2476
2477/*----------------------------------------------------------------
2478 * Pool creation
2479 *--------------------------------------------------------------*/
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002480/* Initialize pool features. */
2481static void pool_features_init(struct pool_features *pf)
2482{
Joe Thornbere49e5822012-07-27 15:08:16 +01002483 pf->mode = PM_WRITE;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002484 pf->zero_new_blocks = true;
2485 pf->discard_enabled = true;
2486 pf->discard_passdown = true;
Mike Snitzer787a996c2013-12-06 16:21:43 -05002487 pf->error_if_no_space = false;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002488}
2489
Joe Thornber991d9fa2011-10-31 20:21:18 +00002490static void __pool_destroy(struct pool *pool)
2491{
2492 __pool_table_remove(pool);
2493
2494 if (dm_pool_metadata_close(pool->pmd) < 0)
2495 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2496
Mike Snitzer44feb382012-10-12 21:02:10 +01002497 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002498 dm_kcopyd_client_destroy(pool->copier);
2499
2500 if (pool->wq)
2501 destroy_workqueue(pool->wq);
2502
2503 if (pool->next_mapping)
2504 mempool_free(pool->next_mapping, pool->mapping_pool);
2505 mempool_destroy(pool->mapping_pool);
Mike Snitzer44feb382012-10-12 21:02:10 +01002506 dm_deferred_set_destroy(pool->shared_read_ds);
2507 dm_deferred_set_destroy(pool->all_io_ds);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002508 kfree(pool);
2509}
2510
Mike Snitzera24c2562012-06-03 00:30:00 +01002511static struct kmem_cache *_new_mapping_cache;
Mike Snitzera24c2562012-06-03 00:30:00 +01002512
Joe Thornber991d9fa2011-10-31 20:21:18 +00002513static struct pool *pool_create(struct mapped_device *pool_md,
2514 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002515 unsigned long block_size,
2516 int read_only, char **error)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002517{
2518 int r;
2519 void *err_p;
2520 struct pool *pool;
2521 struct dm_pool_metadata *pmd;
Joe Thornbere49e5822012-07-27 15:08:16 +01002522 bool format_device = read_only ? false : true;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002523
Joe Thornbere49e5822012-07-27 15:08:16 +01002524 pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002525 if (IS_ERR(pmd)) {
2526 *error = "Error creating metadata object";
2527 return (struct pool *)pmd;
2528 }
2529
2530 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
2531 if (!pool) {
2532 *error = "Error allocating memory for pool";
2533 err_p = ERR_PTR(-ENOMEM);
2534 goto bad_pool;
2535 }
2536
2537 pool->pmd = pmd;
2538 pool->sectors_per_block = block_size;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +01002539 if (block_size & (block_size - 1))
2540 pool->sectors_per_block_shift = -1;
2541 else
2542 pool->sectors_per_block_shift = __ffs(block_size);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002543 pool->low_water_blocks = 0;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002544 pool_features_init(&pool->pf);
Joe Thornbera195db22014-10-06 16:30:06 -04002545 pool->prison = dm_bio_prison_create();
Joe Thornber991d9fa2011-10-31 20:21:18 +00002546 if (!pool->prison) {
2547 *error = "Error creating pool's bio prison";
2548 err_p = ERR_PTR(-ENOMEM);
2549 goto bad_prison;
2550 }
2551
Mikulas Patockadf5d2e92013-03-01 22:45:49 +00002552 pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002553 if (IS_ERR(pool->copier)) {
2554 r = PTR_ERR(pool->copier);
2555 *error = "Error creating pool's kcopyd client";
2556 err_p = ERR_PTR(r);
2557 goto bad_kcopyd_client;
2558 }
2559
2560 /*
2561 * Create singlethreaded workqueue that will service all devices
2562 * that use this metadata.
2563 */
2564 pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2565 if (!pool->wq) {
2566 *error = "Error creating pool's workqueue";
2567 err_p = ERR_PTR(-ENOMEM);
2568 goto bad_wq;
2569 }
2570
Joe Thornber7d327fe2014-10-06 15:45:59 +01002571 throttle_init(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002572 INIT_WORK(&pool->worker, do_worker);
Joe Thornber905e51b2012-03-28 18:41:27 +01002573 INIT_DELAYED_WORK(&pool->waker, do_waker);
Joe Thornber85ad643b2014-05-09 15:59:38 +01002574 INIT_DELAYED_WORK(&pool->no_space_timeout, do_no_space_timeout);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002575 spin_lock_init(&pool->lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002576 bio_list_init(&pool->deferred_flush_bios);
2577 INIT_LIST_HEAD(&pool->prepared_mappings);
Joe Thornber104655f2012-03-28 18:41:28 +01002578 INIT_LIST_HEAD(&pool->prepared_discards);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002579 INIT_LIST_HEAD(&pool->active_thins);
Joe Thornber88a66212013-12-04 20:16:12 -05002580 pool->low_water_triggered = false;
Mike Snitzer80e96c52014-11-07 15:09:46 -05002581 pool->suspended = true;
Mike Snitzer44feb382012-10-12 21:02:10 +01002582
2583 pool->shared_read_ds = dm_deferred_set_create();
2584 if (!pool->shared_read_ds) {
2585 *error = "Error creating pool's shared read deferred set";
2586 err_p = ERR_PTR(-ENOMEM);
2587 goto bad_shared_read_ds;
2588 }
2589
2590 pool->all_io_ds = dm_deferred_set_create();
2591 if (!pool->all_io_ds) {
2592 *error = "Error creating pool's all io deferred set";
2593 err_p = ERR_PTR(-ENOMEM);
2594 goto bad_all_io_ds;
2595 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002596
2597 pool->next_mapping = NULL;
Mike Snitzera24c2562012-06-03 00:30:00 +01002598 pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
2599 _new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002600 if (!pool->mapping_pool) {
2601 *error = "Error creating pool's mapping mempool";
2602 err_p = ERR_PTR(-ENOMEM);
2603 goto bad_mapping_pool;
2604 }
2605
Joe Thornber991d9fa2011-10-31 20:21:18 +00002606 pool->ref_count = 1;
Joe Thornber905e51b2012-03-28 18:41:27 +01002607 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002608 pool->pool_md = pool_md;
2609 pool->md_dev = metadata_dev;
2610 __pool_table_insert(pool);
2611
2612 return pool;
2613
Joe Thornber991d9fa2011-10-31 20:21:18 +00002614bad_mapping_pool:
Mike Snitzer44feb382012-10-12 21:02:10 +01002615 dm_deferred_set_destroy(pool->all_io_ds);
2616bad_all_io_ds:
2617 dm_deferred_set_destroy(pool->shared_read_ds);
2618bad_shared_read_ds:
Joe Thornber991d9fa2011-10-31 20:21:18 +00002619 destroy_workqueue(pool->wq);
2620bad_wq:
2621 dm_kcopyd_client_destroy(pool->copier);
2622bad_kcopyd_client:
Mike Snitzer44feb382012-10-12 21:02:10 +01002623 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002624bad_prison:
2625 kfree(pool);
2626bad_pool:
2627 if (dm_pool_metadata_close(pmd))
2628 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2629
2630 return err_p;
2631}
2632
2633static void __pool_inc(struct pool *pool)
2634{
2635 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2636 pool->ref_count++;
2637}
2638
2639static void __pool_dec(struct pool *pool)
2640{
2641 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2642 BUG_ON(!pool->ref_count);
2643 if (!--pool->ref_count)
2644 __pool_destroy(pool);
2645}
2646
2647static struct pool *__pool_find(struct mapped_device *pool_md,
2648 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002649 unsigned long block_size, int read_only,
2650 char **error, int *created)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002651{
2652 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
2653
2654 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01002655 if (pool->pool_md != pool_md) {
2656 *error = "metadata device already in use by a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00002657 return ERR_PTR(-EBUSY);
Mike Snitzerf09996c2012-07-27 15:07:59 +01002658 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002659 __pool_inc(pool);
2660
2661 } else {
2662 pool = __pool_table_lookup(pool_md);
2663 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01002664 if (pool->md_dev != metadata_dev) {
2665 *error = "different pool cannot replace a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00002666 return ERR_PTR(-EINVAL);
Mike Snitzerf09996c2012-07-27 15:07:59 +01002667 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002668 __pool_inc(pool);
2669
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002670 } else {
Joe Thornbere49e5822012-07-27 15:08:16 +01002671 pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002672 *created = 1;
2673 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002674 }
2675
2676 return pool;
2677}
2678
2679/*----------------------------------------------------------------
2680 * Pool target methods
2681 *--------------------------------------------------------------*/
2682static void pool_dtr(struct dm_target *ti)
2683{
2684 struct pool_c *pt = ti->private;
2685
2686 mutex_lock(&dm_thin_pool_table.mutex);
2687
2688 unbind_control_target(pt->pool, ti);
2689 __pool_dec(pt->pool);
2690 dm_put_device(ti, pt->metadata_dev);
2691 dm_put_device(ti, pt->data_dev);
2692 kfree(pt);
2693
2694 mutex_unlock(&dm_thin_pool_table.mutex);
2695}
2696
Joe Thornber991d9fa2011-10-31 20:21:18 +00002697static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
2698 struct dm_target *ti)
2699{
2700 int r;
2701 unsigned argc;
2702 const char *arg_name;
2703
2704 static struct dm_arg _args[] = {
Mike Snitzer74aa45c2014-01-15 19:07:58 -05002705 {0, 4, "Invalid number of pool feature arguments"},
Joe Thornber991d9fa2011-10-31 20:21:18 +00002706 };
2707
2708 /*
2709 * No feature arguments supplied.
2710 */
2711 if (!as->argc)
2712 return 0;
2713
2714 r = dm_read_arg_group(_args, as, &argc, &ti->error);
2715 if (r)
2716 return -EINVAL;
2717
2718 while (argc && !r) {
2719 arg_name = dm_shift_arg(as);
2720 argc--;
2721
Joe Thornbere49e5822012-07-27 15:08:16 +01002722 if (!strcasecmp(arg_name, "skip_block_zeroing"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002723 pf->zero_new_blocks = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002724
Joe Thornbere49e5822012-07-27 15:08:16 +01002725 else if (!strcasecmp(arg_name, "ignore_discard"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002726 pf->discard_enabled = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01002727
2728 else if (!strcasecmp(arg_name, "no_discard_passdown"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002729 pf->discard_passdown = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01002730
2731 else if (!strcasecmp(arg_name, "read_only"))
2732 pf->mode = PM_READ_ONLY;
2733
Mike Snitzer787a996c2013-12-06 16:21:43 -05002734 else if (!strcasecmp(arg_name, "error_if_no_space"))
2735 pf->error_if_no_space = true;
2736
Joe Thornbere49e5822012-07-27 15:08:16 +01002737 else {
2738 ti->error = "Unrecognised pool feature requested";
2739 r = -EINVAL;
2740 break;
2741 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002742 }
2743
2744 return r;
2745}
2746
Joe Thornberac8c3f32013-05-10 14:37:21 +01002747static void metadata_low_callback(void *context)
2748{
2749 struct pool *pool = context;
2750
2751 DMWARN("%s: reached low water mark for metadata device: sending event.",
2752 dm_device_name(pool->pool_md));
2753
2754 dm_table_event(pool->ti->table);
2755}
2756
Mike Snitzer7d489352014-02-12 23:58:15 -05002757static sector_t get_dev_size(struct block_device *bdev)
Joe Thornberb17446d2013-05-10 14:37:18 +01002758{
Mike Snitzer7d489352014-02-12 23:58:15 -05002759 return i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
2760}
2761
2762static void warn_if_metadata_device_too_big(struct block_device *bdev)
2763{
2764 sector_t metadata_dev_size = get_dev_size(bdev);
Joe Thornberb17446d2013-05-10 14:37:18 +01002765 char buffer[BDEVNAME_SIZE];
2766
Mike Snitzer7d489352014-02-12 23:58:15 -05002767 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
Joe Thornberb17446d2013-05-10 14:37:18 +01002768 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
2769 bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS);
Mike Snitzer7d489352014-02-12 23:58:15 -05002770}
2771
2772static sector_t get_metadata_dev_size(struct block_device *bdev)
2773{
2774 sector_t metadata_dev_size = get_dev_size(bdev);
2775
2776 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS)
2777 metadata_dev_size = THIN_METADATA_MAX_SECTORS;
Joe Thornberb17446d2013-05-10 14:37:18 +01002778
2779 return metadata_dev_size;
2780}
2781
Joe Thornber24347e92013-05-10 14:37:19 +01002782static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev)
2783{
2784 sector_t metadata_dev_size = get_metadata_dev_size(bdev);
2785
Mike Snitzer7d489352014-02-12 23:58:15 -05002786 sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE);
Joe Thornber24347e92013-05-10 14:37:19 +01002787
2788 return metadata_dev_size;
2789}
2790
Joe Thornber991d9fa2011-10-31 20:21:18 +00002791/*
Joe Thornberac8c3f32013-05-10 14:37:21 +01002792 * When a metadata threshold is crossed a dm event is triggered, and
2793 * userland should respond by growing the metadata device. We could let
2794 * userland set the threshold, like we do with the data threshold, but I'm
2795 * not sure they know enough to do this well.
2796 */
2797static dm_block_t calc_metadata_threshold(struct pool_c *pt)
2798{
2799 /*
2800 * 4M is ample for all ops with the possible exception of thin
2801 * device deletion which is harmless if it fails (just retry the
2802 * delete after you've grown the device).
2803 */
2804 dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4;
2805 return min((dm_block_t)1024ULL /* 4M */, quarter);
2806}
2807
2808/*
Joe Thornber991d9fa2011-10-31 20:21:18 +00002809 * thin-pool <metadata dev> <data dev>
2810 * <data block size (sectors)>
2811 * <low water mark (blocks)>
2812 * [<#feature args> [<arg>]*]
2813 *
2814 * Optional feature arguments are:
2815 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002816 * ignore_discard: disable discard
2817 * no_discard_passdown: don't pass discards down to the data device
Mike Snitzer787a996c2013-12-06 16:21:43 -05002818 * read_only: Don't allow any changes to be made to the pool metadata.
2819 * error_if_no_space: error IOs, instead of queueing, if no space.
Joe Thornber991d9fa2011-10-31 20:21:18 +00002820 */
2821static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
2822{
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002823 int r, pool_created = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002824 struct pool_c *pt;
2825 struct pool *pool;
2826 struct pool_features pf;
2827 struct dm_arg_set as;
2828 struct dm_dev *data_dev;
2829 unsigned long block_size;
2830 dm_block_t low_water_blocks;
2831 struct dm_dev *metadata_dev;
Joe Thornber5d0db962013-05-10 14:37:19 +01002832 fmode_t metadata_mode;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002833
2834 /*
2835 * FIXME Remove validation from scope of lock.
2836 */
2837 mutex_lock(&dm_thin_pool_table.mutex);
2838
2839 if (argc < 4) {
2840 ti->error = "Invalid argument count";
2841 r = -EINVAL;
2842 goto out_unlock;
2843 }
Joe Thornber5d0db962013-05-10 14:37:19 +01002844
Joe Thornber991d9fa2011-10-31 20:21:18 +00002845 as.argc = argc;
2846 as.argv = argv;
2847
Joe Thornber5d0db962013-05-10 14:37:19 +01002848 /*
2849 * Set default pool features.
2850 */
2851 pool_features_init(&pf);
2852
2853 dm_consume_args(&as, 4);
2854 r = parse_pool_features(&as, &pf, ti);
2855 if (r)
2856 goto out_unlock;
2857
2858 metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE);
2859 r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002860 if (r) {
2861 ti->error = "Error opening metadata block device";
2862 goto out_unlock;
2863 }
Mike Snitzer7d489352014-02-12 23:58:15 -05002864 warn_if_metadata_device_too_big(metadata_dev->bdev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002865
2866 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
2867 if (r) {
2868 ti->error = "Error getting data device";
2869 goto out_metadata;
2870 }
2871
2872 if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
2873 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
2874 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002875 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00002876 ti->error = "Invalid block size";
2877 r = -EINVAL;
2878 goto out;
2879 }
2880
2881 if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
2882 ti->error = "Invalid low water mark";
2883 r = -EINVAL;
2884 goto out;
2885 }
2886
Joe Thornber991d9fa2011-10-31 20:21:18 +00002887 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
2888 if (!pt) {
2889 r = -ENOMEM;
2890 goto out;
2891 }
2892
2893 pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002894 block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002895 if (IS_ERR(pool)) {
2896 r = PTR_ERR(pool);
2897 goto out_free_pt;
2898 }
2899
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002900 /*
2901 * 'pool_created' reflects whether this is the first table load.
2902 * Top level discard support is not allowed to be changed after
2903 * initial load. This would require a pool reload to trigger thin
2904 * device changes.
2905 */
2906 if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
2907 ti->error = "Discard support cannot be disabled once enabled";
2908 r = -EINVAL;
2909 goto out_flags_changed;
2910 }
2911
Joe Thornber991d9fa2011-10-31 20:21:18 +00002912 pt->pool = pool;
2913 pt->ti = ti;
2914 pt->metadata_dev = metadata_dev;
2915 pt->data_dev = data_dev;
2916 pt->low_water_blocks = low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +01002917 pt->adjusted_pf = pt->requested_pf = pf;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002918 ti->num_flush_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002919
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002920 /*
2921 * Only need to enable discards if the pool should pass
2922 * them down to the data device. The thin device's discard
2923 * processing will cause mappings to be removed from the btree.
2924 */
Mike Snitzerb60ab992013-09-19 18:49:11 -04002925 ti->discard_zeroes_data_unsupported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002926 if (pf.discard_enabled && pf.discard_passdown) {
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002927 ti->num_discard_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002928
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002929 /*
2930 * Setting 'discards_supported' circumvents the normal
2931 * stacking of discard limits (this keeps the pool and
2932 * thin devices' discard limits consistent).
2933 */
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01002934 ti->discards_supported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002935 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002936 ti->private = pt;
2937
Joe Thornberac8c3f32013-05-10 14:37:21 +01002938 r = dm_pool_register_metadata_threshold(pt->pool->pmd,
2939 calc_metadata_threshold(pt),
2940 metadata_low_callback,
2941 pool);
2942 if (r)
2943 goto out_free_pt;
2944
Joe Thornber991d9fa2011-10-31 20:21:18 +00002945 pt->callbacks.congested_fn = pool_is_congested;
2946 dm_table_add_target_callbacks(ti->table, &pt->callbacks);
2947
2948 mutex_unlock(&dm_thin_pool_table.mutex);
2949
2950 return 0;
2951
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002952out_flags_changed:
2953 __pool_dec(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002954out_free_pt:
2955 kfree(pt);
2956out:
2957 dm_put_device(ti, data_dev);
2958out_metadata:
2959 dm_put_device(ti, metadata_dev);
2960out_unlock:
2961 mutex_unlock(&dm_thin_pool_table.mutex);
2962
2963 return r;
2964}
2965
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002966static int pool_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002967{
2968 int r;
2969 struct pool_c *pt = ti->private;
2970 struct pool *pool = pt->pool;
2971 unsigned long flags;
2972
2973 /*
2974 * As this is a singleton target, ti->begin is always zero.
2975 */
2976 spin_lock_irqsave(&pool->lock, flags);
2977 bio->bi_bdev = pt->data_dev->bdev;
2978 r = DM_MAPIO_REMAPPED;
2979 spin_unlock_irqrestore(&pool->lock, flags);
2980
2981 return r;
2982}
2983
Joe Thornberb17446d2013-05-10 14:37:18 +01002984static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit)
2985{
2986 int r;
2987 struct pool_c *pt = ti->private;
2988 struct pool *pool = pt->pool;
2989 sector_t data_size = ti->len;
2990 dm_block_t sb_data_size;
2991
2992 *need_commit = false;
2993
2994 (void) sector_div(data_size, pool->sectors_per_block);
2995
2996 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
2997 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04002998 DMERR("%s: failed to retrieve data device size",
2999 dm_device_name(pool->pool_md));
Joe Thornberb17446d2013-05-10 14:37:18 +01003000 return r;
3001 }
3002
3003 if (data_size < sb_data_size) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003004 DMERR("%s: pool target (%llu blocks) too small: expected %llu",
3005 dm_device_name(pool->pool_md),
Joe Thornberb17446d2013-05-10 14:37:18 +01003006 (unsigned long long)data_size, sb_data_size);
3007 return -EINVAL;
3008
3009 } else if (data_size > sb_data_size) {
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05003010 if (dm_pool_metadata_needs_check(pool->pmd)) {
3011 DMERR("%s: unable to grow the data device until repaired.",
3012 dm_device_name(pool->pool_md));
3013 return 0;
3014 }
3015
Mike Snitzer6f7f51d2013-12-04 10:25:53 -05003016 if (sb_data_size)
3017 DMINFO("%s: growing the data device from %llu to %llu blocks",
3018 dm_device_name(pool->pool_md),
3019 sb_data_size, (unsigned long long)data_size);
Joe Thornberb17446d2013-05-10 14:37:18 +01003020 r = dm_pool_resize_data_dev(pool->pmd, data_size);
3021 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05003022 metadata_operation_failed(pool, "dm_pool_resize_data_dev", r);
Joe Thornberb17446d2013-05-10 14:37:18 +01003023 return r;
3024 }
3025
3026 *need_commit = true;
3027 }
3028
3029 return 0;
3030}
3031
Joe Thornber24347e92013-05-10 14:37:19 +01003032static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit)
3033{
3034 int r;
3035 struct pool_c *pt = ti->private;
3036 struct pool *pool = pt->pool;
3037 dm_block_t metadata_dev_size, sb_metadata_dev_size;
3038
3039 *need_commit = false;
3040
Alasdair G Kergon610bba82013-05-19 18:57:50 +01003041 metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev);
Joe Thornber24347e92013-05-10 14:37:19 +01003042
3043 r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size);
3044 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003045 DMERR("%s: failed to retrieve metadata device size",
3046 dm_device_name(pool->pool_md));
Joe Thornber24347e92013-05-10 14:37:19 +01003047 return r;
3048 }
3049
3050 if (metadata_dev_size < sb_metadata_dev_size) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003051 DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
3052 dm_device_name(pool->pool_md),
Joe Thornber24347e92013-05-10 14:37:19 +01003053 metadata_dev_size, sb_metadata_dev_size);
3054 return -EINVAL;
3055
3056 } else if (metadata_dev_size > sb_metadata_dev_size) {
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05003057 if (dm_pool_metadata_needs_check(pool->pmd)) {
3058 DMERR("%s: unable to grow the metadata device until repaired.",
3059 dm_device_name(pool->pool_md));
3060 return 0;
3061 }
3062
Mike Snitzer7d489352014-02-12 23:58:15 -05003063 warn_if_metadata_device_too_big(pool->md_dev);
Mike Snitzer6f7f51d2013-12-04 10:25:53 -05003064 DMINFO("%s: growing the metadata device from %llu to %llu blocks",
3065 dm_device_name(pool->pool_md),
3066 sb_metadata_dev_size, metadata_dev_size);
Joe Thornber24347e92013-05-10 14:37:19 +01003067 r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size);
3068 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05003069 metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r);
Joe Thornber24347e92013-05-10 14:37:19 +01003070 return r;
3071 }
3072
3073 *need_commit = true;
3074 }
3075
3076 return 0;
3077}
3078
Joe Thornber991d9fa2011-10-31 20:21:18 +00003079/*
3080 * Retrieves the number of blocks of the data device from
3081 * the superblock and compares it to the actual device size,
3082 * thus resizing the data device in case it has grown.
3083 *
3084 * This both copes with opening preallocated data devices in the ctr
3085 * being followed by a resume
3086 * -and-
3087 * calling the resume method individually after userspace has
3088 * grown the data device in reaction to a table event.
3089 */
3090static int pool_preresume(struct dm_target *ti)
3091{
3092 int r;
Joe Thornber24347e92013-05-10 14:37:19 +01003093 bool need_commit1, need_commit2;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003094 struct pool_c *pt = ti->private;
3095 struct pool *pool = pt->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003096
3097 /*
3098 * Take control of the pool object.
3099 */
3100 r = bind_control_target(pool, ti);
3101 if (r)
3102 return r;
3103
Joe Thornberb17446d2013-05-10 14:37:18 +01003104 r = maybe_resize_data_dev(ti, &need_commit1);
3105 if (r)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003106 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003107
Joe Thornber24347e92013-05-10 14:37:19 +01003108 r = maybe_resize_metadata_dev(ti, &need_commit2);
3109 if (r)
3110 return r;
3111
3112 if (need_commit1 || need_commit2)
Joe Thornber020cc3b2013-12-04 15:05:36 -05003113 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003114
3115 return 0;
3116}
3117
Mike Snitzer583024d2014-10-28 20:58:45 -04003118static void pool_suspend_active_thins(struct pool *pool)
3119{
3120 struct thin_c *tc;
3121
3122 /* Suspend all active thin devices */
3123 tc = get_first_thin(pool);
3124 while (tc) {
3125 dm_internal_suspend_noflush(tc->thin_md);
3126 tc = get_next_thin(pool, tc);
3127 }
3128}
3129
3130static void pool_resume_active_thins(struct pool *pool)
3131{
3132 struct thin_c *tc;
3133
3134 /* Resume all active thin devices */
3135 tc = get_first_thin(pool);
3136 while (tc) {
3137 dm_internal_resume(tc->thin_md);
3138 tc = get_next_thin(pool, tc);
3139 }
3140}
3141
Joe Thornber991d9fa2011-10-31 20:21:18 +00003142static void pool_resume(struct dm_target *ti)
3143{
3144 struct pool_c *pt = ti->private;
3145 struct pool *pool = pt->pool;
3146 unsigned long flags;
3147
Mike Snitzer583024d2014-10-28 20:58:45 -04003148 /*
3149 * Must requeue active_thins' bios and then resume
3150 * active_thins _before_ clearing 'suspend' flag.
3151 */
3152 requeue_bios(pool);
3153 pool_resume_active_thins(pool);
3154
Joe Thornber991d9fa2011-10-31 20:21:18 +00003155 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber88a66212013-12-04 20:16:12 -05003156 pool->low_water_triggered = false;
Mike Snitzer80e96c52014-11-07 15:09:46 -05003157 pool->suspended = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003158 spin_unlock_irqrestore(&pool->lock, flags);
Mike Snitzer80e96c52014-11-07 15:09:46 -05003159
Joe Thornber905e51b2012-03-28 18:41:27 +01003160 do_waker(&pool->waker.work);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003161}
3162
Mike Snitzer80e96c52014-11-07 15:09:46 -05003163static void pool_presuspend(struct dm_target *ti)
3164{
3165 struct pool_c *pt = ti->private;
3166 struct pool *pool = pt->pool;
3167 unsigned long flags;
3168
3169 spin_lock_irqsave(&pool->lock, flags);
3170 pool->suspended = true;
3171 spin_unlock_irqrestore(&pool->lock, flags);
Mike Snitzer583024d2014-10-28 20:58:45 -04003172
3173 pool_suspend_active_thins(pool);
Mike Snitzer80e96c52014-11-07 15:09:46 -05003174}
3175
3176static void pool_presuspend_undo(struct dm_target *ti)
3177{
3178 struct pool_c *pt = ti->private;
3179 struct pool *pool = pt->pool;
3180 unsigned long flags;
3181
Mike Snitzer583024d2014-10-28 20:58:45 -04003182 pool_resume_active_thins(pool);
3183
Mike Snitzer80e96c52014-11-07 15:09:46 -05003184 spin_lock_irqsave(&pool->lock, flags);
3185 pool->suspended = false;
3186 spin_unlock_irqrestore(&pool->lock, flags);
3187}
3188
Joe Thornber991d9fa2011-10-31 20:21:18 +00003189static void pool_postsuspend(struct dm_target *ti)
3190{
Joe Thornber991d9fa2011-10-31 20:21:18 +00003191 struct pool_c *pt = ti->private;
3192 struct pool *pool = pt->pool;
3193
Joe Thornber905e51b2012-03-28 18:41:27 +01003194 cancel_delayed_work(&pool->waker);
Joe Thornber85ad643b2014-05-09 15:59:38 +01003195 cancel_delayed_work(&pool->no_space_timeout);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003196 flush_workqueue(pool->wq);
Joe Thornber020cc3b2013-12-04 15:05:36 -05003197 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003198}
3199
3200static int check_arg_count(unsigned argc, unsigned args_required)
3201{
3202 if (argc != args_required) {
3203 DMWARN("Message received with %u arguments instead of %u.",
3204 argc, args_required);
3205 return -EINVAL;
3206 }
3207
3208 return 0;
3209}
3210
3211static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
3212{
3213 if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
3214 *dev_id <= MAX_DEV_ID)
3215 return 0;
3216
3217 if (warning)
3218 DMWARN("Message received with invalid device id: %s", arg);
3219
3220 return -EINVAL;
3221}
3222
3223static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
3224{
3225 dm_thin_id dev_id;
3226 int r;
3227
3228 r = check_arg_count(argc, 2);
3229 if (r)
3230 return r;
3231
3232 r = read_dev_id(argv[1], &dev_id, 1);
3233 if (r)
3234 return r;
3235
3236 r = dm_pool_create_thin(pool->pmd, dev_id);
3237 if (r) {
3238 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
3239 argv[1]);
3240 return r;
3241 }
3242
3243 return 0;
3244}
3245
3246static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3247{
3248 dm_thin_id dev_id;
3249 dm_thin_id origin_dev_id;
3250 int r;
3251
3252 r = check_arg_count(argc, 3);
3253 if (r)
3254 return r;
3255
3256 r = read_dev_id(argv[1], &dev_id, 1);
3257 if (r)
3258 return r;
3259
3260 r = read_dev_id(argv[2], &origin_dev_id, 1);
3261 if (r)
3262 return r;
3263
3264 r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
3265 if (r) {
3266 DMWARN("Creation of new snapshot %s of device %s failed.",
3267 argv[1], argv[2]);
3268 return r;
3269 }
3270
3271 return 0;
3272}
3273
3274static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
3275{
3276 dm_thin_id dev_id;
3277 int r;
3278
3279 r = check_arg_count(argc, 2);
3280 if (r)
3281 return r;
3282
3283 r = read_dev_id(argv[1], &dev_id, 1);
3284 if (r)
3285 return r;
3286
3287 r = dm_pool_delete_thin_device(pool->pmd, dev_id);
3288 if (r)
3289 DMWARN("Deletion of thin device %s failed.", argv[1]);
3290
3291 return r;
3292}
3293
3294static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
3295{
3296 dm_thin_id old_id, new_id;
3297 int r;
3298
3299 r = check_arg_count(argc, 3);
3300 if (r)
3301 return r;
3302
3303 if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
3304 DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
3305 return -EINVAL;
3306 }
3307
3308 if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
3309 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
3310 return -EINVAL;
3311 }
3312
3313 r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
3314 if (r) {
3315 DMWARN("Failed to change transaction id from %s to %s.",
3316 argv[1], argv[2]);
3317 return r;
3318 }
3319
3320 return 0;
3321}
3322
Joe Thornbercc8394d2012-06-03 00:30:01 +01003323static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3324{
3325 int r;
3326
3327 r = check_arg_count(argc, 1);
3328 if (r)
3329 return r;
3330
Joe Thornber020cc3b2013-12-04 15:05:36 -05003331 (void) commit(pool);
Joe Thornber0d200ae2012-07-03 12:55:31 +01003332
Joe Thornbercc8394d2012-06-03 00:30:01 +01003333 r = dm_pool_reserve_metadata_snap(pool->pmd);
3334 if (r)
3335 DMWARN("reserve_metadata_snap message failed.");
3336
3337 return r;
3338}
3339
3340static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3341{
3342 int r;
3343
3344 r = check_arg_count(argc, 1);
3345 if (r)
3346 return r;
3347
3348 r = dm_pool_release_metadata_snap(pool->pmd);
3349 if (r)
3350 DMWARN("release_metadata_snap message failed.");
3351
3352 return r;
3353}
3354
Joe Thornber991d9fa2011-10-31 20:21:18 +00003355/*
3356 * Messages supported:
3357 * create_thin <dev_id>
3358 * create_snap <dev_id> <origin_id>
3359 * delete <dev_id>
Joe Thornber991d9fa2011-10-31 20:21:18 +00003360 * set_transaction_id <current_trans_id> <new_trans_id>
Joe Thornbercc8394d2012-06-03 00:30:01 +01003361 * reserve_metadata_snap
3362 * release_metadata_snap
Joe Thornber991d9fa2011-10-31 20:21:18 +00003363 */
3364static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
3365{
3366 int r = -EINVAL;
3367 struct pool_c *pt = ti->private;
3368 struct pool *pool = pt->pool;
3369
Joe Thornber2a7eaea2015-01-26 11:38:21 +00003370 if (get_pool_mode(pool) >= PM_READ_ONLY) {
3371 DMERR("%s: unable to service pool target messages in READ_ONLY or FAIL mode",
3372 dm_device_name(pool->pool_md));
3373 return -EINVAL;
3374 }
3375
Joe Thornber991d9fa2011-10-31 20:21:18 +00003376 if (!strcasecmp(argv[0], "create_thin"))
3377 r = process_create_thin_mesg(argc, argv, pool);
3378
3379 else if (!strcasecmp(argv[0], "create_snap"))
3380 r = process_create_snap_mesg(argc, argv, pool);
3381
3382 else if (!strcasecmp(argv[0], "delete"))
3383 r = process_delete_mesg(argc, argv, pool);
3384
3385 else if (!strcasecmp(argv[0], "set_transaction_id"))
3386 r = process_set_transaction_id_mesg(argc, argv, pool);
3387
Joe Thornbercc8394d2012-06-03 00:30:01 +01003388 else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
3389 r = process_reserve_metadata_snap_mesg(argc, argv, pool);
3390
3391 else if (!strcasecmp(argv[0], "release_metadata_snap"))
3392 r = process_release_metadata_snap_mesg(argc, argv, pool);
3393
Joe Thornber991d9fa2011-10-31 20:21:18 +00003394 else
3395 DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
3396
Joe Thornbere49e5822012-07-27 15:08:16 +01003397 if (!r)
Joe Thornber020cc3b2013-12-04 15:05:36 -05003398 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003399
3400 return r;
3401}
3402
Joe Thornbere49e5822012-07-27 15:08:16 +01003403static void emit_flags(struct pool_features *pf, char *result,
3404 unsigned sz, unsigned maxlen)
3405{
3406 unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
Mike Snitzer787a996c2013-12-06 16:21:43 -05003407 !pf->discard_passdown + (pf->mode == PM_READ_ONLY) +
3408 pf->error_if_no_space;
Joe Thornbere49e5822012-07-27 15:08:16 +01003409 DMEMIT("%u ", count);
3410
3411 if (!pf->zero_new_blocks)
3412 DMEMIT("skip_block_zeroing ");
3413
3414 if (!pf->discard_enabled)
3415 DMEMIT("ignore_discard ");
3416
3417 if (!pf->discard_passdown)
3418 DMEMIT("no_discard_passdown ");
3419
3420 if (pf->mode == PM_READ_ONLY)
3421 DMEMIT("read_only ");
Mike Snitzer787a996c2013-12-06 16:21:43 -05003422
3423 if (pf->error_if_no_space)
3424 DMEMIT("error_if_no_space ");
Joe Thornbere49e5822012-07-27 15:08:16 +01003425}
3426
Joe Thornber991d9fa2011-10-31 20:21:18 +00003427/*
3428 * Status line is:
3429 * <transaction id> <used metadata sectors>/<total metadata sectors>
3430 * <used data sectors>/<total data sectors> <held metadata root>
3431 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003432static void pool_status(struct dm_target *ti, status_type_t type,
3433 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003434{
Joe Thornbere49e5822012-07-27 15:08:16 +01003435 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003436 unsigned sz = 0;
3437 uint64_t transaction_id;
3438 dm_block_t nr_free_blocks_data;
3439 dm_block_t nr_free_blocks_metadata;
3440 dm_block_t nr_blocks_data;
3441 dm_block_t nr_blocks_metadata;
3442 dm_block_t held_root;
3443 char buf[BDEVNAME_SIZE];
3444 char buf2[BDEVNAME_SIZE];
3445 struct pool_c *pt = ti->private;
3446 struct pool *pool = pt->pool;
3447
3448 switch (type) {
3449 case STATUSTYPE_INFO:
Joe Thornbere49e5822012-07-27 15:08:16 +01003450 if (get_pool_mode(pool) == PM_FAIL) {
3451 DMEMIT("Fail");
3452 break;
3453 }
3454
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01003455 /* Commit to ensure statistics aren't out-of-date */
3456 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
Joe Thornber020cc3b2013-12-04 15:05:36 -05003457 (void) commit(pool);
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01003458
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003459 r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id);
3460 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003461 DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
3462 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003463 goto err;
3464 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003465
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003466 r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata);
3467 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003468 DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
3469 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003470 goto err;
3471 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003472
3473 r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003474 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003475 DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
3476 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003477 goto err;
3478 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003479
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003480 r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data);
3481 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003482 DMERR("%s: dm_pool_get_free_block_count returned %d",
3483 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003484 goto err;
3485 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003486
3487 r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003488 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003489 DMERR("%s: dm_pool_get_data_dev_size returned %d",
3490 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003491 goto err;
3492 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003493
Joe Thornbercc8394d2012-06-03 00:30:01 +01003494 r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003495 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003496 DMERR("%s: dm_pool_get_metadata_snap returned %d",
3497 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003498 goto err;
3499 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003500
3501 DMEMIT("%llu %llu/%llu %llu/%llu ",
3502 (unsigned long long)transaction_id,
3503 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
3504 (unsigned long long)nr_blocks_metadata,
3505 (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
3506 (unsigned long long)nr_blocks_data);
3507
3508 if (held_root)
Joe Thornbere49e5822012-07-27 15:08:16 +01003509 DMEMIT("%llu ", held_root);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003510 else
Joe Thornbere49e5822012-07-27 15:08:16 +01003511 DMEMIT("- ");
3512
Joe Thornber3e1a0692014-03-03 16:03:26 +00003513 if (pool->pf.mode == PM_OUT_OF_DATA_SPACE)
3514 DMEMIT("out_of_data_space ");
3515 else if (pool->pf.mode == PM_READ_ONLY)
Joe Thornbere49e5822012-07-27 15:08:16 +01003516 DMEMIT("ro ");
3517 else
3518 DMEMIT("rw ");
3519
Mike Snitzer018debe2012-12-21 20:23:32 +00003520 if (!pool->pf.discard_enabled)
Mike Snitzer787a996c2013-12-06 16:21:43 -05003521 DMEMIT("ignore_discard ");
Mike Snitzer018debe2012-12-21 20:23:32 +00003522 else if (pool->pf.discard_passdown)
Mike Snitzer787a996c2013-12-06 16:21:43 -05003523 DMEMIT("discard_passdown ");
Joe Thornbere49e5822012-07-27 15:08:16 +01003524 else
Mike Snitzer787a996c2013-12-06 16:21:43 -05003525 DMEMIT("no_discard_passdown ");
3526
3527 if (pool->pf.error_if_no_space)
3528 DMEMIT("error_if_no_space ");
3529 else
3530 DMEMIT("queue_if_no_space ");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003531
3532 break;
3533
3534 case STATUSTYPE_TABLE:
3535 DMEMIT("%s %s %lu %llu ",
3536 format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
3537 format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
3538 (unsigned long)pool->sectors_per_block,
3539 (unsigned long long)pt->low_water_blocks);
Mike Snitzer0424caa2012-09-26 23:45:47 +01003540 emit_flags(&pt->requested_pf, result, sz, maxlen);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003541 break;
3542 }
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003543 return;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003544
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003545err:
3546 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003547}
3548
3549static int pool_iterate_devices(struct dm_target *ti,
3550 iterate_devices_callout_fn fn, void *data)
3551{
3552 struct pool_c *pt = ti->private;
3553
3554 return fn(ti, pt->data_dev, 0, ti->len, data);
3555}
3556
3557static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
3558 struct bio_vec *biovec, int max_size)
3559{
3560 struct pool_c *pt = ti->private;
3561 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
3562
3563 if (!q->merge_bvec_fn)
3564 return max_size;
3565
3566 bvm->bi_bdev = pt->data_dev->bdev;
3567
3568 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
3569}
3570
Mike Snitzer0424caa2012-09-26 23:45:47 +01003571static void set_discard_limits(struct pool_c *pt, struct queue_limits *limits)
Joe Thornber104655f2012-03-28 18:41:28 +01003572{
Mike Snitzer0424caa2012-09-26 23:45:47 +01003573 struct pool *pool = pt->pool;
3574 struct queue_limits *data_limits;
3575
Joe Thornber104655f2012-03-28 18:41:28 +01003576 limits->max_discard_sectors = pool->sectors_per_block;
3577
3578 /*
Mike Snitzer0424caa2012-09-26 23:45:47 +01003579 * discard_granularity is just a hint, and not enforced.
Joe Thornber104655f2012-03-28 18:41:28 +01003580 */
Mike Snitzer0424caa2012-09-26 23:45:47 +01003581 if (pt->adjusted_pf.discard_passdown) {
3582 data_limits = &bdev_get_queue(pt->data_dev->bdev)->limits;
Lukas Czerner09869de2014-06-11 12:28:43 -04003583 limits->discard_granularity = max(data_limits->discard_granularity,
3584 pool->sectors_per_block << SECTOR_SHIFT);
Mike Snitzerf13945d2013-03-01 22:45:44 +00003585 } else
Mike Snitzer0424caa2012-09-26 23:45:47 +01003586 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
Joe Thornber104655f2012-03-28 18:41:28 +01003587}
3588
Joe Thornber991d9fa2011-10-31 20:21:18 +00003589static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
3590{
3591 struct pool_c *pt = ti->private;
3592 struct pool *pool = pt->pool;
Mike Snitzer604ea902014-10-09 18:43:25 -04003593 sector_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
3594
3595 /*
Mike Snitzerd200c302014-11-20 18:07:43 -05003596 * If max_sectors is smaller than pool->sectors_per_block adjust it
3597 * to the highest possible power-of-2 factor of pool->sectors_per_block.
3598 * This is especially beneficial when the pool's data device is a RAID
3599 * device that has a full stripe width that matches pool->sectors_per_block
3600 * -- because even though partial RAID stripe-sized IOs will be issued to a
3601 * single RAID stripe; when aggregated they will end on a full RAID stripe
3602 * boundary.. which avoids additional partial RAID stripe writes cascading
Mike Snitzer604ea902014-10-09 18:43:25 -04003603 */
Mike Snitzer604ea902014-10-09 18:43:25 -04003604 if (limits->max_sectors < pool->sectors_per_block) {
3605 while (!is_factor(pool->sectors_per_block, limits->max_sectors)) {
3606 if ((limits->max_sectors & (limits->max_sectors - 1)) == 0)
3607 limits->max_sectors--;
3608 limits->max_sectors = rounddown_pow_of_two(limits->max_sectors);
3609 }
Mike Snitzer604ea902014-10-09 18:43:25 -04003610 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003611
Mike Snitzer0cc67cd2013-08-20 15:02:41 -04003612 /*
3613 * If the system-determined stacked limits are compatible with the
3614 * pool's blocksize (io_opt is a factor) do not override them.
3615 */
3616 if (io_opt_sectors < pool->sectors_per_block ||
Mike Snitzer604ea902014-10-09 18:43:25 -04003617 !is_factor(io_opt_sectors, pool->sectors_per_block)) {
3618 if (is_factor(pool->sectors_per_block, limits->max_sectors))
3619 blk_limits_io_min(limits, limits->max_sectors << SECTOR_SHIFT);
3620 else
3621 blk_limits_io_min(limits, pool->sectors_per_block << SECTOR_SHIFT);
Mike Snitzer0cc67cd2013-08-20 15:02:41 -04003622 blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
3623 }
Mike Snitzer0424caa2012-09-26 23:45:47 +01003624
3625 /*
3626 * pt->adjusted_pf is a staging area for the actual features to use.
3627 * They get transferred to the live pool in bind_control_target()
3628 * called from pool_preresume().
3629 */
Mike Snitzerb60ab992013-09-19 18:49:11 -04003630 if (!pt->adjusted_pf.discard_enabled) {
3631 /*
3632 * Must explicitly disallow stacking discard limits otherwise the
3633 * block layer will stack them if pool's data device has support.
3634 * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the
3635 * user to see that, so make sure to set all discard limits to 0.
3636 */
3637 limits->discard_granularity = 0;
Mike Snitzer0424caa2012-09-26 23:45:47 +01003638 return;
Mike Snitzerb60ab992013-09-19 18:49:11 -04003639 }
Mike Snitzer0424caa2012-09-26 23:45:47 +01003640
3641 disable_passdown_if_not_supported(pt);
3642
3643 set_discard_limits(pt, limits);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003644}
3645
3646static struct target_type pool_target = {
3647 .name = "thin-pool",
3648 .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
3649 DM_TARGET_IMMUTABLE,
Mike Snitzer36f12ae2014-10-09 15:24:12 -04003650 .version = {1, 14, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00003651 .module = THIS_MODULE,
3652 .ctr = pool_ctr,
3653 .dtr = pool_dtr,
3654 .map = pool_map,
Mike Snitzer80e96c52014-11-07 15:09:46 -05003655 .presuspend = pool_presuspend,
3656 .presuspend_undo = pool_presuspend_undo,
Joe Thornber991d9fa2011-10-31 20:21:18 +00003657 .postsuspend = pool_postsuspend,
3658 .preresume = pool_preresume,
3659 .resume = pool_resume,
3660 .message = pool_message,
3661 .status = pool_status,
3662 .merge = pool_merge,
3663 .iterate_devices = pool_iterate_devices,
3664 .io_hints = pool_io_hints,
3665};
3666
3667/*----------------------------------------------------------------
3668 * Thin target methods
3669 *--------------------------------------------------------------*/
Joe Thornberb10ebd32014-04-08 11:29:01 +01003670static void thin_get(struct thin_c *tc)
3671{
3672 atomic_inc(&tc->refcount);
3673}
3674
3675static void thin_put(struct thin_c *tc)
3676{
3677 if (atomic_dec_and_test(&tc->refcount))
3678 complete(&tc->can_destroy);
3679}
3680
Joe Thornber991d9fa2011-10-31 20:21:18 +00003681static void thin_dtr(struct dm_target *ti)
3682{
3683 struct thin_c *tc = ti->private;
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003684 unsigned long flags;
3685
3686 spin_lock_irqsave(&tc->pool->lock, flags);
3687 list_del_rcu(&tc->list);
3688 spin_unlock_irqrestore(&tc->pool->lock, flags);
3689 synchronize_rcu();
Joe Thornber991d9fa2011-10-31 20:21:18 +00003690
Mikulas Patocka17181fb2014-11-05 17:00:13 -05003691 thin_put(tc);
3692 wait_for_completion(&tc->can_destroy);
3693
Joe Thornber991d9fa2011-10-31 20:21:18 +00003694 mutex_lock(&dm_thin_pool_table.mutex);
3695
3696 __pool_dec(tc->pool);
3697 dm_pool_close_thin_device(tc->td);
3698 dm_put_device(ti, tc->pool_dev);
Joe Thornber2dd9c252012-03-28 18:41:28 +01003699 if (tc->origin_dev)
3700 dm_put_device(ti, tc->origin_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003701 kfree(tc);
3702
3703 mutex_unlock(&dm_thin_pool_table.mutex);
3704}
3705
3706/*
3707 * Thin target parameters:
3708 *
Joe Thornber2dd9c252012-03-28 18:41:28 +01003709 * <pool_dev> <dev_id> [origin_dev]
Joe Thornber991d9fa2011-10-31 20:21:18 +00003710 *
3711 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
3712 * dev_id: the internal device identifier
Joe Thornber2dd9c252012-03-28 18:41:28 +01003713 * origin_dev: a device external to the pool that should act as the origin
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003714 *
3715 * If the pool device has discards disabled, they get disabled for the thin
3716 * device as well.
Joe Thornber991d9fa2011-10-31 20:21:18 +00003717 */
3718static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
3719{
3720 int r;
3721 struct thin_c *tc;
Joe Thornber2dd9c252012-03-28 18:41:28 +01003722 struct dm_dev *pool_dev, *origin_dev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003723 struct mapped_device *pool_md;
Joe Thornber5e3283e2014-04-08 11:08:41 +01003724 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003725
3726 mutex_lock(&dm_thin_pool_table.mutex);
3727
Joe Thornber2dd9c252012-03-28 18:41:28 +01003728 if (argc != 2 && argc != 3) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00003729 ti->error = "Invalid argument count";
3730 r = -EINVAL;
3731 goto out_unlock;
3732 }
3733
3734 tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
3735 if (!tc) {
3736 ti->error = "Out of memory";
3737 r = -ENOMEM;
3738 goto out_unlock;
3739 }
Mike Snitzer583024d2014-10-28 20:58:45 -04003740 tc->thin_md = dm_table_get_md(ti->table);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003741 spin_lock_init(&tc->lock);
Joe Thornbera374bb22014-10-10 13:43:14 +01003742 INIT_LIST_HEAD(&tc->deferred_cells);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003743 bio_list_init(&tc->deferred_bio_list);
3744 bio_list_init(&tc->retry_on_resume_list);
Mike Snitzer67324ea2014-03-21 18:33:41 -04003745 tc->sort_bio_list = RB_ROOT;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003746
Joe Thornber2dd9c252012-03-28 18:41:28 +01003747 if (argc == 3) {
3748 r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
3749 if (r) {
3750 ti->error = "Error opening origin device";
3751 goto bad_origin_dev;
3752 }
3753 tc->origin_dev = origin_dev;
3754 }
3755
Joe Thornber991d9fa2011-10-31 20:21:18 +00003756 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
3757 if (r) {
3758 ti->error = "Error opening pool device";
3759 goto bad_pool_dev;
3760 }
3761 tc->pool_dev = pool_dev;
3762
3763 if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
3764 ti->error = "Invalid device id";
3765 r = -EINVAL;
3766 goto bad_common;
3767 }
3768
3769 pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
3770 if (!pool_md) {
3771 ti->error = "Couldn't get pool mapped device";
3772 r = -EINVAL;
3773 goto bad_common;
3774 }
3775
3776 tc->pool = __pool_table_lookup(pool_md);
3777 if (!tc->pool) {
3778 ti->error = "Couldn't find pool object";
3779 r = -EINVAL;
3780 goto bad_pool_lookup;
3781 }
3782 __pool_inc(tc->pool);
3783
Joe Thornbere49e5822012-07-27 15:08:16 +01003784 if (get_pool_mode(tc->pool) == PM_FAIL) {
3785 ti->error = "Couldn't open thin device, Pool is in fail mode";
Mike Snitzer1acacc02014-02-19 20:32:33 -05003786 r = -EINVAL;
Mike Snitzer80e96c52014-11-07 15:09:46 -05003787 goto bad_pool;
Joe Thornbere49e5822012-07-27 15:08:16 +01003788 }
3789
Joe Thornber991d9fa2011-10-31 20:21:18 +00003790 r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
3791 if (r) {
3792 ti->error = "Couldn't open thin internal device";
Mike Snitzer80e96c52014-11-07 15:09:46 -05003793 goto bad_pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003794 }
3795
Mike Snitzer542f9032012-07-27 15:08:00 +01003796 r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
3797 if (r)
Mike Snitzer80e96c52014-11-07 15:09:46 -05003798 goto bad;
Mike Snitzer542f9032012-07-27 15:08:00 +01003799
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003800 ti->num_flush_bios = 1;
Joe Thornber16ad3d12012-07-27 15:08:07 +01003801 ti->flush_supported = true;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00003802 ti->per_bio_data_size = sizeof(struct dm_thin_endio_hook);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003803
3804 /* In case the pool supports discards, pass them on. */
Mike Snitzerb60ab992013-09-19 18:49:11 -04003805 ti->discard_zeroes_data_unsupported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003806 if (tc->pool->pf.discard_enabled) {
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01003807 ti->discards_supported = true;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003808 ti->num_discard_bios = 1;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003809 /* Discard bios must be split on a block boundary */
3810 ti->split_discard_bios = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003811 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003812
Joe Thornber991d9fa2011-10-31 20:21:18 +00003813 mutex_unlock(&dm_thin_pool_table.mutex);
3814
Joe Thornber5e3283e2014-04-08 11:08:41 +01003815 spin_lock_irqsave(&tc->pool->lock, flags);
Mike Snitzer80e96c52014-11-07 15:09:46 -05003816 if (tc->pool->suspended) {
3817 spin_unlock_irqrestore(&tc->pool->lock, flags);
3818 mutex_lock(&dm_thin_pool_table.mutex); /* reacquire for __pool_dec */
3819 ti->error = "Unable to activate thin device while pool is suspended";
3820 r = -EINVAL;
3821 goto bad;
3822 }
Marc Dionne2b94e892014-12-17 07:59:59 -05003823 atomic_set(&tc->refcount, 1);
3824 init_completion(&tc->can_destroy);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003825 list_add_tail_rcu(&tc->list, &tc->pool->active_thins);
Joe Thornber5e3283e2014-04-08 11:08:41 +01003826 spin_unlock_irqrestore(&tc->pool->lock, flags);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003827 /*
3828 * This synchronize_rcu() call is needed here otherwise we risk a
3829 * wake_worker() call finding no bios to process (because the newly
3830 * added tc isn't yet visible). So this reduces latency since we
3831 * aren't then dependent on the periodic commit to wake_worker().
3832 */
3833 synchronize_rcu();
3834
Mike Snitzer80e96c52014-11-07 15:09:46 -05003835 dm_put(pool_md);
3836
Joe Thornber991d9fa2011-10-31 20:21:18 +00003837 return 0;
3838
Mike Snitzer80e96c52014-11-07 15:09:46 -05003839bad:
Mike Snitzer1acacc02014-02-19 20:32:33 -05003840 dm_pool_close_thin_device(tc->td);
Mike Snitzer80e96c52014-11-07 15:09:46 -05003841bad_pool:
Joe Thornber991d9fa2011-10-31 20:21:18 +00003842 __pool_dec(tc->pool);
3843bad_pool_lookup:
3844 dm_put(pool_md);
3845bad_common:
3846 dm_put_device(ti, tc->pool_dev);
3847bad_pool_dev:
Joe Thornber2dd9c252012-03-28 18:41:28 +01003848 if (tc->origin_dev)
3849 dm_put_device(ti, tc->origin_dev);
3850bad_origin_dev:
Joe Thornber991d9fa2011-10-31 20:21:18 +00003851 kfree(tc);
3852out_unlock:
3853 mutex_unlock(&dm_thin_pool_table.mutex);
3854
3855 return r;
3856}
3857
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003858static int thin_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003859{
Kent Overstreet4f024f32013-10-11 15:44:27 -07003860 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003861
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003862 return thin_bio_map(ti, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003863}
3864
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003865static int thin_endio(struct dm_target *ti, struct bio *bio, int err)
Joe Thornbereb2aa482012-03-28 18:41:28 +01003866{
3867 unsigned long flags;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00003868 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01003869 struct list_head work;
Mike Snitzera24c2562012-06-03 00:30:00 +01003870 struct dm_thin_new_mapping *m, *tmp;
Joe Thornbereb2aa482012-03-28 18:41:28 +01003871 struct pool *pool = h->tc->pool;
3872
3873 if (h->shared_read_entry) {
3874 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01003875 dm_deferred_entry_dec(h->shared_read_entry, &work);
Joe Thornbereb2aa482012-03-28 18:41:28 +01003876
3877 spin_lock_irqsave(&pool->lock, flags);
3878 list_for_each_entry_safe(m, tmp, &work, list) {
3879 list_del(&m->list);
Joe Thornber50f3c3e2014-06-13 13:57:09 +01003880 __complete_mapping_preparation(m);
Joe Thornbereb2aa482012-03-28 18:41:28 +01003881 }
3882 spin_unlock_irqrestore(&pool->lock, flags);
3883 }
3884
Joe Thornber104655f2012-03-28 18:41:28 +01003885 if (h->all_io_entry) {
3886 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01003887 dm_deferred_entry_dec(h->all_io_entry, &work);
Joe Thornber563af182012-12-21 20:23:31 +00003888 if (!list_empty(&work)) {
3889 spin_lock_irqsave(&pool->lock, flags);
3890 list_for_each_entry_safe(m, tmp, &work, list)
Mike Snitzerdaec3382013-12-11 14:01:20 -05003891 list_add_tail(&m->list, &pool->prepared_discards);
Joe Thornber563af182012-12-21 20:23:31 +00003892 spin_unlock_irqrestore(&pool->lock, flags);
3893 wake_worker(pool);
3894 }
Joe Thornber104655f2012-03-28 18:41:28 +01003895 }
3896
Joe Thornbereb2aa482012-03-28 18:41:28 +01003897 return 0;
3898}
3899
Joe Thornber738211f2014-03-03 15:52:28 +00003900static void thin_presuspend(struct dm_target *ti)
3901{
3902 struct thin_c *tc = ti->private;
3903
3904 if (dm_noflush_suspending(ti))
3905 noflush_work(tc, do_noflush_start);
3906}
3907
Joe Thornber991d9fa2011-10-31 20:21:18 +00003908static void thin_postsuspend(struct dm_target *ti)
3909{
Joe Thornber738211f2014-03-03 15:52:28 +00003910 struct thin_c *tc = ti->private;
3911
3912 /*
3913 * The dm_noflush_suspending flag has been cleared by now, so
3914 * unfortunately we must always run this.
3915 */
3916 noflush_work(tc, do_noflush_stop);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003917}
3918
Joe Thornbere5aea7b2014-06-13 14:47:24 +01003919static int thin_preresume(struct dm_target *ti)
3920{
3921 struct thin_c *tc = ti->private;
3922
3923 if (tc->origin_dev)
3924 tc->origin_size = get_dev_size(tc->origin_dev->bdev);
3925
3926 return 0;
3927}
3928
Joe Thornber991d9fa2011-10-31 20:21:18 +00003929/*
3930 * <nr mapped sectors> <highest mapped sector>
3931 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003932static void thin_status(struct dm_target *ti, status_type_t type,
3933 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003934{
3935 int r;
3936 ssize_t sz = 0;
3937 dm_block_t mapped, highest;
3938 char buf[BDEVNAME_SIZE];
3939 struct thin_c *tc = ti->private;
3940
Joe Thornbere49e5822012-07-27 15:08:16 +01003941 if (get_pool_mode(tc->pool) == PM_FAIL) {
3942 DMEMIT("Fail");
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003943 return;
Joe Thornbere49e5822012-07-27 15:08:16 +01003944 }
3945
Joe Thornber991d9fa2011-10-31 20:21:18 +00003946 if (!tc->td)
3947 DMEMIT("-");
3948 else {
3949 switch (type) {
3950 case STATUSTYPE_INFO:
3951 r = dm_thin_get_mapped_count(tc->td, &mapped);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003952 if (r) {
3953 DMERR("dm_thin_get_mapped_count returned %d", r);
3954 goto err;
3955 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003956
3957 r = dm_thin_get_highest_mapped_block(tc->td, &highest);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003958 if (r < 0) {
3959 DMERR("dm_thin_get_highest_mapped_block returned %d", r);
3960 goto err;
3961 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003962
3963 DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
3964 if (r)
3965 DMEMIT("%llu", ((highest + 1) *
3966 tc->pool->sectors_per_block) - 1);
3967 else
3968 DMEMIT("-");
3969 break;
3970
3971 case STATUSTYPE_TABLE:
3972 DMEMIT("%s %lu",
3973 format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
3974 (unsigned long) tc->dev_id);
Joe Thornber2dd9c252012-03-28 18:41:28 +01003975 if (tc->origin_dev)
3976 DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
Joe Thornber991d9fa2011-10-31 20:21:18 +00003977 break;
3978 }
3979 }
3980
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003981 return;
3982
3983err:
3984 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003985}
3986
Mike Snitzer36f12ae2014-10-09 15:24:12 -04003987static int thin_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
3988 struct bio_vec *biovec, int max_size)
3989{
3990 struct thin_c *tc = ti->private;
3991 struct request_queue *q = bdev_get_queue(tc->pool_dev->bdev);
3992
3993 if (!q->merge_bvec_fn)
3994 return max_size;
3995
3996 bvm->bi_bdev = tc->pool_dev->bdev;
3997 bvm->bi_sector = dm_target_offset(ti, bvm->bi_sector);
3998
3999 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
4000}
4001
Joe Thornber991d9fa2011-10-31 20:21:18 +00004002static int thin_iterate_devices(struct dm_target *ti,
4003 iterate_devices_callout_fn fn, void *data)
4004{
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004005 sector_t blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004006 struct thin_c *tc = ti->private;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004007 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004008
4009 /*
4010 * We can't call dm_pool_get_data_dev_size() since that blocks. So
4011 * we follow a more convoluted path through to the pool's target.
4012 */
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004013 if (!pool->ti)
Joe Thornber991d9fa2011-10-31 20:21:18 +00004014 return 0; /* nothing is bound */
4015
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004016 blocks = pool->ti->len;
4017 (void) sector_div(blocks, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004018 if (blocks)
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004019 return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004020
4021 return 0;
4022}
4023
Joe Thornber991d9fa2011-10-31 20:21:18 +00004024static struct target_type thin_target = {
4025 .name = "thin",
Mike Snitzer36f12ae2014-10-09 15:24:12 -04004026 .version = {1, 14, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00004027 .module = THIS_MODULE,
4028 .ctr = thin_ctr,
4029 .dtr = thin_dtr,
4030 .map = thin_map,
Joe Thornbereb2aa482012-03-28 18:41:28 +01004031 .end_io = thin_endio,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01004032 .preresume = thin_preresume,
Joe Thornber738211f2014-03-03 15:52:28 +00004033 .presuspend = thin_presuspend,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004034 .postsuspend = thin_postsuspend,
4035 .status = thin_status,
Mike Snitzer36f12ae2014-10-09 15:24:12 -04004036 .merge = thin_merge,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004037 .iterate_devices = thin_iterate_devices,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004038};
4039
4040/*----------------------------------------------------------------*/
4041
4042static int __init dm_thin_init(void)
4043{
4044 int r;
4045
4046 pool_table_init();
4047
4048 r = dm_register_target(&thin_target);
4049 if (r)
4050 return r;
4051
4052 r = dm_register_target(&pool_target);
4053 if (r)
Mike Snitzera24c2562012-06-03 00:30:00 +01004054 goto bad_pool_target;
4055
4056 r = -ENOMEM;
4057
Mike Snitzera24c2562012-06-03 00:30:00 +01004058 _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
4059 if (!_new_mapping_cache)
4060 goto bad_new_mapping_cache;
4061
Mike Snitzera24c2562012-06-03 00:30:00 +01004062 return 0;
4063
Mike Snitzera24c2562012-06-03 00:30:00 +01004064bad_new_mapping_cache:
Mike Snitzera24c2562012-06-03 00:30:00 +01004065 dm_unregister_target(&pool_target);
4066bad_pool_target:
4067 dm_unregister_target(&thin_target);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004068
4069 return r;
4070}
4071
4072static void dm_thin_exit(void)
4073{
4074 dm_unregister_target(&thin_target);
4075 dm_unregister_target(&pool_target);
Mike Snitzera24c2562012-06-03 00:30:00 +01004076
Mike Snitzera24c2562012-06-03 00:30:00 +01004077 kmem_cache_destroy(_new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004078}
4079
4080module_init(dm_thin_init);
4081module_exit(dm_thin_exit);
4082
Mike Snitzer80c57892014-05-20 13:38:33 -04004083module_param_named(no_space_timeout, no_space_timeout_secs, uint, S_IRUGO | S_IWUSR);
4084MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds");
4085
Alasdair G Kergon7cab8bf2012-05-12 01:43:19 +01004086MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
Joe Thornber991d9fa2011-10-31 20:21:18 +00004087MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
4088MODULE_LICENSE("GPL");