blob: c33f61a4cc28d8b772b9444b710e7f55fa6df2fb [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 */
Joe Thornber34fbcf62015-04-16 12:58:35 +0100114enum lock_space {
115 VIRTUAL,
116 PHYSICAL
117};
118
119static void build_key(struct dm_thin_device *td, enum lock_space ls,
120 dm_block_t b, dm_block_t e, struct dm_cell_key *key)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000121{
Joe Thornber34fbcf62015-04-16 12:58:35 +0100122 key->virtual = (ls == VIRTUAL);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000123 key->dev = dm_thin_dev_id(td);
Joe Thornber5f274d82014-09-17 10:17:39 +0100124 key->block_begin = b;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100125 key->block_end = e;
126}
127
128static void build_data_key(struct dm_thin_device *td, dm_block_t b,
129 struct dm_cell_key *key)
130{
131 build_key(td, PHYSICAL, b, b + 1llu, key);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000132}
133
134static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
Mike Snitzer44feb382012-10-12 21:02:10 +0100135 struct dm_cell_key *key)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000136{
Joe Thornber34fbcf62015-04-16 12:58:35 +0100137 build_key(td, VIRTUAL, b, b + 1llu, key);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000138}
139
140/*----------------------------------------------------------------*/
141
Joe Thornber7d327fe2014-10-06 15:45:59 +0100142#define THROTTLE_THRESHOLD (1 * HZ)
143
144struct throttle {
145 struct rw_semaphore lock;
146 unsigned long threshold;
147 bool throttle_applied;
148};
149
150static void throttle_init(struct throttle *t)
151{
152 init_rwsem(&t->lock);
153 t->throttle_applied = false;
154}
155
156static void throttle_work_start(struct throttle *t)
157{
158 t->threshold = jiffies + THROTTLE_THRESHOLD;
159}
160
161static void throttle_work_update(struct throttle *t)
162{
163 if (!t->throttle_applied && jiffies > t->threshold) {
164 down_write(&t->lock);
165 t->throttle_applied = true;
166 }
167}
168
169static void throttle_work_complete(struct throttle *t)
170{
171 if (t->throttle_applied) {
172 t->throttle_applied = false;
173 up_write(&t->lock);
174 }
175}
176
177static void throttle_lock(struct throttle *t)
178{
179 down_read(&t->lock);
180}
181
182static void throttle_unlock(struct throttle *t)
183{
184 up_read(&t->lock);
185}
186
187/*----------------------------------------------------------------*/
188
Joe Thornber991d9fa2011-10-31 20:21:18 +0000189/*
190 * A pool device ties together a metadata device and a data device. It
191 * also provides the interface for creating and destroying internal
192 * devices.
193 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100194struct dm_thin_new_mapping;
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100195
Joe Thornbere49e5822012-07-27 15:08:16 +0100196/*
Joe Thornber3e1a0692014-03-03 16:03:26 +0000197 * The pool runs in 4 modes. Ordered in degraded order for comparisons.
Joe Thornbere49e5822012-07-27 15:08:16 +0100198 */
199enum pool_mode {
200 PM_WRITE, /* metadata may be changed */
Joe Thornber3e1a0692014-03-03 16:03:26 +0000201 PM_OUT_OF_DATA_SPACE, /* metadata may be changed, though data may not be allocated */
Joe Thornbere49e5822012-07-27 15:08:16 +0100202 PM_READ_ONLY, /* metadata may not be changed */
203 PM_FAIL, /* all I/O fails */
204};
205
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100206struct pool_features {
Joe Thornbere49e5822012-07-27 15:08:16 +0100207 enum pool_mode mode;
208
Mike Snitzer9bc142d2012-09-26 23:45:46 +0100209 bool zero_new_blocks:1;
210 bool discard_enabled:1;
211 bool discard_passdown:1;
Mike Snitzer787a996c2013-12-06 16:21:43 -0500212 bool error_if_no_space:1;
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100213};
214
Joe Thornbere49e5822012-07-27 15:08:16 +0100215struct thin_c;
216typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio);
Joe Thornbera374bb22014-10-10 13:43:14 +0100217typedef void (*process_cell_fn)(struct thin_c *tc, struct dm_bio_prison_cell *cell);
Joe Thornbere49e5822012-07-27 15:08:16 +0100218typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m);
219
Joe Thornberac4c3f32014-10-10 16:42:10 +0100220#define CELL_SORT_ARRAY_SIZE 8192
221
Joe Thornber991d9fa2011-10-31 20:21:18 +0000222struct pool {
223 struct list_head list;
224 struct dm_target *ti; /* Only set if a pool target is bound */
225
226 struct mapped_device *pool_md;
227 struct block_device *md_dev;
228 struct dm_pool_metadata *pmd;
229
Joe Thornber991d9fa2011-10-31 20:21:18 +0000230 dm_block_t low_water_blocks;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100231 uint32_t sectors_per_block;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100232 int sectors_per_block_shift;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000233
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100234 struct pool_features pf;
Joe Thornber88a66212013-12-04 20:16:12 -0500235 bool low_water_triggered:1; /* A dm event has been sent */
Mike Snitzer80e96c52014-11-07 15:09:46 -0500236 bool suspended:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000237
Mike Snitzer44feb382012-10-12 21:02:10 +0100238 struct dm_bio_prison *prison;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000239 struct dm_kcopyd_client *copier;
240
241 struct workqueue_struct *wq;
Joe Thornber7d327fe2014-10-06 15:45:59 +0100242 struct throttle throttle;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000243 struct work_struct worker;
Joe Thornber905e51b2012-03-28 18:41:27 +0100244 struct delayed_work waker;
Joe Thornber85ad643b2014-05-09 15:59:38 +0100245 struct delayed_work no_space_timeout;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000246
Joe Thornber905e51b2012-03-28 18:41:27 +0100247 unsigned long last_commit_jiffies;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100248 unsigned ref_count;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000249
250 spinlock_t lock;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000251 struct bio_list deferred_flush_bios;
252 struct list_head prepared_mappings;
Joe Thornber104655f2012-03-28 18:41:28 +0100253 struct list_head prepared_discards;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400254 struct list_head active_thins;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000255
Mike Snitzer44feb382012-10-12 21:02:10 +0100256 struct dm_deferred_set *shared_read_ds;
257 struct dm_deferred_set *all_io_ds;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000258
Mike Snitzera24c2562012-06-03 00:30:00 +0100259 struct dm_thin_new_mapping *next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000260 mempool_t *mapping_pool;
Joe Thornbere49e5822012-07-27 15:08:16 +0100261
262 process_bio_fn process_bio;
263 process_bio_fn process_discard;
264
Joe Thornbera374bb22014-10-10 13:43:14 +0100265 process_cell_fn process_cell;
266 process_cell_fn process_discard_cell;
267
Joe Thornbere49e5822012-07-27 15:08:16 +0100268 process_mapping_fn process_prepared_mapping;
269 process_mapping_fn process_prepared_discard;
Joe Thornberac4c3f32014-10-10 16:42:10 +0100270
271 struct dm_bio_prison_cell *cell_sort_array[CELL_SORT_ARRAY_SIZE];
Joe Thornber991d9fa2011-10-31 20:21:18 +0000272};
273
Joe Thornbere49e5822012-07-27 15:08:16 +0100274static enum pool_mode get_pool_mode(struct pool *pool);
Joe Thornberb5330652013-12-04 19:51:33 -0500275static void metadata_operation_failed(struct pool *pool, const char *op, int r);
Joe Thornbere49e5822012-07-27 15:08:16 +0100276
Joe Thornber991d9fa2011-10-31 20:21:18 +0000277/*
278 * Target context for a pool.
279 */
280struct pool_c {
281 struct dm_target *ti;
282 struct pool *pool;
283 struct dm_dev *data_dev;
284 struct dm_dev *metadata_dev;
285 struct dm_target_callbacks callbacks;
286
287 dm_block_t low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +0100288 struct pool_features requested_pf; /* Features requested during table load */
289 struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000290};
291
292/*
293 * Target context for a thin.
294 */
295struct thin_c {
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400296 struct list_head list;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000297 struct dm_dev *pool_dev;
Joe Thornber2dd9c252012-03-28 18:41:28 +0100298 struct dm_dev *origin_dev;
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100299 sector_t origin_size;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000300 dm_thin_id dev_id;
301
302 struct pool *pool;
303 struct dm_thin_device *td;
Mike Snitzer583024d2014-10-28 20:58:45 -0400304 struct mapped_device *thin_md;
305
Joe Thornber738211f2014-03-03 15:52:28 +0000306 bool requeue_mode:1;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400307 spinlock_t lock;
Joe Thornbera374bb22014-10-10 13:43:14 +0100308 struct list_head deferred_cells;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400309 struct bio_list deferred_bio_list;
310 struct bio_list retry_on_resume_list;
Mike Snitzer67324ea2014-03-21 18:33:41 -0400311 struct rb_root sort_bio_list; /* sorted list of deferred bios */
Joe Thornberb10ebd32014-04-08 11:29:01 +0100312
313 /*
314 * Ensures the thin is not destroyed until the worker has finished
315 * iterating the active_thins list.
316 */
317 atomic_t refcount;
318 struct completion can_destroy;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000319};
320
321/*----------------------------------------------------------------*/
322
Joe Thornber34fbcf62015-04-16 12:58:35 +0100323/**
324 * __blkdev_issue_discard_async - queue a discard with async completion
325 * @bdev: blockdev to issue discard for
326 * @sector: start sector
327 * @nr_sects: number of sectors to discard
328 * @gfp_mask: memory allocation flags (for bio_alloc)
329 * @flags: BLKDEV_IFL_* flags to control behaviour
330 * @parent_bio: parent discard bio that all sub discards get chained to
331 *
332 * Description:
333 * Asynchronously issue a discard request for the sectors in question.
334 * NOTE: this variant of blk-core's blkdev_issue_discard() is a stop-gap
335 * that is being kept local to DM thinp until the block changes to allow
336 * late bio splitting land upstream.
337 */
338static int __blkdev_issue_discard_async(struct block_device *bdev, sector_t sector,
339 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags,
340 struct bio *parent_bio)
341{
342 struct request_queue *q = bdev_get_queue(bdev);
343 int type = REQ_WRITE | REQ_DISCARD;
344 unsigned int max_discard_sectors, granularity;
345 int alignment;
346 struct bio *bio;
347 int ret = 0;
348 struct blk_plug plug;
349
350 if (!q)
351 return -ENXIO;
352
353 if (!blk_queue_discard(q))
354 return -EOPNOTSUPP;
355
356 /* Zero-sector (unknown) and one-sector granularities are the same. */
357 granularity = max(q->limits.discard_granularity >> 9, 1U);
358 alignment = (bdev_discard_alignment(bdev) >> 9) % granularity;
359
360 /*
361 * Ensure that max_discard_sectors is of the proper
362 * granularity, so that requests stay aligned after a split.
363 */
364 max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9);
365 max_discard_sectors -= max_discard_sectors % granularity;
366 if (unlikely(!max_discard_sectors)) {
367 /* Avoid infinite loop below. Being cautious never hurts. */
368 return -EOPNOTSUPP;
369 }
370
371 if (flags & BLKDEV_DISCARD_SECURE) {
372 if (!blk_queue_secdiscard(q))
373 return -EOPNOTSUPP;
374 type |= REQ_SECURE;
375 }
376
377 blk_start_plug(&plug);
378 while (nr_sects) {
379 unsigned int req_sects;
380 sector_t end_sect, tmp;
381
382 /*
383 * Required bio_put occurs in bio_endio thanks to bio_chain below
384 */
385 bio = bio_alloc(gfp_mask, 1);
386 if (!bio) {
387 ret = -ENOMEM;
388 break;
389 }
390
391 req_sects = min_t(sector_t, nr_sects, max_discard_sectors);
392
393 /*
394 * If splitting a request, and the next starting sector would be
395 * misaligned, stop the discard at the previous aligned sector.
396 */
397 end_sect = sector + req_sects;
398 tmp = end_sect;
399 if (req_sects < nr_sects &&
400 sector_div(tmp, granularity) != alignment) {
401 end_sect = end_sect - alignment;
402 sector_div(end_sect, granularity);
403 end_sect = end_sect * granularity + alignment;
404 req_sects = end_sect - sector;
405 }
406
407 bio_chain(bio, parent_bio);
408
409 bio->bi_iter.bi_sector = sector;
410 bio->bi_bdev = bdev;
411
412 bio->bi_iter.bi_size = req_sects << 9;
413 nr_sects -= req_sects;
414 sector = end_sect;
415
416 submit_bio(type, bio);
417
418 /*
419 * We can loop for a long time in here, if someone does
420 * full device discards (like mkfs). Be nice and allow
421 * us to schedule out to avoid softlocking if preempt
422 * is disabled.
423 */
424 cond_resched();
425 }
426 blk_finish_plug(&plug);
427
428 return ret;
429}
430
431static bool block_size_is_power_of_two(struct pool *pool)
432{
433 return pool->sectors_per_block_shift >= 0;
434}
435
436static sector_t block_to_sectors(struct pool *pool, dm_block_t b)
437{
438 return block_size_is_power_of_two(pool) ?
439 (b << pool->sectors_per_block_shift) :
440 (b * pool->sectors_per_block);
441}
442
443static int issue_discard(struct thin_c *tc, dm_block_t data_b, dm_block_t data_e,
444 struct bio *parent_bio)
445{
446 sector_t s = block_to_sectors(tc->pool, data_b);
447 sector_t len = block_to_sectors(tc->pool, data_e - data_b);
448
449 return __blkdev_issue_discard_async(tc->pool_dev->bdev, s, len,
450 GFP_NOWAIT, 0, parent_bio);
451}
452
453/*----------------------------------------------------------------*/
454
Joe Thornber025b9682013-03-01 22:45:50 +0000455/*
456 * wake_worker() is used when new work is queued and when pool_resume is
457 * ready to continue deferred IO processing.
458 */
459static void wake_worker(struct pool *pool)
460{
461 queue_work(pool->wq, &pool->worker);
462}
463
464/*----------------------------------------------------------------*/
465
Joe Thornber6beca5e2013-03-01 22:45:50 +0000466static int bio_detain(struct pool *pool, struct dm_cell_key *key, struct bio *bio,
467 struct dm_bio_prison_cell **cell_result)
468{
469 int r;
470 struct dm_bio_prison_cell *cell_prealloc;
471
472 /*
473 * Allocate a cell from the prison's mempool.
474 * This might block but it can't fail.
475 */
476 cell_prealloc = dm_bio_prison_alloc_cell(pool->prison, GFP_NOIO);
477
478 r = dm_bio_detain(pool->prison, key, bio, cell_prealloc, cell_result);
479 if (r)
480 /*
481 * We reused an old cell; we can get rid of
482 * the new one.
483 */
484 dm_bio_prison_free_cell(pool->prison, cell_prealloc);
485
486 return r;
487}
488
489static void cell_release(struct pool *pool,
490 struct dm_bio_prison_cell *cell,
491 struct bio_list *bios)
492{
493 dm_cell_release(pool->prison, cell, bios);
494 dm_bio_prison_free_cell(pool->prison, cell);
495}
496
Joe Thornber2d759a42014-10-10 15:27:16 +0100497static void cell_visit_release(struct pool *pool,
498 void (*fn)(void *, struct dm_bio_prison_cell *),
499 void *context,
500 struct dm_bio_prison_cell *cell)
501{
502 dm_cell_visit_release(pool->prison, fn, context, cell);
503 dm_bio_prison_free_cell(pool->prison, cell);
504}
505
Joe Thornber6beca5e2013-03-01 22:45:50 +0000506static void cell_release_no_holder(struct pool *pool,
507 struct dm_bio_prison_cell *cell,
508 struct bio_list *bios)
509{
510 dm_cell_release_no_holder(pool->prison, cell, bios);
511 dm_bio_prison_free_cell(pool->prison, cell);
512}
513
Mike Snitzeraf918052014-05-22 14:32:51 -0400514static void cell_error_with_code(struct pool *pool,
515 struct dm_bio_prison_cell *cell, int error_code)
Joe Thornber6beca5e2013-03-01 22:45:50 +0000516{
Mike Snitzeraf918052014-05-22 14:32:51 -0400517 dm_cell_error(pool->prison, cell, error_code);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000518 dm_bio_prison_free_cell(pool->prison, cell);
519}
520
Mike Snitzeraf918052014-05-22 14:32:51 -0400521static void cell_error(struct pool *pool, struct dm_bio_prison_cell *cell)
522{
523 cell_error_with_code(pool, cell, -EIO);
524}
525
Joe Thornbera374bb22014-10-10 13:43:14 +0100526static void cell_success(struct pool *pool, struct dm_bio_prison_cell *cell)
527{
528 cell_error_with_code(pool, cell, 0);
529}
530
531static void cell_requeue(struct pool *pool, struct dm_bio_prison_cell *cell)
532{
533 cell_error_with_code(pool, cell, DM_ENDIO_REQUEUE);
534}
535
Joe Thornber6beca5e2013-03-01 22:45:50 +0000536/*----------------------------------------------------------------*/
537
Joe Thornber991d9fa2011-10-31 20:21:18 +0000538/*
539 * A global list of pools that uses a struct mapped_device as a key.
540 */
541static struct dm_thin_pool_table {
542 struct mutex mutex;
543 struct list_head pools;
544} dm_thin_pool_table;
545
546static void pool_table_init(void)
547{
548 mutex_init(&dm_thin_pool_table.mutex);
549 INIT_LIST_HEAD(&dm_thin_pool_table.pools);
550}
551
552static void __pool_table_insert(struct pool *pool)
553{
554 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
555 list_add(&pool->list, &dm_thin_pool_table.pools);
556}
557
558static void __pool_table_remove(struct pool *pool)
559{
560 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
561 list_del(&pool->list);
562}
563
564static struct pool *__pool_table_lookup(struct mapped_device *md)
565{
566 struct pool *pool = NULL, *tmp;
567
568 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
569
570 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
571 if (tmp->pool_md == md) {
572 pool = tmp;
573 break;
574 }
575 }
576
577 return pool;
578}
579
580static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
581{
582 struct pool *pool = NULL, *tmp;
583
584 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
585
586 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
587 if (tmp->md_dev == md_dev) {
588 pool = tmp;
589 break;
590 }
591 }
592
593 return pool;
594}
595
596/*----------------------------------------------------------------*/
597
Mike Snitzera24c2562012-06-03 00:30:00 +0100598struct dm_thin_endio_hook {
Joe Thornbereb2aa482012-03-28 18:41:28 +0100599 struct thin_c *tc;
Mike Snitzer44feb382012-10-12 21:02:10 +0100600 struct dm_deferred_entry *shared_read_entry;
601 struct dm_deferred_entry *all_io_entry;
Mike Snitzera24c2562012-06-03 00:30:00 +0100602 struct dm_thin_new_mapping *overwrite_mapping;
Mike Snitzer67324ea2014-03-21 18:33:41 -0400603 struct rb_node rb_node;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100604 struct dm_bio_prison_cell *cell;
Joe Thornbereb2aa482012-03-28 18:41:28 +0100605};
606
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400607static void __merge_bio_list(struct bio_list *bios, struct bio_list *master)
608{
609 bio_list_merge(bios, master);
610 bio_list_init(master);
611}
612
613static void error_bio_list(struct bio_list *bios, int error)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000614{
615 struct bio *bio;
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400616
617 while ((bio = bio_list_pop(bios)))
618 bio_endio(bio, error);
619}
620
621static void error_thin_bio_list(struct thin_c *tc, struct bio_list *master, int error)
622{
Joe Thornber991d9fa2011-10-31 20:21:18 +0000623 struct bio_list bios;
Joe Thornber18adc572014-03-03 15:46:42 +0000624 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000625
626 bio_list_init(&bios);
Joe Thornber18adc572014-03-03 15:46:42 +0000627
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400628 spin_lock_irqsave(&tc->lock, flags);
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400629 __merge_bio_list(&bios, master);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400630 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000631
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400632 error_bio_list(&bios, error);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000633}
634
Joe Thornbera374bb22014-10-10 13:43:14 +0100635static void requeue_deferred_cells(struct thin_c *tc)
636{
637 struct pool *pool = tc->pool;
638 unsigned long flags;
639 struct list_head cells;
640 struct dm_bio_prison_cell *cell, *tmp;
641
642 INIT_LIST_HEAD(&cells);
643
644 spin_lock_irqsave(&tc->lock, flags);
645 list_splice_init(&tc->deferred_cells, &cells);
646 spin_unlock_irqrestore(&tc->lock, flags);
647
648 list_for_each_entry_safe(cell, tmp, &cells, user_list)
649 cell_requeue(pool, cell);
650}
651
Joe Thornber991d9fa2011-10-31 20:21:18 +0000652static void requeue_io(struct thin_c *tc)
653{
Joe Thornber3e1a0692014-03-03 16:03:26 +0000654 struct bio_list bios;
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400655 unsigned long flags;
Joe Thornber3e1a0692014-03-03 16:03:26 +0000656
657 bio_list_init(&bios);
658
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400659 spin_lock_irqsave(&tc->lock, flags);
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400660 __merge_bio_list(&bios, &tc->deferred_bio_list);
661 __merge_bio_list(&bios, &tc->retry_on_resume_list);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400662 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber3e1a0692014-03-03 16:03:26 +0000663
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400664 error_bio_list(&bios, DM_ENDIO_REQUEUE);
665 requeue_deferred_cells(tc);
Joe Thornber3e1a0692014-03-03 16:03:26 +0000666}
667
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400668static void error_retry_list(struct pool *pool)
669{
670 struct thin_c *tc;
671
672 rcu_read_lock();
673 list_for_each_entry_rcu(tc, &pool->active_thins, list)
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400674 error_thin_bio_list(tc, &tc->retry_on_resume_list, -EIO);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400675 rcu_read_unlock();
676}
677
Joe Thornber991d9fa2011-10-31 20:21:18 +0000678/*
679 * This section of code contains the logic for processing a thin device's IO.
680 * Much of the code depends on pool object resources (lists, workqueues, etc)
681 * but most is exclusively called from the thin target rather than the thin-pool
682 * target.
683 */
684
685static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
686{
Mike Snitzer58f77a22013-03-01 22:45:45 +0000687 struct pool *pool = tc->pool;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700688 sector_t block_nr = bio->bi_iter.bi_sector;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100689
Mike Snitzer58f77a22013-03-01 22:45:45 +0000690 if (block_size_is_power_of_two(pool))
691 block_nr >>= pool->sectors_per_block_shift;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100692 else
Mike Snitzer58f77a22013-03-01 22:45:45 +0000693 (void) sector_div(block_nr, pool->sectors_per_block);
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100694
695 return block_nr;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000696}
697
Joe Thornber34fbcf62015-04-16 12:58:35 +0100698/*
699 * Returns the _complete_ blocks that this bio covers.
700 */
701static void get_bio_block_range(struct thin_c *tc, struct bio *bio,
702 dm_block_t *begin, dm_block_t *end)
703{
704 struct pool *pool = tc->pool;
705 sector_t b = bio->bi_iter.bi_sector;
706 sector_t e = b + (bio->bi_iter.bi_size >> SECTOR_SHIFT);
707
708 b += pool->sectors_per_block - 1ull; /* so we round up */
709
710 if (block_size_is_power_of_two(pool)) {
711 b >>= pool->sectors_per_block_shift;
712 e >>= pool->sectors_per_block_shift;
713 } else {
714 (void) sector_div(b, pool->sectors_per_block);
715 (void) sector_div(e, pool->sectors_per_block);
716 }
717
718 if (e < b)
719 /* Can happen if the bio is within a single block. */
720 e = b;
721
722 *begin = b;
723 *end = e;
724}
725
Joe Thornber991d9fa2011-10-31 20:21:18 +0000726static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
727{
728 struct pool *pool = tc->pool;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700729 sector_t bi_sector = bio->bi_iter.bi_sector;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000730
731 bio->bi_bdev = tc->pool_dev->bdev;
Mike Snitzer58f77a22013-03-01 22:45:45 +0000732 if (block_size_is_power_of_two(pool))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700733 bio->bi_iter.bi_sector =
734 (block << pool->sectors_per_block_shift) |
735 (bi_sector & (pool->sectors_per_block - 1));
Mike Snitzer58f77a22013-03-01 22:45:45 +0000736 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700737 bio->bi_iter.bi_sector = (block * pool->sectors_per_block) +
Mike Snitzer58f77a22013-03-01 22:45:45 +0000738 sector_div(bi_sector, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000739}
740
Joe Thornber2dd9c252012-03-28 18:41:28 +0100741static void remap_to_origin(struct thin_c *tc, struct bio *bio)
742{
743 bio->bi_bdev = tc->origin_dev->bdev;
744}
745
Joe Thornber4afdd682012-07-27 15:08:14 +0100746static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
747{
748 return (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) &&
749 dm_thin_changed_this_transaction(tc->td);
750}
751
Joe Thornbere8088072012-12-21 20:23:31 +0000752static void inc_all_io_entry(struct pool *pool, struct bio *bio)
753{
754 struct dm_thin_endio_hook *h;
755
756 if (bio->bi_rw & REQ_DISCARD)
757 return;
758
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000759 h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbere8088072012-12-21 20:23:31 +0000760 h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
761}
762
Joe Thornber2dd9c252012-03-28 18:41:28 +0100763static void issue(struct thin_c *tc, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000764{
765 struct pool *pool = tc->pool;
766 unsigned long flags;
767
Joe Thornbere49e5822012-07-27 15:08:16 +0100768 if (!bio_triggers_commit(tc, bio)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000769 generic_make_request(bio);
Joe Thornbere49e5822012-07-27 15:08:16 +0100770 return;
771 }
772
773 /*
774 * Complete bio with an error if earlier I/O caused changes to
775 * the metadata that can't be committed e.g, due to I/O errors
776 * on the metadata device.
777 */
778 if (dm_thin_aborted_changes(tc->td)) {
779 bio_io_error(bio);
780 return;
781 }
782
783 /*
784 * Batch together any bios that trigger commits and then issue a
785 * single commit for them in process_deferred_bios().
786 */
787 spin_lock_irqsave(&pool->lock, flags);
788 bio_list_add(&pool->deferred_flush_bios, bio);
789 spin_unlock_irqrestore(&pool->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000790}
791
Joe Thornber2dd9c252012-03-28 18:41:28 +0100792static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
793{
794 remap_to_origin(tc, bio);
795 issue(tc, bio);
796}
797
798static void remap_and_issue(struct thin_c *tc, struct bio *bio,
799 dm_block_t block)
800{
801 remap(tc, bio, block);
802 issue(tc, bio);
803}
804
Joe Thornber991d9fa2011-10-31 20:21:18 +0000805/*----------------------------------------------------------------*/
806
807/*
808 * Bio endio functions.
809 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100810struct dm_thin_new_mapping {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000811 struct list_head list;
812
Mike Snitzer7f214662013-12-17 13:43:31 -0500813 bool pass_discard:1;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100814 bool maybe_shared:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000815
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100816 /*
817 * Track quiescing, copying and zeroing preparation actions. When this
818 * counter hits zero the block is prepared and can be inserted into the
819 * btree.
820 */
821 atomic_t prepare_actions;
822
Mike Snitzer7f214662013-12-17 13:43:31 -0500823 int err;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000824 struct thin_c *tc;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100825 dm_block_t virt_begin, virt_end;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000826 dm_block_t data_block;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100827 struct dm_bio_prison_cell *cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000828
829 /*
830 * If the bio covers the whole area of a block then we can avoid
831 * zeroing or copying. Instead this bio is hooked. The bio will
832 * still be in the cell, so care has to be taken to avoid issuing
833 * the bio twice.
834 */
835 struct bio *bio;
836 bio_end_io_t *saved_bi_end_io;
837};
838
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100839static void __complete_mapping_preparation(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000840{
841 struct pool *pool = m->tc->pool;
842
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100843 if (atomic_dec_and_test(&m->prepare_actions)) {
Mike Snitzerdaec3382013-12-11 14:01:20 -0500844 list_add_tail(&m->list, &pool->prepared_mappings);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000845 wake_worker(pool);
846 }
847}
848
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100849static void complete_mapping_preparation(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000850{
851 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000852 struct pool *pool = m->tc->pool;
853
Joe Thornber991d9fa2011-10-31 20:21:18 +0000854 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100855 __complete_mapping_preparation(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000856 spin_unlock_irqrestore(&pool->lock, flags);
857}
858
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100859static void copy_complete(int read_err, unsigned long write_err, void *context)
860{
861 struct dm_thin_new_mapping *m = context;
862
863 m->err = read_err || write_err ? -EIO : 0;
864 complete_mapping_preparation(m);
865}
866
Joe Thornber991d9fa2011-10-31 20:21:18 +0000867static void overwrite_endio(struct bio *bio, int err)
868{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000869 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Mike Snitzera24c2562012-06-03 00:30:00 +0100870 struct dm_thin_new_mapping *m = h->overwrite_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000871
Mike Snitzer8b908f82015-05-13 17:53:13 -0400872 bio->bi_end_io = m->saved_bi_end_io;
873
Joe Thornber991d9fa2011-10-31 20:21:18 +0000874 m->err = err;
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100875 complete_mapping_preparation(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000876}
877
Joe Thornber991d9fa2011-10-31 20:21:18 +0000878/*----------------------------------------------------------------*/
879
880/*
881 * Workqueue.
882 */
883
884/*
885 * Prepared mapping jobs.
886 */
887
888/*
Joe Thornber2d759a42014-10-10 15:27:16 +0100889 * This sends the bios in the cell, except the original holder, back
890 * to the deferred_bios list.
Joe Thornber991d9fa2011-10-31 20:21:18 +0000891 */
Joe Thornberf286ba02012-12-21 20:23:33 +0000892static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000893{
Joe Thornber991d9fa2011-10-31 20:21:18 +0000894 struct pool *pool = tc->pool;
895 unsigned long flags;
896
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400897 spin_lock_irqsave(&tc->lock, flags);
898 cell_release_no_holder(pool, cell, &tc->deferred_bio_list);
899 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000900
901 wake_worker(pool);
902}
903
Joe Thornbera374bb22014-10-10 13:43:14 +0100904static void thin_defer_bio(struct thin_c *tc, struct bio *bio);
905
Joe Thornber2d759a42014-10-10 15:27:16 +0100906struct remap_info {
907 struct thin_c *tc;
908 struct bio_list defer_bios;
909 struct bio_list issue_bios;
910};
911
912static void __inc_remap_and_issue_cell(void *context,
913 struct dm_bio_prison_cell *cell)
914{
915 struct remap_info *info = context;
916 struct bio *bio;
917
918 while ((bio = bio_list_pop(&cell->bios))) {
919 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA))
920 bio_list_add(&info->defer_bios, bio);
921 else {
922 inc_all_io_entry(info->tc->pool, bio);
923
924 /*
925 * We can't issue the bios with the bio prison lock
926 * held, so we add them to a list to issue on
927 * return from this function.
928 */
929 bio_list_add(&info->issue_bios, bio);
930 }
931 }
932}
933
Joe Thornbera374bb22014-10-10 13:43:14 +0100934static void inc_remap_and_issue_cell(struct thin_c *tc,
935 struct dm_bio_prison_cell *cell,
936 dm_block_t block)
937{
938 struct bio *bio;
Joe Thornber2d759a42014-10-10 15:27:16 +0100939 struct remap_info info;
Joe Thornbera374bb22014-10-10 13:43:14 +0100940
Joe Thornber2d759a42014-10-10 15:27:16 +0100941 info.tc = tc;
942 bio_list_init(&info.defer_bios);
943 bio_list_init(&info.issue_bios);
Joe Thornbera374bb22014-10-10 13:43:14 +0100944
Joe Thornber2d759a42014-10-10 15:27:16 +0100945 /*
946 * We have to be careful to inc any bios we're about to issue
947 * before the cell is released, and avoid a race with new bios
948 * being added to the cell.
949 */
950 cell_visit_release(tc->pool, __inc_remap_and_issue_cell,
951 &info, cell);
952
953 while ((bio = bio_list_pop(&info.defer_bios)))
954 thin_defer_bio(tc, bio);
955
956 while ((bio = bio_list_pop(&info.issue_bios)))
957 remap_and_issue(info.tc, bio, block);
Joe Thornbera374bb22014-10-10 13:43:14 +0100958}
959
Joe Thornbere49e5822012-07-27 15:08:16 +0100960static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
961{
Joe Thornber6beca5e2013-03-01 22:45:50 +0000962 cell_error(m->tc->pool, m->cell);
Joe Thornbere49e5822012-07-27 15:08:16 +0100963 list_del(&m->list);
964 mempool_free(m, m->tc->pool->mapping_pool);
965}
Joe Thornber025b9682013-03-01 22:45:50 +0000966
Mike Snitzera24c2562012-06-03 00:30:00 +0100967static void process_prepared_mapping(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000968{
969 struct thin_c *tc = m->tc;
Joe Thornber6beca5e2013-03-01 22:45:50 +0000970 struct pool *pool = tc->pool;
Mike Snitzer8b908f82015-05-13 17:53:13 -0400971 struct bio *bio = m->bio;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000972 int r;
973
Joe Thornber991d9fa2011-10-31 20:21:18 +0000974 if (m->err) {
Joe Thornber6beca5e2013-03-01 22:45:50 +0000975 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100976 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000977 }
978
979 /*
980 * Commit the prepared block into the mapping btree.
981 * Any I/O for this block arriving after this point will get
982 * remapped to it directly.
983 */
Joe Thornber34fbcf62015-04-16 12:58:35 +0100984 r = dm_thin_insert_block(tc->td, m->virt_begin, m->data_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000985 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -0500986 metadata_operation_failed(pool, "dm_thin_insert_block", r);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000987 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100988 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000989 }
990
991 /*
992 * Release any bios held while the block was being provisioned.
993 * If we are processing a write bio that completely covers the block,
994 * we already processed it so can ignore it now when processing
995 * the bios in the cell.
996 */
997 if (bio) {
Joe Thornber2d759a42014-10-10 15:27:16 +0100998 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000999 bio_endio(bio, 0);
Joe Thornber2d759a42014-10-10 15:27:16 +01001000 } else {
1001 inc_all_io_entry(tc->pool, m->cell->holder);
1002 remap_and_issue(tc, m->cell->holder, m->data_block);
1003 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
1004 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001005
Joe Thornber905386f2012-07-27 15:08:05 +01001006out:
Joe Thornber991d9fa2011-10-31 20:21:18 +00001007 list_del(&m->list);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001008 mempool_free(m, pool->mapping_pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001009}
1010
Joe Thornber34fbcf62015-04-16 12:58:35 +01001011/*----------------------------------------------------------------*/
1012
1013static void free_discard_mapping(struct dm_thin_new_mapping *m)
1014{
1015 struct thin_c *tc = m->tc;
1016 if (m->cell)
1017 cell_defer_no_holder(tc, m->cell);
1018 mempool_free(m, tc->pool->mapping_pool);
1019}
1020
Joe Thornbere49e5822012-07-27 15:08:16 +01001021static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
Joe Thornber104655f2012-03-28 18:41:28 +01001022{
Joe Thornbere49e5822012-07-27 15:08:16 +01001023 bio_io_error(m->bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001024 free_discard_mapping(m);
Joe Thornbere49e5822012-07-27 15:08:16 +01001025}
Joe Thornber104655f2012-03-28 18:41:28 +01001026
Joe Thornber34fbcf62015-04-16 12:58:35 +01001027static void process_prepared_discard_success(struct dm_thin_new_mapping *m)
Joe Thornbere49e5822012-07-27 15:08:16 +01001028{
Joe Thornber34fbcf62015-04-16 12:58:35 +01001029 bio_endio(m->bio, 0);
1030 free_discard_mapping(m);
Joe Thornber104655f2012-03-28 18:41:28 +01001031}
1032
Joe Thornber34fbcf62015-04-16 12:58:35 +01001033static void process_prepared_discard_no_passdown(struct dm_thin_new_mapping *m)
Joe Thornbere49e5822012-07-27 15:08:16 +01001034{
1035 int r;
1036 struct thin_c *tc = m->tc;
1037
Joe Thornber34fbcf62015-04-16 12:58:35 +01001038 r = dm_thin_remove_range(tc->td, m->cell->key.block_begin, m->cell->key.block_end);
1039 if (r) {
1040 metadata_operation_failed(tc->pool, "dm_thin_remove_range", r);
1041 bio_io_error(m->bio);
1042 } else
1043 bio_endio(m->bio, 0);
Joe Thornbere49e5822012-07-27 15:08:16 +01001044
Joe Thornber34fbcf62015-04-16 12:58:35 +01001045 cell_defer_no_holder(tc, m->cell);
1046 mempool_free(m, tc->pool->mapping_pool);
1047}
1048
1049static int passdown_double_checking_shared_status(struct dm_thin_new_mapping *m)
1050{
1051 /*
1052 * We've already unmapped this range of blocks, but before we
1053 * passdown we have to check that these blocks are now unused.
1054 */
1055 int r;
1056 bool used = true;
1057 struct thin_c *tc = m->tc;
1058 struct pool *pool = tc->pool;
1059 dm_block_t b = m->data_block, e, end = m->data_block + m->virt_end - m->virt_begin;
1060
1061 while (b != end) {
1062 /* find start of unmapped run */
1063 for (; b < end; b++) {
1064 r = dm_pool_block_is_used(pool->pmd, b, &used);
1065 if (r)
1066 return r;
1067
1068 if (!used)
1069 break;
1070 }
1071
1072 if (b == end)
1073 break;
1074
1075 /* find end of run */
1076 for (e = b + 1; e != end; e++) {
1077 r = dm_pool_block_is_used(pool->pmd, e, &used);
1078 if (r)
1079 return r;
1080
1081 if (used)
1082 break;
1083 }
1084
1085 r = issue_discard(tc, b, e, m->bio);
1086 if (r)
1087 return r;
1088
1089 b = e;
1090 }
1091
1092 return 0;
1093}
1094
1095static void process_prepared_discard_passdown(struct dm_thin_new_mapping *m)
1096{
1097 int r;
1098 struct thin_c *tc = m->tc;
1099 struct pool *pool = tc->pool;
1100
1101 r = dm_thin_remove_range(tc->td, m->virt_begin, m->virt_end);
1102 if (r)
1103 metadata_operation_failed(pool, "dm_thin_remove_range", r);
1104
1105 else if (m->maybe_shared)
1106 r = passdown_double_checking_shared_status(m);
1107 else
1108 r = issue_discard(tc, m->data_block, m->data_block + (m->virt_end - m->virt_begin), m->bio);
1109
1110 /*
1111 * Even if r is set, there could be sub discards in flight that we
1112 * need to wait for.
1113 */
1114 bio_endio(m->bio, r);
1115 cell_defer_no_holder(tc, m->cell);
1116 mempool_free(m, pool->mapping_pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01001117}
1118
Joe Thornber104655f2012-03-28 18:41:28 +01001119static void process_prepared(struct pool *pool, struct list_head *head,
Joe Thornbere49e5822012-07-27 15:08:16 +01001120 process_mapping_fn *fn)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001121{
1122 unsigned long flags;
1123 struct list_head maps;
Mike Snitzera24c2562012-06-03 00:30:00 +01001124 struct dm_thin_new_mapping *m, *tmp;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001125
1126 INIT_LIST_HEAD(&maps);
1127 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +01001128 list_splice_init(head, &maps);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001129 spin_unlock_irqrestore(&pool->lock, flags);
1130
1131 list_for_each_entry_safe(m, tmp, &maps, list)
Joe Thornbere49e5822012-07-27 15:08:16 +01001132 (*fn)(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001133}
1134
1135/*
1136 * Deferred bio jobs.
1137 */
Joe Thornber104655f2012-03-28 18:41:28 +01001138static int io_overlaps_block(struct pool *pool, struct bio *bio)
1139{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001140 return bio->bi_iter.bi_size ==
1141 (pool->sectors_per_block << SECTOR_SHIFT);
Joe Thornber104655f2012-03-28 18:41:28 +01001142}
1143
Joe Thornber991d9fa2011-10-31 20:21:18 +00001144static int io_overwrites_block(struct pool *pool, struct bio *bio)
1145{
Joe Thornber104655f2012-03-28 18:41:28 +01001146 return (bio_data_dir(bio) == WRITE) &&
1147 io_overlaps_block(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001148}
1149
1150static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
1151 bio_end_io_t *fn)
1152{
1153 *save = bio->bi_end_io;
1154 bio->bi_end_io = fn;
1155}
1156
1157static int ensure_next_mapping(struct pool *pool)
1158{
1159 if (pool->next_mapping)
1160 return 0;
1161
1162 pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
1163
1164 return pool->next_mapping ? 0 : -ENOMEM;
1165}
1166
Mike Snitzera24c2562012-06-03 00:30:00 +01001167static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001168{
Mike Snitzer16961b02013-12-17 13:19:11 -05001169 struct dm_thin_new_mapping *m = pool->next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001170
1171 BUG_ON(!pool->next_mapping);
1172
Mike Snitzer16961b02013-12-17 13:19:11 -05001173 memset(m, 0, sizeof(struct dm_thin_new_mapping));
1174 INIT_LIST_HEAD(&m->list);
1175 m->bio = NULL;
1176
Joe Thornber991d9fa2011-10-31 20:21:18 +00001177 pool->next_mapping = NULL;
1178
Mike Snitzer16961b02013-12-17 13:19:11 -05001179 return m;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001180}
1181
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001182static void ll_zero(struct thin_c *tc, struct dm_thin_new_mapping *m,
1183 sector_t begin, sector_t end)
1184{
1185 int r;
1186 struct dm_io_region to;
1187
1188 to.bdev = tc->pool_dev->bdev;
1189 to.sector = begin;
1190 to.count = end - begin;
1191
1192 r = dm_kcopyd_zero(tc->pool->copier, 1, &to, 0, copy_complete, m);
1193 if (r < 0) {
1194 DMERR_LIMIT("dm_kcopyd_zero() failed");
1195 copy_complete(1, 1, m);
1196 }
1197}
1198
Mike Snitzer452d7a62014-10-09 19:20:21 -04001199static void remap_and_issue_overwrite(struct thin_c *tc, struct bio *bio,
Joe Thornber34fbcf62015-04-16 12:58:35 +01001200 dm_block_t data_begin,
Mike Snitzer452d7a62014-10-09 19:20:21 -04001201 struct dm_thin_new_mapping *m)
1202{
1203 struct pool *pool = tc->pool;
1204 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1205
1206 h->overwrite_mapping = m;
1207 m->bio = bio;
1208 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
1209 inc_all_io_entry(pool, bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001210 remap_and_issue(tc, bio, data_begin);
Mike Snitzer452d7a62014-10-09 19:20:21 -04001211}
1212
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001213/*
1214 * A partial copy also needs to zero the uncopied region.
1215 */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001216static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
Joe Thornber2dd9c252012-03-28 18:41:28 +01001217 struct dm_dev *origin, dm_block_t data_origin,
1218 dm_block_t data_dest,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001219 struct dm_bio_prison_cell *cell, struct bio *bio,
1220 sector_t len)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001221{
1222 int r;
1223 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +01001224 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001225
Joe Thornber991d9fa2011-10-31 20:21:18 +00001226 m->tc = tc;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001227 m->virt_begin = virt_block;
1228 m->virt_end = virt_block + 1u;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001229 m->data_block = data_dest;
1230 m->cell = cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001231
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001232 /*
1233 * quiesce action + copy action + an extra reference held for the
1234 * duration of this function (we may need to inc later for a
1235 * partial zero).
1236 */
1237 atomic_set(&m->prepare_actions, 3);
1238
Mike Snitzer44feb382012-10-12 21:02:10 +01001239 if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001240 complete_mapping_preparation(m); /* already quiesced */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001241
1242 /*
1243 * IO to pool_dev remaps to the pool target's data_dev.
1244 *
1245 * If the whole block of data is being overwritten, we can issue the
1246 * bio immediately. Otherwise we use kcopyd to clone the data first.
1247 */
Mike Snitzer452d7a62014-10-09 19:20:21 -04001248 if (io_overwrites_block(pool, bio))
1249 remap_and_issue_overwrite(tc, bio, data_dest, m);
1250 else {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001251 struct dm_io_region from, to;
1252
Joe Thornber2dd9c252012-03-28 18:41:28 +01001253 from.bdev = origin->bdev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001254 from.sector = data_origin * pool->sectors_per_block;
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001255 from.count = len;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001256
1257 to.bdev = tc->pool_dev->bdev;
1258 to.sector = data_dest * pool->sectors_per_block;
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001259 to.count = len;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001260
1261 r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
1262 0, copy_complete, m);
1263 if (r < 0) {
Mike Snitzerc3977412012-12-21 20:23:34 +00001264 DMERR_LIMIT("dm_kcopyd_copy() failed");
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001265 copy_complete(1, 1, m);
1266
1267 /*
1268 * We allow the zero to be issued, to simplify the
1269 * error path. Otherwise we'd need to start
1270 * worrying about decrementing the prepare_actions
1271 * counter.
1272 */
1273 }
1274
1275 /*
1276 * Do we need to zero a tail region?
1277 */
1278 if (len < pool->sectors_per_block && pool->pf.zero_new_blocks) {
1279 atomic_inc(&m->prepare_actions);
1280 ll_zero(tc, m,
1281 data_dest * pool->sectors_per_block + len,
1282 (data_dest + 1) * pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001283 }
1284 }
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001285
1286 complete_mapping_preparation(m); /* drop our ref */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001287}
1288
Joe Thornber2dd9c252012-03-28 18:41:28 +01001289static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
1290 dm_block_t data_origin, dm_block_t data_dest,
Mike Snitzera24c2562012-06-03 00:30:00 +01001291 struct dm_bio_prison_cell *cell, struct bio *bio)
Joe Thornber2dd9c252012-03-28 18:41:28 +01001292{
1293 schedule_copy(tc, virt_block, tc->pool_dev,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001294 data_origin, data_dest, cell, bio,
1295 tc->pool->sectors_per_block);
Joe Thornber2dd9c252012-03-28 18:41:28 +01001296}
1297
Joe Thornber991d9fa2011-10-31 20:21:18 +00001298static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
Mike Snitzera24c2562012-06-03 00:30:00 +01001299 dm_block_t data_block, struct dm_bio_prison_cell *cell,
Joe Thornber991d9fa2011-10-31 20:21:18 +00001300 struct bio *bio)
1301{
1302 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +01001303 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001304
Joe Thornber50f3c3e2014-06-13 13:57:09 +01001305 atomic_set(&m->prepare_actions, 1); /* no need to quiesce */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001306 m->tc = tc;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001307 m->virt_begin = virt_block;
1308 m->virt_end = virt_block + 1u;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001309 m->data_block = data_block;
1310 m->cell = cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001311
1312 /*
1313 * If the whole block of data is being overwritten or we are not
1314 * zeroing pre-existing data, we can issue the bio immediately.
1315 * Otherwise we use kcopyd to zero the data first.
1316 */
Mike Snitzerf8ae7522015-05-14 11:28:37 -04001317 if (pool->pf.zero_new_blocks) {
1318 if (io_overwrites_block(pool, bio))
1319 remap_and_issue_overwrite(tc, bio, data_block, m);
1320 else
1321 ll_zero(tc, m, data_block * pool->sectors_per_block,
1322 (data_block + 1) * pool->sectors_per_block);
1323 } else
Joe Thornber991d9fa2011-10-31 20:21:18 +00001324 process_prepared_mapping(m);
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001325}
Joe Thornber991d9fa2011-10-31 20:21:18 +00001326
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001327static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
1328 dm_block_t data_dest,
1329 struct dm_bio_prison_cell *cell, struct bio *bio)
1330{
1331 struct pool *pool = tc->pool;
1332 sector_t virt_block_begin = virt_block * pool->sectors_per_block;
1333 sector_t virt_block_end = (virt_block + 1) * pool->sectors_per_block;
1334
1335 if (virt_block_end <= tc->origin_size)
1336 schedule_copy(tc, virt_block, tc->origin_dev,
1337 virt_block, data_dest, cell, bio,
1338 pool->sectors_per_block);
1339
1340 else if (virt_block_begin < tc->origin_size)
1341 schedule_copy(tc, virt_block, tc->origin_dev,
1342 virt_block, data_dest, cell, bio,
1343 tc->origin_size - virt_block_begin);
1344
1345 else
1346 schedule_zero(tc, virt_block, data_dest, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001347}
1348
Joe Thornber2c43fd22014-12-11 11:12:19 +00001349static void set_pool_mode(struct pool *pool, enum pool_mode new_mode);
1350
1351static void check_for_space(struct pool *pool)
1352{
1353 int r;
1354 dm_block_t nr_free;
1355
1356 if (get_pool_mode(pool) != PM_OUT_OF_DATA_SPACE)
1357 return;
1358
1359 r = dm_pool_get_free_block_count(pool->pmd, &nr_free);
1360 if (r)
1361 return;
1362
1363 if (nr_free)
1364 set_pool_mode(pool, PM_WRITE);
1365}
1366
Joe Thornbere49e5822012-07-27 15:08:16 +01001367/*
1368 * A non-zero return indicates read_only or fail_io mode.
1369 * Many callers don't care about the return value.
1370 */
Joe Thornber020cc3b2013-12-04 15:05:36 -05001371static int commit(struct pool *pool)
Joe Thornbere49e5822012-07-27 15:08:16 +01001372{
1373 int r;
1374
Joe Thornber8d07e8a2014-05-06 16:28:14 +01001375 if (get_pool_mode(pool) >= PM_READ_ONLY)
Joe Thornbere49e5822012-07-27 15:08:16 +01001376 return -EINVAL;
1377
Joe Thornber020cc3b2013-12-04 15:05:36 -05001378 r = dm_pool_commit_metadata(pool->pmd);
Joe Thornberb5330652013-12-04 19:51:33 -05001379 if (r)
1380 metadata_operation_failed(pool, "dm_pool_commit_metadata", r);
Joe Thornber2c43fd22014-12-11 11:12:19 +00001381 else
1382 check_for_space(pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01001383
1384 return r;
1385}
1386
Joe Thornber88a66212013-12-04 20:16:12 -05001387static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks)
1388{
1389 unsigned long flags;
1390
1391 if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
1392 DMWARN("%s: reached low water mark for data device: sending event.",
1393 dm_device_name(pool->pool_md));
1394 spin_lock_irqsave(&pool->lock, flags);
1395 pool->low_water_triggered = true;
1396 spin_unlock_irqrestore(&pool->lock, flags);
1397 dm_table_event(pool->ti->table);
1398 }
1399}
1400
Joe Thornber991d9fa2011-10-31 20:21:18 +00001401static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
1402{
1403 int r;
1404 dm_block_t free_blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001405 struct pool *pool = tc->pool;
1406
Joe Thornber3e1a0692014-03-03 16:03:26 +00001407 if (WARN_ON(get_pool_mode(pool) != PM_WRITE))
Joe Thornber8d30abf2013-12-04 19:16:11 -05001408 return -EINVAL;
1409
Joe Thornber991d9fa2011-10-31 20:21:18 +00001410 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
Joe Thornberb5330652013-12-04 19:51:33 -05001411 if (r) {
1412 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001413 return r;
Joe Thornberb5330652013-12-04 19:51:33 -05001414 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001415
Joe Thornber88a66212013-12-04 20:16:12 -05001416 check_low_water_mark(pool, free_blocks);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001417
1418 if (!free_blocks) {
Mike Snitzer94563ba2013-08-22 09:56:18 -04001419 /*
1420 * Try to commit to see if that will free up some
1421 * more space.
1422 */
Joe Thornber020cc3b2013-12-04 15:05:36 -05001423 r = commit(pool);
1424 if (r)
1425 return r;
Mike Snitzer94563ba2013-08-22 09:56:18 -04001426
1427 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
Joe Thornberb5330652013-12-04 19:51:33 -05001428 if (r) {
1429 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
Mike Snitzer94563ba2013-08-22 09:56:18 -04001430 return r;
Joe Thornberb5330652013-12-04 19:51:33 -05001431 }
Mike Snitzer94563ba2013-08-22 09:56:18 -04001432
Mike Snitzer94563ba2013-08-22 09:56:18 -04001433 if (!free_blocks) {
Joe Thornber3e1a0692014-03-03 16:03:26 +00001434 set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001435 return -ENOSPC;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001436 }
1437 }
1438
1439 r = dm_pool_alloc_data_block(pool->pmd, result);
Mike Snitzer4a02b342013-12-03 12:20:57 -05001440 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05001441 metadata_operation_failed(pool, "dm_pool_alloc_data_block", r);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001442 return r;
Mike Snitzer4a02b342013-12-03 12:20:57 -05001443 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001444
1445 return 0;
1446}
1447
1448/*
1449 * If we have run out of space, queue bios until the device is
1450 * resumed, presumably after having been reloaded with more space.
1451 */
1452static void retry_on_resume(struct bio *bio)
1453{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001454 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01001455 struct thin_c *tc = h->tc;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001456 unsigned long flags;
1457
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001458 spin_lock_irqsave(&tc->lock, flags);
1459 bio_list_add(&tc->retry_on_resume_list, bio);
1460 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001461}
1462
Mike Snitzeraf918052014-05-22 14:32:51 -04001463static int should_error_unserviceable_bio(struct pool *pool)
Joe Thornber3e1a0692014-03-03 16:03:26 +00001464{
1465 enum pool_mode m = get_pool_mode(pool);
1466
1467 switch (m) {
1468 case PM_WRITE:
1469 /* Shouldn't get here */
1470 DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
Mike Snitzeraf918052014-05-22 14:32:51 -04001471 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001472
1473 case PM_OUT_OF_DATA_SPACE:
Mike Snitzeraf918052014-05-22 14:32:51 -04001474 return pool->pf.error_if_no_space ? -ENOSPC : 0;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001475
1476 case PM_READ_ONLY:
1477 case PM_FAIL:
Mike Snitzeraf918052014-05-22 14:32:51 -04001478 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001479 default:
1480 /* Shouldn't get here */
1481 DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
Mike Snitzeraf918052014-05-22 14:32:51 -04001482 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001483 }
1484}
1485
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001486static void handle_unserviceable_bio(struct pool *pool, struct bio *bio)
1487{
Mike Snitzeraf918052014-05-22 14:32:51 -04001488 int error = should_error_unserviceable_bio(pool);
1489
1490 if (error)
1491 bio_endio(bio, error);
Mike Snitzer6d162022013-12-20 18:09:02 -05001492 else
1493 retry_on_resume(bio);
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001494}
1495
Mike Snitzer399cadd2013-12-05 16:03:33 -05001496static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001497{
1498 struct bio *bio;
1499 struct bio_list bios;
Mike Snitzeraf918052014-05-22 14:32:51 -04001500 int error;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001501
Mike Snitzeraf918052014-05-22 14:32:51 -04001502 error = should_error_unserviceable_bio(pool);
1503 if (error) {
1504 cell_error_with_code(pool, cell, error);
Joe Thornber3e1a0692014-03-03 16:03:26 +00001505 return;
1506 }
1507
Joe Thornber991d9fa2011-10-31 20:21:18 +00001508 bio_list_init(&bios);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001509 cell_release(pool, cell, &bios);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001510
Mike Snitzer9d094ee2014-10-19 08:23:09 -04001511 while ((bio = bio_list_pop(&bios)))
1512 retry_on_resume(bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001513}
1514
Joe Thornber34fbcf62015-04-16 12:58:35 +01001515static void process_discard_cell_no_passdown(struct thin_c *tc,
1516 struct dm_bio_prison_cell *virt_cell)
Joe Thornber104655f2012-03-28 18:41:28 +01001517{
Joe Thornber104655f2012-03-28 18:41:28 +01001518 struct pool *pool = tc->pool;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001519 struct dm_thin_new_mapping *m = get_next_mapping(pool);
1520
1521 /*
1522 * We don't need to lock the data blocks, since there's no
1523 * passdown. We only lock data blocks for allocation and breaking sharing.
1524 */
1525 m->tc = tc;
1526 m->virt_begin = virt_cell->key.block_begin;
1527 m->virt_end = virt_cell->key.block_end;
1528 m->cell = virt_cell;
1529 m->bio = virt_cell->holder;
1530
1531 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
1532 pool->process_prepared_discard(m);
1533}
1534
1535/*
1536 * FIXME: DM local hack to defer parent bios's end_io until we
1537 * _know_ all chained sub range discard bios have completed.
1538 * Will go away once late bio splitting lands upstream!
1539 */
1540static inline void __bio_inc_remaining(struct bio *bio)
1541{
1542 bio->bi_flags |= (1 << BIO_CHAIN);
1543 smp_mb__before_atomic();
1544 atomic_inc(&bio->__bi_remaining);
1545}
1546
1547static void break_up_discard_bio(struct thin_c *tc, dm_block_t begin, dm_block_t end,
1548 struct bio *bio)
1549{
1550 struct pool *pool = tc->pool;
1551
1552 int r;
1553 bool maybe_shared;
1554 struct dm_cell_key data_key;
1555 struct dm_bio_prison_cell *data_cell;
Mike Snitzera24c2562012-06-03 00:30:00 +01001556 struct dm_thin_new_mapping *m;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001557 dm_block_t virt_begin, virt_end, data_begin;
Joe Thornber104655f2012-03-28 18:41:28 +01001558
Joe Thornber34fbcf62015-04-16 12:58:35 +01001559 while (begin != end) {
1560 r = ensure_next_mapping(pool);
1561 if (r)
1562 /* we did our best */
1563 return;
Joe Thornber104655f2012-03-28 18:41:28 +01001564
Joe Thornber34fbcf62015-04-16 12:58:35 +01001565 r = dm_thin_find_mapped_range(tc->td, begin, end, &virt_begin, &virt_end,
1566 &data_begin, &maybe_shared);
1567 if (r)
1568 /*
1569 * Silently fail, letting any mappings we've
1570 * created complete.
1571 */
Joe Thornber104655f2012-03-28 18:41:28 +01001572 break;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001573
1574 build_key(tc->td, PHYSICAL, data_begin, data_begin + (virt_end - virt_begin), &data_key);
1575 if (bio_detain(tc->pool, &data_key, NULL, &data_cell)) {
1576 /* contention, we'll give up with this range */
1577 begin = virt_end;
1578 continue;
Joe Thornber104655f2012-03-28 18:41:28 +01001579 }
1580
Joe Thornber104655f2012-03-28 18:41:28 +01001581 /*
Joe Thornber34fbcf62015-04-16 12:58:35 +01001582 * IO may still be going to the destination block. We must
1583 * quiesce before we can do the removal.
Joe Thornber104655f2012-03-28 18:41:28 +01001584 */
Joe Thornber34fbcf62015-04-16 12:58:35 +01001585 m = get_next_mapping(pool);
1586 m->tc = tc;
1587 m->maybe_shared = maybe_shared;
1588 m->virt_begin = virt_begin;
1589 m->virt_end = virt_end;
1590 m->data_block = data_begin;
1591 m->cell = data_cell;
1592 m->bio = bio;
Joe Thornber104655f2012-03-28 18:41:28 +01001593
Joe Thornber34fbcf62015-04-16 12:58:35 +01001594 /*
1595 * The parent bio must not complete before sub discard bios are
1596 * chained to it (see __blkdev_issue_discard_async's bio_chain)!
1597 *
1598 * This per-mapping bi_remaining increment is paired with
1599 * the implicit decrement that occurs via bio_endio() in
1600 * process_prepared_discard_{passdown,no_passdown}.
1601 */
1602 __bio_inc_remaining(bio);
1603 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
1604 pool->process_prepared_discard(m);
1605
1606 begin = virt_end;
Joe Thornber104655f2012-03-28 18:41:28 +01001607 }
1608}
1609
Joe Thornber34fbcf62015-04-16 12:58:35 +01001610static void process_discard_cell_passdown(struct thin_c *tc, struct dm_bio_prison_cell *virt_cell)
1611{
1612 struct bio *bio = virt_cell->holder;
1613 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1614
1615 /*
1616 * The virt_cell will only get freed once the origin bio completes.
1617 * This means it will remain locked while all the individual
1618 * passdown bios are in flight.
1619 */
1620 h->cell = virt_cell;
1621 break_up_discard_bio(tc, virt_cell->key.block_begin, virt_cell->key.block_end, bio);
1622
1623 /*
1624 * We complete the bio now, knowing that the bi_remaining field
1625 * will prevent completion until the sub range discards have
1626 * completed.
1627 */
1628 bio_endio(bio, 0);
1629}
1630
Joe Thornbera374bb22014-10-10 13:43:14 +01001631static void process_discard_bio(struct thin_c *tc, struct bio *bio)
1632{
Joe Thornber34fbcf62015-04-16 12:58:35 +01001633 dm_block_t begin, end;
1634 struct dm_cell_key virt_key;
1635 struct dm_bio_prison_cell *virt_cell;
Joe Thornbera374bb22014-10-10 13:43:14 +01001636
Joe Thornber34fbcf62015-04-16 12:58:35 +01001637 get_bio_block_range(tc, bio, &begin, &end);
1638 if (begin == end) {
1639 /*
1640 * The discard covers less than a block.
1641 */
1642 bio_endio(bio, 0);
1643 return;
1644 }
1645
1646 build_key(tc->td, VIRTUAL, begin, end, &virt_key);
1647 if (bio_detain(tc->pool, &virt_key, bio, &virt_cell))
1648 /*
1649 * Potential starvation issue: We're relying on the
1650 * fs/application being well behaved, and not trying to
1651 * send IO to a region at the same time as discarding it.
1652 * If they do this persistently then it's possible this
1653 * cell will never be granted.
1654 */
Joe Thornbera374bb22014-10-10 13:43:14 +01001655 return;
1656
Joe Thornber34fbcf62015-04-16 12:58:35 +01001657 tc->pool->process_discard_cell(tc, virt_cell);
Joe Thornbera374bb22014-10-10 13:43:14 +01001658}
1659
Joe Thornber991d9fa2011-10-31 20:21:18 +00001660static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzer44feb382012-10-12 21:02:10 +01001661 struct dm_cell_key *key,
Joe Thornber991d9fa2011-10-31 20:21:18 +00001662 struct dm_thin_lookup_result *lookup_result,
Mike Snitzera24c2562012-06-03 00:30:00 +01001663 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001664{
1665 int r;
1666 dm_block_t data_block;
Mike Snitzerd6fc2042013-08-21 17:40:11 -04001667 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001668
1669 r = alloc_data_block(tc, &data_block);
1670 switch (r) {
1671 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001672 schedule_internal_copy(tc, block, lookup_result->block,
1673 data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001674 break;
1675
1676 case -ENOSPC:
Mike Snitzer399cadd2013-12-05 16:03:33 -05001677 retry_bios_on_resume(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001678 break;
1679
1680 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001681 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1682 __func__, r);
Mike Snitzerd6fc2042013-08-21 17:40:11 -04001683 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001684 break;
1685 }
1686}
1687
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001688static void __remap_and_issue_shared_cell(void *context,
1689 struct dm_bio_prison_cell *cell)
1690{
1691 struct remap_info *info = context;
1692 struct bio *bio;
1693
1694 while ((bio = bio_list_pop(&cell->bios))) {
1695 if ((bio_data_dir(bio) == WRITE) ||
1696 (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)))
1697 bio_list_add(&info->defer_bios, bio);
1698 else {
1699 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));;
1700
1701 h->shared_read_entry = dm_deferred_entry_inc(info->tc->pool->shared_read_ds);
1702 inc_all_io_entry(info->tc->pool, bio);
1703 bio_list_add(&info->issue_bios, bio);
1704 }
1705 }
1706}
1707
1708static void remap_and_issue_shared_cell(struct thin_c *tc,
1709 struct dm_bio_prison_cell *cell,
1710 dm_block_t block)
1711{
1712 struct bio *bio;
1713 struct remap_info info;
1714
1715 info.tc = tc;
1716 bio_list_init(&info.defer_bios);
1717 bio_list_init(&info.issue_bios);
1718
1719 cell_visit_release(tc->pool, __remap_and_issue_shared_cell,
1720 &info, cell);
1721
1722 while ((bio = bio_list_pop(&info.defer_bios)))
1723 thin_defer_bio(tc, bio);
1724
1725 while ((bio = bio_list_pop(&info.issue_bios)))
1726 remap_and_issue(tc, bio, block);
1727}
1728
Joe Thornber991d9fa2011-10-31 20:21:18 +00001729static void process_shared_bio(struct thin_c *tc, struct bio *bio,
1730 dm_block_t block,
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001731 struct dm_thin_lookup_result *lookup_result,
1732 struct dm_bio_prison_cell *virt_cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001733{
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001734 struct dm_bio_prison_cell *data_cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001735 struct pool *pool = tc->pool;
Mike Snitzer44feb382012-10-12 21:02:10 +01001736 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001737
1738 /*
1739 * If cell is already occupied, then sharing is already in the process
1740 * of being broken so we have nothing further to do here.
1741 */
1742 build_data_key(tc->td, lookup_result->block, &key);
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001743 if (bio_detain(pool, &key, bio, &data_cell)) {
1744 cell_defer_no_holder(tc, virt_cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001745 return;
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001746 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001747
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001748 if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size) {
1749 break_sharing(tc, bio, block, &key, lookup_result, data_cell);
1750 cell_defer_no_holder(tc, virt_cell);
1751 } else {
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001752 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornber991d9fa2011-10-31 20:21:18 +00001753
Mike Snitzer44feb382012-10-12 21:02:10 +01001754 h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
Joe Thornbere8088072012-12-21 20:23:31 +00001755 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001756 remap_and_issue(tc, bio, lookup_result->block);
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001757
1758 remap_and_issue_shared_cell(tc, data_cell, lookup_result->block);
1759 remap_and_issue_shared_cell(tc, virt_cell, lookup_result->block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001760 }
1761}
1762
1763static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzera24c2562012-06-03 00:30:00 +01001764 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001765{
1766 int r;
1767 dm_block_t data_block;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001768 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001769
1770 /*
1771 * Remap empty bios (flushes) immediately, without provisioning.
1772 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07001773 if (!bio->bi_iter.bi_size) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001774 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001775 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001776
Joe Thornber991d9fa2011-10-31 20:21:18 +00001777 remap_and_issue(tc, bio, 0);
1778 return;
1779 }
1780
1781 /*
1782 * Fill read bios with zeroes and complete them immediately.
1783 */
1784 if (bio_data_dir(bio) == READ) {
1785 zero_fill_bio(bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001786 cell_defer_no_holder(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001787 bio_endio(bio, 0);
1788 return;
1789 }
1790
1791 r = alloc_data_block(tc, &data_block);
1792 switch (r) {
1793 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001794 if (tc->origin_dev)
1795 schedule_external_copy(tc, block, data_block, cell, bio);
1796 else
1797 schedule_zero(tc, block, data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001798 break;
1799
1800 case -ENOSPC:
Mike Snitzer399cadd2013-12-05 16:03:33 -05001801 retry_bios_on_resume(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001802 break;
1803
1804 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001805 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1806 __func__, r);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001807 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001808 break;
1809 }
1810}
1811
Joe Thornbera374bb22014-10-10 13:43:14 +01001812static void process_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001813{
1814 int r;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001815 struct pool *pool = tc->pool;
Joe Thornbera374bb22014-10-10 13:43:14 +01001816 struct bio *bio = cell->holder;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001817 dm_block_t block = get_bio_block(tc, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001818 struct dm_thin_lookup_result lookup_result;
1819
Joe Thornbera374bb22014-10-10 13:43:14 +01001820 if (tc->requeue_mode) {
1821 cell_requeue(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001822 return;
Joe Thornbera374bb22014-10-10 13:43:14 +01001823 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001824
1825 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1826 switch (r) {
1827 case 0:
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001828 if (lookup_result.shared)
1829 process_shared_bio(tc, bio, block, &lookup_result, cell);
1830 else {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001831 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001832 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbera374bb22014-10-10 13:43:14 +01001833 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001834 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001835 break;
1836
1837 case -ENODATA:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001838 if (bio_data_dir(bio) == READ && tc->origin_dev) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001839 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001840 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001841
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001842 if (bio_end_sector(bio) <= tc->origin_size)
1843 remap_to_origin_and_issue(tc, bio);
1844
1845 else if (bio->bi_iter.bi_sector < tc->origin_size) {
1846 zero_fill_bio(bio);
1847 bio->bi_iter.bi_size = (tc->origin_size - bio->bi_iter.bi_sector) << SECTOR_SHIFT;
1848 remap_to_origin_and_issue(tc, bio);
1849
1850 } else {
1851 zero_fill_bio(bio);
1852 bio_endio(bio, 0);
1853 }
Joe Thornber2dd9c252012-03-28 18:41:28 +01001854 } else
1855 provision_block(tc, bio, block, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001856 break;
1857
1858 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001859 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1860 __func__, r);
Joe Thornberf286ba02012-12-21 20:23:33 +00001861 cell_defer_no_holder(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001862 bio_io_error(bio);
1863 break;
1864 }
1865}
1866
Joe Thornbera374bb22014-10-10 13:43:14 +01001867static void process_bio(struct thin_c *tc, struct bio *bio)
1868{
1869 struct pool *pool = tc->pool;
1870 dm_block_t block = get_bio_block(tc, bio);
1871 struct dm_bio_prison_cell *cell;
1872 struct dm_cell_key key;
1873
1874 /*
1875 * If cell is already occupied, then the block is already
1876 * being provisioned so we have nothing further to do here.
1877 */
1878 build_virtual_key(tc->td, block, &key);
1879 if (bio_detain(pool, &key, bio, &cell))
1880 return;
1881
1882 process_cell(tc, cell);
1883}
1884
1885static void __process_bio_read_only(struct thin_c *tc, struct bio *bio,
1886 struct dm_bio_prison_cell *cell)
Joe Thornbere49e5822012-07-27 15:08:16 +01001887{
1888 int r;
1889 int rw = bio_data_dir(bio);
1890 dm_block_t block = get_bio_block(tc, bio);
1891 struct dm_thin_lookup_result lookup_result;
1892
1893 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1894 switch (r) {
1895 case 0:
Joe Thornbera374bb22014-10-10 13:43:14 +01001896 if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size) {
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001897 handle_unserviceable_bio(tc->pool, bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01001898 if (cell)
1899 cell_defer_no_holder(tc, cell);
1900 } else {
Joe Thornbere8088072012-12-21 20:23:31 +00001901 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001902 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbera374bb22014-10-10 13:43:14 +01001903 if (cell)
1904 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001905 }
Joe Thornbere49e5822012-07-27 15:08:16 +01001906 break;
1907
1908 case -ENODATA:
Joe Thornbera374bb22014-10-10 13:43:14 +01001909 if (cell)
1910 cell_defer_no_holder(tc, cell);
Joe Thornbere49e5822012-07-27 15:08:16 +01001911 if (rw != READ) {
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001912 handle_unserviceable_bio(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001913 break;
1914 }
1915
1916 if (tc->origin_dev) {
Joe Thornbere8088072012-12-21 20:23:31 +00001917 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001918 remap_to_origin_and_issue(tc, bio);
1919 break;
1920 }
1921
1922 zero_fill_bio(bio);
1923 bio_endio(bio, 0);
1924 break;
1925
1926 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001927 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1928 __func__, r);
Joe Thornbera374bb22014-10-10 13:43:14 +01001929 if (cell)
1930 cell_defer_no_holder(tc, cell);
Joe Thornbere49e5822012-07-27 15:08:16 +01001931 bio_io_error(bio);
1932 break;
1933 }
1934}
1935
Joe Thornbera374bb22014-10-10 13:43:14 +01001936static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
1937{
1938 __process_bio_read_only(tc, bio, NULL);
1939}
1940
1941static void process_cell_read_only(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1942{
1943 __process_bio_read_only(tc, cell->holder, cell);
1944}
1945
Joe Thornber3e1a0692014-03-03 16:03:26 +00001946static void process_bio_success(struct thin_c *tc, struct bio *bio)
1947{
1948 bio_endio(bio, 0);
1949}
1950
Joe Thornbere49e5822012-07-27 15:08:16 +01001951static void process_bio_fail(struct thin_c *tc, struct bio *bio)
1952{
1953 bio_io_error(bio);
1954}
1955
Joe Thornbera374bb22014-10-10 13:43:14 +01001956static void process_cell_success(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1957{
1958 cell_success(tc->pool, cell);
1959}
1960
1961static void process_cell_fail(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1962{
1963 cell_error(tc->pool, cell);
1964}
1965
Joe Thornberac8c3f32013-05-10 14:37:21 +01001966/*
1967 * FIXME: should we also commit due to size of transaction, measured in
1968 * metadata blocks?
1969 */
Joe Thornber905e51b2012-03-28 18:41:27 +01001970static int need_commit_due_to_time(struct pool *pool)
1971{
Manuel Schölling0f30af92014-05-22 22:42:37 +02001972 return !time_in_range(jiffies, pool->last_commit_jiffies,
1973 pool->last_commit_jiffies + COMMIT_PERIOD);
Joe Thornber905e51b2012-03-28 18:41:27 +01001974}
1975
Mike Snitzer67324ea2014-03-21 18:33:41 -04001976#define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node)
1977#define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook))
1978
1979static void __thin_bio_rb_add(struct thin_c *tc, struct bio *bio)
1980{
1981 struct rb_node **rbp, *parent;
1982 struct dm_thin_endio_hook *pbd;
1983 sector_t bi_sector = bio->bi_iter.bi_sector;
1984
1985 rbp = &tc->sort_bio_list.rb_node;
1986 parent = NULL;
1987 while (*rbp) {
1988 parent = *rbp;
1989 pbd = thin_pbd(parent);
1990
1991 if (bi_sector < thin_bio(pbd)->bi_iter.bi_sector)
1992 rbp = &(*rbp)->rb_left;
1993 else
1994 rbp = &(*rbp)->rb_right;
1995 }
1996
1997 pbd = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1998 rb_link_node(&pbd->rb_node, parent, rbp);
1999 rb_insert_color(&pbd->rb_node, &tc->sort_bio_list);
2000}
2001
2002static void __extract_sorted_bios(struct thin_c *tc)
2003{
2004 struct rb_node *node;
2005 struct dm_thin_endio_hook *pbd;
2006 struct bio *bio;
2007
2008 for (node = rb_first(&tc->sort_bio_list); node; node = rb_next(node)) {
2009 pbd = thin_pbd(node);
2010 bio = thin_bio(pbd);
2011
2012 bio_list_add(&tc->deferred_bio_list, bio);
2013 rb_erase(&pbd->rb_node, &tc->sort_bio_list);
2014 }
2015
2016 WARN_ON(!RB_EMPTY_ROOT(&tc->sort_bio_list));
2017}
2018
2019static void __sort_thin_deferred_bios(struct thin_c *tc)
2020{
2021 struct bio *bio;
2022 struct bio_list bios;
2023
2024 bio_list_init(&bios);
2025 bio_list_merge(&bios, &tc->deferred_bio_list);
2026 bio_list_init(&tc->deferred_bio_list);
2027
2028 /* Sort deferred_bio_list using rb-tree */
2029 while ((bio = bio_list_pop(&bios)))
2030 __thin_bio_rb_add(tc, bio);
2031
2032 /*
2033 * Transfer the sorted bios in sort_bio_list back to
2034 * deferred_bio_list to allow lockless submission of
2035 * all bios.
2036 */
2037 __extract_sorted_bios(tc);
2038}
2039
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002040static void process_thin_deferred_bios(struct thin_c *tc)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002041{
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002042 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002043 unsigned long flags;
2044 struct bio *bio;
2045 struct bio_list bios;
Mike Snitzer67324ea2014-03-21 18:33:41 -04002046 struct blk_plug plug;
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002047 unsigned count = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002048
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002049 if (tc->requeue_mode) {
Mike Snitzer42d6a8c2014-10-19 07:52:44 -04002050 error_thin_bio_list(tc, &tc->deferred_bio_list, DM_ENDIO_REQUEUE);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002051 return;
2052 }
2053
Joe Thornber991d9fa2011-10-31 20:21:18 +00002054 bio_list_init(&bios);
2055
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002056 spin_lock_irqsave(&tc->lock, flags);
Mike Snitzer67324ea2014-03-21 18:33:41 -04002057
2058 if (bio_list_empty(&tc->deferred_bio_list)) {
2059 spin_unlock_irqrestore(&tc->lock, flags);
2060 return;
2061 }
2062
2063 __sort_thin_deferred_bios(tc);
2064
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002065 bio_list_merge(&bios, &tc->deferred_bio_list);
2066 bio_list_init(&tc->deferred_bio_list);
Mike Snitzer67324ea2014-03-21 18:33:41 -04002067
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002068 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002069
Mike Snitzer67324ea2014-03-21 18:33:41 -04002070 blk_start_plug(&plug);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002071 while ((bio = bio_list_pop(&bios))) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00002072 /*
2073 * If we've got no free new_mapping structs, and processing
2074 * this bio might require one, we pause until there are some
2075 * prepared mappings to process.
2076 */
2077 if (ensure_next_mapping(pool)) {
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002078 spin_lock_irqsave(&tc->lock, flags);
2079 bio_list_add(&tc->deferred_bio_list, bio);
2080 bio_list_merge(&tc->deferred_bio_list, &bios);
2081 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002082 break;
2083 }
Joe Thornber104655f2012-03-28 18:41:28 +01002084
2085 if (bio->bi_rw & REQ_DISCARD)
Joe Thornbere49e5822012-07-27 15:08:16 +01002086 pool->process_discard(tc, bio);
Joe Thornber104655f2012-03-28 18:41:28 +01002087 else
Joe Thornbere49e5822012-07-27 15:08:16 +01002088 pool->process_bio(tc, bio);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002089
2090 if ((count++ & 127) == 0) {
Joe Thornber7d327fe2014-10-06 15:45:59 +01002091 throttle_work_update(&pool->throttle);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002092 dm_pool_issue_prefetches(pool->pmd);
2093 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002094 }
Mike Snitzer67324ea2014-03-21 18:33:41 -04002095 blk_finish_plug(&plug);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002096}
2097
Joe Thornberac4c3f32014-10-10 16:42:10 +01002098static int cmp_cells(const void *lhs, const void *rhs)
2099{
2100 struct dm_bio_prison_cell *lhs_cell = *((struct dm_bio_prison_cell **) lhs);
2101 struct dm_bio_prison_cell *rhs_cell = *((struct dm_bio_prison_cell **) rhs);
2102
2103 BUG_ON(!lhs_cell->holder);
2104 BUG_ON(!rhs_cell->holder);
2105
2106 if (lhs_cell->holder->bi_iter.bi_sector < rhs_cell->holder->bi_iter.bi_sector)
2107 return -1;
2108
2109 if (lhs_cell->holder->bi_iter.bi_sector > rhs_cell->holder->bi_iter.bi_sector)
2110 return 1;
2111
2112 return 0;
2113}
2114
2115static unsigned sort_cells(struct pool *pool, struct list_head *cells)
2116{
2117 unsigned count = 0;
2118 struct dm_bio_prison_cell *cell, *tmp;
2119
2120 list_for_each_entry_safe(cell, tmp, cells, user_list) {
2121 if (count >= CELL_SORT_ARRAY_SIZE)
2122 break;
2123
2124 pool->cell_sort_array[count++] = cell;
2125 list_del(&cell->user_list);
2126 }
2127
2128 sort(pool->cell_sort_array, count, sizeof(cell), cmp_cells, NULL);
2129
2130 return count;
2131}
2132
Joe Thornbera374bb22014-10-10 13:43:14 +01002133static void process_thin_deferred_cells(struct thin_c *tc)
2134{
2135 struct pool *pool = tc->pool;
2136 unsigned long flags;
2137 struct list_head cells;
Joe Thornberac4c3f32014-10-10 16:42:10 +01002138 struct dm_bio_prison_cell *cell;
2139 unsigned i, j, count;
Joe Thornbera374bb22014-10-10 13:43:14 +01002140
2141 INIT_LIST_HEAD(&cells);
2142
2143 spin_lock_irqsave(&tc->lock, flags);
2144 list_splice_init(&tc->deferred_cells, &cells);
2145 spin_unlock_irqrestore(&tc->lock, flags);
2146
2147 if (list_empty(&cells))
2148 return;
2149
Joe Thornberac4c3f32014-10-10 16:42:10 +01002150 do {
2151 count = sort_cells(tc->pool, &cells);
Joe Thornbera374bb22014-10-10 13:43:14 +01002152
Joe Thornberac4c3f32014-10-10 16:42:10 +01002153 for (i = 0; i < count; i++) {
2154 cell = pool->cell_sort_array[i];
2155 BUG_ON(!cell->holder);
2156
2157 /*
2158 * If we've got no free new_mapping structs, and processing
2159 * this bio might require one, we pause until there are some
2160 * prepared mappings to process.
2161 */
2162 if (ensure_next_mapping(pool)) {
2163 for (j = i; j < count; j++)
2164 list_add(&pool->cell_sort_array[j]->user_list, &cells);
2165
2166 spin_lock_irqsave(&tc->lock, flags);
2167 list_splice(&cells, &tc->deferred_cells);
2168 spin_unlock_irqrestore(&tc->lock, flags);
2169 return;
2170 }
2171
2172 if (cell->holder->bi_rw & REQ_DISCARD)
2173 pool->process_discard_cell(tc, cell);
2174 else
2175 pool->process_cell(tc, cell);
Joe Thornbera374bb22014-10-10 13:43:14 +01002176 }
Joe Thornberac4c3f32014-10-10 16:42:10 +01002177 } while (!list_empty(&cells));
Joe Thornbera374bb22014-10-10 13:43:14 +01002178}
2179
Joe Thornberb10ebd32014-04-08 11:29:01 +01002180static void thin_get(struct thin_c *tc);
2181static void thin_put(struct thin_c *tc);
2182
2183/*
2184 * We can't hold rcu_read_lock() around code that can block. So we
2185 * find a thin with the rcu lock held; bump a refcount; then drop
2186 * the lock.
2187 */
2188static struct thin_c *get_first_thin(struct pool *pool)
2189{
2190 struct thin_c *tc = NULL;
2191
2192 rcu_read_lock();
2193 if (!list_empty(&pool->active_thins)) {
2194 tc = list_entry_rcu(pool->active_thins.next, struct thin_c, list);
2195 thin_get(tc);
2196 }
2197 rcu_read_unlock();
2198
2199 return tc;
2200}
2201
2202static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc)
2203{
2204 struct thin_c *old_tc = tc;
2205
2206 rcu_read_lock();
2207 list_for_each_entry_continue_rcu(tc, &pool->active_thins, list) {
2208 thin_get(tc);
2209 thin_put(old_tc);
2210 rcu_read_unlock();
2211 return tc;
2212 }
2213 thin_put(old_tc);
2214 rcu_read_unlock();
2215
2216 return NULL;
2217}
2218
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002219static void process_deferred_bios(struct pool *pool)
2220{
2221 unsigned long flags;
2222 struct bio *bio;
2223 struct bio_list bios;
2224 struct thin_c *tc;
2225
Joe Thornberb10ebd32014-04-08 11:29:01 +01002226 tc = get_first_thin(pool);
2227 while (tc) {
Joe Thornbera374bb22014-10-10 13:43:14 +01002228 process_thin_deferred_cells(tc);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002229 process_thin_deferred_bios(tc);
Joe Thornberb10ebd32014-04-08 11:29:01 +01002230 tc = get_next_thin(pool, tc);
2231 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002232
2233 /*
2234 * If there are any deferred flush bios, we must commit
2235 * the metadata before issuing them.
2236 */
2237 bio_list_init(&bios);
2238 spin_lock_irqsave(&pool->lock, flags);
2239 bio_list_merge(&bios, &pool->deferred_flush_bios);
2240 bio_list_init(&pool->deferred_flush_bios);
2241 spin_unlock_irqrestore(&pool->lock, flags);
2242
Mike Snitzer4d1662a2014-02-06 06:08:56 -05002243 if (bio_list_empty(&bios) &&
2244 !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool)))
Joe Thornber991d9fa2011-10-31 20:21:18 +00002245 return;
2246
Joe Thornber020cc3b2013-12-04 15:05:36 -05002247 if (commit(pool)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00002248 while ((bio = bio_list_pop(&bios)))
2249 bio_io_error(bio);
2250 return;
2251 }
Joe Thornber905e51b2012-03-28 18:41:27 +01002252 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002253
2254 while ((bio = bio_list_pop(&bios)))
2255 generic_make_request(bio);
2256}
2257
2258static void do_worker(struct work_struct *ws)
2259{
2260 struct pool *pool = container_of(ws, struct pool, worker);
2261
Joe Thornber7d327fe2014-10-06 15:45:59 +01002262 throttle_work_start(&pool->throttle);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002263 dm_pool_issue_prefetches(pool->pmd);
Joe Thornber7d327fe2014-10-06 15:45:59 +01002264 throttle_work_update(&pool->throttle);
Joe Thornbere49e5822012-07-27 15:08:16 +01002265 process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
Joe Thornber7d327fe2014-10-06 15:45:59 +01002266 throttle_work_update(&pool->throttle);
Joe Thornbere49e5822012-07-27 15:08:16 +01002267 process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
Joe Thornber7d327fe2014-10-06 15:45:59 +01002268 throttle_work_update(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002269 process_deferred_bios(pool);
Joe Thornber7d327fe2014-10-06 15:45:59 +01002270 throttle_work_complete(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002271}
2272
Joe Thornber905e51b2012-03-28 18:41:27 +01002273/*
2274 * We want to commit periodically so that not too much
2275 * unwritten data builds up.
2276 */
2277static void do_waker(struct work_struct *ws)
2278{
2279 struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
2280 wake_worker(pool);
2281 queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
2282}
2283
Joe Thornber85ad643b2014-05-09 15:59:38 +01002284/*
2285 * We're holding onto IO to allow userland time to react. After the
2286 * timeout either the pool will have been resized (and thus back in
2287 * PM_WRITE mode), or we degrade to PM_READ_ONLY and start erroring IO.
2288 */
2289static void do_no_space_timeout(struct work_struct *ws)
2290{
2291 struct pool *pool = container_of(to_delayed_work(ws), struct pool,
2292 no_space_timeout);
2293
2294 if (get_pool_mode(pool) == PM_OUT_OF_DATA_SPACE && !pool->pf.error_if_no_space)
2295 set_pool_mode(pool, PM_READ_ONLY);
2296}
2297
Joe Thornber991d9fa2011-10-31 20:21:18 +00002298/*----------------------------------------------------------------*/
2299
Joe Thornbere7a3e872014-05-13 16:14:14 -04002300struct pool_work {
Joe Thornber738211f2014-03-03 15:52:28 +00002301 struct work_struct worker;
Joe Thornbere7a3e872014-05-13 16:14:14 -04002302 struct completion complete;
Joe Thornber738211f2014-03-03 15:52:28 +00002303};
2304
Joe Thornbere7a3e872014-05-13 16:14:14 -04002305static struct pool_work *to_pool_work(struct work_struct *ws)
Joe Thornber738211f2014-03-03 15:52:28 +00002306{
Joe Thornbere7a3e872014-05-13 16:14:14 -04002307 return container_of(ws, struct pool_work, worker);
2308}
2309
2310static void pool_work_complete(struct pool_work *pw)
2311{
2312 complete(&pw->complete);
2313}
2314
2315static void pool_work_wait(struct pool_work *pw, struct pool *pool,
2316 void (*fn)(struct work_struct *))
2317{
2318 INIT_WORK_ONSTACK(&pw->worker, fn);
2319 init_completion(&pw->complete);
2320 queue_work(pool->wq, &pw->worker);
2321 wait_for_completion(&pw->complete);
2322}
2323
2324/*----------------------------------------------------------------*/
2325
2326struct noflush_work {
2327 struct pool_work pw;
2328 struct thin_c *tc;
2329};
2330
2331static struct noflush_work *to_noflush(struct work_struct *ws)
2332{
2333 return container_of(to_pool_work(ws), struct noflush_work, pw);
Joe Thornber738211f2014-03-03 15:52:28 +00002334}
2335
2336static void do_noflush_start(struct work_struct *ws)
2337{
Joe Thornbere7a3e872014-05-13 16:14:14 -04002338 struct noflush_work *w = to_noflush(ws);
Joe Thornber738211f2014-03-03 15:52:28 +00002339 w->tc->requeue_mode = true;
2340 requeue_io(w->tc);
Joe Thornbere7a3e872014-05-13 16:14:14 -04002341 pool_work_complete(&w->pw);
Joe Thornber738211f2014-03-03 15:52:28 +00002342}
2343
2344static void do_noflush_stop(struct work_struct *ws)
2345{
Joe Thornbere7a3e872014-05-13 16:14:14 -04002346 struct noflush_work *w = to_noflush(ws);
Joe Thornber738211f2014-03-03 15:52:28 +00002347 w->tc->requeue_mode = false;
Joe Thornbere7a3e872014-05-13 16:14:14 -04002348 pool_work_complete(&w->pw);
Joe Thornber738211f2014-03-03 15:52:28 +00002349}
2350
2351static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *))
2352{
2353 struct noflush_work w;
2354
Joe Thornber738211f2014-03-03 15:52:28 +00002355 w.tc = tc;
Joe Thornbere7a3e872014-05-13 16:14:14 -04002356 pool_work_wait(&w.pw, tc->pool, fn);
Joe Thornber738211f2014-03-03 15:52:28 +00002357}
2358
2359/*----------------------------------------------------------------*/
2360
Joe Thornbere49e5822012-07-27 15:08:16 +01002361static enum pool_mode get_pool_mode(struct pool *pool)
2362{
2363 return pool->pf.mode;
2364}
2365
Joe Thornber3e1a0692014-03-03 16:03:26 +00002366static void notify_of_pool_mode_change(struct pool *pool, const char *new_mode)
2367{
2368 dm_table_event(pool->ti->table);
2369 DMINFO("%s: switching pool to %s mode",
2370 dm_device_name(pool->pool_md), new_mode);
2371}
2372
Joe Thornber34fbcf62015-04-16 12:58:35 +01002373static bool passdown_enabled(struct pool_c *pt)
2374{
2375 return pt->adjusted_pf.discard_passdown;
2376}
2377
2378static void set_discard_callbacks(struct pool *pool)
2379{
2380 struct pool_c *pt = pool->ti->private;
2381
2382 if (passdown_enabled(pt)) {
2383 pool->process_discard_cell = process_discard_cell_passdown;
2384 pool->process_prepared_discard = process_prepared_discard_passdown;
2385 } else {
2386 pool->process_discard_cell = process_discard_cell_no_passdown;
2387 pool->process_prepared_discard = process_prepared_discard_no_passdown;
2388 }
2389}
2390
Mike Snitzer8b64e882013-12-20 14:27:28 -05002391static void set_pool_mode(struct pool *pool, enum pool_mode new_mode)
Joe Thornbere49e5822012-07-27 15:08:16 +01002392{
Mike Snitzercdc2b412014-02-14 18:10:55 -05002393 struct pool_c *pt = pool->ti->private;
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002394 bool needs_check = dm_pool_metadata_needs_check(pool->pmd);
2395 enum pool_mode old_mode = get_pool_mode(pool);
Mike Snitzer80c57892014-05-20 13:38:33 -04002396 unsigned long no_space_timeout = ACCESS_ONCE(no_space_timeout_secs) * HZ;
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002397
2398 /*
2399 * Never allow the pool to transition to PM_WRITE mode if user
2400 * intervention is required to verify metadata and data consistency.
2401 */
2402 if (new_mode == PM_WRITE && needs_check) {
2403 DMERR("%s: unable to switch pool to write mode until repaired.",
2404 dm_device_name(pool->pool_md));
2405 if (old_mode != new_mode)
2406 new_mode = old_mode;
2407 else
2408 new_mode = PM_READ_ONLY;
2409 }
2410 /*
2411 * If we were in PM_FAIL mode, rollback of metadata failed. We're
2412 * not going to recover without a thin_repair. So we never let the
2413 * pool move out of the old mode.
2414 */
2415 if (old_mode == PM_FAIL)
2416 new_mode = old_mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002417
Mike Snitzer8b64e882013-12-20 14:27:28 -05002418 switch (new_mode) {
Joe Thornbere49e5822012-07-27 15:08:16 +01002419 case PM_FAIL:
Mike Snitzer8b64e882013-12-20 14:27:28 -05002420 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00002421 notify_of_pool_mode_change(pool, "failure");
Joe Thornber5383ef32013-12-04 16:30:01 -05002422 dm_pool_metadata_read_only(pool->pmd);
Joe Thornbere49e5822012-07-27 15:08:16 +01002423 pool->process_bio = process_bio_fail;
2424 pool->process_discard = process_bio_fail;
Joe Thornbera374bb22014-10-10 13:43:14 +01002425 pool->process_cell = process_cell_fail;
2426 pool->process_discard_cell = process_cell_fail;
Joe Thornbere49e5822012-07-27 15:08:16 +01002427 pool->process_prepared_mapping = process_prepared_mapping_fail;
2428 pool->process_prepared_discard = process_prepared_discard_fail;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002429
2430 error_retry_list(pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01002431 break;
2432
2433 case PM_READ_ONLY:
Mike Snitzer8b64e882013-12-20 14:27:28 -05002434 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00002435 notify_of_pool_mode_change(pool, "read-only");
2436 dm_pool_metadata_read_only(pool->pmd);
2437 pool->process_bio = process_bio_read_only;
2438 pool->process_discard = process_bio_success;
Joe Thornbera374bb22014-10-10 13:43:14 +01002439 pool->process_cell = process_cell_read_only;
2440 pool->process_discard_cell = process_cell_success;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002441 pool->process_prepared_mapping = process_prepared_mapping_fail;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002442 pool->process_prepared_discard = process_prepared_discard_success;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002443
2444 error_retry_list(pool);
2445 break;
2446
2447 case PM_OUT_OF_DATA_SPACE:
2448 /*
2449 * Ideally we'd never hit this state; the low water mark
2450 * would trigger userland to extend the pool before we
2451 * completely run out of data space. However, many small
2452 * IOs to unprovisioned space can consume data space at an
2453 * alarming rate. Adjust your low water mark if you're
2454 * frequently seeing this mode.
2455 */
2456 if (old_mode != new_mode)
2457 notify_of_pool_mode_change(pool, "out-of-data-space");
2458 pool->process_bio = process_bio_read_only;
Joe Thornbera374bb22014-10-10 13:43:14 +01002459 pool->process_discard = process_discard_bio;
2460 pool->process_cell = process_cell_read_only;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002461 pool->process_prepared_mapping = process_prepared_mapping;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002462 set_discard_callbacks(pool);
Joe Thornber85ad643b2014-05-09 15:59:38 +01002463
Mike Snitzer80c57892014-05-20 13:38:33 -04002464 if (!pool->pf.error_if_no_space && no_space_timeout)
2465 queue_delayed_work(pool->wq, &pool->no_space_timeout, no_space_timeout);
Joe Thornbere49e5822012-07-27 15:08:16 +01002466 break;
2467
2468 case PM_WRITE:
Mike Snitzer8b64e882013-12-20 14:27:28 -05002469 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00002470 notify_of_pool_mode_change(pool, "write");
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002471 dm_pool_metadata_read_write(pool->pmd);
Joe Thornbere49e5822012-07-27 15:08:16 +01002472 pool->process_bio = process_bio;
Joe Thornbera374bb22014-10-10 13:43:14 +01002473 pool->process_discard = process_discard_bio;
2474 pool->process_cell = process_cell;
Joe Thornbere49e5822012-07-27 15:08:16 +01002475 pool->process_prepared_mapping = process_prepared_mapping;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002476 set_discard_callbacks(pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01002477 break;
2478 }
Mike Snitzer8b64e882013-12-20 14:27:28 -05002479
2480 pool->pf.mode = new_mode;
Mike Snitzercdc2b412014-02-14 18:10:55 -05002481 /*
2482 * The pool mode may have changed, sync it so bind_control_target()
2483 * doesn't cause an unexpected mode transition on resume.
2484 */
2485 pt->adjusted_pf.mode = new_mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002486}
2487
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002488static void abort_transaction(struct pool *pool)
2489{
2490 const char *dev_name = dm_device_name(pool->pool_md);
2491
2492 DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
2493 if (dm_pool_abort_metadata(pool->pmd)) {
2494 DMERR("%s: failed to abort metadata transaction", dev_name);
2495 set_pool_mode(pool, PM_FAIL);
2496 }
2497
2498 if (dm_pool_metadata_set_needs_check(pool->pmd)) {
2499 DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
2500 set_pool_mode(pool, PM_FAIL);
2501 }
2502}
2503
Joe Thornberb5330652013-12-04 19:51:33 -05002504static void metadata_operation_failed(struct pool *pool, const char *op, int r)
2505{
2506 DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
2507 dm_device_name(pool->pool_md), op, r);
2508
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002509 abort_transaction(pool);
Joe Thornberb5330652013-12-04 19:51:33 -05002510 set_pool_mode(pool, PM_READ_ONLY);
2511}
2512
Joe Thornbere49e5822012-07-27 15:08:16 +01002513/*----------------------------------------------------------------*/
2514
Joe Thornber991d9fa2011-10-31 20:21:18 +00002515/*
2516 * Mapping functions.
2517 */
2518
2519/*
2520 * Called only while mapping a thin bio to hand it over to the workqueue.
2521 */
2522static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
2523{
2524 unsigned long flags;
2525 struct pool *pool = tc->pool;
2526
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002527 spin_lock_irqsave(&tc->lock, flags);
2528 bio_list_add(&tc->deferred_bio_list, bio);
2529 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002530
2531 wake_worker(pool);
2532}
2533
Joe Thornber7d327fe2014-10-06 15:45:59 +01002534static void thin_defer_bio_with_throttle(struct thin_c *tc, struct bio *bio)
2535{
2536 struct pool *pool = tc->pool;
2537
2538 throttle_lock(&pool->throttle);
2539 thin_defer_bio(tc, bio);
2540 throttle_unlock(&pool->throttle);
2541}
2542
Joe Thornbera374bb22014-10-10 13:43:14 +01002543static void thin_defer_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
2544{
2545 unsigned long flags;
2546 struct pool *pool = tc->pool;
2547
2548 throttle_lock(&pool->throttle);
2549 spin_lock_irqsave(&tc->lock, flags);
2550 list_add_tail(&cell->user_list, &tc->deferred_cells);
2551 spin_unlock_irqrestore(&tc->lock, flags);
2552 throttle_unlock(&pool->throttle);
2553
2554 wake_worker(pool);
2555}
2556
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002557static void thin_hook_bio(struct thin_c *tc, struct bio *bio)
Joe Thornbereb2aa482012-03-28 18:41:28 +01002558{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002559 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01002560
2561 h->tc = tc;
2562 h->shared_read_entry = NULL;
Joe Thornbere8088072012-12-21 20:23:31 +00002563 h->all_io_entry = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01002564 h->overwrite_mapping = NULL;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002565 h->cell = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01002566}
2567
Joe Thornber991d9fa2011-10-31 20:21:18 +00002568/*
2569 * Non-blocking function called from the thin target's map function.
2570 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002571static int thin_bio_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002572{
2573 int r;
2574 struct thin_c *tc = ti->private;
2575 dm_block_t block = get_bio_block(tc, bio);
2576 struct dm_thin_device *td = tc->td;
2577 struct dm_thin_lookup_result result;
Joe Thornbera374bb22014-10-10 13:43:14 +01002578 struct dm_bio_prison_cell *virt_cell, *data_cell;
Joe Thornbere8088072012-12-21 20:23:31 +00002579 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002580
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002581 thin_hook_bio(tc, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01002582
Joe Thornber738211f2014-03-03 15:52:28 +00002583 if (tc->requeue_mode) {
2584 bio_endio(bio, DM_ENDIO_REQUEUE);
2585 return DM_MAPIO_SUBMITTED;
2586 }
2587
Joe Thornbere49e5822012-07-27 15:08:16 +01002588 if (get_pool_mode(tc->pool) == PM_FAIL) {
2589 bio_io_error(bio);
2590 return DM_MAPIO_SUBMITTED;
2591 }
2592
Joe Thornber104655f2012-03-28 18:41:28 +01002593 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) {
Joe Thornber7d327fe2014-10-06 15:45:59 +01002594 thin_defer_bio_with_throttle(tc, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002595 return DM_MAPIO_SUBMITTED;
2596 }
2597
Joe Thornberc822ed92014-10-10 09:41:09 +01002598 /*
2599 * We must hold the virtual cell before doing the lookup, otherwise
2600 * there's a race with discard.
2601 */
2602 build_virtual_key(tc->td, block, &key);
Joe Thornbera374bb22014-10-10 13:43:14 +01002603 if (bio_detain(tc->pool, &key, bio, &virt_cell))
Joe Thornberc822ed92014-10-10 09:41:09 +01002604 return DM_MAPIO_SUBMITTED;
2605
Joe Thornber991d9fa2011-10-31 20:21:18 +00002606 r = dm_thin_find_block(td, block, 0, &result);
2607
2608 /*
2609 * Note that we defer readahead too.
2610 */
2611 switch (r) {
2612 case 0:
2613 if (unlikely(result.shared)) {
2614 /*
2615 * We have a race condition here between the
2616 * result.shared value returned by the lookup and
2617 * snapshot creation, which may cause new
2618 * sharing.
2619 *
2620 * To avoid this always quiesce the origin before
2621 * taking the snap. You want to do this anyway to
2622 * ensure a consistent application view
2623 * (i.e. lockfs).
2624 *
2625 * More distant ancestors are irrelevant. The
2626 * shared flag will be set in their case.
2627 */
Joe Thornbera374bb22014-10-10 13:43:14 +01002628 thin_defer_cell(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002629 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002630 }
Joe Thornbere8088072012-12-21 20:23:31 +00002631
Joe Thornbere8088072012-12-21 20:23:31 +00002632 build_data_key(tc->td, result.block, &key);
Joe Thornbera374bb22014-10-10 13:43:14 +01002633 if (bio_detain(tc->pool, &key, bio, &data_cell)) {
2634 cell_defer_no_holder(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002635 return DM_MAPIO_SUBMITTED;
2636 }
2637
2638 inc_all_io_entry(tc->pool, bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01002639 cell_defer_no_holder(tc, data_cell);
2640 cell_defer_no_holder(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002641
2642 remap(tc, bio, result.block);
2643 return DM_MAPIO_REMAPPED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002644
2645 case -ENODATA:
Joe Thornbere49e5822012-07-27 15:08:16 +01002646 case -EWOULDBLOCK:
Joe Thornbera374bb22014-10-10 13:43:14 +01002647 thin_defer_cell(tc, virt_cell);
Joe Thornber2aab3852012-12-21 20:23:33 +00002648 return DM_MAPIO_SUBMITTED;
Joe Thornbere49e5822012-07-27 15:08:16 +01002649
2650 default:
2651 /*
2652 * Must always call bio_io_error on failure.
2653 * dm_thin_find_block can fail with -EINVAL if the
2654 * pool is switched to fail-io mode.
2655 */
2656 bio_io_error(bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01002657 cell_defer_no_holder(tc, virt_cell);
Joe Thornber2aab3852012-12-21 20:23:33 +00002658 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002659 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002660}
2661
2662static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
2663{
Joe Thornber991d9fa2011-10-31 20:21:18 +00002664 struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
Mike Snitzer760fe672014-03-20 08:36:47 -04002665 struct request_queue *q;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002666
Mike Snitzer760fe672014-03-20 08:36:47 -04002667 if (get_pool_mode(pt->pool) == PM_OUT_OF_DATA_SPACE)
2668 return 1;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002669
Mike Snitzer760fe672014-03-20 08:36:47 -04002670 q = bdev_get_queue(pt->data_dev->bdev);
2671 return bdi_congested(&q->backing_dev_info, bdi_bits);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002672}
2673
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002674static void requeue_bios(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002675{
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002676 unsigned long flags;
2677 struct thin_c *tc;
2678
2679 rcu_read_lock();
2680 list_for_each_entry_rcu(tc, &pool->active_thins, list) {
2681 spin_lock_irqsave(&tc->lock, flags);
2682 bio_list_merge(&tc->deferred_bio_list, &tc->retry_on_resume_list);
2683 bio_list_init(&tc->retry_on_resume_list);
2684 spin_unlock_irqrestore(&tc->lock, flags);
2685 }
2686 rcu_read_unlock();
Joe Thornber991d9fa2011-10-31 20:21:18 +00002687}
2688
2689/*----------------------------------------------------------------
2690 * Binding of control targets to a pool object
2691 *--------------------------------------------------------------*/
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002692static bool data_dev_supports_discard(struct pool_c *pt)
2693{
2694 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
2695
2696 return q && blk_queue_discard(q);
2697}
2698
Joe Thornber58051b92013-03-20 17:21:25 +00002699static bool is_factor(sector_t block_size, uint32_t n)
2700{
2701 return !sector_div(block_size, n);
2702}
2703
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002704/*
2705 * If discard_passdown was enabled verify that the data device
Mike Snitzer0424caa2012-09-26 23:45:47 +01002706 * supports discards. Disable discard_passdown if not.
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002707 */
Mike Snitzer0424caa2012-09-26 23:45:47 +01002708static void disable_passdown_if_not_supported(struct pool_c *pt)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002709{
Mike Snitzer0424caa2012-09-26 23:45:47 +01002710 struct pool *pool = pt->pool;
2711 struct block_device *data_bdev = pt->data_dev->bdev;
2712 struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
Mike Snitzer0424caa2012-09-26 23:45:47 +01002713 const char *reason = NULL;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002714 char buf[BDEVNAME_SIZE];
2715
Mike Snitzer0424caa2012-09-26 23:45:47 +01002716 if (!pt->adjusted_pf.discard_passdown)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002717 return;
2718
Mike Snitzer0424caa2012-09-26 23:45:47 +01002719 if (!data_dev_supports_discard(pt))
2720 reason = "discard unsupported";
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002721
Mike Snitzer0424caa2012-09-26 23:45:47 +01002722 else if (data_limits->max_discard_sectors < pool->sectors_per_block)
2723 reason = "max discard sectors smaller than a block";
2724
Mike Snitzer0424caa2012-09-26 23:45:47 +01002725 if (reason) {
2726 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
2727 pt->adjusted_pf.discard_passdown = false;
2728 }
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002729}
2730
Joe Thornber991d9fa2011-10-31 20:21:18 +00002731static int bind_control_target(struct pool *pool, struct dm_target *ti)
2732{
2733 struct pool_c *pt = ti->private;
2734
Joe Thornbere49e5822012-07-27 15:08:16 +01002735 /*
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002736 * We want to make sure that a pool in PM_FAIL mode is never upgraded.
Joe Thornbere49e5822012-07-27 15:08:16 +01002737 */
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002738 enum pool_mode old_mode = get_pool_mode(pool);
Mike Snitzer0424caa2012-09-26 23:45:47 +01002739 enum pool_mode new_mode = pt->adjusted_pf.mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002740
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002741 /*
Mike Snitzer8b64e882013-12-20 14:27:28 -05002742 * Don't change the pool's mode until set_pool_mode() below.
2743 * Otherwise the pool's process_* function pointers may
2744 * not match the desired pool mode.
2745 */
2746 pt->adjusted_pf.mode = old_mode;
2747
2748 pool->ti = ti;
2749 pool->pf = pt->adjusted_pf;
2750 pool->low_water_blocks = pt->low_water_blocks;
2751
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002752 set_pool_mode(pool, new_mode);
Mike Snitzerf4026932012-05-19 01:01:01 +01002753
Joe Thornber991d9fa2011-10-31 20:21:18 +00002754 return 0;
2755}
2756
2757static void unbind_control_target(struct pool *pool, struct dm_target *ti)
2758{
2759 if (pool->ti == ti)
2760 pool->ti = NULL;
2761}
2762
2763/*----------------------------------------------------------------
2764 * Pool creation
2765 *--------------------------------------------------------------*/
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002766/* Initialize pool features. */
2767static void pool_features_init(struct pool_features *pf)
2768{
Joe Thornbere49e5822012-07-27 15:08:16 +01002769 pf->mode = PM_WRITE;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002770 pf->zero_new_blocks = true;
2771 pf->discard_enabled = true;
2772 pf->discard_passdown = true;
Mike Snitzer787a996c2013-12-06 16:21:43 -05002773 pf->error_if_no_space = false;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002774}
2775
Joe Thornber991d9fa2011-10-31 20:21:18 +00002776static void __pool_destroy(struct pool *pool)
2777{
2778 __pool_table_remove(pool);
2779
2780 if (dm_pool_metadata_close(pool->pmd) < 0)
2781 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2782
Mike Snitzer44feb382012-10-12 21:02:10 +01002783 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002784 dm_kcopyd_client_destroy(pool->copier);
2785
2786 if (pool->wq)
2787 destroy_workqueue(pool->wq);
2788
2789 if (pool->next_mapping)
2790 mempool_free(pool->next_mapping, pool->mapping_pool);
2791 mempool_destroy(pool->mapping_pool);
Mike Snitzer44feb382012-10-12 21:02:10 +01002792 dm_deferred_set_destroy(pool->shared_read_ds);
2793 dm_deferred_set_destroy(pool->all_io_ds);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002794 kfree(pool);
2795}
2796
Mike Snitzera24c2562012-06-03 00:30:00 +01002797static struct kmem_cache *_new_mapping_cache;
Mike Snitzera24c2562012-06-03 00:30:00 +01002798
Joe Thornber991d9fa2011-10-31 20:21:18 +00002799static struct pool *pool_create(struct mapped_device *pool_md,
2800 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002801 unsigned long block_size,
2802 int read_only, char **error)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002803{
2804 int r;
2805 void *err_p;
2806 struct pool *pool;
2807 struct dm_pool_metadata *pmd;
Joe Thornbere49e5822012-07-27 15:08:16 +01002808 bool format_device = read_only ? false : true;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002809
Joe Thornbere49e5822012-07-27 15:08:16 +01002810 pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002811 if (IS_ERR(pmd)) {
2812 *error = "Error creating metadata object";
2813 return (struct pool *)pmd;
2814 }
2815
2816 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
2817 if (!pool) {
2818 *error = "Error allocating memory for pool";
2819 err_p = ERR_PTR(-ENOMEM);
2820 goto bad_pool;
2821 }
2822
2823 pool->pmd = pmd;
2824 pool->sectors_per_block = block_size;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +01002825 if (block_size & (block_size - 1))
2826 pool->sectors_per_block_shift = -1;
2827 else
2828 pool->sectors_per_block_shift = __ffs(block_size);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002829 pool->low_water_blocks = 0;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002830 pool_features_init(&pool->pf);
Joe Thornbera195db22014-10-06 16:30:06 -04002831 pool->prison = dm_bio_prison_create();
Joe Thornber991d9fa2011-10-31 20:21:18 +00002832 if (!pool->prison) {
2833 *error = "Error creating pool's bio prison";
2834 err_p = ERR_PTR(-ENOMEM);
2835 goto bad_prison;
2836 }
2837
Mikulas Patockadf5d2e92013-03-01 22:45:49 +00002838 pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002839 if (IS_ERR(pool->copier)) {
2840 r = PTR_ERR(pool->copier);
2841 *error = "Error creating pool's kcopyd client";
2842 err_p = ERR_PTR(r);
2843 goto bad_kcopyd_client;
2844 }
2845
2846 /*
2847 * Create singlethreaded workqueue that will service all devices
2848 * that use this metadata.
2849 */
2850 pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2851 if (!pool->wq) {
2852 *error = "Error creating pool's workqueue";
2853 err_p = ERR_PTR(-ENOMEM);
2854 goto bad_wq;
2855 }
2856
Joe Thornber7d327fe2014-10-06 15:45:59 +01002857 throttle_init(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002858 INIT_WORK(&pool->worker, do_worker);
Joe Thornber905e51b2012-03-28 18:41:27 +01002859 INIT_DELAYED_WORK(&pool->waker, do_waker);
Joe Thornber85ad643b2014-05-09 15:59:38 +01002860 INIT_DELAYED_WORK(&pool->no_space_timeout, do_no_space_timeout);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002861 spin_lock_init(&pool->lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002862 bio_list_init(&pool->deferred_flush_bios);
2863 INIT_LIST_HEAD(&pool->prepared_mappings);
Joe Thornber104655f2012-03-28 18:41:28 +01002864 INIT_LIST_HEAD(&pool->prepared_discards);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002865 INIT_LIST_HEAD(&pool->active_thins);
Joe Thornber88a66212013-12-04 20:16:12 -05002866 pool->low_water_triggered = false;
Mike Snitzer80e96c52014-11-07 15:09:46 -05002867 pool->suspended = true;
Mike Snitzer44feb382012-10-12 21:02:10 +01002868
2869 pool->shared_read_ds = dm_deferred_set_create();
2870 if (!pool->shared_read_ds) {
2871 *error = "Error creating pool's shared read deferred set";
2872 err_p = ERR_PTR(-ENOMEM);
2873 goto bad_shared_read_ds;
2874 }
2875
2876 pool->all_io_ds = dm_deferred_set_create();
2877 if (!pool->all_io_ds) {
2878 *error = "Error creating pool's all io deferred set";
2879 err_p = ERR_PTR(-ENOMEM);
2880 goto bad_all_io_ds;
2881 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002882
2883 pool->next_mapping = NULL;
Mike Snitzera24c2562012-06-03 00:30:00 +01002884 pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
2885 _new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002886 if (!pool->mapping_pool) {
2887 *error = "Error creating pool's mapping mempool";
2888 err_p = ERR_PTR(-ENOMEM);
2889 goto bad_mapping_pool;
2890 }
2891
Joe Thornber991d9fa2011-10-31 20:21:18 +00002892 pool->ref_count = 1;
Joe Thornber905e51b2012-03-28 18:41:27 +01002893 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002894 pool->pool_md = pool_md;
2895 pool->md_dev = metadata_dev;
2896 __pool_table_insert(pool);
2897
2898 return pool;
2899
Joe Thornber991d9fa2011-10-31 20:21:18 +00002900bad_mapping_pool:
Mike Snitzer44feb382012-10-12 21:02:10 +01002901 dm_deferred_set_destroy(pool->all_io_ds);
2902bad_all_io_ds:
2903 dm_deferred_set_destroy(pool->shared_read_ds);
2904bad_shared_read_ds:
Joe Thornber991d9fa2011-10-31 20:21:18 +00002905 destroy_workqueue(pool->wq);
2906bad_wq:
2907 dm_kcopyd_client_destroy(pool->copier);
2908bad_kcopyd_client:
Mike Snitzer44feb382012-10-12 21:02:10 +01002909 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002910bad_prison:
2911 kfree(pool);
2912bad_pool:
2913 if (dm_pool_metadata_close(pmd))
2914 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2915
2916 return err_p;
2917}
2918
2919static void __pool_inc(struct pool *pool)
2920{
2921 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2922 pool->ref_count++;
2923}
2924
2925static void __pool_dec(struct pool *pool)
2926{
2927 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2928 BUG_ON(!pool->ref_count);
2929 if (!--pool->ref_count)
2930 __pool_destroy(pool);
2931}
2932
2933static struct pool *__pool_find(struct mapped_device *pool_md,
2934 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002935 unsigned long block_size, int read_only,
2936 char **error, int *created)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002937{
2938 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
2939
2940 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01002941 if (pool->pool_md != pool_md) {
2942 *error = "metadata device already in use by a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00002943 return ERR_PTR(-EBUSY);
Mike Snitzerf09996c2012-07-27 15:07:59 +01002944 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002945 __pool_inc(pool);
2946
2947 } else {
2948 pool = __pool_table_lookup(pool_md);
2949 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01002950 if (pool->md_dev != metadata_dev) {
2951 *error = "different pool cannot replace a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00002952 return ERR_PTR(-EINVAL);
Mike Snitzerf09996c2012-07-27 15:07:59 +01002953 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002954 __pool_inc(pool);
2955
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002956 } else {
Joe Thornbere49e5822012-07-27 15:08:16 +01002957 pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002958 *created = 1;
2959 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002960 }
2961
2962 return pool;
2963}
2964
2965/*----------------------------------------------------------------
2966 * Pool target methods
2967 *--------------------------------------------------------------*/
2968static void pool_dtr(struct dm_target *ti)
2969{
2970 struct pool_c *pt = ti->private;
2971
2972 mutex_lock(&dm_thin_pool_table.mutex);
2973
2974 unbind_control_target(pt->pool, ti);
2975 __pool_dec(pt->pool);
2976 dm_put_device(ti, pt->metadata_dev);
2977 dm_put_device(ti, pt->data_dev);
2978 kfree(pt);
2979
2980 mutex_unlock(&dm_thin_pool_table.mutex);
2981}
2982
Joe Thornber991d9fa2011-10-31 20:21:18 +00002983static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
2984 struct dm_target *ti)
2985{
2986 int r;
2987 unsigned argc;
2988 const char *arg_name;
2989
2990 static struct dm_arg _args[] = {
Mike Snitzer74aa45c2014-01-15 19:07:58 -05002991 {0, 4, "Invalid number of pool feature arguments"},
Joe Thornber991d9fa2011-10-31 20:21:18 +00002992 };
2993
2994 /*
2995 * No feature arguments supplied.
2996 */
2997 if (!as->argc)
2998 return 0;
2999
3000 r = dm_read_arg_group(_args, as, &argc, &ti->error);
3001 if (r)
3002 return -EINVAL;
3003
3004 while (argc && !r) {
3005 arg_name = dm_shift_arg(as);
3006 argc--;
3007
Joe Thornbere49e5822012-07-27 15:08:16 +01003008 if (!strcasecmp(arg_name, "skip_block_zeroing"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003009 pf->zero_new_blocks = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003010
Joe Thornbere49e5822012-07-27 15:08:16 +01003011 else if (!strcasecmp(arg_name, "ignore_discard"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003012 pf->discard_enabled = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01003013
3014 else if (!strcasecmp(arg_name, "no_discard_passdown"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003015 pf->discard_passdown = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01003016
3017 else if (!strcasecmp(arg_name, "read_only"))
3018 pf->mode = PM_READ_ONLY;
3019
Mike Snitzer787a996c2013-12-06 16:21:43 -05003020 else if (!strcasecmp(arg_name, "error_if_no_space"))
3021 pf->error_if_no_space = true;
3022
Joe Thornbere49e5822012-07-27 15:08:16 +01003023 else {
3024 ti->error = "Unrecognised pool feature requested";
3025 r = -EINVAL;
3026 break;
3027 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003028 }
3029
3030 return r;
3031}
3032
Joe Thornberac8c3f32013-05-10 14:37:21 +01003033static void metadata_low_callback(void *context)
3034{
3035 struct pool *pool = context;
3036
3037 DMWARN("%s: reached low water mark for metadata device: sending event.",
3038 dm_device_name(pool->pool_md));
3039
3040 dm_table_event(pool->ti->table);
3041}
3042
Mike Snitzer7d489352014-02-12 23:58:15 -05003043static sector_t get_dev_size(struct block_device *bdev)
Joe Thornberb17446d2013-05-10 14:37:18 +01003044{
Mike Snitzer7d489352014-02-12 23:58:15 -05003045 return i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
3046}
3047
3048static void warn_if_metadata_device_too_big(struct block_device *bdev)
3049{
3050 sector_t metadata_dev_size = get_dev_size(bdev);
Joe Thornberb17446d2013-05-10 14:37:18 +01003051 char buffer[BDEVNAME_SIZE];
3052
Mike Snitzer7d489352014-02-12 23:58:15 -05003053 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
Joe Thornberb17446d2013-05-10 14:37:18 +01003054 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
3055 bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS);
Mike Snitzer7d489352014-02-12 23:58:15 -05003056}
3057
3058static sector_t get_metadata_dev_size(struct block_device *bdev)
3059{
3060 sector_t metadata_dev_size = get_dev_size(bdev);
3061
3062 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS)
3063 metadata_dev_size = THIN_METADATA_MAX_SECTORS;
Joe Thornberb17446d2013-05-10 14:37:18 +01003064
3065 return metadata_dev_size;
3066}
3067
Joe Thornber24347e92013-05-10 14:37:19 +01003068static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev)
3069{
3070 sector_t metadata_dev_size = get_metadata_dev_size(bdev);
3071
Mike Snitzer7d489352014-02-12 23:58:15 -05003072 sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE);
Joe Thornber24347e92013-05-10 14:37:19 +01003073
3074 return metadata_dev_size;
3075}
3076
Joe Thornber991d9fa2011-10-31 20:21:18 +00003077/*
Joe Thornberac8c3f32013-05-10 14:37:21 +01003078 * When a metadata threshold is crossed a dm event is triggered, and
3079 * userland should respond by growing the metadata device. We could let
3080 * userland set the threshold, like we do with the data threshold, but I'm
3081 * not sure they know enough to do this well.
3082 */
3083static dm_block_t calc_metadata_threshold(struct pool_c *pt)
3084{
3085 /*
3086 * 4M is ample for all ops with the possible exception of thin
3087 * device deletion which is harmless if it fails (just retry the
3088 * delete after you've grown the device).
3089 */
3090 dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4;
3091 return min((dm_block_t)1024ULL /* 4M */, quarter);
3092}
3093
3094/*
Joe Thornber991d9fa2011-10-31 20:21:18 +00003095 * thin-pool <metadata dev> <data dev>
3096 * <data block size (sectors)>
3097 * <low water mark (blocks)>
3098 * [<#feature args> [<arg>]*]
3099 *
3100 * Optional feature arguments are:
3101 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003102 * ignore_discard: disable discard
3103 * no_discard_passdown: don't pass discards down to the data device
Mike Snitzer787a996c2013-12-06 16:21:43 -05003104 * read_only: Don't allow any changes to be made to the pool metadata.
3105 * error_if_no_space: error IOs, instead of queueing, if no space.
Joe Thornber991d9fa2011-10-31 20:21:18 +00003106 */
3107static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
3108{
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003109 int r, pool_created = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003110 struct pool_c *pt;
3111 struct pool *pool;
3112 struct pool_features pf;
3113 struct dm_arg_set as;
3114 struct dm_dev *data_dev;
3115 unsigned long block_size;
3116 dm_block_t low_water_blocks;
3117 struct dm_dev *metadata_dev;
Joe Thornber5d0db962013-05-10 14:37:19 +01003118 fmode_t metadata_mode;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003119
3120 /*
3121 * FIXME Remove validation from scope of lock.
3122 */
3123 mutex_lock(&dm_thin_pool_table.mutex);
3124
3125 if (argc < 4) {
3126 ti->error = "Invalid argument count";
3127 r = -EINVAL;
3128 goto out_unlock;
3129 }
Joe Thornber5d0db962013-05-10 14:37:19 +01003130
Joe Thornber991d9fa2011-10-31 20:21:18 +00003131 as.argc = argc;
3132 as.argv = argv;
3133
Joe Thornber5d0db962013-05-10 14:37:19 +01003134 /*
3135 * Set default pool features.
3136 */
3137 pool_features_init(&pf);
3138
3139 dm_consume_args(&as, 4);
3140 r = parse_pool_features(&as, &pf, ti);
3141 if (r)
3142 goto out_unlock;
3143
3144 metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE);
3145 r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003146 if (r) {
3147 ti->error = "Error opening metadata block device";
3148 goto out_unlock;
3149 }
Mike Snitzer7d489352014-02-12 23:58:15 -05003150 warn_if_metadata_device_too_big(metadata_dev->bdev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003151
3152 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
3153 if (r) {
3154 ti->error = "Error getting data device";
3155 goto out_metadata;
3156 }
3157
3158 if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
3159 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
3160 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01003161 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00003162 ti->error = "Invalid block size";
3163 r = -EINVAL;
3164 goto out;
3165 }
3166
3167 if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
3168 ti->error = "Invalid low water mark";
3169 r = -EINVAL;
3170 goto out;
3171 }
3172
Joe Thornber991d9fa2011-10-31 20:21:18 +00003173 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
3174 if (!pt) {
3175 r = -ENOMEM;
3176 goto out;
3177 }
3178
3179 pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
Joe Thornbere49e5822012-07-27 15:08:16 +01003180 block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003181 if (IS_ERR(pool)) {
3182 r = PTR_ERR(pool);
3183 goto out_free_pt;
3184 }
3185
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003186 /*
3187 * 'pool_created' reflects whether this is the first table load.
3188 * Top level discard support is not allowed to be changed after
3189 * initial load. This would require a pool reload to trigger thin
3190 * device changes.
3191 */
3192 if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
3193 ti->error = "Discard support cannot be disabled once enabled";
3194 r = -EINVAL;
3195 goto out_flags_changed;
3196 }
3197
Joe Thornber991d9fa2011-10-31 20:21:18 +00003198 pt->pool = pool;
3199 pt->ti = ti;
3200 pt->metadata_dev = metadata_dev;
3201 pt->data_dev = data_dev;
3202 pt->low_water_blocks = low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +01003203 pt->adjusted_pf = pt->requested_pf = pf;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003204 ti->num_flush_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003205
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003206 /*
3207 * Only need to enable discards if the pool should pass
3208 * them down to the data device. The thin device's discard
3209 * processing will cause mappings to be removed from the btree.
3210 */
Mike Snitzerb60ab992013-09-19 18:49:11 -04003211 ti->discard_zeroes_data_unsupported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003212 if (pf.discard_enabled && pf.discard_passdown) {
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003213 ti->num_discard_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003214
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003215 /*
3216 * Setting 'discards_supported' circumvents the normal
3217 * stacking of discard limits (this keeps the pool and
3218 * thin devices' discard limits consistent).
3219 */
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01003220 ti->discards_supported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003221 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003222 ti->private = pt;
3223
Joe Thornberac8c3f32013-05-10 14:37:21 +01003224 r = dm_pool_register_metadata_threshold(pt->pool->pmd,
3225 calc_metadata_threshold(pt),
3226 metadata_low_callback,
3227 pool);
3228 if (r)
3229 goto out_free_pt;
3230
Joe Thornber991d9fa2011-10-31 20:21:18 +00003231 pt->callbacks.congested_fn = pool_is_congested;
3232 dm_table_add_target_callbacks(ti->table, &pt->callbacks);
3233
3234 mutex_unlock(&dm_thin_pool_table.mutex);
3235
3236 return 0;
3237
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003238out_flags_changed:
3239 __pool_dec(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003240out_free_pt:
3241 kfree(pt);
3242out:
3243 dm_put_device(ti, data_dev);
3244out_metadata:
3245 dm_put_device(ti, metadata_dev);
3246out_unlock:
3247 mutex_unlock(&dm_thin_pool_table.mutex);
3248
3249 return r;
3250}
3251
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003252static int pool_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003253{
3254 int r;
3255 struct pool_c *pt = ti->private;
3256 struct pool *pool = pt->pool;
3257 unsigned long flags;
3258
3259 /*
3260 * As this is a singleton target, ti->begin is always zero.
3261 */
3262 spin_lock_irqsave(&pool->lock, flags);
3263 bio->bi_bdev = pt->data_dev->bdev;
3264 r = DM_MAPIO_REMAPPED;
3265 spin_unlock_irqrestore(&pool->lock, flags);
3266
3267 return r;
3268}
3269
Joe Thornberb17446d2013-05-10 14:37:18 +01003270static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit)
3271{
3272 int r;
3273 struct pool_c *pt = ti->private;
3274 struct pool *pool = pt->pool;
3275 sector_t data_size = ti->len;
3276 dm_block_t sb_data_size;
3277
3278 *need_commit = false;
3279
3280 (void) sector_div(data_size, pool->sectors_per_block);
3281
3282 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
3283 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003284 DMERR("%s: failed to retrieve data device size",
3285 dm_device_name(pool->pool_md));
Joe Thornberb17446d2013-05-10 14:37:18 +01003286 return r;
3287 }
3288
3289 if (data_size < sb_data_size) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003290 DMERR("%s: pool target (%llu blocks) too small: expected %llu",
3291 dm_device_name(pool->pool_md),
Joe Thornberb17446d2013-05-10 14:37:18 +01003292 (unsigned long long)data_size, sb_data_size);
3293 return -EINVAL;
3294
3295 } else if (data_size > sb_data_size) {
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05003296 if (dm_pool_metadata_needs_check(pool->pmd)) {
3297 DMERR("%s: unable to grow the data device until repaired.",
3298 dm_device_name(pool->pool_md));
3299 return 0;
3300 }
3301
Mike Snitzer6f7f51d2013-12-04 10:25:53 -05003302 if (sb_data_size)
3303 DMINFO("%s: growing the data device from %llu to %llu blocks",
3304 dm_device_name(pool->pool_md),
3305 sb_data_size, (unsigned long long)data_size);
Joe Thornberb17446d2013-05-10 14:37:18 +01003306 r = dm_pool_resize_data_dev(pool->pmd, data_size);
3307 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05003308 metadata_operation_failed(pool, "dm_pool_resize_data_dev", r);
Joe Thornberb17446d2013-05-10 14:37:18 +01003309 return r;
3310 }
3311
3312 *need_commit = true;
3313 }
3314
3315 return 0;
3316}
3317
Joe Thornber24347e92013-05-10 14:37:19 +01003318static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit)
3319{
3320 int r;
3321 struct pool_c *pt = ti->private;
3322 struct pool *pool = pt->pool;
3323 dm_block_t metadata_dev_size, sb_metadata_dev_size;
3324
3325 *need_commit = false;
3326
Alasdair G Kergon610bba82013-05-19 18:57:50 +01003327 metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev);
Joe Thornber24347e92013-05-10 14:37:19 +01003328
3329 r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size);
3330 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003331 DMERR("%s: failed to retrieve metadata device size",
3332 dm_device_name(pool->pool_md));
Joe Thornber24347e92013-05-10 14:37:19 +01003333 return r;
3334 }
3335
3336 if (metadata_dev_size < sb_metadata_dev_size) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003337 DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
3338 dm_device_name(pool->pool_md),
Joe Thornber24347e92013-05-10 14:37:19 +01003339 metadata_dev_size, sb_metadata_dev_size);
3340 return -EINVAL;
3341
3342 } else if (metadata_dev_size > sb_metadata_dev_size) {
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05003343 if (dm_pool_metadata_needs_check(pool->pmd)) {
3344 DMERR("%s: unable to grow the metadata device until repaired.",
3345 dm_device_name(pool->pool_md));
3346 return 0;
3347 }
3348
Mike Snitzer7d489352014-02-12 23:58:15 -05003349 warn_if_metadata_device_too_big(pool->md_dev);
Mike Snitzer6f7f51d2013-12-04 10:25:53 -05003350 DMINFO("%s: growing the metadata device from %llu to %llu blocks",
3351 dm_device_name(pool->pool_md),
3352 sb_metadata_dev_size, metadata_dev_size);
Joe Thornber24347e92013-05-10 14:37:19 +01003353 r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size);
3354 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05003355 metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r);
Joe Thornber24347e92013-05-10 14:37:19 +01003356 return r;
3357 }
3358
3359 *need_commit = true;
3360 }
3361
3362 return 0;
3363}
3364
Joe Thornber991d9fa2011-10-31 20:21:18 +00003365/*
3366 * Retrieves the number of blocks of the data device from
3367 * the superblock and compares it to the actual device size,
3368 * thus resizing the data device in case it has grown.
3369 *
3370 * This both copes with opening preallocated data devices in the ctr
3371 * being followed by a resume
3372 * -and-
3373 * calling the resume method individually after userspace has
3374 * grown the data device in reaction to a table event.
3375 */
3376static int pool_preresume(struct dm_target *ti)
3377{
3378 int r;
Joe Thornber24347e92013-05-10 14:37:19 +01003379 bool need_commit1, need_commit2;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003380 struct pool_c *pt = ti->private;
3381 struct pool *pool = pt->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003382
3383 /*
3384 * Take control of the pool object.
3385 */
3386 r = bind_control_target(pool, ti);
3387 if (r)
3388 return r;
3389
Joe Thornberb17446d2013-05-10 14:37:18 +01003390 r = maybe_resize_data_dev(ti, &need_commit1);
3391 if (r)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003392 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003393
Joe Thornber24347e92013-05-10 14:37:19 +01003394 r = maybe_resize_metadata_dev(ti, &need_commit2);
3395 if (r)
3396 return r;
3397
3398 if (need_commit1 || need_commit2)
Joe Thornber020cc3b2013-12-04 15:05:36 -05003399 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003400
3401 return 0;
3402}
3403
Mike Snitzer583024d2014-10-28 20:58:45 -04003404static void pool_suspend_active_thins(struct pool *pool)
3405{
3406 struct thin_c *tc;
3407
3408 /* Suspend all active thin devices */
3409 tc = get_first_thin(pool);
3410 while (tc) {
3411 dm_internal_suspend_noflush(tc->thin_md);
3412 tc = get_next_thin(pool, tc);
3413 }
3414}
3415
3416static void pool_resume_active_thins(struct pool *pool)
3417{
3418 struct thin_c *tc;
3419
3420 /* Resume all active thin devices */
3421 tc = get_first_thin(pool);
3422 while (tc) {
3423 dm_internal_resume(tc->thin_md);
3424 tc = get_next_thin(pool, tc);
3425 }
3426}
3427
Joe Thornber991d9fa2011-10-31 20:21:18 +00003428static void pool_resume(struct dm_target *ti)
3429{
3430 struct pool_c *pt = ti->private;
3431 struct pool *pool = pt->pool;
3432 unsigned long flags;
3433
Mike Snitzer583024d2014-10-28 20:58:45 -04003434 /*
3435 * Must requeue active_thins' bios and then resume
3436 * active_thins _before_ clearing 'suspend' flag.
3437 */
3438 requeue_bios(pool);
3439 pool_resume_active_thins(pool);
3440
Joe Thornber991d9fa2011-10-31 20:21:18 +00003441 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber88a66212013-12-04 20:16:12 -05003442 pool->low_water_triggered = false;
Mike Snitzer80e96c52014-11-07 15:09:46 -05003443 pool->suspended = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003444 spin_unlock_irqrestore(&pool->lock, flags);
Mike Snitzer80e96c52014-11-07 15:09:46 -05003445
Joe Thornber905e51b2012-03-28 18:41:27 +01003446 do_waker(&pool->waker.work);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003447}
3448
Mike Snitzer80e96c52014-11-07 15:09:46 -05003449static void pool_presuspend(struct dm_target *ti)
3450{
3451 struct pool_c *pt = ti->private;
3452 struct pool *pool = pt->pool;
3453 unsigned long flags;
3454
3455 spin_lock_irqsave(&pool->lock, flags);
3456 pool->suspended = true;
3457 spin_unlock_irqrestore(&pool->lock, flags);
Mike Snitzer583024d2014-10-28 20:58:45 -04003458
3459 pool_suspend_active_thins(pool);
Mike Snitzer80e96c52014-11-07 15:09:46 -05003460}
3461
3462static void pool_presuspend_undo(struct dm_target *ti)
3463{
3464 struct pool_c *pt = ti->private;
3465 struct pool *pool = pt->pool;
3466 unsigned long flags;
3467
Mike Snitzer583024d2014-10-28 20:58:45 -04003468 pool_resume_active_thins(pool);
3469
Mike Snitzer80e96c52014-11-07 15:09:46 -05003470 spin_lock_irqsave(&pool->lock, flags);
3471 pool->suspended = false;
3472 spin_unlock_irqrestore(&pool->lock, flags);
3473}
3474
Joe Thornber991d9fa2011-10-31 20:21:18 +00003475static void pool_postsuspend(struct dm_target *ti)
3476{
Joe Thornber991d9fa2011-10-31 20:21:18 +00003477 struct pool_c *pt = ti->private;
3478 struct pool *pool = pt->pool;
3479
Joe Thornber905e51b2012-03-28 18:41:27 +01003480 cancel_delayed_work(&pool->waker);
Joe Thornber85ad643b2014-05-09 15:59:38 +01003481 cancel_delayed_work(&pool->no_space_timeout);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003482 flush_workqueue(pool->wq);
Joe Thornber020cc3b2013-12-04 15:05:36 -05003483 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003484}
3485
3486static int check_arg_count(unsigned argc, unsigned args_required)
3487{
3488 if (argc != args_required) {
3489 DMWARN("Message received with %u arguments instead of %u.",
3490 argc, args_required);
3491 return -EINVAL;
3492 }
3493
3494 return 0;
3495}
3496
3497static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
3498{
3499 if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
3500 *dev_id <= MAX_DEV_ID)
3501 return 0;
3502
3503 if (warning)
3504 DMWARN("Message received with invalid device id: %s", arg);
3505
3506 return -EINVAL;
3507}
3508
3509static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
3510{
3511 dm_thin_id dev_id;
3512 int r;
3513
3514 r = check_arg_count(argc, 2);
3515 if (r)
3516 return r;
3517
3518 r = read_dev_id(argv[1], &dev_id, 1);
3519 if (r)
3520 return r;
3521
3522 r = dm_pool_create_thin(pool->pmd, dev_id);
3523 if (r) {
3524 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
3525 argv[1]);
3526 return r;
3527 }
3528
3529 return 0;
3530}
3531
3532static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3533{
3534 dm_thin_id dev_id;
3535 dm_thin_id origin_dev_id;
3536 int r;
3537
3538 r = check_arg_count(argc, 3);
3539 if (r)
3540 return r;
3541
3542 r = read_dev_id(argv[1], &dev_id, 1);
3543 if (r)
3544 return r;
3545
3546 r = read_dev_id(argv[2], &origin_dev_id, 1);
3547 if (r)
3548 return r;
3549
3550 r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
3551 if (r) {
3552 DMWARN("Creation of new snapshot %s of device %s failed.",
3553 argv[1], argv[2]);
3554 return r;
3555 }
3556
3557 return 0;
3558}
3559
3560static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
3561{
3562 dm_thin_id dev_id;
3563 int r;
3564
3565 r = check_arg_count(argc, 2);
3566 if (r)
3567 return r;
3568
3569 r = read_dev_id(argv[1], &dev_id, 1);
3570 if (r)
3571 return r;
3572
3573 r = dm_pool_delete_thin_device(pool->pmd, dev_id);
3574 if (r)
3575 DMWARN("Deletion of thin device %s failed.", argv[1]);
3576
3577 return r;
3578}
3579
3580static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
3581{
3582 dm_thin_id old_id, new_id;
3583 int r;
3584
3585 r = check_arg_count(argc, 3);
3586 if (r)
3587 return r;
3588
3589 if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
3590 DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
3591 return -EINVAL;
3592 }
3593
3594 if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
3595 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
3596 return -EINVAL;
3597 }
3598
3599 r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
3600 if (r) {
3601 DMWARN("Failed to change transaction id from %s to %s.",
3602 argv[1], argv[2]);
3603 return r;
3604 }
3605
3606 return 0;
3607}
3608
Joe Thornbercc8394d2012-06-03 00:30:01 +01003609static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3610{
3611 int r;
3612
3613 r = check_arg_count(argc, 1);
3614 if (r)
3615 return r;
3616
Joe Thornber020cc3b2013-12-04 15:05:36 -05003617 (void) commit(pool);
Joe Thornber0d200ae2012-07-03 12:55:31 +01003618
Joe Thornbercc8394d2012-06-03 00:30:01 +01003619 r = dm_pool_reserve_metadata_snap(pool->pmd);
3620 if (r)
3621 DMWARN("reserve_metadata_snap message failed.");
3622
3623 return r;
3624}
3625
3626static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3627{
3628 int r;
3629
3630 r = check_arg_count(argc, 1);
3631 if (r)
3632 return r;
3633
3634 r = dm_pool_release_metadata_snap(pool->pmd);
3635 if (r)
3636 DMWARN("release_metadata_snap message failed.");
3637
3638 return r;
3639}
3640
Joe Thornber991d9fa2011-10-31 20:21:18 +00003641/*
3642 * Messages supported:
3643 * create_thin <dev_id>
3644 * create_snap <dev_id> <origin_id>
3645 * delete <dev_id>
Joe Thornber991d9fa2011-10-31 20:21:18 +00003646 * set_transaction_id <current_trans_id> <new_trans_id>
Joe Thornbercc8394d2012-06-03 00:30:01 +01003647 * reserve_metadata_snap
3648 * release_metadata_snap
Joe Thornber991d9fa2011-10-31 20:21:18 +00003649 */
3650static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
3651{
3652 int r = -EINVAL;
3653 struct pool_c *pt = ti->private;
3654 struct pool *pool = pt->pool;
3655
Joe Thornber2a7eaea2015-01-26 11:38:21 +00003656 if (get_pool_mode(pool) >= PM_READ_ONLY) {
3657 DMERR("%s: unable to service pool target messages in READ_ONLY or FAIL mode",
3658 dm_device_name(pool->pool_md));
Mike Snitzerfd467692015-06-09 12:31:26 -04003659 return -EOPNOTSUPP;
Joe Thornber2a7eaea2015-01-26 11:38:21 +00003660 }
3661
Joe Thornber991d9fa2011-10-31 20:21:18 +00003662 if (!strcasecmp(argv[0], "create_thin"))
3663 r = process_create_thin_mesg(argc, argv, pool);
3664
3665 else if (!strcasecmp(argv[0], "create_snap"))
3666 r = process_create_snap_mesg(argc, argv, pool);
3667
3668 else if (!strcasecmp(argv[0], "delete"))
3669 r = process_delete_mesg(argc, argv, pool);
3670
3671 else if (!strcasecmp(argv[0], "set_transaction_id"))
3672 r = process_set_transaction_id_mesg(argc, argv, pool);
3673
Joe Thornbercc8394d2012-06-03 00:30:01 +01003674 else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
3675 r = process_reserve_metadata_snap_mesg(argc, argv, pool);
3676
3677 else if (!strcasecmp(argv[0], "release_metadata_snap"))
3678 r = process_release_metadata_snap_mesg(argc, argv, pool);
3679
Joe Thornber991d9fa2011-10-31 20:21:18 +00003680 else
3681 DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
3682
Joe Thornbere49e5822012-07-27 15:08:16 +01003683 if (!r)
Joe Thornber020cc3b2013-12-04 15:05:36 -05003684 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003685
3686 return r;
3687}
3688
Joe Thornbere49e5822012-07-27 15:08:16 +01003689static void emit_flags(struct pool_features *pf, char *result,
3690 unsigned sz, unsigned maxlen)
3691{
3692 unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
Mike Snitzer787a996c2013-12-06 16:21:43 -05003693 !pf->discard_passdown + (pf->mode == PM_READ_ONLY) +
3694 pf->error_if_no_space;
Joe Thornbere49e5822012-07-27 15:08:16 +01003695 DMEMIT("%u ", count);
3696
3697 if (!pf->zero_new_blocks)
3698 DMEMIT("skip_block_zeroing ");
3699
3700 if (!pf->discard_enabled)
3701 DMEMIT("ignore_discard ");
3702
3703 if (!pf->discard_passdown)
3704 DMEMIT("no_discard_passdown ");
3705
3706 if (pf->mode == PM_READ_ONLY)
3707 DMEMIT("read_only ");
Mike Snitzer787a996c2013-12-06 16:21:43 -05003708
3709 if (pf->error_if_no_space)
3710 DMEMIT("error_if_no_space ");
Joe Thornbere49e5822012-07-27 15:08:16 +01003711}
3712
Joe Thornber991d9fa2011-10-31 20:21:18 +00003713/*
3714 * Status line is:
3715 * <transaction id> <used metadata sectors>/<total metadata sectors>
3716 * <used data sectors>/<total data sectors> <held metadata root>
3717 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003718static void pool_status(struct dm_target *ti, status_type_t type,
3719 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003720{
Joe Thornbere49e5822012-07-27 15:08:16 +01003721 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003722 unsigned sz = 0;
3723 uint64_t transaction_id;
3724 dm_block_t nr_free_blocks_data;
3725 dm_block_t nr_free_blocks_metadata;
3726 dm_block_t nr_blocks_data;
3727 dm_block_t nr_blocks_metadata;
3728 dm_block_t held_root;
3729 char buf[BDEVNAME_SIZE];
3730 char buf2[BDEVNAME_SIZE];
3731 struct pool_c *pt = ti->private;
3732 struct pool *pool = pt->pool;
3733
3734 switch (type) {
3735 case STATUSTYPE_INFO:
Joe Thornbere49e5822012-07-27 15:08:16 +01003736 if (get_pool_mode(pool) == PM_FAIL) {
3737 DMEMIT("Fail");
3738 break;
3739 }
3740
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01003741 /* Commit to ensure statistics aren't out-of-date */
3742 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
Joe Thornber020cc3b2013-12-04 15:05:36 -05003743 (void) commit(pool);
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01003744
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003745 r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id);
3746 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003747 DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
3748 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003749 goto err;
3750 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003751
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003752 r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata);
3753 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003754 DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
3755 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003756 goto err;
3757 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003758
3759 r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003760 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003761 DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
3762 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003763 goto err;
3764 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003765
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003766 r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data);
3767 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003768 DMERR("%s: dm_pool_get_free_block_count returned %d",
3769 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003770 goto err;
3771 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003772
3773 r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003774 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003775 DMERR("%s: dm_pool_get_data_dev_size returned %d",
3776 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003777 goto err;
3778 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003779
Joe Thornbercc8394d2012-06-03 00:30:01 +01003780 r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003781 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003782 DMERR("%s: dm_pool_get_metadata_snap returned %d",
3783 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003784 goto err;
3785 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003786
3787 DMEMIT("%llu %llu/%llu %llu/%llu ",
3788 (unsigned long long)transaction_id,
3789 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
3790 (unsigned long long)nr_blocks_metadata,
3791 (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
3792 (unsigned long long)nr_blocks_data);
3793
3794 if (held_root)
Joe Thornbere49e5822012-07-27 15:08:16 +01003795 DMEMIT("%llu ", held_root);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003796 else
Joe Thornbere49e5822012-07-27 15:08:16 +01003797 DMEMIT("- ");
3798
Joe Thornber3e1a0692014-03-03 16:03:26 +00003799 if (pool->pf.mode == PM_OUT_OF_DATA_SPACE)
3800 DMEMIT("out_of_data_space ");
3801 else if (pool->pf.mode == PM_READ_ONLY)
Joe Thornbere49e5822012-07-27 15:08:16 +01003802 DMEMIT("ro ");
3803 else
3804 DMEMIT("rw ");
3805
Mike Snitzer018debe2012-12-21 20:23:32 +00003806 if (!pool->pf.discard_enabled)
Mike Snitzer787a996c2013-12-06 16:21:43 -05003807 DMEMIT("ignore_discard ");
Mike Snitzer018debe2012-12-21 20:23:32 +00003808 else if (pool->pf.discard_passdown)
Mike Snitzer787a996c2013-12-06 16:21:43 -05003809 DMEMIT("discard_passdown ");
Joe Thornbere49e5822012-07-27 15:08:16 +01003810 else
Mike Snitzer787a996c2013-12-06 16:21:43 -05003811 DMEMIT("no_discard_passdown ");
3812
3813 if (pool->pf.error_if_no_space)
3814 DMEMIT("error_if_no_space ");
3815 else
3816 DMEMIT("queue_if_no_space ");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003817
3818 break;
3819
3820 case STATUSTYPE_TABLE:
3821 DMEMIT("%s %s %lu %llu ",
3822 format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
3823 format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
3824 (unsigned long)pool->sectors_per_block,
3825 (unsigned long long)pt->low_water_blocks);
Mike Snitzer0424caa2012-09-26 23:45:47 +01003826 emit_flags(&pt->requested_pf, result, sz, maxlen);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003827 break;
3828 }
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003829 return;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003830
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003831err:
3832 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003833}
3834
3835static int pool_iterate_devices(struct dm_target *ti,
3836 iterate_devices_callout_fn fn, void *data)
3837{
3838 struct pool_c *pt = ti->private;
3839
3840 return fn(ti, pt->data_dev, 0, ti->len, data);
3841}
3842
3843static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
3844 struct bio_vec *biovec, int max_size)
3845{
3846 struct pool_c *pt = ti->private;
3847 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
3848
3849 if (!q->merge_bvec_fn)
3850 return max_size;
3851
3852 bvm->bi_bdev = pt->data_dev->bdev;
3853
3854 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
3855}
3856
3857static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
3858{
3859 struct pool_c *pt = ti->private;
3860 struct pool *pool = pt->pool;
Mike Snitzer604ea902014-10-09 18:43:25 -04003861 sector_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
3862
3863 /*
Mike Snitzerd200c302014-11-20 18:07:43 -05003864 * If max_sectors is smaller than pool->sectors_per_block adjust it
3865 * to the highest possible power-of-2 factor of pool->sectors_per_block.
3866 * This is especially beneficial when the pool's data device is a RAID
3867 * device that has a full stripe width that matches pool->sectors_per_block
3868 * -- because even though partial RAID stripe-sized IOs will be issued to a
3869 * single RAID stripe; when aggregated they will end on a full RAID stripe
3870 * boundary.. which avoids additional partial RAID stripe writes cascading
Mike Snitzer604ea902014-10-09 18:43:25 -04003871 */
Mike Snitzer604ea902014-10-09 18:43:25 -04003872 if (limits->max_sectors < pool->sectors_per_block) {
3873 while (!is_factor(pool->sectors_per_block, limits->max_sectors)) {
3874 if ((limits->max_sectors & (limits->max_sectors - 1)) == 0)
3875 limits->max_sectors--;
3876 limits->max_sectors = rounddown_pow_of_two(limits->max_sectors);
3877 }
Mike Snitzer604ea902014-10-09 18:43:25 -04003878 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003879
Mike Snitzer0cc67cd2013-08-20 15:02:41 -04003880 /*
3881 * If the system-determined stacked limits are compatible with the
3882 * pool's blocksize (io_opt is a factor) do not override them.
3883 */
3884 if (io_opt_sectors < pool->sectors_per_block ||
Mike Snitzer604ea902014-10-09 18:43:25 -04003885 !is_factor(io_opt_sectors, pool->sectors_per_block)) {
3886 if (is_factor(pool->sectors_per_block, limits->max_sectors))
3887 blk_limits_io_min(limits, limits->max_sectors << SECTOR_SHIFT);
3888 else
3889 blk_limits_io_min(limits, pool->sectors_per_block << SECTOR_SHIFT);
Mike Snitzer0cc67cd2013-08-20 15:02:41 -04003890 blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
3891 }
Mike Snitzer0424caa2012-09-26 23:45:47 +01003892
3893 /*
3894 * pt->adjusted_pf is a staging area for the actual features to use.
3895 * They get transferred to the live pool in bind_control_target()
3896 * called from pool_preresume().
3897 */
Mike Snitzerb60ab992013-09-19 18:49:11 -04003898 if (!pt->adjusted_pf.discard_enabled) {
3899 /*
3900 * Must explicitly disallow stacking discard limits otherwise the
3901 * block layer will stack them if pool's data device has support.
3902 * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the
3903 * user to see that, so make sure to set all discard limits to 0.
3904 */
3905 limits->discard_granularity = 0;
Mike Snitzer0424caa2012-09-26 23:45:47 +01003906 return;
Mike Snitzerb60ab992013-09-19 18:49:11 -04003907 }
Mike Snitzer0424caa2012-09-26 23:45:47 +01003908
3909 disable_passdown_if_not_supported(pt);
3910
Joe Thornber34fbcf62015-04-16 12:58:35 +01003911 /*
3912 * The pool uses the same discard limits as the underlying data
3913 * device. DM core has already set this up.
3914 */
Joe Thornber991d9fa2011-10-31 20:21:18 +00003915}
3916
3917static struct target_type pool_target = {
3918 .name = "thin-pool",
3919 .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
3920 DM_TARGET_IMMUTABLE,
Joe Thornber34fbcf62015-04-16 12:58:35 +01003921 .version = {1, 15, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00003922 .module = THIS_MODULE,
3923 .ctr = pool_ctr,
3924 .dtr = pool_dtr,
3925 .map = pool_map,
Mike Snitzer80e96c52014-11-07 15:09:46 -05003926 .presuspend = pool_presuspend,
3927 .presuspend_undo = pool_presuspend_undo,
Joe Thornber991d9fa2011-10-31 20:21:18 +00003928 .postsuspend = pool_postsuspend,
3929 .preresume = pool_preresume,
3930 .resume = pool_resume,
3931 .message = pool_message,
3932 .status = pool_status,
3933 .merge = pool_merge,
3934 .iterate_devices = pool_iterate_devices,
3935 .io_hints = pool_io_hints,
3936};
3937
3938/*----------------------------------------------------------------
3939 * Thin target methods
3940 *--------------------------------------------------------------*/
Joe Thornberb10ebd32014-04-08 11:29:01 +01003941static void thin_get(struct thin_c *tc)
3942{
3943 atomic_inc(&tc->refcount);
3944}
3945
3946static void thin_put(struct thin_c *tc)
3947{
3948 if (atomic_dec_and_test(&tc->refcount))
3949 complete(&tc->can_destroy);
3950}
3951
Joe Thornber991d9fa2011-10-31 20:21:18 +00003952static void thin_dtr(struct dm_target *ti)
3953{
3954 struct thin_c *tc = ti->private;
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003955 unsigned long flags;
3956
3957 spin_lock_irqsave(&tc->pool->lock, flags);
3958 list_del_rcu(&tc->list);
3959 spin_unlock_irqrestore(&tc->pool->lock, flags);
3960 synchronize_rcu();
Joe Thornber991d9fa2011-10-31 20:21:18 +00003961
Mikulas Patocka17181fb2014-11-05 17:00:13 -05003962 thin_put(tc);
3963 wait_for_completion(&tc->can_destroy);
3964
Joe Thornber991d9fa2011-10-31 20:21:18 +00003965 mutex_lock(&dm_thin_pool_table.mutex);
3966
3967 __pool_dec(tc->pool);
3968 dm_pool_close_thin_device(tc->td);
3969 dm_put_device(ti, tc->pool_dev);
Joe Thornber2dd9c252012-03-28 18:41:28 +01003970 if (tc->origin_dev)
3971 dm_put_device(ti, tc->origin_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003972 kfree(tc);
3973
3974 mutex_unlock(&dm_thin_pool_table.mutex);
3975}
3976
3977/*
3978 * Thin target parameters:
3979 *
Joe Thornber2dd9c252012-03-28 18:41:28 +01003980 * <pool_dev> <dev_id> [origin_dev]
Joe Thornber991d9fa2011-10-31 20:21:18 +00003981 *
3982 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
3983 * dev_id: the internal device identifier
Joe Thornber2dd9c252012-03-28 18:41:28 +01003984 * origin_dev: a device external to the pool that should act as the origin
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003985 *
3986 * If the pool device has discards disabled, they get disabled for the thin
3987 * device as well.
Joe Thornber991d9fa2011-10-31 20:21:18 +00003988 */
3989static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
3990{
3991 int r;
3992 struct thin_c *tc;
Joe Thornber2dd9c252012-03-28 18:41:28 +01003993 struct dm_dev *pool_dev, *origin_dev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003994 struct mapped_device *pool_md;
Joe Thornber5e3283e2014-04-08 11:08:41 +01003995 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003996
3997 mutex_lock(&dm_thin_pool_table.mutex);
3998
Joe Thornber2dd9c252012-03-28 18:41:28 +01003999 if (argc != 2 && argc != 3) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00004000 ti->error = "Invalid argument count";
4001 r = -EINVAL;
4002 goto out_unlock;
4003 }
4004
4005 tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
4006 if (!tc) {
4007 ti->error = "Out of memory";
4008 r = -ENOMEM;
4009 goto out_unlock;
4010 }
Mike Snitzer583024d2014-10-28 20:58:45 -04004011 tc->thin_md = dm_table_get_md(ti->table);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004012 spin_lock_init(&tc->lock);
Joe Thornbera374bb22014-10-10 13:43:14 +01004013 INIT_LIST_HEAD(&tc->deferred_cells);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004014 bio_list_init(&tc->deferred_bio_list);
4015 bio_list_init(&tc->retry_on_resume_list);
Mike Snitzer67324ea2014-03-21 18:33:41 -04004016 tc->sort_bio_list = RB_ROOT;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004017
Joe Thornber2dd9c252012-03-28 18:41:28 +01004018 if (argc == 3) {
4019 r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
4020 if (r) {
4021 ti->error = "Error opening origin device";
4022 goto bad_origin_dev;
4023 }
4024 tc->origin_dev = origin_dev;
4025 }
4026
Joe Thornber991d9fa2011-10-31 20:21:18 +00004027 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
4028 if (r) {
4029 ti->error = "Error opening pool device";
4030 goto bad_pool_dev;
4031 }
4032 tc->pool_dev = pool_dev;
4033
4034 if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
4035 ti->error = "Invalid device id";
4036 r = -EINVAL;
4037 goto bad_common;
4038 }
4039
4040 pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
4041 if (!pool_md) {
4042 ti->error = "Couldn't get pool mapped device";
4043 r = -EINVAL;
4044 goto bad_common;
4045 }
4046
4047 tc->pool = __pool_table_lookup(pool_md);
4048 if (!tc->pool) {
4049 ti->error = "Couldn't find pool object";
4050 r = -EINVAL;
4051 goto bad_pool_lookup;
4052 }
4053 __pool_inc(tc->pool);
4054
Joe Thornbere49e5822012-07-27 15:08:16 +01004055 if (get_pool_mode(tc->pool) == PM_FAIL) {
4056 ti->error = "Couldn't open thin device, Pool is in fail mode";
Mike Snitzer1acacc02014-02-19 20:32:33 -05004057 r = -EINVAL;
Mike Snitzer80e96c52014-11-07 15:09:46 -05004058 goto bad_pool;
Joe Thornbere49e5822012-07-27 15:08:16 +01004059 }
4060
Joe Thornber991d9fa2011-10-31 20:21:18 +00004061 r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
4062 if (r) {
4063 ti->error = "Couldn't open thin internal device";
Mike Snitzer80e96c52014-11-07 15:09:46 -05004064 goto bad_pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004065 }
4066
Mike Snitzer542f9032012-07-27 15:08:00 +01004067 r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
4068 if (r)
Mike Snitzer80e96c52014-11-07 15:09:46 -05004069 goto bad;
Mike Snitzer542f9032012-07-27 15:08:00 +01004070
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00004071 ti->num_flush_bios = 1;
Joe Thornber16ad3d12012-07-27 15:08:07 +01004072 ti->flush_supported = true;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00004073 ti->per_bio_data_size = sizeof(struct dm_thin_endio_hook);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01004074
4075 /* In case the pool supports discards, pass them on. */
Mike Snitzerb60ab992013-09-19 18:49:11 -04004076 ti->discard_zeroes_data_unsupported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01004077 if (tc->pool->pf.discard_enabled) {
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01004078 ti->discards_supported = true;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00004079 ti->num_discard_bios = 1;
Joe Thornber34fbcf62015-04-16 12:58:35 +01004080 ti->split_discard_bios = false;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01004081 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00004082
Joe Thornber991d9fa2011-10-31 20:21:18 +00004083 mutex_unlock(&dm_thin_pool_table.mutex);
4084
Joe Thornber5e3283e2014-04-08 11:08:41 +01004085 spin_lock_irqsave(&tc->pool->lock, flags);
Mike Snitzer80e96c52014-11-07 15:09:46 -05004086 if (tc->pool->suspended) {
4087 spin_unlock_irqrestore(&tc->pool->lock, flags);
4088 mutex_lock(&dm_thin_pool_table.mutex); /* reacquire for __pool_dec */
4089 ti->error = "Unable to activate thin device while pool is suspended";
4090 r = -EINVAL;
4091 goto bad;
4092 }
Marc Dionne2b94e892014-12-17 07:59:59 -05004093 atomic_set(&tc->refcount, 1);
4094 init_completion(&tc->can_destroy);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004095 list_add_tail_rcu(&tc->list, &tc->pool->active_thins);
Joe Thornber5e3283e2014-04-08 11:08:41 +01004096 spin_unlock_irqrestore(&tc->pool->lock, flags);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004097 /*
4098 * This synchronize_rcu() call is needed here otherwise we risk a
4099 * wake_worker() call finding no bios to process (because the newly
4100 * added tc isn't yet visible). So this reduces latency since we
4101 * aren't then dependent on the periodic commit to wake_worker().
4102 */
4103 synchronize_rcu();
4104
Mike Snitzer80e96c52014-11-07 15:09:46 -05004105 dm_put(pool_md);
4106
Joe Thornber991d9fa2011-10-31 20:21:18 +00004107 return 0;
4108
Mike Snitzer80e96c52014-11-07 15:09:46 -05004109bad:
Mike Snitzer1acacc02014-02-19 20:32:33 -05004110 dm_pool_close_thin_device(tc->td);
Mike Snitzer80e96c52014-11-07 15:09:46 -05004111bad_pool:
Joe Thornber991d9fa2011-10-31 20:21:18 +00004112 __pool_dec(tc->pool);
4113bad_pool_lookup:
4114 dm_put(pool_md);
4115bad_common:
4116 dm_put_device(ti, tc->pool_dev);
4117bad_pool_dev:
Joe Thornber2dd9c252012-03-28 18:41:28 +01004118 if (tc->origin_dev)
4119 dm_put_device(ti, tc->origin_dev);
4120bad_origin_dev:
Joe Thornber991d9fa2011-10-31 20:21:18 +00004121 kfree(tc);
4122out_unlock:
4123 mutex_unlock(&dm_thin_pool_table.mutex);
4124
4125 return r;
4126}
4127
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00004128static int thin_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00004129{
Kent Overstreet4f024f32013-10-11 15:44:27 -07004130 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004131
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00004132 return thin_bio_map(ti, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004133}
4134
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00004135static int thin_endio(struct dm_target *ti, struct bio *bio, int err)
Joe Thornbereb2aa482012-03-28 18:41:28 +01004136{
4137 unsigned long flags;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00004138 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01004139 struct list_head work;
Mike Snitzera24c2562012-06-03 00:30:00 +01004140 struct dm_thin_new_mapping *m, *tmp;
Joe Thornbereb2aa482012-03-28 18:41:28 +01004141 struct pool *pool = h->tc->pool;
4142
4143 if (h->shared_read_entry) {
4144 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01004145 dm_deferred_entry_dec(h->shared_read_entry, &work);
Joe Thornbereb2aa482012-03-28 18:41:28 +01004146
4147 spin_lock_irqsave(&pool->lock, flags);
4148 list_for_each_entry_safe(m, tmp, &work, list) {
4149 list_del(&m->list);
Joe Thornber50f3c3e2014-06-13 13:57:09 +01004150 __complete_mapping_preparation(m);
Joe Thornbereb2aa482012-03-28 18:41:28 +01004151 }
4152 spin_unlock_irqrestore(&pool->lock, flags);
4153 }
4154
Joe Thornber104655f2012-03-28 18:41:28 +01004155 if (h->all_io_entry) {
4156 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01004157 dm_deferred_entry_dec(h->all_io_entry, &work);
Joe Thornber563af182012-12-21 20:23:31 +00004158 if (!list_empty(&work)) {
4159 spin_lock_irqsave(&pool->lock, flags);
4160 list_for_each_entry_safe(m, tmp, &work, list)
Mike Snitzerdaec3382013-12-11 14:01:20 -05004161 list_add_tail(&m->list, &pool->prepared_discards);
Joe Thornber563af182012-12-21 20:23:31 +00004162 spin_unlock_irqrestore(&pool->lock, flags);
4163 wake_worker(pool);
4164 }
Joe Thornber104655f2012-03-28 18:41:28 +01004165 }
4166
Joe Thornber34fbcf62015-04-16 12:58:35 +01004167 if (h->cell)
4168 cell_defer_no_holder(h->tc, h->cell);
4169
Joe Thornbereb2aa482012-03-28 18:41:28 +01004170 return 0;
4171}
4172
Joe Thornber738211f2014-03-03 15:52:28 +00004173static void thin_presuspend(struct dm_target *ti)
4174{
4175 struct thin_c *tc = ti->private;
4176
4177 if (dm_noflush_suspending(ti))
4178 noflush_work(tc, do_noflush_start);
4179}
4180
Joe Thornber991d9fa2011-10-31 20:21:18 +00004181static void thin_postsuspend(struct dm_target *ti)
4182{
Joe Thornber738211f2014-03-03 15:52:28 +00004183 struct thin_c *tc = ti->private;
4184
4185 /*
4186 * The dm_noflush_suspending flag has been cleared by now, so
4187 * unfortunately we must always run this.
4188 */
4189 noflush_work(tc, do_noflush_stop);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004190}
4191
Joe Thornbere5aea7b2014-06-13 14:47:24 +01004192static int thin_preresume(struct dm_target *ti)
4193{
4194 struct thin_c *tc = ti->private;
4195
4196 if (tc->origin_dev)
4197 tc->origin_size = get_dev_size(tc->origin_dev->bdev);
4198
4199 return 0;
4200}
4201
Joe Thornber991d9fa2011-10-31 20:21:18 +00004202/*
4203 * <nr mapped sectors> <highest mapped sector>
4204 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004205static void thin_status(struct dm_target *ti, status_type_t type,
4206 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00004207{
4208 int r;
4209 ssize_t sz = 0;
4210 dm_block_t mapped, highest;
4211 char buf[BDEVNAME_SIZE];
4212 struct thin_c *tc = ti->private;
4213
Joe Thornbere49e5822012-07-27 15:08:16 +01004214 if (get_pool_mode(tc->pool) == PM_FAIL) {
4215 DMEMIT("Fail");
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004216 return;
Joe Thornbere49e5822012-07-27 15:08:16 +01004217 }
4218
Joe Thornber991d9fa2011-10-31 20:21:18 +00004219 if (!tc->td)
4220 DMEMIT("-");
4221 else {
4222 switch (type) {
4223 case STATUSTYPE_INFO:
4224 r = dm_thin_get_mapped_count(tc->td, &mapped);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004225 if (r) {
4226 DMERR("dm_thin_get_mapped_count returned %d", r);
4227 goto err;
4228 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00004229
4230 r = dm_thin_get_highest_mapped_block(tc->td, &highest);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004231 if (r < 0) {
4232 DMERR("dm_thin_get_highest_mapped_block returned %d", r);
4233 goto err;
4234 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00004235
4236 DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
4237 if (r)
4238 DMEMIT("%llu", ((highest + 1) *
4239 tc->pool->sectors_per_block) - 1);
4240 else
4241 DMEMIT("-");
4242 break;
4243
4244 case STATUSTYPE_TABLE:
4245 DMEMIT("%s %lu",
4246 format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
4247 (unsigned long) tc->dev_id);
Joe Thornber2dd9c252012-03-28 18:41:28 +01004248 if (tc->origin_dev)
4249 DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
Joe Thornber991d9fa2011-10-31 20:21:18 +00004250 break;
4251 }
4252 }
4253
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004254 return;
4255
4256err:
4257 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00004258}
4259
Mike Snitzer36f12ae2014-10-09 15:24:12 -04004260static int thin_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
4261 struct bio_vec *biovec, int max_size)
4262{
4263 struct thin_c *tc = ti->private;
4264 struct request_queue *q = bdev_get_queue(tc->pool_dev->bdev);
4265
4266 if (!q->merge_bvec_fn)
4267 return max_size;
4268
4269 bvm->bi_bdev = tc->pool_dev->bdev;
4270 bvm->bi_sector = dm_target_offset(ti, bvm->bi_sector);
4271
4272 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
4273}
4274
Joe Thornber991d9fa2011-10-31 20:21:18 +00004275static int thin_iterate_devices(struct dm_target *ti,
4276 iterate_devices_callout_fn fn, void *data)
4277{
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004278 sector_t blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004279 struct thin_c *tc = ti->private;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004280 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004281
4282 /*
4283 * We can't call dm_pool_get_data_dev_size() since that blocks. So
4284 * we follow a more convoluted path through to the pool's target.
4285 */
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004286 if (!pool->ti)
Joe Thornber991d9fa2011-10-31 20:21:18 +00004287 return 0; /* nothing is bound */
4288
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004289 blocks = pool->ti->len;
4290 (void) sector_div(blocks, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004291 if (blocks)
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004292 return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004293
4294 return 0;
4295}
4296
Joe Thornber34fbcf62015-04-16 12:58:35 +01004297static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)
4298{
4299 struct thin_c *tc = ti->private;
4300 struct pool *pool = tc->pool;
4301
4302 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
4303 limits->max_discard_sectors = 2048 * 1024 * 16; /* 16G */
4304}
4305
Joe Thornber991d9fa2011-10-31 20:21:18 +00004306static struct target_type thin_target = {
4307 .name = "thin",
Joe Thornber34fbcf62015-04-16 12:58:35 +01004308 .version = {1, 15, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00004309 .module = THIS_MODULE,
4310 .ctr = thin_ctr,
4311 .dtr = thin_dtr,
4312 .map = thin_map,
Joe Thornbereb2aa482012-03-28 18:41:28 +01004313 .end_io = thin_endio,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01004314 .preresume = thin_preresume,
Joe Thornber738211f2014-03-03 15:52:28 +00004315 .presuspend = thin_presuspend,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004316 .postsuspend = thin_postsuspend,
4317 .status = thin_status,
Mike Snitzer36f12ae2014-10-09 15:24:12 -04004318 .merge = thin_merge,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004319 .iterate_devices = thin_iterate_devices,
Joe Thornber34fbcf62015-04-16 12:58:35 +01004320 .io_hints = thin_io_hints,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004321};
4322
4323/*----------------------------------------------------------------*/
4324
4325static int __init dm_thin_init(void)
4326{
4327 int r;
4328
4329 pool_table_init();
4330
4331 r = dm_register_target(&thin_target);
4332 if (r)
4333 return r;
4334
4335 r = dm_register_target(&pool_target);
4336 if (r)
Mike Snitzera24c2562012-06-03 00:30:00 +01004337 goto bad_pool_target;
4338
4339 r = -ENOMEM;
4340
Mike Snitzera24c2562012-06-03 00:30:00 +01004341 _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
4342 if (!_new_mapping_cache)
4343 goto bad_new_mapping_cache;
4344
Mike Snitzera24c2562012-06-03 00:30:00 +01004345 return 0;
4346
Mike Snitzera24c2562012-06-03 00:30:00 +01004347bad_new_mapping_cache:
Mike Snitzera24c2562012-06-03 00:30:00 +01004348 dm_unregister_target(&pool_target);
4349bad_pool_target:
4350 dm_unregister_target(&thin_target);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004351
4352 return r;
4353}
4354
4355static void dm_thin_exit(void)
4356{
4357 dm_unregister_target(&thin_target);
4358 dm_unregister_target(&pool_target);
Mike Snitzera24c2562012-06-03 00:30:00 +01004359
Mike Snitzera24c2562012-06-03 00:30:00 +01004360 kmem_cache_destroy(_new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004361}
4362
4363module_init(dm_thin_init);
4364module_exit(dm_thin_exit);
4365
Mike Snitzer80c57892014-05-20 13:38:33 -04004366module_param_named(no_space_timeout, no_space_timeout_secs, uint, S_IRUGO | S_IWUSR);
4367MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds");
4368
Alasdair G Kergon7cab8bf2012-05-12 01:43:19 +01004369MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
Joe Thornber991d9fa2011-10-31 20:21:18 +00004370MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
4371MODULE_LICENSE("GPL");