blob: 2ade2c46dca9e30473775b7f2d9cf0ea163c0857 [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
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200617 while ((bio = bio_list_pop(bios))) {
618 bio->bi_error = error;
619 bio_endio(bio);
620 }
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400621}
622
623static void error_thin_bio_list(struct thin_c *tc, struct bio_list *master, int error)
624{
Joe Thornber991d9fa2011-10-31 20:21:18 +0000625 struct bio_list bios;
Joe Thornber18adc572014-03-03 15:46:42 +0000626 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000627
628 bio_list_init(&bios);
Joe Thornber18adc572014-03-03 15:46:42 +0000629
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400630 spin_lock_irqsave(&tc->lock, flags);
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400631 __merge_bio_list(&bios, master);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400632 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000633
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400634 error_bio_list(&bios, error);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000635}
636
Joe Thornbera374bb22014-10-10 13:43:14 +0100637static void requeue_deferred_cells(struct thin_c *tc)
638{
639 struct pool *pool = tc->pool;
640 unsigned long flags;
641 struct list_head cells;
642 struct dm_bio_prison_cell *cell, *tmp;
643
644 INIT_LIST_HEAD(&cells);
645
646 spin_lock_irqsave(&tc->lock, flags);
647 list_splice_init(&tc->deferred_cells, &cells);
648 spin_unlock_irqrestore(&tc->lock, flags);
649
650 list_for_each_entry_safe(cell, tmp, &cells, user_list)
651 cell_requeue(pool, cell);
652}
653
Joe Thornber991d9fa2011-10-31 20:21:18 +0000654static void requeue_io(struct thin_c *tc)
655{
Joe Thornber3e1a0692014-03-03 16:03:26 +0000656 struct bio_list bios;
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400657 unsigned long flags;
Joe Thornber3e1a0692014-03-03 16:03:26 +0000658
659 bio_list_init(&bios);
660
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400661 spin_lock_irqsave(&tc->lock, flags);
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400662 __merge_bio_list(&bios, &tc->deferred_bio_list);
663 __merge_bio_list(&bios, &tc->retry_on_resume_list);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400664 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber3e1a0692014-03-03 16:03:26 +0000665
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400666 error_bio_list(&bios, DM_ENDIO_REQUEUE);
667 requeue_deferred_cells(tc);
Joe Thornber3e1a0692014-03-03 16:03:26 +0000668}
669
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400670static void error_retry_list(struct pool *pool)
671{
672 struct thin_c *tc;
673
674 rcu_read_lock();
675 list_for_each_entry_rcu(tc, &pool->active_thins, list)
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400676 error_thin_bio_list(tc, &tc->retry_on_resume_list, -EIO);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400677 rcu_read_unlock();
678}
679
Joe Thornber991d9fa2011-10-31 20:21:18 +0000680/*
681 * This section of code contains the logic for processing a thin device's IO.
682 * Much of the code depends on pool object resources (lists, workqueues, etc)
683 * but most is exclusively called from the thin target rather than the thin-pool
684 * target.
685 */
686
687static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
688{
Mike Snitzer58f77a22013-03-01 22:45:45 +0000689 struct pool *pool = tc->pool;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700690 sector_t block_nr = bio->bi_iter.bi_sector;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100691
Mike Snitzer58f77a22013-03-01 22:45:45 +0000692 if (block_size_is_power_of_two(pool))
693 block_nr >>= pool->sectors_per_block_shift;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100694 else
Mike Snitzer58f77a22013-03-01 22:45:45 +0000695 (void) sector_div(block_nr, pool->sectors_per_block);
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100696
697 return block_nr;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000698}
699
Joe Thornber34fbcf62015-04-16 12:58:35 +0100700/*
701 * Returns the _complete_ blocks that this bio covers.
702 */
703static void get_bio_block_range(struct thin_c *tc, struct bio *bio,
704 dm_block_t *begin, dm_block_t *end)
705{
706 struct pool *pool = tc->pool;
707 sector_t b = bio->bi_iter.bi_sector;
708 sector_t e = b + (bio->bi_iter.bi_size >> SECTOR_SHIFT);
709
710 b += pool->sectors_per_block - 1ull; /* so we round up */
711
712 if (block_size_is_power_of_two(pool)) {
713 b >>= pool->sectors_per_block_shift;
714 e >>= pool->sectors_per_block_shift;
715 } else {
716 (void) sector_div(b, pool->sectors_per_block);
717 (void) sector_div(e, pool->sectors_per_block);
718 }
719
720 if (e < b)
721 /* Can happen if the bio is within a single block. */
722 e = b;
723
724 *begin = b;
725 *end = e;
726}
727
Joe Thornber991d9fa2011-10-31 20:21:18 +0000728static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
729{
730 struct pool *pool = tc->pool;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700731 sector_t bi_sector = bio->bi_iter.bi_sector;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000732
733 bio->bi_bdev = tc->pool_dev->bdev;
Mike Snitzer58f77a22013-03-01 22:45:45 +0000734 if (block_size_is_power_of_two(pool))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700735 bio->bi_iter.bi_sector =
736 (block << pool->sectors_per_block_shift) |
737 (bi_sector & (pool->sectors_per_block - 1));
Mike Snitzer58f77a22013-03-01 22:45:45 +0000738 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700739 bio->bi_iter.bi_sector = (block * pool->sectors_per_block) +
Mike Snitzer58f77a22013-03-01 22:45:45 +0000740 sector_div(bi_sector, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000741}
742
Joe Thornber2dd9c252012-03-28 18:41:28 +0100743static void remap_to_origin(struct thin_c *tc, struct bio *bio)
744{
745 bio->bi_bdev = tc->origin_dev->bdev;
746}
747
Joe Thornber4afdd682012-07-27 15:08:14 +0100748static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
749{
750 return (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) &&
751 dm_thin_changed_this_transaction(tc->td);
752}
753
Joe Thornbere8088072012-12-21 20:23:31 +0000754static void inc_all_io_entry(struct pool *pool, struct bio *bio)
755{
756 struct dm_thin_endio_hook *h;
757
758 if (bio->bi_rw & REQ_DISCARD)
759 return;
760
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000761 h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbere8088072012-12-21 20:23:31 +0000762 h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
763}
764
Joe Thornber2dd9c252012-03-28 18:41:28 +0100765static void issue(struct thin_c *tc, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000766{
767 struct pool *pool = tc->pool;
768 unsigned long flags;
769
Joe Thornbere49e5822012-07-27 15:08:16 +0100770 if (!bio_triggers_commit(tc, bio)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000771 generic_make_request(bio);
Joe Thornbere49e5822012-07-27 15:08:16 +0100772 return;
773 }
774
775 /*
776 * Complete bio with an error if earlier I/O caused changes to
777 * the metadata that can't be committed e.g, due to I/O errors
778 * on the metadata device.
779 */
780 if (dm_thin_aborted_changes(tc->td)) {
781 bio_io_error(bio);
782 return;
783 }
784
785 /*
786 * Batch together any bios that trigger commits and then issue a
787 * single commit for them in process_deferred_bios().
788 */
789 spin_lock_irqsave(&pool->lock, flags);
790 bio_list_add(&pool->deferred_flush_bios, bio);
791 spin_unlock_irqrestore(&pool->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000792}
793
Joe Thornber2dd9c252012-03-28 18:41:28 +0100794static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
795{
796 remap_to_origin(tc, bio);
797 issue(tc, bio);
798}
799
800static void remap_and_issue(struct thin_c *tc, struct bio *bio,
801 dm_block_t block)
802{
803 remap(tc, bio, block);
804 issue(tc, bio);
805}
806
Joe Thornber991d9fa2011-10-31 20:21:18 +0000807/*----------------------------------------------------------------*/
808
809/*
810 * Bio endio functions.
811 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100812struct dm_thin_new_mapping {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000813 struct list_head list;
814
Mike Snitzer7f214662013-12-17 13:43:31 -0500815 bool pass_discard:1;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100816 bool maybe_shared:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000817
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100818 /*
819 * Track quiescing, copying and zeroing preparation actions. When this
820 * counter hits zero the block is prepared and can be inserted into the
821 * btree.
822 */
823 atomic_t prepare_actions;
824
Mike Snitzer7f214662013-12-17 13:43:31 -0500825 int err;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000826 struct thin_c *tc;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100827 dm_block_t virt_begin, virt_end;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000828 dm_block_t data_block;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100829 struct dm_bio_prison_cell *cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000830
831 /*
832 * If the bio covers the whole area of a block then we can avoid
833 * zeroing or copying. Instead this bio is hooked. The bio will
834 * still be in the cell, so care has to be taken to avoid issuing
835 * the bio twice.
836 */
837 struct bio *bio;
838 bio_end_io_t *saved_bi_end_io;
839};
840
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100841static void __complete_mapping_preparation(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000842{
843 struct pool *pool = m->tc->pool;
844
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100845 if (atomic_dec_and_test(&m->prepare_actions)) {
Mike Snitzerdaec3382013-12-11 14:01:20 -0500846 list_add_tail(&m->list, &pool->prepared_mappings);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000847 wake_worker(pool);
848 }
849}
850
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100851static void complete_mapping_preparation(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000852{
853 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000854 struct pool *pool = m->tc->pool;
855
Joe Thornber991d9fa2011-10-31 20:21:18 +0000856 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100857 __complete_mapping_preparation(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000858 spin_unlock_irqrestore(&pool->lock, flags);
859}
860
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100861static void copy_complete(int read_err, unsigned long write_err, void *context)
862{
863 struct dm_thin_new_mapping *m = context;
864
865 m->err = read_err || write_err ? -EIO : 0;
866 complete_mapping_preparation(m);
867}
868
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200869static void overwrite_endio(struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000870{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000871 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Mike Snitzera24c2562012-06-03 00:30:00 +0100872 struct dm_thin_new_mapping *m = h->overwrite_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000873
Mike Snitzer8b908f82015-05-13 17:53:13 -0400874 bio->bi_end_io = m->saved_bi_end_io;
875
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200876 m->err = bio->bi_error;
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100877 complete_mapping_preparation(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000878}
879
Joe Thornber991d9fa2011-10-31 20:21:18 +0000880/*----------------------------------------------------------------*/
881
882/*
883 * Workqueue.
884 */
885
886/*
887 * Prepared mapping jobs.
888 */
889
890/*
Joe Thornber2d759a42014-10-10 15:27:16 +0100891 * This sends the bios in the cell, except the original holder, back
892 * to the deferred_bios list.
Joe Thornber991d9fa2011-10-31 20:21:18 +0000893 */
Joe Thornberf286ba02012-12-21 20:23:33 +0000894static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000895{
Joe Thornber991d9fa2011-10-31 20:21:18 +0000896 struct pool *pool = tc->pool;
897 unsigned long flags;
898
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400899 spin_lock_irqsave(&tc->lock, flags);
900 cell_release_no_holder(pool, cell, &tc->deferred_bio_list);
901 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000902
903 wake_worker(pool);
904}
905
Joe Thornbera374bb22014-10-10 13:43:14 +0100906static void thin_defer_bio(struct thin_c *tc, struct bio *bio);
907
Joe Thornber2d759a42014-10-10 15:27:16 +0100908struct remap_info {
909 struct thin_c *tc;
910 struct bio_list defer_bios;
911 struct bio_list issue_bios;
912};
913
914static void __inc_remap_and_issue_cell(void *context,
915 struct dm_bio_prison_cell *cell)
916{
917 struct remap_info *info = context;
918 struct bio *bio;
919
920 while ((bio = bio_list_pop(&cell->bios))) {
921 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA))
922 bio_list_add(&info->defer_bios, bio);
923 else {
924 inc_all_io_entry(info->tc->pool, bio);
925
926 /*
927 * We can't issue the bios with the bio prison lock
928 * held, so we add them to a list to issue on
929 * return from this function.
930 */
931 bio_list_add(&info->issue_bios, bio);
932 }
933 }
934}
935
Joe Thornbera374bb22014-10-10 13:43:14 +0100936static void inc_remap_and_issue_cell(struct thin_c *tc,
937 struct dm_bio_prison_cell *cell,
938 dm_block_t block)
939{
940 struct bio *bio;
Joe Thornber2d759a42014-10-10 15:27:16 +0100941 struct remap_info info;
Joe Thornbera374bb22014-10-10 13:43:14 +0100942
Joe Thornber2d759a42014-10-10 15:27:16 +0100943 info.tc = tc;
944 bio_list_init(&info.defer_bios);
945 bio_list_init(&info.issue_bios);
Joe Thornbera374bb22014-10-10 13:43:14 +0100946
Joe Thornber2d759a42014-10-10 15:27:16 +0100947 /*
948 * We have to be careful to inc any bios we're about to issue
949 * before the cell is released, and avoid a race with new bios
950 * being added to the cell.
951 */
952 cell_visit_release(tc->pool, __inc_remap_and_issue_cell,
953 &info, cell);
954
955 while ((bio = bio_list_pop(&info.defer_bios)))
956 thin_defer_bio(tc, bio);
957
958 while ((bio = bio_list_pop(&info.issue_bios)))
959 remap_and_issue(info.tc, bio, block);
Joe Thornbera374bb22014-10-10 13:43:14 +0100960}
961
Joe Thornbere49e5822012-07-27 15:08:16 +0100962static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
963{
Joe Thornber6beca5e2013-03-01 22:45:50 +0000964 cell_error(m->tc->pool, m->cell);
Joe Thornbere49e5822012-07-27 15:08:16 +0100965 list_del(&m->list);
966 mempool_free(m, m->tc->pool->mapping_pool);
967}
Joe Thornber025b9682013-03-01 22:45:50 +0000968
Mike Snitzera24c2562012-06-03 00:30:00 +0100969static void process_prepared_mapping(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000970{
971 struct thin_c *tc = m->tc;
Joe Thornber6beca5e2013-03-01 22:45:50 +0000972 struct pool *pool = tc->pool;
Mike Snitzer8b908f82015-05-13 17:53:13 -0400973 struct bio *bio = m->bio;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000974 int r;
975
Joe Thornber991d9fa2011-10-31 20:21:18 +0000976 if (m->err) {
Joe Thornber6beca5e2013-03-01 22:45:50 +0000977 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100978 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000979 }
980
981 /*
982 * Commit the prepared block into the mapping btree.
983 * Any I/O for this block arriving after this point will get
984 * remapped to it directly.
985 */
Joe Thornber34fbcf62015-04-16 12:58:35 +0100986 r = dm_thin_insert_block(tc->td, m->virt_begin, m->data_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000987 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -0500988 metadata_operation_failed(pool, "dm_thin_insert_block", r);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000989 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100990 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000991 }
992
993 /*
994 * Release any bios held while the block was being provisioned.
995 * If we are processing a write bio that completely covers the block,
996 * we already processed it so can ignore it now when processing
997 * the bios in the cell.
998 */
999 if (bio) {
Joe Thornber2d759a42014-10-10 15:27:16 +01001000 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001001 bio_endio(bio);
Joe Thornber2d759a42014-10-10 15:27:16 +01001002 } else {
1003 inc_all_io_entry(tc->pool, m->cell->holder);
1004 remap_and_issue(tc, m->cell->holder, m->data_block);
1005 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
1006 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001007
Joe Thornber905386f2012-07-27 15:08:05 +01001008out:
Joe Thornber991d9fa2011-10-31 20:21:18 +00001009 list_del(&m->list);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001010 mempool_free(m, pool->mapping_pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001011}
1012
Joe Thornber34fbcf62015-04-16 12:58:35 +01001013/*----------------------------------------------------------------*/
1014
1015static void free_discard_mapping(struct dm_thin_new_mapping *m)
1016{
1017 struct thin_c *tc = m->tc;
1018 if (m->cell)
1019 cell_defer_no_holder(tc, m->cell);
1020 mempool_free(m, tc->pool->mapping_pool);
1021}
1022
Joe Thornbere49e5822012-07-27 15:08:16 +01001023static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
Joe Thornber104655f2012-03-28 18:41:28 +01001024{
Joe Thornbere49e5822012-07-27 15:08:16 +01001025 bio_io_error(m->bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001026 free_discard_mapping(m);
Joe Thornbere49e5822012-07-27 15:08:16 +01001027}
Joe Thornber104655f2012-03-28 18:41:28 +01001028
Joe Thornber34fbcf62015-04-16 12:58:35 +01001029static void process_prepared_discard_success(struct dm_thin_new_mapping *m)
Joe Thornbere49e5822012-07-27 15:08:16 +01001030{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001031 bio_endio(m->bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001032 free_discard_mapping(m);
Joe Thornber104655f2012-03-28 18:41:28 +01001033}
1034
Joe Thornber34fbcf62015-04-16 12:58:35 +01001035static void process_prepared_discard_no_passdown(struct dm_thin_new_mapping *m)
Joe Thornbere49e5822012-07-27 15:08:16 +01001036{
1037 int r;
1038 struct thin_c *tc = m->tc;
1039
Joe Thornber34fbcf62015-04-16 12:58:35 +01001040 r = dm_thin_remove_range(tc->td, m->cell->key.block_begin, m->cell->key.block_end);
1041 if (r) {
1042 metadata_operation_failed(tc->pool, "dm_thin_remove_range", r);
1043 bio_io_error(m->bio);
1044 } else
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001045 bio_endio(m->bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001046
Joe Thornber34fbcf62015-04-16 12:58:35 +01001047 cell_defer_no_holder(tc, m->cell);
1048 mempool_free(m, tc->pool->mapping_pool);
1049}
1050
1051static int passdown_double_checking_shared_status(struct dm_thin_new_mapping *m)
1052{
1053 /*
1054 * We've already unmapped this range of blocks, but before we
1055 * passdown we have to check that these blocks are now unused.
1056 */
1057 int r;
1058 bool used = true;
1059 struct thin_c *tc = m->tc;
1060 struct pool *pool = tc->pool;
1061 dm_block_t b = m->data_block, e, end = m->data_block + m->virt_end - m->virt_begin;
1062
1063 while (b != end) {
1064 /* find start of unmapped run */
1065 for (; b < end; b++) {
1066 r = dm_pool_block_is_used(pool->pmd, b, &used);
1067 if (r)
1068 return r;
1069
1070 if (!used)
1071 break;
1072 }
1073
1074 if (b == end)
1075 break;
1076
1077 /* find end of run */
1078 for (e = b + 1; e != end; e++) {
1079 r = dm_pool_block_is_used(pool->pmd, e, &used);
1080 if (r)
1081 return r;
1082
1083 if (used)
1084 break;
1085 }
1086
1087 r = issue_discard(tc, b, e, m->bio);
1088 if (r)
1089 return r;
1090
1091 b = e;
1092 }
1093
1094 return 0;
1095}
1096
1097static void process_prepared_discard_passdown(struct dm_thin_new_mapping *m)
1098{
1099 int r;
1100 struct thin_c *tc = m->tc;
1101 struct pool *pool = tc->pool;
1102
1103 r = dm_thin_remove_range(tc->td, m->virt_begin, m->virt_end);
1104 if (r)
1105 metadata_operation_failed(pool, "dm_thin_remove_range", r);
1106
1107 else if (m->maybe_shared)
1108 r = passdown_double_checking_shared_status(m);
1109 else
1110 r = issue_discard(tc, m->data_block, m->data_block + (m->virt_end - m->virt_begin), m->bio);
1111
1112 /*
1113 * Even if r is set, there could be sub discards in flight that we
1114 * need to wait for.
1115 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001116 m->bio->bi_error = r;
1117 bio_endio(m->bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001118 cell_defer_no_holder(tc, m->cell);
1119 mempool_free(m, pool->mapping_pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01001120}
1121
Joe Thornber104655f2012-03-28 18:41:28 +01001122static void process_prepared(struct pool *pool, struct list_head *head,
Joe Thornbere49e5822012-07-27 15:08:16 +01001123 process_mapping_fn *fn)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001124{
1125 unsigned long flags;
1126 struct list_head maps;
Mike Snitzera24c2562012-06-03 00:30:00 +01001127 struct dm_thin_new_mapping *m, *tmp;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001128
1129 INIT_LIST_HEAD(&maps);
1130 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +01001131 list_splice_init(head, &maps);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001132 spin_unlock_irqrestore(&pool->lock, flags);
1133
1134 list_for_each_entry_safe(m, tmp, &maps, list)
Joe Thornbere49e5822012-07-27 15:08:16 +01001135 (*fn)(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001136}
1137
1138/*
1139 * Deferred bio jobs.
1140 */
Joe Thornber104655f2012-03-28 18:41:28 +01001141static int io_overlaps_block(struct pool *pool, struct bio *bio)
1142{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001143 return bio->bi_iter.bi_size ==
1144 (pool->sectors_per_block << SECTOR_SHIFT);
Joe Thornber104655f2012-03-28 18:41:28 +01001145}
1146
Joe Thornber991d9fa2011-10-31 20:21:18 +00001147static int io_overwrites_block(struct pool *pool, struct bio *bio)
1148{
Joe Thornber104655f2012-03-28 18:41:28 +01001149 return (bio_data_dir(bio) == WRITE) &&
1150 io_overlaps_block(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001151}
1152
1153static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
1154 bio_end_io_t *fn)
1155{
1156 *save = bio->bi_end_io;
1157 bio->bi_end_io = fn;
1158}
1159
1160static int ensure_next_mapping(struct pool *pool)
1161{
1162 if (pool->next_mapping)
1163 return 0;
1164
1165 pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
1166
1167 return pool->next_mapping ? 0 : -ENOMEM;
1168}
1169
Mike Snitzera24c2562012-06-03 00:30:00 +01001170static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001171{
Mike Snitzer16961b02013-12-17 13:19:11 -05001172 struct dm_thin_new_mapping *m = pool->next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001173
1174 BUG_ON(!pool->next_mapping);
1175
Mike Snitzer16961b02013-12-17 13:19:11 -05001176 memset(m, 0, sizeof(struct dm_thin_new_mapping));
1177 INIT_LIST_HEAD(&m->list);
1178 m->bio = NULL;
1179
Joe Thornber991d9fa2011-10-31 20:21:18 +00001180 pool->next_mapping = NULL;
1181
Mike Snitzer16961b02013-12-17 13:19:11 -05001182 return m;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001183}
1184
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001185static void ll_zero(struct thin_c *tc, struct dm_thin_new_mapping *m,
1186 sector_t begin, sector_t end)
1187{
1188 int r;
1189 struct dm_io_region to;
1190
1191 to.bdev = tc->pool_dev->bdev;
1192 to.sector = begin;
1193 to.count = end - begin;
1194
1195 r = dm_kcopyd_zero(tc->pool->copier, 1, &to, 0, copy_complete, m);
1196 if (r < 0) {
1197 DMERR_LIMIT("dm_kcopyd_zero() failed");
1198 copy_complete(1, 1, m);
1199 }
1200}
1201
Mike Snitzer452d7a62014-10-09 19:20:21 -04001202static void remap_and_issue_overwrite(struct thin_c *tc, struct bio *bio,
Joe Thornber34fbcf62015-04-16 12:58:35 +01001203 dm_block_t data_begin,
Mike Snitzer452d7a62014-10-09 19:20:21 -04001204 struct dm_thin_new_mapping *m)
1205{
1206 struct pool *pool = tc->pool;
1207 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1208
1209 h->overwrite_mapping = m;
1210 m->bio = bio;
1211 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
1212 inc_all_io_entry(pool, bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001213 remap_and_issue(tc, bio, data_begin);
Mike Snitzer452d7a62014-10-09 19:20:21 -04001214}
1215
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001216/*
1217 * A partial copy also needs to zero the uncopied region.
1218 */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001219static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
Joe Thornber2dd9c252012-03-28 18:41:28 +01001220 struct dm_dev *origin, dm_block_t data_origin,
1221 dm_block_t data_dest,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001222 struct dm_bio_prison_cell *cell, struct bio *bio,
1223 sector_t len)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001224{
1225 int r;
1226 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +01001227 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001228
Joe Thornber991d9fa2011-10-31 20:21:18 +00001229 m->tc = tc;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001230 m->virt_begin = virt_block;
1231 m->virt_end = virt_block + 1u;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001232 m->data_block = data_dest;
1233 m->cell = cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001234
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001235 /*
1236 * quiesce action + copy action + an extra reference held for the
1237 * duration of this function (we may need to inc later for a
1238 * partial zero).
1239 */
1240 atomic_set(&m->prepare_actions, 3);
1241
Mike Snitzer44feb382012-10-12 21:02:10 +01001242 if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001243 complete_mapping_preparation(m); /* already quiesced */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001244
1245 /*
1246 * IO to pool_dev remaps to the pool target's data_dev.
1247 *
1248 * If the whole block of data is being overwritten, we can issue the
1249 * bio immediately. Otherwise we use kcopyd to clone the data first.
1250 */
Mike Snitzer452d7a62014-10-09 19:20:21 -04001251 if (io_overwrites_block(pool, bio))
1252 remap_and_issue_overwrite(tc, bio, data_dest, m);
1253 else {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001254 struct dm_io_region from, to;
1255
Joe Thornber2dd9c252012-03-28 18:41:28 +01001256 from.bdev = origin->bdev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001257 from.sector = data_origin * pool->sectors_per_block;
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001258 from.count = len;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001259
1260 to.bdev = tc->pool_dev->bdev;
1261 to.sector = data_dest * pool->sectors_per_block;
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001262 to.count = len;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001263
1264 r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
1265 0, copy_complete, m);
1266 if (r < 0) {
Mike Snitzerc3977412012-12-21 20:23:34 +00001267 DMERR_LIMIT("dm_kcopyd_copy() failed");
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001268 copy_complete(1, 1, m);
1269
1270 /*
1271 * We allow the zero to be issued, to simplify the
1272 * error path. Otherwise we'd need to start
1273 * worrying about decrementing the prepare_actions
1274 * counter.
1275 */
1276 }
1277
1278 /*
1279 * Do we need to zero a tail region?
1280 */
1281 if (len < pool->sectors_per_block && pool->pf.zero_new_blocks) {
1282 atomic_inc(&m->prepare_actions);
1283 ll_zero(tc, m,
1284 data_dest * pool->sectors_per_block + len,
1285 (data_dest + 1) * pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001286 }
1287 }
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001288
1289 complete_mapping_preparation(m); /* drop our ref */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001290}
1291
Joe Thornber2dd9c252012-03-28 18:41:28 +01001292static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
1293 dm_block_t data_origin, dm_block_t data_dest,
Mike Snitzera24c2562012-06-03 00:30:00 +01001294 struct dm_bio_prison_cell *cell, struct bio *bio)
Joe Thornber2dd9c252012-03-28 18:41:28 +01001295{
1296 schedule_copy(tc, virt_block, tc->pool_dev,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001297 data_origin, data_dest, cell, bio,
1298 tc->pool->sectors_per_block);
Joe Thornber2dd9c252012-03-28 18:41:28 +01001299}
1300
Joe Thornber991d9fa2011-10-31 20:21:18 +00001301static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
Mike Snitzera24c2562012-06-03 00:30:00 +01001302 dm_block_t data_block, struct dm_bio_prison_cell *cell,
Joe Thornber991d9fa2011-10-31 20:21:18 +00001303 struct bio *bio)
1304{
1305 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +01001306 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001307
Joe Thornber50f3c3e2014-06-13 13:57:09 +01001308 atomic_set(&m->prepare_actions, 1); /* no need to quiesce */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001309 m->tc = tc;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001310 m->virt_begin = virt_block;
1311 m->virt_end = virt_block + 1u;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001312 m->data_block = data_block;
1313 m->cell = cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001314
1315 /*
1316 * If the whole block of data is being overwritten or we are not
1317 * zeroing pre-existing data, we can issue the bio immediately.
1318 * Otherwise we use kcopyd to zero the data first.
1319 */
Mike Snitzerf8ae7522015-05-14 11:28:37 -04001320 if (pool->pf.zero_new_blocks) {
1321 if (io_overwrites_block(pool, bio))
1322 remap_and_issue_overwrite(tc, bio, data_block, m);
1323 else
1324 ll_zero(tc, m, data_block * pool->sectors_per_block,
1325 (data_block + 1) * pool->sectors_per_block);
1326 } else
Joe Thornber991d9fa2011-10-31 20:21:18 +00001327 process_prepared_mapping(m);
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001328}
Joe Thornber991d9fa2011-10-31 20:21:18 +00001329
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001330static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
1331 dm_block_t data_dest,
1332 struct dm_bio_prison_cell *cell, struct bio *bio)
1333{
1334 struct pool *pool = tc->pool;
1335 sector_t virt_block_begin = virt_block * pool->sectors_per_block;
1336 sector_t virt_block_end = (virt_block + 1) * pool->sectors_per_block;
1337
1338 if (virt_block_end <= tc->origin_size)
1339 schedule_copy(tc, virt_block, tc->origin_dev,
1340 virt_block, data_dest, cell, bio,
1341 pool->sectors_per_block);
1342
1343 else if (virt_block_begin < tc->origin_size)
1344 schedule_copy(tc, virt_block, tc->origin_dev,
1345 virt_block, data_dest, cell, bio,
1346 tc->origin_size - virt_block_begin);
1347
1348 else
1349 schedule_zero(tc, virt_block, data_dest, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001350}
1351
Joe Thornber2c43fd22014-12-11 11:12:19 +00001352static void set_pool_mode(struct pool *pool, enum pool_mode new_mode);
1353
1354static void check_for_space(struct pool *pool)
1355{
1356 int r;
1357 dm_block_t nr_free;
1358
1359 if (get_pool_mode(pool) != PM_OUT_OF_DATA_SPACE)
1360 return;
1361
1362 r = dm_pool_get_free_block_count(pool->pmd, &nr_free);
1363 if (r)
1364 return;
1365
1366 if (nr_free)
1367 set_pool_mode(pool, PM_WRITE);
1368}
1369
Joe Thornbere49e5822012-07-27 15:08:16 +01001370/*
1371 * A non-zero return indicates read_only or fail_io mode.
1372 * Many callers don't care about the return value.
1373 */
Joe Thornber020cc3b2013-12-04 15:05:36 -05001374static int commit(struct pool *pool)
Joe Thornbere49e5822012-07-27 15:08:16 +01001375{
1376 int r;
1377
Joe Thornber8d07e8a2014-05-06 16:28:14 +01001378 if (get_pool_mode(pool) >= PM_READ_ONLY)
Joe Thornbere49e5822012-07-27 15:08:16 +01001379 return -EINVAL;
1380
Joe Thornber020cc3b2013-12-04 15:05:36 -05001381 r = dm_pool_commit_metadata(pool->pmd);
Joe Thornberb5330652013-12-04 19:51:33 -05001382 if (r)
1383 metadata_operation_failed(pool, "dm_pool_commit_metadata", r);
Joe Thornber2c43fd22014-12-11 11:12:19 +00001384 else
1385 check_for_space(pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01001386
1387 return r;
1388}
1389
Joe Thornber88a66212013-12-04 20:16:12 -05001390static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks)
1391{
1392 unsigned long flags;
1393
1394 if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
1395 DMWARN("%s: reached low water mark for data device: sending event.",
1396 dm_device_name(pool->pool_md));
1397 spin_lock_irqsave(&pool->lock, flags);
1398 pool->low_water_triggered = true;
1399 spin_unlock_irqrestore(&pool->lock, flags);
1400 dm_table_event(pool->ti->table);
1401 }
1402}
1403
Joe Thornber991d9fa2011-10-31 20:21:18 +00001404static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
1405{
1406 int r;
1407 dm_block_t free_blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001408 struct pool *pool = tc->pool;
1409
Joe Thornber3e1a0692014-03-03 16:03:26 +00001410 if (WARN_ON(get_pool_mode(pool) != PM_WRITE))
Joe Thornber8d30abf2013-12-04 19:16:11 -05001411 return -EINVAL;
1412
Joe Thornber991d9fa2011-10-31 20:21:18 +00001413 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
Joe Thornberb5330652013-12-04 19:51:33 -05001414 if (r) {
1415 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001416 return r;
Joe Thornberb5330652013-12-04 19:51:33 -05001417 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001418
Joe Thornber88a66212013-12-04 20:16:12 -05001419 check_low_water_mark(pool, free_blocks);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001420
1421 if (!free_blocks) {
Mike Snitzer94563ba2013-08-22 09:56:18 -04001422 /*
1423 * Try to commit to see if that will free up some
1424 * more space.
1425 */
Joe Thornber020cc3b2013-12-04 15:05:36 -05001426 r = commit(pool);
1427 if (r)
1428 return r;
Mike Snitzer94563ba2013-08-22 09:56:18 -04001429
1430 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
Joe Thornberb5330652013-12-04 19:51:33 -05001431 if (r) {
1432 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
Mike Snitzer94563ba2013-08-22 09:56:18 -04001433 return r;
Joe Thornberb5330652013-12-04 19:51:33 -05001434 }
Mike Snitzer94563ba2013-08-22 09:56:18 -04001435
Mike Snitzer94563ba2013-08-22 09:56:18 -04001436 if (!free_blocks) {
Joe Thornber3e1a0692014-03-03 16:03:26 +00001437 set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001438 return -ENOSPC;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001439 }
1440 }
1441
1442 r = dm_pool_alloc_data_block(pool->pmd, result);
Mike Snitzer4a02b342013-12-03 12:20:57 -05001443 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05001444 metadata_operation_failed(pool, "dm_pool_alloc_data_block", r);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001445 return r;
Mike Snitzer4a02b342013-12-03 12:20:57 -05001446 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001447
1448 return 0;
1449}
1450
1451/*
1452 * If we have run out of space, queue bios until the device is
1453 * resumed, presumably after having been reloaded with more space.
1454 */
1455static void retry_on_resume(struct bio *bio)
1456{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001457 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01001458 struct thin_c *tc = h->tc;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001459 unsigned long flags;
1460
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001461 spin_lock_irqsave(&tc->lock, flags);
1462 bio_list_add(&tc->retry_on_resume_list, bio);
1463 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001464}
1465
Mike Snitzeraf918052014-05-22 14:32:51 -04001466static int should_error_unserviceable_bio(struct pool *pool)
Joe Thornber3e1a0692014-03-03 16:03:26 +00001467{
1468 enum pool_mode m = get_pool_mode(pool);
1469
1470 switch (m) {
1471 case PM_WRITE:
1472 /* Shouldn't get here */
1473 DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
Mike Snitzeraf918052014-05-22 14:32:51 -04001474 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001475
1476 case PM_OUT_OF_DATA_SPACE:
Mike Snitzeraf918052014-05-22 14:32:51 -04001477 return pool->pf.error_if_no_space ? -ENOSPC : 0;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001478
1479 case PM_READ_ONLY:
1480 case PM_FAIL:
Mike Snitzeraf918052014-05-22 14:32:51 -04001481 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001482 default:
1483 /* Shouldn't get here */
1484 DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
Mike Snitzeraf918052014-05-22 14:32:51 -04001485 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001486 }
1487}
1488
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001489static void handle_unserviceable_bio(struct pool *pool, struct bio *bio)
1490{
Mike Snitzeraf918052014-05-22 14:32:51 -04001491 int error = should_error_unserviceable_bio(pool);
1492
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001493 if (error) {
1494 bio->bi_error = error;
1495 bio_endio(bio);
1496 } else
Mike Snitzer6d162022013-12-20 18:09:02 -05001497 retry_on_resume(bio);
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001498}
1499
Mike Snitzer399cadd2013-12-05 16:03:33 -05001500static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001501{
1502 struct bio *bio;
1503 struct bio_list bios;
Mike Snitzeraf918052014-05-22 14:32:51 -04001504 int error;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001505
Mike Snitzeraf918052014-05-22 14:32:51 -04001506 error = should_error_unserviceable_bio(pool);
1507 if (error) {
1508 cell_error_with_code(pool, cell, error);
Joe Thornber3e1a0692014-03-03 16:03:26 +00001509 return;
1510 }
1511
Joe Thornber991d9fa2011-10-31 20:21:18 +00001512 bio_list_init(&bios);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001513 cell_release(pool, cell, &bios);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001514
Mike Snitzer9d094ee2014-10-19 08:23:09 -04001515 while ((bio = bio_list_pop(&bios)))
1516 retry_on_resume(bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001517}
1518
Joe Thornber34fbcf62015-04-16 12:58:35 +01001519static void process_discard_cell_no_passdown(struct thin_c *tc,
1520 struct dm_bio_prison_cell *virt_cell)
Joe Thornber104655f2012-03-28 18:41:28 +01001521{
Joe Thornber104655f2012-03-28 18:41:28 +01001522 struct pool *pool = tc->pool;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001523 struct dm_thin_new_mapping *m = get_next_mapping(pool);
1524
1525 /*
1526 * We don't need to lock the data blocks, since there's no
1527 * passdown. We only lock data blocks for allocation and breaking sharing.
1528 */
1529 m->tc = tc;
1530 m->virt_begin = virt_cell->key.block_begin;
1531 m->virt_end = virt_cell->key.block_end;
1532 m->cell = virt_cell;
1533 m->bio = virt_cell->holder;
1534
1535 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
1536 pool->process_prepared_discard(m);
1537}
1538
1539/*
1540 * FIXME: DM local hack to defer parent bios's end_io until we
1541 * _know_ all chained sub range discard bios have completed.
1542 * Will go away once late bio splitting lands upstream!
1543 */
1544static inline void __bio_inc_remaining(struct bio *bio)
1545{
1546 bio->bi_flags |= (1 << BIO_CHAIN);
1547 smp_mb__before_atomic();
1548 atomic_inc(&bio->__bi_remaining);
1549}
1550
1551static void break_up_discard_bio(struct thin_c *tc, dm_block_t begin, dm_block_t end,
1552 struct bio *bio)
1553{
1554 struct pool *pool = tc->pool;
1555
1556 int r;
1557 bool maybe_shared;
1558 struct dm_cell_key data_key;
1559 struct dm_bio_prison_cell *data_cell;
Mike Snitzera24c2562012-06-03 00:30:00 +01001560 struct dm_thin_new_mapping *m;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001561 dm_block_t virt_begin, virt_end, data_begin;
Joe Thornber104655f2012-03-28 18:41:28 +01001562
Joe Thornber34fbcf62015-04-16 12:58:35 +01001563 while (begin != end) {
1564 r = ensure_next_mapping(pool);
1565 if (r)
1566 /* we did our best */
1567 return;
Joe Thornber104655f2012-03-28 18:41:28 +01001568
Joe Thornber34fbcf62015-04-16 12:58:35 +01001569 r = dm_thin_find_mapped_range(tc->td, begin, end, &virt_begin, &virt_end,
1570 &data_begin, &maybe_shared);
1571 if (r)
1572 /*
1573 * Silently fail, letting any mappings we've
1574 * created complete.
1575 */
Joe Thornber104655f2012-03-28 18:41:28 +01001576 break;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001577
1578 build_key(tc->td, PHYSICAL, data_begin, data_begin + (virt_end - virt_begin), &data_key);
1579 if (bio_detain(tc->pool, &data_key, NULL, &data_cell)) {
1580 /* contention, we'll give up with this range */
1581 begin = virt_end;
1582 continue;
Joe Thornber104655f2012-03-28 18:41:28 +01001583 }
1584
Joe Thornber104655f2012-03-28 18:41:28 +01001585 /*
Joe Thornber34fbcf62015-04-16 12:58:35 +01001586 * IO may still be going to the destination block. We must
1587 * quiesce before we can do the removal.
Joe Thornber104655f2012-03-28 18:41:28 +01001588 */
Joe Thornber34fbcf62015-04-16 12:58:35 +01001589 m = get_next_mapping(pool);
1590 m->tc = tc;
1591 m->maybe_shared = maybe_shared;
1592 m->virt_begin = virt_begin;
1593 m->virt_end = virt_end;
1594 m->data_block = data_begin;
1595 m->cell = data_cell;
1596 m->bio = bio;
Joe Thornber104655f2012-03-28 18:41:28 +01001597
Joe Thornber34fbcf62015-04-16 12:58:35 +01001598 /*
1599 * The parent bio must not complete before sub discard bios are
1600 * chained to it (see __blkdev_issue_discard_async's bio_chain)!
1601 *
1602 * This per-mapping bi_remaining increment is paired with
1603 * the implicit decrement that occurs via bio_endio() in
1604 * process_prepared_discard_{passdown,no_passdown}.
1605 */
1606 __bio_inc_remaining(bio);
1607 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
1608 pool->process_prepared_discard(m);
1609
1610 begin = virt_end;
Joe Thornber104655f2012-03-28 18:41:28 +01001611 }
1612}
1613
Joe Thornber34fbcf62015-04-16 12:58:35 +01001614static void process_discard_cell_passdown(struct thin_c *tc, struct dm_bio_prison_cell *virt_cell)
1615{
1616 struct bio *bio = virt_cell->holder;
1617 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1618
1619 /*
1620 * The virt_cell will only get freed once the origin bio completes.
1621 * This means it will remain locked while all the individual
1622 * passdown bios are in flight.
1623 */
1624 h->cell = virt_cell;
1625 break_up_discard_bio(tc, virt_cell->key.block_begin, virt_cell->key.block_end, bio);
1626
1627 /*
1628 * We complete the bio now, knowing that the bi_remaining field
1629 * will prevent completion until the sub range discards have
1630 * completed.
1631 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001632 bio_endio(bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001633}
1634
Joe Thornbera374bb22014-10-10 13:43:14 +01001635static void process_discard_bio(struct thin_c *tc, struct bio *bio)
1636{
Joe Thornber34fbcf62015-04-16 12:58:35 +01001637 dm_block_t begin, end;
1638 struct dm_cell_key virt_key;
1639 struct dm_bio_prison_cell *virt_cell;
Joe Thornbera374bb22014-10-10 13:43:14 +01001640
Joe Thornber34fbcf62015-04-16 12:58:35 +01001641 get_bio_block_range(tc, bio, &begin, &end);
1642 if (begin == end) {
1643 /*
1644 * The discard covers less than a block.
1645 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001646 bio_endio(bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001647 return;
1648 }
1649
1650 build_key(tc->td, VIRTUAL, begin, end, &virt_key);
1651 if (bio_detain(tc->pool, &virt_key, bio, &virt_cell))
1652 /*
1653 * Potential starvation issue: We're relying on the
1654 * fs/application being well behaved, and not trying to
1655 * send IO to a region at the same time as discarding it.
1656 * If they do this persistently then it's possible this
1657 * cell will never be granted.
1658 */
Joe Thornbera374bb22014-10-10 13:43:14 +01001659 return;
1660
Joe Thornber34fbcf62015-04-16 12:58:35 +01001661 tc->pool->process_discard_cell(tc, virt_cell);
Joe Thornbera374bb22014-10-10 13:43:14 +01001662}
1663
Joe Thornber991d9fa2011-10-31 20:21:18 +00001664static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzer44feb382012-10-12 21:02:10 +01001665 struct dm_cell_key *key,
Joe Thornber991d9fa2011-10-31 20:21:18 +00001666 struct dm_thin_lookup_result *lookup_result,
Mike Snitzera24c2562012-06-03 00:30:00 +01001667 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001668{
1669 int r;
1670 dm_block_t data_block;
Mike Snitzerd6fc2042013-08-21 17:40:11 -04001671 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001672
1673 r = alloc_data_block(tc, &data_block);
1674 switch (r) {
1675 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001676 schedule_internal_copy(tc, block, lookup_result->block,
1677 data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001678 break;
1679
1680 case -ENOSPC:
Mike Snitzer399cadd2013-12-05 16:03:33 -05001681 retry_bios_on_resume(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001682 break;
1683
1684 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001685 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1686 __func__, r);
Mike Snitzerd6fc2042013-08-21 17:40:11 -04001687 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001688 break;
1689 }
1690}
1691
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001692static void __remap_and_issue_shared_cell(void *context,
1693 struct dm_bio_prison_cell *cell)
1694{
1695 struct remap_info *info = context;
1696 struct bio *bio;
1697
1698 while ((bio = bio_list_pop(&cell->bios))) {
1699 if ((bio_data_dir(bio) == WRITE) ||
1700 (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)))
1701 bio_list_add(&info->defer_bios, bio);
1702 else {
1703 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));;
1704
1705 h->shared_read_entry = dm_deferred_entry_inc(info->tc->pool->shared_read_ds);
1706 inc_all_io_entry(info->tc->pool, bio);
1707 bio_list_add(&info->issue_bios, bio);
1708 }
1709 }
1710}
1711
1712static void remap_and_issue_shared_cell(struct thin_c *tc,
1713 struct dm_bio_prison_cell *cell,
1714 dm_block_t block)
1715{
1716 struct bio *bio;
1717 struct remap_info info;
1718
1719 info.tc = tc;
1720 bio_list_init(&info.defer_bios);
1721 bio_list_init(&info.issue_bios);
1722
1723 cell_visit_release(tc->pool, __remap_and_issue_shared_cell,
1724 &info, cell);
1725
1726 while ((bio = bio_list_pop(&info.defer_bios)))
1727 thin_defer_bio(tc, bio);
1728
1729 while ((bio = bio_list_pop(&info.issue_bios)))
1730 remap_and_issue(tc, bio, block);
1731}
1732
Joe Thornber991d9fa2011-10-31 20:21:18 +00001733static void process_shared_bio(struct thin_c *tc, struct bio *bio,
1734 dm_block_t block,
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001735 struct dm_thin_lookup_result *lookup_result,
1736 struct dm_bio_prison_cell *virt_cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001737{
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001738 struct dm_bio_prison_cell *data_cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001739 struct pool *pool = tc->pool;
Mike Snitzer44feb382012-10-12 21:02:10 +01001740 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001741
1742 /*
1743 * If cell is already occupied, then sharing is already in the process
1744 * of being broken so we have nothing further to do here.
1745 */
1746 build_data_key(tc->td, lookup_result->block, &key);
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001747 if (bio_detain(pool, &key, bio, &data_cell)) {
1748 cell_defer_no_holder(tc, virt_cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001749 return;
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001750 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001751
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001752 if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size) {
1753 break_sharing(tc, bio, block, &key, lookup_result, data_cell);
1754 cell_defer_no_holder(tc, virt_cell);
1755 } else {
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001756 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornber991d9fa2011-10-31 20:21:18 +00001757
Mike Snitzer44feb382012-10-12 21:02:10 +01001758 h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
Joe Thornbere8088072012-12-21 20:23:31 +00001759 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001760 remap_and_issue(tc, bio, lookup_result->block);
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001761
1762 remap_and_issue_shared_cell(tc, data_cell, lookup_result->block);
1763 remap_and_issue_shared_cell(tc, virt_cell, lookup_result->block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001764 }
1765}
1766
1767static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzera24c2562012-06-03 00:30:00 +01001768 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001769{
1770 int r;
1771 dm_block_t data_block;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001772 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001773
1774 /*
1775 * Remap empty bios (flushes) immediately, without provisioning.
1776 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07001777 if (!bio->bi_iter.bi_size) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001778 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001779 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001780
Joe Thornber991d9fa2011-10-31 20:21:18 +00001781 remap_and_issue(tc, bio, 0);
1782 return;
1783 }
1784
1785 /*
1786 * Fill read bios with zeroes and complete them immediately.
1787 */
1788 if (bio_data_dir(bio) == READ) {
1789 zero_fill_bio(bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001790 cell_defer_no_holder(tc, cell);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001791 bio_endio(bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001792 return;
1793 }
1794
1795 r = alloc_data_block(tc, &data_block);
1796 switch (r) {
1797 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001798 if (tc->origin_dev)
1799 schedule_external_copy(tc, block, data_block, cell, bio);
1800 else
1801 schedule_zero(tc, block, data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001802 break;
1803
1804 case -ENOSPC:
Mike Snitzer399cadd2013-12-05 16:03:33 -05001805 retry_bios_on_resume(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001806 break;
1807
1808 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001809 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1810 __func__, r);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001811 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001812 break;
1813 }
1814}
1815
Joe Thornbera374bb22014-10-10 13:43:14 +01001816static void process_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001817{
1818 int r;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001819 struct pool *pool = tc->pool;
Joe Thornbera374bb22014-10-10 13:43:14 +01001820 struct bio *bio = cell->holder;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001821 dm_block_t block = get_bio_block(tc, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001822 struct dm_thin_lookup_result lookup_result;
1823
Joe Thornbera374bb22014-10-10 13:43:14 +01001824 if (tc->requeue_mode) {
1825 cell_requeue(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001826 return;
Joe Thornbera374bb22014-10-10 13:43:14 +01001827 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001828
1829 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1830 switch (r) {
1831 case 0:
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001832 if (lookup_result.shared)
1833 process_shared_bio(tc, bio, block, &lookup_result, cell);
1834 else {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001835 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001836 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbera374bb22014-10-10 13:43:14 +01001837 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001838 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001839 break;
1840
1841 case -ENODATA:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001842 if (bio_data_dir(bio) == READ && tc->origin_dev) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001843 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001844 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001845
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001846 if (bio_end_sector(bio) <= tc->origin_size)
1847 remap_to_origin_and_issue(tc, bio);
1848
1849 else if (bio->bi_iter.bi_sector < tc->origin_size) {
1850 zero_fill_bio(bio);
1851 bio->bi_iter.bi_size = (tc->origin_size - bio->bi_iter.bi_sector) << SECTOR_SHIFT;
1852 remap_to_origin_and_issue(tc, bio);
1853
1854 } else {
1855 zero_fill_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001856 bio_endio(bio);
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001857 }
Joe Thornber2dd9c252012-03-28 18:41:28 +01001858 } else
1859 provision_block(tc, bio, block, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001860 break;
1861
1862 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001863 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1864 __func__, r);
Joe Thornberf286ba02012-12-21 20:23:33 +00001865 cell_defer_no_holder(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001866 bio_io_error(bio);
1867 break;
1868 }
1869}
1870
Joe Thornbera374bb22014-10-10 13:43:14 +01001871static void process_bio(struct thin_c *tc, struct bio *bio)
1872{
1873 struct pool *pool = tc->pool;
1874 dm_block_t block = get_bio_block(tc, bio);
1875 struct dm_bio_prison_cell *cell;
1876 struct dm_cell_key key;
1877
1878 /*
1879 * If cell is already occupied, then the block is already
1880 * being provisioned so we have nothing further to do here.
1881 */
1882 build_virtual_key(tc->td, block, &key);
1883 if (bio_detain(pool, &key, bio, &cell))
1884 return;
1885
1886 process_cell(tc, cell);
1887}
1888
1889static void __process_bio_read_only(struct thin_c *tc, struct bio *bio,
1890 struct dm_bio_prison_cell *cell)
Joe Thornbere49e5822012-07-27 15:08:16 +01001891{
1892 int r;
1893 int rw = bio_data_dir(bio);
1894 dm_block_t block = get_bio_block(tc, bio);
1895 struct dm_thin_lookup_result lookup_result;
1896
1897 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1898 switch (r) {
1899 case 0:
Joe Thornbera374bb22014-10-10 13:43:14 +01001900 if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size) {
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001901 handle_unserviceable_bio(tc->pool, bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01001902 if (cell)
1903 cell_defer_no_holder(tc, cell);
1904 } else {
Joe Thornbere8088072012-12-21 20:23:31 +00001905 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001906 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbera374bb22014-10-10 13:43:14 +01001907 if (cell)
1908 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001909 }
Joe Thornbere49e5822012-07-27 15:08:16 +01001910 break;
1911
1912 case -ENODATA:
Joe Thornbera374bb22014-10-10 13:43:14 +01001913 if (cell)
1914 cell_defer_no_holder(tc, cell);
Joe Thornbere49e5822012-07-27 15:08:16 +01001915 if (rw != READ) {
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001916 handle_unserviceable_bio(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001917 break;
1918 }
1919
1920 if (tc->origin_dev) {
Joe Thornbere8088072012-12-21 20:23:31 +00001921 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001922 remap_to_origin_and_issue(tc, bio);
1923 break;
1924 }
1925
1926 zero_fill_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001927 bio_endio(bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001928 break;
1929
1930 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001931 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1932 __func__, r);
Joe Thornbera374bb22014-10-10 13:43:14 +01001933 if (cell)
1934 cell_defer_no_holder(tc, cell);
Joe Thornbere49e5822012-07-27 15:08:16 +01001935 bio_io_error(bio);
1936 break;
1937 }
1938}
1939
Joe Thornbera374bb22014-10-10 13:43:14 +01001940static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
1941{
1942 __process_bio_read_only(tc, bio, NULL);
1943}
1944
1945static void process_cell_read_only(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1946{
1947 __process_bio_read_only(tc, cell->holder, cell);
1948}
1949
Joe Thornber3e1a0692014-03-03 16:03:26 +00001950static void process_bio_success(struct thin_c *tc, struct bio *bio)
1951{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001952 bio_endio(bio);
Joe Thornber3e1a0692014-03-03 16:03:26 +00001953}
1954
Joe Thornbere49e5822012-07-27 15:08:16 +01001955static void process_bio_fail(struct thin_c *tc, struct bio *bio)
1956{
1957 bio_io_error(bio);
1958}
1959
Joe Thornbera374bb22014-10-10 13:43:14 +01001960static void process_cell_success(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1961{
1962 cell_success(tc->pool, cell);
1963}
1964
1965static void process_cell_fail(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1966{
1967 cell_error(tc->pool, cell);
1968}
1969
Joe Thornberac8c3f32013-05-10 14:37:21 +01001970/*
1971 * FIXME: should we also commit due to size of transaction, measured in
1972 * metadata blocks?
1973 */
Joe Thornber905e51b2012-03-28 18:41:27 +01001974static int need_commit_due_to_time(struct pool *pool)
1975{
Manuel Schölling0f30af92014-05-22 22:42:37 +02001976 return !time_in_range(jiffies, pool->last_commit_jiffies,
1977 pool->last_commit_jiffies + COMMIT_PERIOD);
Joe Thornber905e51b2012-03-28 18:41:27 +01001978}
1979
Mike Snitzer67324ea2014-03-21 18:33:41 -04001980#define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node)
1981#define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook))
1982
1983static void __thin_bio_rb_add(struct thin_c *tc, struct bio *bio)
1984{
1985 struct rb_node **rbp, *parent;
1986 struct dm_thin_endio_hook *pbd;
1987 sector_t bi_sector = bio->bi_iter.bi_sector;
1988
1989 rbp = &tc->sort_bio_list.rb_node;
1990 parent = NULL;
1991 while (*rbp) {
1992 parent = *rbp;
1993 pbd = thin_pbd(parent);
1994
1995 if (bi_sector < thin_bio(pbd)->bi_iter.bi_sector)
1996 rbp = &(*rbp)->rb_left;
1997 else
1998 rbp = &(*rbp)->rb_right;
1999 }
2000
2001 pbd = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
2002 rb_link_node(&pbd->rb_node, parent, rbp);
2003 rb_insert_color(&pbd->rb_node, &tc->sort_bio_list);
2004}
2005
2006static void __extract_sorted_bios(struct thin_c *tc)
2007{
2008 struct rb_node *node;
2009 struct dm_thin_endio_hook *pbd;
2010 struct bio *bio;
2011
2012 for (node = rb_first(&tc->sort_bio_list); node; node = rb_next(node)) {
2013 pbd = thin_pbd(node);
2014 bio = thin_bio(pbd);
2015
2016 bio_list_add(&tc->deferred_bio_list, bio);
2017 rb_erase(&pbd->rb_node, &tc->sort_bio_list);
2018 }
2019
2020 WARN_ON(!RB_EMPTY_ROOT(&tc->sort_bio_list));
2021}
2022
2023static void __sort_thin_deferred_bios(struct thin_c *tc)
2024{
2025 struct bio *bio;
2026 struct bio_list bios;
2027
2028 bio_list_init(&bios);
2029 bio_list_merge(&bios, &tc->deferred_bio_list);
2030 bio_list_init(&tc->deferred_bio_list);
2031
2032 /* Sort deferred_bio_list using rb-tree */
2033 while ((bio = bio_list_pop(&bios)))
2034 __thin_bio_rb_add(tc, bio);
2035
2036 /*
2037 * Transfer the sorted bios in sort_bio_list back to
2038 * deferred_bio_list to allow lockless submission of
2039 * all bios.
2040 */
2041 __extract_sorted_bios(tc);
2042}
2043
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002044static void process_thin_deferred_bios(struct thin_c *tc)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002045{
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002046 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002047 unsigned long flags;
2048 struct bio *bio;
2049 struct bio_list bios;
Mike Snitzer67324ea2014-03-21 18:33:41 -04002050 struct blk_plug plug;
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002051 unsigned count = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002052
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002053 if (tc->requeue_mode) {
Mike Snitzer42d6a8c2014-10-19 07:52:44 -04002054 error_thin_bio_list(tc, &tc->deferred_bio_list, DM_ENDIO_REQUEUE);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002055 return;
2056 }
2057
Joe Thornber991d9fa2011-10-31 20:21:18 +00002058 bio_list_init(&bios);
2059
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002060 spin_lock_irqsave(&tc->lock, flags);
Mike Snitzer67324ea2014-03-21 18:33:41 -04002061
2062 if (bio_list_empty(&tc->deferred_bio_list)) {
2063 spin_unlock_irqrestore(&tc->lock, flags);
2064 return;
2065 }
2066
2067 __sort_thin_deferred_bios(tc);
2068
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002069 bio_list_merge(&bios, &tc->deferred_bio_list);
2070 bio_list_init(&tc->deferred_bio_list);
Mike Snitzer67324ea2014-03-21 18:33:41 -04002071
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002072 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002073
Mike Snitzer67324ea2014-03-21 18:33:41 -04002074 blk_start_plug(&plug);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002075 while ((bio = bio_list_pop(&bios))) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00002076 /*
2077 * If we've got no free new_mapping structs, and processing
2078 * this bio might require one, we pause until there are some
2079 * prepared mappings to process.
2080 */
2081 if (ensure_next_mapping(pool)) {
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002082 spin_lock_irqsave(&tc->lock, flags);
2083 bio_list_add(&tc->deferred_bio_list, bio);
2084 bio_list_merge(&tc->deferred_bio_list, &bios);
2085 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002086 break;
2087 }
Joe Thornber104655f2012-03-28 18:41:28 +01002088
2089 if (bio->bi_rw & REQ_DISCARD)
Joe Thornbere49e5822012-07-27 15:08:16 +01002090 pool->process_discard(tc, bio);
Joe Thornber104655f2012-03-28 18:41:28 +01002091 else
Joe Thornbere49e5822012-07-27 15:08:16 +01002092 pool->process_bio(tc, bio);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002093
2094 if ((count++ & 127) == 0) {
Joe Thornber7d327fe2014-10-06 15:45:59 +01002095 throttle_work_update(&pool->throttle);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002096 dm_pool_issue_prefetches(pool->pmd);
2097 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002098 }
Mike Snitzer67324ea2014-03-21 18:33:41 -04002099 blk_finish_plug(&plug);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002100}
2101
Joe Thornberac4c3f32014-10-10 16:42:10 +01002102static int cmp_cells(const void *lhs, const void *rhs)
2103{
2104 struct dm_bio_prison_cell *lhs_cell = *((struct dm_bio_prison_cell **) lhs);
2105 struct dm_bio_prison_cell *rhs_cell = *((struct dm_bio_prison_cell **) rhs);
2106
2107 BUG_ON(!lhs_cell->holder);
2108 BUG_ON(!rhs_cell->holder);
2109
2110 if (lhs_cell->holder->bi_iter.bi_sector < rhs_cell->holder->bi_iter.bi_sector)
2111 return -1;
2112
2113 if (lhs_cell->holder->bi_iter.bi_sector > rhs_cell->holder->bi_iter.bi_sector)
2114 return 1;
2115
2116 return 0;
2117}
2118
2119static unsigned sort_cells(struct pool *pool, struct list_head *cells)
2120{
2121 unsigned count = 0;
2122 struct dm_bio_prison_cell *cell, *tmp;
2123
2124 list_for_each_entry_safe(cell, tmp, cells, user_list) {
2125 if (count >= CELL_SORT_ARRAY_SIZE)
2126 break;
2127
2128 pool->cell_sort_array[count++] = cell;
2129 list_del(&cell->user_list);
2130 }
2131
2132 sort(pool->cell_sort_array, count, sizeof(cell), cmp_cells, NULL);
2133
2134 return count;
2135}
2136
Joe Thornbera374bb22014-10-10 13:43:14 +01002137static void process_thin_deferred_cells(struct thin_c *tc)
2138{
2139 struct pool *pool = tc->pool;
2140 unsigned long flags;
2141 struct list_head cells;
Joe Thornberac4c3f32014-10-10 16:42:10 +01002142 struct dm_bio_prison_cell *cell;
2143 unsigned i, j, count;
Joe Thornbera374bb22014-10-10 13:43:14 +01002144
2145 INIT_LIST_HEAD(&cells);
2146
2147 spin_lock_irqsave(&tc->lock, flags);
2148 list_splice_init(&tc->deferred_cells, &cells);
2149 spin_unlock_irqrestore(&tc->lock, flags);
2150
2151 if (list_empty(&cells))
2152 return;
2153
Joe Thornberac4c3f32014-10-10 16:42:10 +01002154 do {
2155 count = sort_cells(tc->pool, &cells);
Joe Thornbera374bb22014-10-10 13:43:14 +01002156
Joe Thornberac4c3f32014-10-10 16:42:10 +01002157 for (i = 0; i < count; i++) {
2158 cell = pool->cell_sort_array[i];
2159 BUG_ON(!cell->holder);
2160
2161 /*
2162 * If we've got no free new_mapping structs, and processing
2163 * this bio might require one, we pause until there are some
2164 * prepared mappings to process.
2165 */
2166 if (ensure_next_mapping(pool)) {
2167 for (j = i; j < count; j++)
2168 list_add(&pool->cell_sort_array[j]->user_list, &cells);
2169
2170 spin_lock_irqsave(&tc->lock, flags);
2171 list_splice(&cells, &tc->deferred_cells);
2172 spin_unlock_irqrestore(&tc->lock, flags);
2173 return;
2174 }
2175
2176 if (cell->holder->bi_rw & REQ_DISCARD)
2177 pool->process_discard_cell(tc, cell);
2178 else
2179 pool->process_cell(tc, cell);
Joe Thornbera374bb22014-10-10 13:43:14 +01002180 }
Joe Thornberac4c3f32014-10-10 16:42:10 +01002181 } while (!list_empty(&cells));
Joe Thornbera374bb22014-10-10 13:43:14 +01002182}
2183
Joe Thornberb10ebd32014-04-08 11:29:01 +01002184static void thin_get(struct thin_c *tc);
2185static void thin_put(struct thin_c *tc);
2186
2187/*
2188 * We can't hold rcu_read_lock() around code that can block. So we
2189 * find a thin with the rcu lock held; bump a refcount; then drop
2190 * the lock.
2191 */
2192static struct thin_c *get_first_thin(struct pool *pool)
2193{
2194 struct thin_c *tc = NULL;
2195
2196 rcu_read_lock();
2197 if (!list_empty(&pool->active_thins)) {
2198 tc = list_entry_rcu(pool->active_thins.next, struct thin_c, list);
2199 thin_get(tc);
2200 }
2201 rcu_read_unlock();
2202
2203 return tc;
2204}
2205
2206static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc)
2207{
2208 struct thin_c *old_tc = tc;
2209
2210 rcu_read_lock();
2211 list_for_each_entry_continue_rcu(tc, &pool->active_thins, list) {
2212 thin_get(tc);
2213 thin_put(old_tc);
2214 rcu_read_unlock();
2215 return tc;
2216 }
2217 thin_put(old_tc);
2218 rcu_read_unlock();
2219
2220 return NULL;
2221}
2222
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002223static void process_deferred_bios(struct pool *pool)
2224{
2225 unsigned long flags;
2226 struct bio *bio;
2227 struct bio_list bios;
2228 struct thin_c *tc;
2229
Joe Thornberb10ebd32014-04-08 11:29:01 +01002230 tc = get_first_thin(pool);
2231 while (tc) {
Joe Thornbera374bb22014-10-10 13:43:14 +01002232 process_thin_deferred_cells(tc);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002233 process_thin_deferred_bios(tc);
Joe Thornberb10ebd32014-04-08 11:29:01 +01002234 tc = get_next_thin(pool, tc);
2235 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002236
2237 /*
2238 * If there are any deferred flush bios, we must commit
2239 * the metadata before issuing them.
2240 */
2241 bio_list_init(&bios);
2242 spin_lock_irqsave(&pool->lock, flags);
2243 bio_list_merge(&bios, &pool->deferred_flush_bios);
2244 bio_list_init(&pool->deferred_flush_bios);
2245 spin_unlock_irqrestore(&pool->lock, flags);
2246
Mike Snitzer4d1662a2014-02-06 06:08:56 -05002247 if (bio_list_empty(&bios) &&
2248 !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool)))
Joe Thornber991d9fa2011-10-31 20:21:18 +00002249 return;
2250
Joe Thornber020cc3b2013-12-04 15:05:36 -05002251 if (commit(pool)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00002252 while ((bio = bio_list_pop(&bios)))
2253 bio_io_error(bio);
2254 return;
2255 }
Joe Thornber905e51b2012-03-28 18:41:27 +01002256 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002257
2258 while ((bio = bio_list_pop(&bios)))
2259 generic_make_request(bio);
2260}
2261
2262static void do_worker(struct work_struct *ws)
2263{
2264 struct pool *pool = container_of(ws, struct pool, worker);
2265
Joe Thornber7d327fe2014-10-06 15:45:59 +01002266 throttle_work_start(&pool->throttle);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002267 dm_pool_issue_prefetches(pool->pmd);
Joe Thornber7d327fe2014-10-06 15:45:59 +01002268 throttle_work_update(&pool->throttle);
Joe Thornbere49e5822012-07-27 15:08:16 +01002269 process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
Joe Thornber7d327fe2014-10-06 15:45:59 +01002270 throttle_work_update(&pool->throttle);
Joe Thornbere49e5822012-07-27 15:08:16 +01002271 process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
Joe Thornber7d327fe2014-10-06 15:45:59 +01002272 throttle_work_update(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002273 process_deferred_bios(pool);
Joe Thornber7d327fe2014-10-06 15:45:59 +01002274 throttle_work_complete(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002275}
2276
Joe Thornber905e51b2012-03-28 18:41:27 +01002277/*
2278 * We want to commit periodically so that not too much
2279 * unwritten data builds up.
2280 */
2281static void do_waker(struct work_struct *ws)
2282{
2283 struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
2284 wake_worker(pool);
2285 queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
2286}
2287
Joe Thornber85ad643b2014-05-09 15:59:38 +01002288/*
2289 * We're holding onto IO to allow userland time to react. After the
2290 * timeout either the pool will have been resized (and thus back in
2291 * PM_WRITE mode), or we degrade to PM_READ_ONLY and start erroring IO.
2292 */
2293static void do_no_space_timeout(struct work_struct *ws)
2294{
2295 struct pool *pool = container_of(to_delayed_work(ws), struct pool,
2296 no_space_timeout);
2297
2298 if (get_pool_mode(pool) == PM_OUT_OF_DATA_SPACE && !pool->pf.error_if_no_space)
2299 set_pool_mode(pool, PM_READ_ONLY);
2300}
2301
Joe Thornber991d9fa2011-10-31 20:21:18 +00002302/*----------------------------------------------------------------*/
2303
Joe Thornbere7a3e872014-05-13 16:14:14 -04002304struct pool_work {
Joe Thornber738211f2014-03-03 15:52:28 +00002305 struct work_struct worker;
Joe Thornbere7a3e872014-05-13 16:14:14 -04002306 struct completion complete;
Joe Thornber738211f2014-03-03 15:52:28 +00002307};
2308
Joe Thornbere7a3e872014-05-13 16:14:14 -04002309static struct pool_work *to_pool_work(struct work_struct *ws)
Joe Thornber738211f2014-03-03 15:52:28 +00002310{
Joe Thornbere7a3e872014-05-13 16:14:14 -04002311 return container_of(ws, struct pool_work, worker);
2312}
2313
2314static void pool_work_complete(struct pool_work *pw)
2315{
2316 complete(&pw->complete);
2317}
2318
2319static void pool_work_wait(struct pool_work *pw, struct pool *pool,
2320 void (*fn)(struct work_struct *))
2321{
2322 INIT_WORK_ONSTACK(&pw->worker, fn);
2323 init_completion(&pw->complete);
2324 queue_work(pool->wq, &pw->worker);
2325 wait_for_completion(&pw->complete);
2326}
2327
2328/*----------------------------------------------------------------*/
2329
2330struct noflush_work {
2331 struct pool_work pw;
2332 struct thin_c *tc;
2333};
2334
2335static struct noflush_work *to_noflush(struct work_struct *ws)
2336{
2337 return container_of(to_pool_work(ws), struct noflush_work, pw);
Joe Thornber738211f2014-03-03 15:52:28 +00002338}
2339
2340static void do_noflush_start(struct work_struct *ws)
2341{
Joe Thornbere7a3e872014-05-13 16:14:14 -04002342 struct noflush_work *w = to_noflush(ws);
Joe Thornber738211f2014-03-03 15:52:28 +00002343 w->tc->requeue_mode = true;
2344 requeue_io(w->tc);
Joe Thornbere7a3e872014-05-13 16:14:14 -04002345 pool_work_complete(&w->pw);
Joe Thornber738211f2014-03-03 15:52:28 +00002346}
2347
2348static void do_noflush_stop(struct work_struct *ws)
2349{
Joe Thornbere7a3e872014-05-13 16:14:14 -04002350 struct noflush_work *w = to_noflush(ws);
Joe Thornber738211f2014-03-03 15:52:28 +00002351 w->tc->requeue_mode = false;
Joe Thornbere7a3e872014-05-13 16:14:14 -04002352 pool_work_complete(&w->pw);
Joe Thornber738211f2014-03-03 15:52:28 +00002353}
2354
2355static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *))
2356{
2357 struct noflush_work w;
2358
Joe Thornber738211f2014-03-03 15:52:28 +00002359 w.tc = tc;
Joe Thornbere7a3e872014-05-13 16:14:14 -04002360 pool_work_wait(&w.pw, tc->pool, fn);
Joe Thornber738211f2014-03-03 15:52:28 +00002361}
2362
2363/*----------------------------------------------------------------*/
2364
Joe Thornbere49e5822012-07-27 15:08:16 +01002365static enum pool_mode get_pool_mode(struct pool *pool)
2366{
2367 return pool->pf.mode;
2368}
2369
Joe Thornber3e1a0692014-03-03 16:03:26 +00002370static void notify_of_pool_mode_change(struct pool *pool, const char *new_mode)
2371{
2372 dm_table_event(pool->ti->table);
2373 DMINFO("%s: switching pool to %s mode",
2374 dm_device_name(pool->pool_md), new_mode);
2375}
2376
Joe Thornber34fbcf62015-04-16 12:58:35 +01002377static bool passdown_enabled(struct pool_c *pt)
2378{
2379 return pt->adjusted_pf.discard_passdown;
2380}
2381
2382static void set_discard_callbacks(struct pool *pool)
2383{
2384 struct pool_c *pt = pool->ti->private;
2385
2386 if (passdown_enabled(pt)) {
2387 pool->process_discard_cell = process_discard_cell_passdown;
2388 pool->process_prepared_discard = process_prepared_discard_passdown;
2389 } else {
2390 pool->process_discard_cell = process_discard_cell_no_passdown;
2391 pool->process_prepared_discard = process_prepared_discard_no_passdown;
2392 }
2393}
2394
Mike Snitzer8b64e882013-12-20 14:27:28 -05002395static void set_pool_mode(struct pool *pool, enum pool_mode new_mode)
Joe Thornbere49e5822012-07-27 15:08:16 +01002396{
Mike Snitzercdc2b412014-02-14 18:10:55 -05002397 struct pool_c *pt = pool->ti->private;
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002398 bool needs_check = dm_pool_metadata_needs_check(pool->pmd);
2399 enum pool_mode old_mode = get_pool_mode(pool);
Mike Snitzer80c57892014-05-20 13:38:33 -04002400 unsigned long no_space_timeout = ACCESS_ONCE(no_space_timeout_secs) * HZ;
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002401
2402 /*
2403 * Never allow the pool to transition to PM_WRITE mode if user
2404 * intervention is required to verify metadata and data consistency.
2405 */
2406 if (new_mode == PM_WRITE && needs_check) {
2407 DMERR("%s: unable to switch pool to write mode until repaired.",
2408 dm_device_name(pool->pool_md));
2409 if (old_mode != new_mode)
2410 new_mode = old_mode;
2411 else
2412 new_mode = PM_READ_ONLY;
2413 }
2414 /*
2415 * If we were in PM_FAIL mode, rollback of metadata failed. We're
2416 * not going to recover without a thin_repair. So we never let the
2417 * pool move out of the old mode.
2418 */
2419 if (old_mode == PM_FAIL)
2420 new_mode = old_mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002421
Mike Snitzer8b64e882013-12-20 14:27:28 -05002422 switch (new_mode) {
Joe Thornbere49e5822012-07-27 15:08:16 +01002423 case PM_FAIL:
Mike Snitzer8b64e882013-12-20 14:27:28 -05002424 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00002425 notify_of_pool_mode_change(pool, "failure");
Joe Thornber5383ef32013-12-04 16:30:01 -05002426 dm_pool_metadata_read_only(pool->pmd);
Joe Thornbere49e5822012-07-27 15:08:16 +01002427 pool->process_bio = process_bio_fail;
2428 pool->process_discard = process_bio_fail;
Joe Thornbera374bb22014-10-10 13:43:14 +01002429 pool->process_cell = process_cell_fail;
2430 pool->process_discard_cell = process_cell_fail;
Joe Thornbere49e5822012-07-27 15:08:16 +01002431 pool->process_prepared_mapping = process_prepared_mapping_fail;
2432 pool->process_prepared_discard = process_prepared_discard_fail;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002433
2434 error_retry_list(pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01002435 break;
2436
2437 case PM_READ_ONLY:
Mike Snitzer8b64e882013-12-20 14:27:28 -05002438 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00002439 notify_of_pool_mode_change(pool, "read-only");
2440 dm_pool_metadata_read_only(pool->pmd);
2441 pool->process_bio = process_bio_read_only;
2442 pool->process_discard = process_bio_success;
Joe Thornbera374bb22014-10-10 13:43:14 +01002443 pool->process_cell = process_cell_read_only;
2444 pool->process_discard_cell = process_cell_success;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002445 pool->process_prepared_mapping = process_prepared_mapping_fail;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002446 pool->process_prepared_discard = process_prepared_discard_success;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002447
2448 error_retry_list(pool);
2449 break;
2450
2451 case PM_OUT_OF_DATA_SPACE:
2452 /*
2453 * Ideally we'd never hit this state; the low water mark
2454 * would trigger userland to extend the pool before we
2455 * completely run out of data space. However, many small
2456 * IOs to unprovisioned space can consume data space at an
2457 * alarming rate. Adjust your low water mark if you're
2458 * frequently seeing this mode.
2459 */
2460 if (old_mode != new_mode)
2461 notify_of_pool_mode_change(pool, "out-of-data-space");
2462 pool->process_bio = process_bio_read_only;
Joe Thornbera374bb22014-10-10 13:43:14 +01002463 pool->process_discard = process_discard_bio;
2464 pool->process_cell = process_cell_read_only;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002465 pool->process_prepared_mapping = process_prepared_mapping;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002466 set_discard_callbacks(pool);
Joe Thornber85ad643b2014-05-09 15:59:38 +01002467
Mike Snitzer80c57892014-05-20 13:38:33 -04002468 if (!pool->pf.error_if_no_space && no_space_timeout)
2469 queue_delayed_work(pool->wq, &pool->no_space_timeout, no_space_timeout);
Joe Thornbere49e5822012-07-27 15:08:16 +01002470 break;
2471
2472 case PM_WRITE:
Mike Snitzer8b64e882013-12-20 14:27:28 -05002473 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00002474 notify_of_pool_mode_change(pool, "write");
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002475 dm_pool_metadata_read_write(pool->pmd);
Joe Thornbere49e5822012-07-27 15:08:16 +01002476 pool->process_bio = process_bio;
Joe Thornbera374bb22014-10-10 13:43:14 +01002477 pool->process_discard = process_discard_bio;
2478 pool->process_cell = process_cell;
Joe Thornbere49e5822012-07-27 15:08:16 +01002479 pool->process_prepared_mapping = process_prepared_mapping;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002480 set_discard_callbacks(pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01002481 break;
2482 }
Mike Snitzer8b64e882013-12-20 14:27:28 -05002483
2484 pool->pf.mode = new_mode;
Mike Snitzercdc2b412014-02-14 18:10:55 -05002485 /*
2486 * The pool mode may have changed, sync it so bind_control_target()
2487 * doesn't cause an unexpected mode transition on resume.
2488 */
2489 pt->adjusted_pf.mode = new_mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002490}
2491
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002492static void abort_transaction(struct pool *pool)
2493{
2494 const char *dev_name = dm_device_name(pool->pool_md);
2495
2496 DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
2497 if (dm_pool_abort_metadata(pool->pmd)) {
2498 DMERR("%s: failed to abort metadata transaction", dev_name);
2499 set_pool_mode(pool, PM_FAIL);
2500 }
2501
2502 if (dm_pool_metadata_set_needs_check(pool->pmd)) {
2503 DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
2504 set_pool_mode(pool, PM_FAIL);
2505 }
2506}
2507
Joe Thornberb5330652013-12-04 19:51:33 -05002508static void metadata_operation_failed(struct pool *pool, const char *op, int r)
2509{
2510 DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
2511 dm_device_name(pool->pool_md), op, r);
2512
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002513 abort_transaction(pool);
Joe Thornberb5330652013-12-04 19:51:33 -05002514 set_pool_mode(pool, PM_READ_ONLY);
2515}
2516
Joe Thornbere49e5822012-07-27 15:08:16 +01002517/*----------------------------------------------------------------*/
2518
Joe Thornber991d9fa2011-10-31 20:21:18 +00002519/*
2520 * Mapping functions.
2521 */
2522
2523/*
2524 * Called only while mapping a thin bio to hand it over to the workqueue.
2525 */
2526static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
2527{
2528 unsigned long flags;
2529 struct pool *pool = tc->pool;
2530
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002531 spin_lock_irqsave(&tc->lock, flags);
2532 bio_list_add(&tc->deferred_bio_list, bio);
2533 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002534
2535 wake_worker(pool);
2536}
2537
Joe Thornber7d327fe2014-10-06 15:45:59 +01002538static void thin_defer_bio_with_throttle(struct thin_c *tc, struct bio *bio)
2539{
2540 struct pool *pool = tc->pool;
2541
2542 throttle_lock(&pool->throttle);
2543 thin_defer_bio(tc, bio);
2544 throttle_unlock(&pool->throttle);
2545}
2546
Joe Thornbera374bb22014-10-10 13:43:14 +01002547static void thin_defer_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
2548{
2549 unsigned long flags;
2550 struct pool *pool = tc->pool;
2551
2552 throttle_lock(&pool->throttle);
2553 spin_lock_irqsave(&tc->lock, flags);
2554 list_add_tail(&cell->user_list, &tc->deferred_cells);
2555 spin_unlock_irqrestore(&tc->lock, flags);
2556 throttle_unlock(&pool->throttle);
2557
2558 wake_worker(pool);
2559}
2560
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002561static void thin_hook_bio(struct thin_c *tc, struct bio *bio)
Joe Thornbereb2aa482012-03-28 18:41:28 +01002562{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002563 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01002564
2565 h->tc = tc;
2566 h->shared_read_entry = NULL;
Joe Thornbere8088072012-12-21 20:23:31 +00002567 h->all_io_entry = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01002568 h->overwrite_mapping = NULL;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002569 h->cell = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01002570}
2571
Joe Thornber991d9fa2011-10-31 20:21:18 +00002572/*
2573 * Non-blocking function called from the thin target's map function.
2574 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002575static int thin_bio_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002576{
2577 int r;
2578 struct thin_c *tc = ti->private;
2579 dm_block_t block = get_bio_block(tc, bio);
2580 struct dm_thin_device *td = tc->td;
2581 struct dm_thin_lookup_result result;
Joe Thornbera374bb22014-10-10 13:43:14 +01002582 struct dm_bio_prison_cell *virt_cell, *data_cell;
Joe Thornbere8088072012-12-21 20:23:31 +00002583 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002584
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002585 thin_hook_bio(tc, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01002586
Joe Thornber738211f2014-03-03 15:52:28 +00002587 if (tc->requeue_mode) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002588 bio->bi_error = DM_ENDIO_REQUEUE;
2589 bio_endio(bio);
Joe Thornber738211f2014-03-03 15:52:28 +00002590 return DM_MAPIO_SUBMITTED;
2591 }
2592
Joe Thornbere49e5822012-07-27 15:08:16 +01002593 if (get_pool_mode(tc->pool) == PM_FAIL) {
2594 bio_io_error(bio);
2595 return DM_MAPIO_SUBMITTED;
2596 }
2597
Joe Thornber104655f2012-03-28 18:41:28 +01002598 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) {
Joe Thornber7d327fe2014-10-06 15:45:59 +01002599 thin_defer_bio_with_throttle(tc, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002600 return DM_MAPIO_SUBMITTED;
2601 }
2602
Joe Thornberc822ed92014-10-10 09:41:09 +01002603 /*
2604 * We must hold the virtual cell before doing the lookup, otherwise
2605 * there's a race with discard.
2606 */
2607 build_virtual_key(tc->td, block, &key);
Joe Thornbera374bb22014-10-10 13:43:14 +01002608 if (bio_detain(tc->pool, &key, bio, &virt_cell))
Joe Thornberc822ed92014-10-10 09:41:09 +01002609 return DM_MAPIO_SUBMITTED;
2610
Joe Thornber991d9fa2011-10-31 20:21:18 +00002611 r = dm_thin_find_block(td, block, 0, &result);
2612
2613 /*
2614 * Note that we defer readahead too.
2615 */
2616 switch (r) {
2617 case 0:
2618 if (unlikely(result.shared)) {
2619 /*
2620 * We have a race condition here between the
2621 * result.shared value returned by the lookup and
2622 * snapshot creation, which may cause new
2623 * sharing.
2624 *
2625 * To avoid this always quiesce the origin before
2626 * taking the snap. You want to do this anyway to
2627 * ensure a consistent application view
2628 * (i.e. lockfs).
2629 *
2630 * More distant ancestors are irrelevant. The
2631 * shared flag will be set in their case.
2632 */
Joe Thornbera374bb22014-10-10 13:43:14 +01002633 thin_defer_cell(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002634 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002635 }
Joe Thornbere8088072012-12-21 20:23:31 +00002636
Joe Thornbere8088072012-12-21 20:23:31 +00002637 build_data_key(tc->td, result.block, &key);
Joe Thornbera374bb22014-10-10 13:43:14 +01002638 if (bio_detain(tc->pool, &key, bio, &data_cell)) {
2639 cell_defer_no_holder(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002640 return DM_MAPIO_SUBMITTED;
2641 }
2642
2643 inc_all_io_entry(tc->pool, bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01002644 cell_defer_no_holder(tc, data_cell);
2645 cell_defer_no_holder(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002646
2647 remap(tc, bio, result.block);
2648 return DM_MAPIO_REMAPPED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002649
2650 case -ENODATA:
Joe Thornbere49e5822012-07-27 15:08:16 +01002651 case -EWOULDBLOCK:
Joe Thornbera374bb22014-10-10 13:43:14 +01002652 thin_defer_cell(tc, virt_cell);
Joe Thornber2aab3852012-12-21 20:23:33 +00002653 return DM_MAPIO_SUBMITTED;
Joe Thornbere49e5822012-07-27 15:08:16 +01002654
2655 default:
2656 /*
2657 * Must always call bio_io_error on failure.
2658 * dm_thin_find_block can fail with -EINVAL if the
2659 * pool is switched to fail-io mode.
2660 */
2661 bio_io_error(bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01002662 cell_defer_no_holder(tc, virt_cell);
Joe Thornber2aab3852012-12-21 20:23:33 +00002663 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002664 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002665}
2666
2667static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
2668{
Joe Thornber991d9fa2011-10-31 20:21:18 +00002669 struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
Mike Snitzer760fe672014-03-20 08:36:47 -04002670 struct request_queue *q;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002671
Mike Snitzer760fe672014-03-20 08:36:47 -04002672 if (get_pool_mode(pt->pool) == PM_OUT_OF_DATA_SPACE)
2673 return 1;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002674
Mike Snitzer760fe672014-03-20 08:36:47 -04002675 q = bdev_get_queue(pt->data_dev->bdev);
2676 return bdi_congested(&q->backing_dev_info, bdi_bits);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002677}
2678
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002679static void requeue_bios(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002680{
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002681 unsigned long flags;
2682 struct thin_c *tc;
2683
2684 rcu_read_lock();
2685 list_for_each_entry_rcu(tc, &pool->active_thins, list) {
2686 spin_lock_irqsave(&tc->lock, flags);
2687 bio_list_merge(&tc->deferred_bio_list, &tc->retry_on_resume_list);
2688 bio_list_init(&tc->retry_on_resume_list);
2689 spin_unlock_irqrestore(&tc->lock, flags);
2690 }
2691 rcu_read_unlock();
Joe Thornber991d9fa2011-10-31 20:21:18 +00002692}
2693
2694/*----------------------------------------------------------------
2695 * Binding of control targets to a pool object
2696 *--------------------------------------------------------------*/
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002697static bool data_dev_supports_discard(struct pool_c *pt)
2698{
2699 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
2700
2701 return q && blk_queue_discard(q);
2702}
2703
Joe Thornber58051b92013-03-20 17:21:25 +00002704static bool is_factor(sector_t block_size, uint32_t n)
2705{
2706 return !sector_div(block_size, n);
2707}
2708
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002709/*
2710 * If discard_passdown was enabled verify that the data device
Mike Snitzer0424caa2012-09-26 23:45:47 +01002711 * supports discards. Disable discard_passdown if not.
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002712 */
Mike Snitzer0424caa2012-09-26 23:45:47 +01002713static void disable_passdown_if_not_supported(struct pool_c *pt)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002714{
Mike Snitzer0424caa2012-09-26 23:45:47 +01002715 struct pool *pool = pt->pool;
2716 struct block_device *data_bdev = pt->data_dev->bdev;
2717 struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
Mike Snitzer0424caa2012-09-26 23:45:47 +01002718 const char *reason = NULL;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002719 char buf[BDEVNAME_SIZE];
2720
Mike Snitzer0424caa2012-09-26 23:45:47 +01002721 if (!pt->adjusted_pf.discard_passdown)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002722 return;
2723
Mike Snitzer0424caa2012-09-26 23:45:47 +01002724 if (!data_dev_supports_discard(pt))
2725 reason = "discard unsupported";
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002726
Mike Snitzer0424caa2012-09-26 23:45:47 +01002727 else if (data_limits->max_discard_sectors < pool->sectors_per_block)
2728 reason = "max discard sectors smaller than a block";
2729
Mike Snitzer0424caa2012-09-26 23:45:47 +01002730 if (reason) {
2731 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
2732 pt->adjusted_pf.discard_passdown = false;
2733 }
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002734}
2735
Joe Thornber991d9fa2011-10-31 20:21:18 +00002736static int bind_control_target(struct pool *pool, struct dm_target *ti)
2737{
2738 struct pool_c *pt = ti->private;
2739
Joe Thornbere49e5822012-07-27 15:08:16 +01002740 /*
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002741 * We want to make sure that a pool in PM_FAIL mode is never upgraded.
Joe Thornbere49e5822012-07-27 15:08:16 +01002742 */
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002743 enum pool_mode old_mode = get_pool_mode(pool);
Mike Snitzer0424caa2012-09-26 23:45:47 +01002744 enum pool_mode new_mode = pt->adjusted_pf.mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002745
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002746 /*
Mike Snitzer8b64e882013-12-20 14:27:28 -05002747 * Don't change the pool's mode until set_pool_mode() below.
2748 * Otherwise the pool's process_* function pointers may
2749 * not match the desired pool mode.
2750 */
2751 pt->adjusted_pf.mode = old_mode;
2752
2753 pool->ti = ti;
2754 pool->pf = pt->adjusted_pf;
2755 pool->low_water_blocks = pt->low_water_blocks;
2756
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002757 set_pool_mode(pool, new_mode);
Mike Snitzerf4026932012-05-19 01:01:01 +01002758
Joe Thornber991d9fa2011-10-31 20:21:18 +00002759 return 0;
2760}
2761
2762static void unbind_control_target(struct pool *pool, struct dm_target *ti)
2763{
2764 if (pool->ti == ti)
2765 pool->ti = NULL;
2766}
2767
2768/*----------------------------------------------------------------
2769 * Pool creation
2770 *--------------------------------------------------------------*/
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002771/* Initialize pool features. */
2772static void pool_features_init(struct pool_features *pf)
2773{
Joe Thornbere49e5822012-07-27 15:08:16 +01002774 pf->mode = PM_WRITE;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002775 pf->zero_new_blocks = true;
2776 pf->discard_enabled = true;
2777 pf->discard_passdown = true;
Mike Snitzer787a996c2013-12-06 16:21:43 -05002778 pf->error_if_no_space = false;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002779}
2780
Joe Thornber991d9fa2011-10-31 20:21:18 +00002781static void __pool_destroy(struct pool *pool)
2782{
2783 __pool_table_remove(pool);
2784
2785 if (dm_pool_metadata_close(pool->pmd) < 0)
2786 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2787
Mike Snitzer44feb382012-10-12 21:02:10 +01002788 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002789 dm_kcopyd_client_destroy(pool->copier);
2790
2791 if (pool->wq)
2792 destroy_workqueue(pool->wq);
2793
2794 if (pool->next_mapping)
2795 mempool_free(pool->next_mapping, pool->mapping_pool);
2796 mempool_destroy(pool->mapping_pool);
Mike Snitzer44feb382012-10-12 21:02:10 +01002797 dm_deferred_set_destroy(pool->shared_read_ds);
2798 dm_deferred_set_destroy(pool->all_io_ds);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002799 kfree(pool);
2800}
2801
Mike Snitzera24c2562012-06-03 00:30:00 +01002802static struct kmem_cache *_new_mapping_cache;
Mike Snitzera24c2562012-06-03 00:30:00 +01002803
Joe Thornber991d9fa2011-10-31 20:21:18 +00002804static struct pool *pool_create(struct mapped_device *pool_md,
2805 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002806 unsigned long block_size,
2807 int read_only, char **error)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002808{
2809 int r;
2810 void *err_p;
2811 struct pool *pool;
2812 struct dm_pool_metadata *pmd;
Joe Thornbere49e5822012-07-27 15:08:16 +01002813 bool format_device = read_only ? false : true;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002814
Joe Thornbere49e5822012-07-27 15:08:16 +01002815 pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002816 if (IS_ERR(pmd)) {
2817 *error = "Error creating metadata object";
2818 return (struct pool *)pmd;
2819 }
2820
2821 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
2822 if (!pool) {
2823 *error = "Error allocating memory for pool";
2824 err_p = ERR_PTR(-ENOMEM);
2825 goto bad_pool;
2826 }
2827
2828 pool->pmd = pmd;
2829 pool->sectors_per_block = block_size;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +01002830 if (block_size & (block_size - 1))
2831 pool->sectors_per_block_shift = -1;
2832 else
2833 pool->sectors_per_block_shift = __ffs(block_size);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002834 pool->low_water_blocks = 0;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002835 pool_features_init(&pool->pf);
Joe Thornbera195db22014-10-06 16:30:06 -04002836 pool->prison = dm_bio_prison_create();
Joe Thornber991d9fa2011-10-31 20:21:18 +00002837 if (!pool->prison) {
2838 *error = "Error creating pool's bio prison";
2839 err_p = ERR_PTR(-ENOMEM);
2840 goto bad_prison;
2841 }
2842
Mikulas Patockadf5d2e92013-03-01 22:45:49 +00002843 pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002844 if (IS_ERR(pool->copier)) {
2845 r = PTR_ERR(pool->copier);
2846 *error = "Error creating pool's kcopyd client";
2847 err_p = ERR_PTR(r);
2848 goto bad_kcopyd_client;
2849 }
2850
2851 /*
2852 * Create singlethreaded workqueue that will service all devices
2853 * that use this metadata.
2854 */
2855 pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2856 if (!pool->wq) {
2857 *error = "Error creating pool's workqueue";
2858 err_p = ERR_PTR(-ENOMEM);
2859 goto bad_wq;
2860 }
2861
Joe Thornber7d327fe2014-10-06 15:45:59 +01002862 throttle_init(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002863 INIT_WORK(&pool->worker, do_worker);
Joe Thornber905e51b2012-03-28 18:41:27 +01002864 INIT_DELAYED_WORK(&pool->waker, do_waker);
Joe Thornber85ad643b2014-05-09 15:59:38 +01002865 INIT_DELAYED_WORK(&pool->no_space_timeout, do_no_space_timeout);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002866 spin_lock_init(&pool->lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002867 bio_list_init(&pool->deferred_flush_bios);
2868 INIT_LIST_HEAD(&pool->prepared_mappings);
Joe Thornber104655f2012-03-28 18:41:28 +01002869 INIT_LIST_HEAD(&pool->prepared_discards);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002870 INIT_LIST_HEAD(&pool->active_thins);
Joe Thornber88a66212013-12-04 20:16:12 -05002871 pool->low_water_triggered = false;
Mike Snitzer80e96c52014-11-07 15:09:46 -05002872 pool->suspended = true;
Mike Snitzer44feb382012-10-12 21:02:10 +01002873
2874 pool->shared_read_ds = dm_deferred_set_create();
2875 if (!pool->shared_read_ds) {
2876 *error = "Error creating pool's shared read deferred set";
2877 err_p = ERR_PTR(-ENOMEM);
2878 goto bad_shared_read_ds;
2879 }
2880
2881 pool->all_io_ds = dm_deferred_set_create();
2882 if (!pool->all_io_ds) {
2883 *error = "Error creating pool's all io deferred set";
2884 err_p = ERR_PTR(-ENOMEM);
2885 goto bad_all_io_ds;
2886 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002887
2888 pool->next_mapping = NULL;
Mike Snitzera24c2562012-06-03 00:30:00 +01002889 pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
2890 _new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002891 if (!pool->mapping_pool) {
2892 *error = "Error creating pool's mapping mempool";
2893 err_p = ERR_PTR(-ENOMEM);
2894 goto bad_mapping_pool;
2895 }
2896
Joe Thornber991d9fa2011-10-31 20:21:18 +00002897 pool->ref_count = 1;
Joe Thornber905e51b2012-03-28 18:41:27 +01002898 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002899 pool->pool_md = pool_md;
2900 pool->md_dev = metadata_dev;
2901 __pool_table_insert(pool);
2902
2903 return pool;
2904
Joe Thornber991d9fa2011-10-31 20:21:18 +00002905bad_mapping_pool:
Mike Snitzer44feb382012-10-12 21:02:10 +01002906 dm_deferred_set_destroy(pool->all_io_ds);
2907bad_all_io_ds:
2908 dm_deferred_set_destroy(pool->shared_read_ds);
2909bad_shared_read_ds:
Joe Thornber991d9fa2011-10-31 20:21:18 +00002910 destroy_workqueue(pool->wq);
2911bad_wq:
2912 dm_kcopyd_client_destroy(pool->copier);
2913bad_kcopyd_client:
Mike Snitzer44feb382012-10-12 21:02:10 +01002914 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002915bad_prison:
2916 kfree(pool);
2917bad_pool:
2918 if (dm_pool_metadata_close(pmd))
2919 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2920
2921 return err_p;
2922}
2923
2924static void __pool_inc(struct pool *pool)
2925{
2926 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2927 pool->ref_count++;
2928}
2929
2930static void __pool_dec(struct pool *pool)
2931{
2932 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2933 BUG_ON(!pool->ref_count);
2934 if (!--pool->ref_count)
2935 __pool_destroy(pool);
2936}
2937
2938static struct pool *__pool_find(struct mapped_device *pool_md,
2939 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002940 unsigned long block_size, int read_only,
2941 char **error, int *created)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002942{
2943 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
2944
2945 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01002946 if (pool->pool_md != pool_md) {
2947 *error = "metadata device already in use by a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00002948 return ERR_PTR(-EBUSY);
Mike Snitzerf09996c2012-07-27 15:07:59 +01002949 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002950 __pool_inc(pool);
2951
2952 } else {
2953 pool = __pool_table_lookup(pool_md);
2954 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01002955 if (pool->md_dev != metadata_dev) {
2956 *error = "different pool cannot replace a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00002957 return ERR_PTR(-EINVAL);
Mike Snitzerf09996c2012-07-27 15:07:59 +01002958 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002959 __pool_inc(pool);
2960
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002961 } else {
Joe Thornbere49e5822012-07-27 15:08:16 +01002962 pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002963 *created = 1;
2964 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002965 }
2966
2967 return pool;
2968}
2969
2970/*----------------------------------------------------------------
2971 * Pool target methods
2972 *--------------------------------------------------------------*/
2973static void pool_dtr(struct dm_target *ti)
2974{
2975 struct pool_c *pt = ti->private;
2976
2977 mutex_lock(&dm_thin_pool_table.mutex);
2978
2979 unbind_control_target(pt->pool, ti);
2980 __pool_dec(pt->pool);
2981 dm_put_device(ti, pt->metadata_dev);
2982 dm_put_device(ti, pt->data_dev);
2983 kfree(pt);
2984
2985 mutex_unlock(&dm_thin_pool_table.mutex);
2986}
2987
Joe Thornber991d9fa2011-10-31 20:21:18 +00002988static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
2989 struct dm_target *ti)
2990{
2991 int r;
2992 unsigned argc;
2993 const char *arg_name;
2994
2995 static struct dm_arg _args[] = {
Mike Snitzer74aa45c2014-01-15 19:07:58 -05002996 {0, 4, "Invalid number of pool feature arguments"},
Joe Thornber991d9fa2011-10-31 20:21:18 +00002997 };
2998
2999 /*
3000 * No feature arguments supplied.
3001 */
3002 if (!as->argc)
3003 return 0;
3004
3005 r = dm_read_arg_group(_args, as, &argc, &ti->error);
3006 if (r)
3007 return -EINVAL;
3008
3009 while (argc && !r) {
3010 arg_name = dm_shift_arg(as);
3011 argc--;
3012
Joe Thornbere49e5822012-07-27 15:08:16 +01003013 if (!strcasecmp(arg_name, "skip_block_zeroing"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003014 pf->zero_new_blocks = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003015
Joe Thornbere49e5822012-07-27 15:08:16 +01003016 else if (!strcasecmp(arg_name, "ignore_discard"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003017 pf->discard_enabled = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01003018
3019 else if (!strcasecmp(arg_name, "no_discard_passdown"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003020 pf->discard_passdown = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01003021
3022 else if (!strcasecmp(arg_name, "read_only"))
3023 pf->mode = PM_READ_ONLY;
3024
Mike Snitzer787a996c2013-12-06 16:21:43 -05003025 else if (!strcasecmp(arg_name, "error_if_no_space"))
3026 pf->error_if_no_space = true;
3027
Joe Thornbere49e5822012-07-27 15:08:16 +01003028 else {
3029 ti->error = "Unrecognised pool feature requested";
3030 r = -EINVAL;
3031 break;
3032 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003033 }
3034
3035 return r;
3036}
3037
Joe Thornberac8c3f32013-05-10 14:37:21 +01003038static void metadata_low_callback(void *context)
3039{
3040 struct pool *pool = context;
3041
3042 DMWARN("%s: reached low water mark for metadata device: sending event.",
3043 dm_device_name(pool->pool_md));
3044
3045 dm_table_event(pool->ti->table);
3046}
3047
Mike Snitzer7d489352014-02-12 23:58:15 -05003048static sector_t get_dev_size(struct block_device *bdev)
Joe Thornberb17446d2013-05-10 14:37:18 +01003049{
Mike Snitzer7d489352014-02-12 23:58:15 -05003050 return i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
3051}
3052
3053static void warn_if_metadata_device_too_big(struct block_device *bdev)
3054{
3055 sector_t metadata_dev_size = get_dev_size(bdev);
Joe Thornberb17446d2013-05-10 14:37:18 +01003056 char buffer[BDEVNAME_SIZE];
3057
Mike Snitzer7d489352014-02-12 23:58:15 -05003058 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
Joe Thornberb17446d2013-05-10 14:37:18 +01003059 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
3060 bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS);
Mike Snitzer7d489352014-02-12 23:58:15 -05003061}
3062
3063static sector_t get_metadata_dev_size(struct block_device *bdev)
3064{
3065 sector_t metadata_dev_size = get_dev_size(bdev);
3066
3067 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS)
3068 metadata_dev_size = THIN_METADATA_MAX_SECTORS;
Joe Thornberb17446d2013-05-10 14:37:18 +01003069
3070 return metadata_dev_size;
3071}
3072
Joe Thornber24347e92013-05-10 14:37:19 +01003073static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev)
3074{
3075 sector_t metadata_dev_size = get_metadata_dev_size(bdev);
3076
Mike Snitzer7d489352014-02-12 23:58:15 -05003077 sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE);
Joe Thornber24347e92013-05-10 14:37:19 +01003078
3079 return metadata_dev_size;
3080}
3081
Joe Thornber991d9fa2011-10-31 20:21:18 +00003082/*
Joe Thornberac8c3f32013-05-10 14:37:21 +01003083 * When a metadata threshold is crossed a dm event is triggered, and
3084 * userland should respond by growing the metadata device. We could let
3085 * userland set the threshold, like we do with the data threshold, but I'm
3086 * not sure they know enough to do this well.
3087 */
3088static dm_block_t calc_metadata_threshold(struct pool_c *pt)
3089{
3090 /*
3091 * 4M is ample for all ops with the possible exception of thin
3092 * device deletion which is harmless if it fails (just retry the
3093 * delete after you've grown the device).
3094 */
3095 dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4;
3096 return min((dm_block_t)1024ULL /* 4M */, quarter);
3097}
3098
3099/*
Joe Thornber991d9fa2011-10-31 20:21:18 +00003100 * thin-pool <metadata dev> <data dev>
3101 * <data block size (sectors)>
3102 * <low water mark (blocks)>
3103 * [<#feature args> [<arg>]*]
3104 *
3105 * Optional feature arguments are:
3106 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003107 * ignore_discard: disable discard
3108 * no_discard_passdown: don't pass discards down to the data device
Mike Snitzer787a996c2013-12-06 16:21:43 -05003109 * read_only: Don't allow any changes to be made to the pool metadata.
3110 * error_if_no_space: error IOs, instead of queueing, if no space.
Joe Thornber991d9fa2011-10-31 20:21:18 +00003111 */
3112static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
3113{
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003114 int r, pool_created = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003115 struct pool_c *pt;
3116 struct pool *pool;
3117 struct pool_features pf;
3118 struct dm_arg_set as;
3119 struct dm_dev *data_dev;
3120 unsigned long block_size;
3121 dm_block_t low_water_blocks;
3122 struct dm_dev *metadata_dev;
Joe Thornber5d0db962013-05-10 14:37:19 +01003123 fmode_t metadata_mode;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003124
3125 /*
3126 * FIXME Remove validation from scope of lock.
3127 */
3128 mutex_lock(&dm_thin_pool_table.mutex);
3129
3130 if (argc < 4) {
3131 ti->error = "Invalid argument count";
3132 r = -EINVAL;
3133 goto out_unlock;
3134 }
Joe Thornber5d0db962013-05-10 14:37:19 +01003135
Joe Thornber991d9fa2011-10-31 20:21:18 +00003136 as.argc = argc;
3137 as.argv = argv;
3138
Joe Thornber5d0db962013-05-10 14:37:19 +01003139 /*
3140 * Set default pool features.
3141 */
3142 pool_features_init(&pf);
3143
3144 dm_consume_args(&as, 4);
3145 r = parse_pool_features(&as, &pf, ti);
3146 if (r)
3147 goto out_unlock;
3148
3149 metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE);
3150 r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003151 if (r) {
3152 ti->error = "Error opening metadata block device";
3153 goto out_unlock;
3154 }
Mike Snitzer7d489352014-02-12 23:58:15 -05003155 warn_if_metadata_device_too_big(metadata_dev->bdev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003156
3157 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
3158 if (r) {
3159 ti->error = "Error getting data device";
3160 goto out_metadata;
3161 }
3162
3163 if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
3164 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
3165 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01003166 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00003167 ti->error = "Invalid block size";
3168 r = -EINVAL;
3169 goto out;
3170 }
3171
3172 if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
3173 ti->error = "Invalid low water mark";
3174 r = -EINVAL;
3175 goto out;
3176 }
3177
Joe Thornber991d9fa2011-10-31 20:21:18 +00003178 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
3179 if (!pt) {
3180 r = -ENOMEM;
3181 goto out;
3182 }
3183
3184 pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
Joe Thornbere49e5822012-07-27 15:08:16 +01003185 block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003186 if (IS_ERR(pool)) {
3187 r = PTR_ERR(pool);
3188 goto out_free_pt;
3189 }
3190
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003191 /*
3192 * 'pool_created' reflects whether this is the first table load.
3193 * Top level discard support is not allowed to be changed after
3194 * initial load. This would require a pool reload to trigger thin
3195 * device changes.
3196 */
3197 if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
3198 ti->error = "Discard support cannot be disabled once enabled";
3199 r = -EINVAL;
3200 goto out_flags_changed;
3201 }
3202
Joe Thornber991d9fa2011-10-31 20:21:18 +00003203 pt->pool = pool;
3204 pt->ti = ti;
3205 pt->metadata_dev = metadata_dev;
3206 pt->data_dev = data_dev;
3207 pt->low_water_blocks = low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +01003208 pt->adjusted_pf = pt->requested_pf = pf;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003209 ti->num_flush_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003210
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003211 /*
3212 * Only need to enable discards if the pool should pass
3213 * them down to the data device. The thin device's discard
3214 * processing will cause mappings to be removed from the btree.
3215 */
Mike Snitzerb60ab992013-09-19 18:49:11 -04003216 ti->discard_zeroes_data_unsupported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003217 if (pf.discard_enabled && pf.discard_passdown) {
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003218 ti->num_discard_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003219
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003220 /*
3221 * Setting 'discards_supported' circumvents the normal
3222 * stacking of discard limits (this keeps the pool and
3223 * thin devices' discard limits consistent).
3224 */
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01003225 ti->discards_supported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003226 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003227 ti->private = pt;
3228
Joe Thornberac8c3f32013-05-10 14:37:21 +01003229 r = dm_pool_register_metadata_threshold(pt->pool->pmd,
3230 calc_metadata_threshold(pt),
3231 metadata_low_callback,
3232 pool);
3233 if (r)
3234 goto out_free_pt;
3235
Joe Thornber991d9fa2011-10-31 20:21:18 +00003236 pt->callbacks.congested_fn = pool_is_congested;
3237 dm_table_add_target_callbacks(ti->table, &pt->callbacks);
3238
3239 mutex_unlock(&dm_thin_pool_table.mutex);
3240
3241 return 0;
3242
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003243out_flags_changed:
3244 __pool_dec(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003245out_free_pt:
3246 kfree(pt);
3247out:
3248 dm_put_device(ti, data_dev);
3249out_metadata:
3250 dm_put_device(ti, metadata_dev);
3251out_unlock:
3252 mutex_unlock(&dm_thin_pool_table.mutex);
3253
3254 return r;
3255}
3256
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003257static int pool_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003258{
3259 int r;
3260 struct pool_c *pt = ti->private;
3261 struct pool *pool = pt->pool;
3262 unsigned long flags;
3263
3264 /*
3265 * As this is a singleton target, ti->begin is always zero.
3266 */
3267 spin_lock_irqsave(&pool->lock, flags);
3268 bio->bi_bdev = pt->data_dev->bdev;
3269 r = DM_MAPIO_REMAPPED;
3270 spin_unlock_irqrestore(&pool->lock, flags);
3271
3272 return r;
3273}
3274
Joe Thornberb17446d2013-05-10 14:37:18 +01003275static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit)
3276{
3277 int r;
3278 struct pool_c *pt = ti->private;
3279 struct pool *pool = pt->pool;
3280 sector_t data_size = ti->len;
3281 dm_block_t sb_data_size;
3282
3283 *need_commit = false;
3284
3285 (void) sector_div(data_size, pool->sectors_per_block);
3286
3287 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
3288 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003289 DMERR("%s: failed to retrieve data device size",
3290 dm_device_name(pool->pool_md));
Joe Thornberb17446d2013-05-10 14:37:18 +01003291 return r;
3292 }
3293
3294 if (data_size < sb_data_size) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003295 DMERR("%s: pool target (%llu blocks) too small: expected %llu",
3296 dm_device_name(pool->pool_md),
Joe Thornberb17446d2013-05-10 14:37:18 +01003297 (unsigned long long)data_size, sb_data_size);
3298 return -EINVAL;
3299
3300 } else if (data_size > sb_data_size) {
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05003301 if (dm_pool_metadata_needs_check(pool->pmd)) {
3302 DMERR("%s: unable to grow the data device until repaired.",
3303 dm_device_name(pool->pool_md));
3304 return 0;
3305 }
3306
Mike Snitzer6f7f51d2013-12-04 10:25:53 -05003307 if (sb_data_size)
3308 DMINFO("%s: growing the data device from %llu to %llu blocks",
3309 dm_device_name(pool->pool_md),
3310 sb_data_size, (unsigned long long)data_size);
Joe Thornberb17446d2013-05-10 14:37:18 +01003311 r = dm_pool_resize_data_dev(pool->pmd, data_size);
3312 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05003313 metadata_operation_failed(pool, "dm_pool_resize_data_dev", r);
Joe Thornberb17446d2013-05-10 14:37:18 +01003314 return r;
3315 }
3316
3317 *need_commit = true;
3318 }
3319
3320 return 0;
3321}
3322
Joe Thornber24347e92013-05-10 14:37:19 +01003323static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit)
3324{
3325 int r;
3326 struct pool_c *pt = ti->private;
3327 struct pool *pool = pt->pool;
3328 dm_block_t metadata_dev_size, sb_metadata_dev_size;
3329
3330 *need_commit = false;
3331
Alasdair G Kergon610bba82013-05-19 18:57:50 +01003332 metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev);
Joe Thornber24347e92013-05-10 14:37:19 +01003333
3334 r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size);
3335 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003336 DMERR("%s: failed to retrieve metadata device size",
3337 dm_device_name(pool->pool_md));
Joe Thornber24347e92013-05-10 14:37:19 +01003338 return r;
3339 }
3340
3341 if (metadata_dev_size < sb_metadata_dev_size) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003342 DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
3343 dm_device_name(pool->pool_md),
Joe Thornber24347e92013-05-10 14:37:19 +01003344 metadata_dev_size, sb_metadata_dev_size);
3345 return -EINVAL;
3346
3347 } else if (metadata_dev_size > sb_metadata_dev_size) {
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05003348 if (dm_pool_metadata_needs_check(pool->pmd)) {
3349 DMERR("%s: unable to grow the metadata device until repaired.",
3350 dm_device_name(pool->pool_md));
3351 return 0;
3352 }
3353
Mike Snitzer7d489352014-02-12 23:58:15 -05003354 warn_if_metadata_device_too_big(pool->md_dev);
Mike Snitzer6f7f51d2013-12-04 10:25:53 -05003355 DMINFO("%s: growing the metadata device from %llu to %llu blocks",
3356 dm_device_name(pool->pool_md),
3357 sb_metadata_dev_size, metadata_dev_size);
Joe Thornber24347e92013-05-10 14:37:19 +01003358 r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size);
3359 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05003360 metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r);
Joe Thornber24347e92013-05-10 14:37:19 +01003361 return r;
3362 }
3363
3364 *need_commit = true;
3365 }
3366
3367 return 0;
3368}
3369
Joe Thornber991d9fa2011-10-31 20:21:18 +00003370/*
3371 * Retrieves the number of blocks of the data device from
3372 * the superblock and compares it to the actual device size,
3373 * thus resizing the data device in case it has grown.
3374 *
3375 * This both copes with opening preallocated data devices in the ctr
3376 * being followed by a resume
3377 * -and-
3378 * calling the resume method individually after userspace has
3379 * grown the data device in reaction to a table event.
3380 */
3381static int pool_preresume(struct dm_target *ti)
3382{
3383 int r;
Joe Thornber24347e92013-05-10 14:37:19 +01003384 bool need_commit1, need_commit2;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003385 struct pool_c *pt = ti->private;
3386 struct pool *pool = pt->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003387
3388 /*
3389 * Take control of the pool object.
3390 */
3391 r = bind_control_target(pool, ti);
3392 if (r)
3393 return r;
3394
Joe Thornberb17446d2013-05-10 14:37:18 +01003395 r = maybe_resize_data_dev(ti, &need_commit1);
3396 if (r)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003397 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003398
Joe Thornber24347e92013-05-10 14:37:19 +01003399 r = maybe_resize_metadata_dev(ti, &need_commit2);
3400 if (r)
3401 return r;
3402
3403 if (need_commit1 || need_commit2)
Joe Thornber020cc3b2013-12-04 15:05:36 -05003404 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003405
3406 return 0;
3407}
3408
Mike Snitzer583024d2014-10-28 20:58:45 -04003409static void pool_suspend_active_thins(struct pool *pool)
3410{
3411 struct thin_c *tc;
3412
3413 /* Suspend all active thin devices */
3414 tc = get_first_thin(pool);
3415 while (tc) {
3416 dm_internal_suspend_noflush(tc->thin_md);
3417 tc = get_next_thin(pool, tc);
3418 }
3419}
3420
3421static void pool_resume_active_thins(struct pool *pool)
3422{
3423 struct thin_c *tc;
3424
3425 /* Resume all active thin devices */
3426 tc = get_first_thin(pool);
3427 while (tc) {
3428 dm_internal_resume(tc->thin_md);
3429 tc = get_next_thin(pool, tc);
3430 }
3431}
3432
Joe Thornber991d9fa2011-10-31 20:21:18 +00003433static void pool_resume(struct dm_target *ti)
3434{
3435 struct pool_c *pt = ti->private;
3436 struct pool *pool = pt->pool;
3437 unsigned long flags;
3438
Mike Snitzer583024d2014-10-28 20:58:45 -04003439 /*
3440 * Must requeue active_thins' bios and then resume
3441 * active_thins _before_ clearing 'suspend' flag.
3442 */
3443 requeue_bios(pool);
3444 pool_resume_active_thins(pool);
3445
Joe Thornber991d9fa2011-10-31 20:21:18 +00003446 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber88a66212013-12-04 20:16:12 -05003447 pool->low_water_triggered = false;
Mike Snitzer80e96c52014-11-07 15:09:46 -05003448 pool->suspended = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003449 spin_unlock_irqrestore(&pool->lock, flags);
Mike Snitzer80e96c52014-11-07 15:09:46 -05003450
Joe Thornber905e51b2012-03-28 18:41:27 +01003451 do_waker(&pool->waker.work);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003452}
3453
Mike Snitzer80e96c52014-11-07 15:09:46 -05003454static void pool_presuspend(struct dm_target *ti)
3455{
3456 struct pool_c *pt = ti->private;
3457 struct pool *pool = pt->pool;
3458 unsigned long flags;
3459
3460 spin_lock_irqsave(&pool->lock, flags);
3461 pool->suspended = true;
3462 spin_unlock_irqrestore(&pool->lock, flags);
Mike Snitzer583024d2014-10-28 20:58:45 -04003463
3464 pool_suspend_active_thins(pool);
Mike Snitzer80e96c52014-11-07 15:09:46 -05003465}
3466
3467static void pool_presuspend_undo(struct dm_target *ti)
3468{
3469 struct pool_c *pt = ti->private;
3470 struct pool *pool = pt->pool;
3471 unsigned long flags;
3472
Mike Snitzer583024d2014-10-28 20:58:45 -04003473 pool_resume_active_thins(pool);
3474
Mike Snitzer80e96c52014-11-07 15:09:46 -05003475 spin_lock_irqsave(&pool->lock, flags);
3476 pool->suspended = false;
3477 spin_unlock_irqrestore(&pool->lock, flags);
3478}
3479
Joe Thornber991d9fa2011-10-31 20:21:18 +00003480static void pool_postsuspend(struct dm_target *ti)
3481{
Joe Thornber991d9fa2011-10-31 20:21:18 +00003482 struct pool_c *pt = ti->private;
3483 struct pool *pool = pt->pool;
3484
Joe Thornber905e51b2012-03-28 18:41:27 +01003485 cancel_delayed_work(&pool->waker);
Joe Thornber85ad643b2014-05-09 15:59:38 +01003486 cancel_delayed_work(&pool->no_space_timeout);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003487 flush_workqueue(pool->wq);
Joe Thornber020cc3b2013-12-04 15:05:36 -05003488 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003489}
3490
3491static int check_arg_count(unsigned argc, unsigned args_required)
3492{
3493 if (argc != args_required) {
3494 DMWARN("Message received with %u arguments instead of %u.",
3495 argc, args_required);
3496 return -EINVAL;
3497 }
3498
3499 return 0;
3500}
3501
3502static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
3503{
3504 if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
3505 *dev_id <= MAX_DEV_ID)
3506 return 0;
3507
3508 if (warning)
3509 DMWARN("Message received with invalid device id: %s", arg);
3510
3511 return -EINVAL;
3512}
3513
3514static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
3515{
3516 dm_thin_id dev_id;
3517 int r;
3518
3519 r = check_arg_count(argc, 2);
3520 if (r)
3521 return r;
3522
3523 r = read_dev_id(argv[1], &dev_id, 1);
3524 if (r)
3525 return r;
3526
3527 r = dm_pool_create_thin(pool->pmd, dev_id);
3528 if (r) {
3529 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
3530 argv[1]);
3531 return r;
3532 }
3533
3534 return 0;
3535}
3536
3537static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3538{
3539 dm_thin_id dev_id;
3540 dm_thin_id origin_dev_id;
3541 int r;
3542
3543 r = check_arg_count(argc, 3);
3544 if (r)
3545 return r;
3546
3547 r = read_dev_id(argv[1], &dev_id, 1);
3548 if (r)
3549 return r;
3550
3551 r = read_dev_id(argv[2], &origin_dev_id, 1);
3552 if (r)
3553 return r;
3554
3555 r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
3556 if (r) {
3557 DMWARN("Creation of new snapshot %s of device %s failed.",
3558 argv[1], argv[2]);
3559 return r;
3560 }
3561
3562 return 0;
3563}
3564
3565static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
3566{
3567 dm_thin_id dev_id;
3568 int r;
3569
3570 r = check_arg_count(argc, 2);
3571 if (r)
3572 return r;
3573
3574 r = read_dev_id(argv[1], &dev_id, 1);
3575 if (r)
3576 return r;
3577
3578 r = dm_pool_delete_thin_device(pool->pmd, dev_id);
3579 if (r)
3580 DMWARN("Deletion of thin device %s failed.", argv[1]);
3581
3582 return r;
3583}
3584
3585static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
3586{
3587 dm_thin_id old_id, new_id;
3588 int r;
3589
3590 r = check_arg_count(argc, 3);
3591 if (r)
3592 return r;
3593
3594 if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
3595 DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
3596 return -EINVAL;
3597 }
3598
3599 if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
3600 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
3601 return -EINVAL;
3602 }
3603
3604 r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
3605 if (r) {
3606 DMWARN("Failed to change transaction id from %s to %s.",
3607 argv[1], argv[2]);
3608 return r;
3609 }
3610
3611 return 0;
3612}
3613
Joe Thornbercc8394d2012-06-03 00:30:01 +01003614static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3615{
3616 int r;
3617
3618 r = check_arg_count(argc, 1);
3619 if (r)
3620 return r;
3621
Joe Thornber020cc3b2013-12-04 15:05:36 -05003622 (void) commit(pool);
Joe Thornber0d200ae2012-07-03 12:55:31 +01003623
Joe Thornbercc8394d2012-06-03 00:30:01 +01003624 r = dm_pool_reserve_metadata_snap(pool->pmd);
3625 if (r)
3626 DMWARN("reserve_metadata_snap message failed.");
3627
3628 return r;
3629}
3630
3631static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3632{
3633 int r;
3634
3635 r = check_arg_count(argc, 1);
3636 if (r)
3637 return r;
3638
3639 r = dm_pool_release_metadata_snap(pool->pmd);
3640 if (r)
3641 DMWARN("release_metadata_snap message failed.");
3642
3643 return r;
3644}
3645
Joe Thornber991d9fa2011-10-31 20:21:18 +00003646/*
3647 * Messages supported:
3648 * create_thin <dev_id>
3649 * create_snap <dev_id> <origin_id>
3650 * delete <dev_id>
Joe Thornber991d9fa2011-10-31 20:21:18 +00003651 * set_transaction_id <current_trans_id> <new_trans_id>
Joe Thornbercc8394d2012-06-03 00:30:01 +01003652 * reserve_metadata_snap
3653 * release_metadata_snap
Joe Thornber991d9fa2011-10-31 20:21:18 +00003654 */
3655static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
3656{
3657 int r = -EINVAL;
3658 struct pool_c *pt = ti->private;
3659 struct pool *pool = pt->pool;
3660
Joe Thornber2a7eaea2015-01-26 11:38:21 +00003661 if (get_pool_mode(pool) >= PM_READ_ONLY) {
3662 DMERR("%s: unable to service pool target messages in READ_ONLY or FAIL mode",
3663 dm_device_name(pool->pool_md));
Mike Snitzerfd467692015-06-09 12:31:26 -04003664 return -EOPNOTSUPP;
Joe Thornber2a7eaea2015-01-26 11:38:21 +00003665 }
3666
Joe Thornber991d9fa2011-10-31 20:21:18 +00003667 if (!strcasecmp(argv[0], "create_thin"))
3668 r = process_create_thin_mesg(argc, argv, pool);
3669
3670 else if (!strcasecmp(argv[0], "create_snap"))
3671 r = process_create_snap_mesg(argc, argv, pool);
3672
3673 else if (!strcasecmp(argv[0], "delete"))
3674 r = process_delete_mesg(argc, argv, pool);
3675
3676 else if (!strcasecmp(argv[0], "set_transaction_id"))
3677 r = process_set_transaction_id_mesg(argc, argv, pool);
3678
Joe Thornbercc8394d2012-06-03 00:30:01 +01003679 else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
3680 r = process_reserve_metadata_snap_mesg(argc, argv, pool);
3681
3682 else if (!strcasecmp(argv[0], "release_metadata_snap"))
3683 r = process_release_metadata_snap_mesg(argc, argv, pool);
3684
Joe Thornber991d9fa2011-10-31 20:21:18 +00003685 else
3686 DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
3687
Joe Thornbere49e5822012-07-27 15:08:16 +01003688 if (!r)
Joe Thornber020cc3b2013-12-04 15:05:36 -05003689 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003690
3691 return r;
3692}
3693
Joe Thornbere49e5822012-07-27 15:08:16 +01003694static void emit_flags(struct pool_features *pf, char *result,
3695 unsigned sz, unsigned maxlen)
3696{
3697 unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
Mike Snitzer787a996c2013-12-06 16:21:43 -05003698 !pf->discard_passdown + (pf->mode == PM_READ_ONLY) +
3699 pf->error_if_no_space;
Joe Thornbere49e5822012-07-27 15:08:16 +01003700 DMEMIT("%u ", count);
3701
3702 if (!pf->zero_new_blocks)
3703 DMEMIT("skip_block_zeroing ");
3704
3705 if (!pf->discard_enabled)
3706 DMEMIT("ignore_discard ");
3707
3708 if (!pf->discard_passdown)
3709 DMEMIT("no_discard_passdown ");
3710
3711 if (pf->mode == PM_READ_ONLY)
3712 DMEMIT("read_only ");
Mike Snitzer787a996c2013-12-06 16:21:43 -05003713
3714 if (pf->error_if_no_space)
3715 DMEMIT("error_if_no_space ");
Joe Thornbere49e5822012-07-27 15:08:16 +01003716}
3717
Joe Thornber991d9fa2011-10-31 20:21:18 +00003718/*
3719 * Status line is:
3720 * <transaction id> <used metadata sectors>/<total metadata sectors>
3721 * <used data sectors>/<total data sectors> <held metadata root>
3722 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003723static void pool_status(struct dm_target *ti, status_type_t type,
3724 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003725{
Joe Thornbere49e5822012-07-27 15:08:16 +01003726 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003727 unsigned sz = 0;
3728 uint64_t transaction_id;
3729 dm_block_t nr_free_blocks_data;
3730 dm_block_t nr_free_blocks_metadata;
3731 dm_block_t nr_blocks_data;
3732 dm_block_t nr_blocks_metadata;
3733 dm_block_t held_root;
3734 char buf[BDEVNAME_SIZE];
3735 char buf2[BDEVNAME_SIZE];
3736 struct pool_c *pt = ti->private;
3737 struct pool *pool = pt->pool;
3738
3739 switch (type) {
3740 case STATUSTYPE_INFO:
Joe Thornbere49e5822012-07-27 15:08:16 +01003741 if (get_pool_mode(pool) == PM_FAIL) {
3742 DMEMIT("Fail");
3743 break;
3744 }
3745
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01003746 /* Commit to ensure statistics aren't out-of-date */
3747 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
Joe Thornber020cc3b2013-12-04 15:05:36 -05003748 (void) commit(pool);
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01003749
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003750 r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id);
3751 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003752 DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
3753 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003754 goto err;
3755 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003756
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003757 r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata);
3758 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003759 DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
3760 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003761 goto err;
3762 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003763
3764 r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003765 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003766 DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
3767 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003768 goto err;
3769 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003770
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003771 r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data);
3772 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003773 DMERR("%s: dm_pool_get_free_block_count returned %d",
3774 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003775 goto err;
3776 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003777
3778 r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003779 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003780 DMERR("%s: dm_pool_get_data_dev_size returned %d",
3781 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003782 goto err;
3783 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003784
Joe Thornbercc8394d2012-06-03 00:30:01 +01003785 r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003786 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003787 DMERR("%s: dm_pool_get_metadata_snap returned %d",
3788 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003789 goto err;
3790 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003791
3792 DMEMIT("%llu %llu/%llu %llu/%llu ",
3793 (unsigned long long)transaction_id,
3794 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
3795 (unsigned long long)nr_blocks_metadata,
3796 (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
3797 (unsigned long long)nr_blocks_data);
3798
3799 if (held_root)
Joe Thornbere49e5822012-07-27 15:08:16 +01003800 DMEMIT("%llu ", held_root);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003801 else
Joe Thornbere49e5822012-07-27 15:08:16 +01003802 DMEMIT("- ");
3803
Joe Thornber3e1a0692014-03-03 16:03:26 +00003804 if (pool->pf.mode == PM_OUT_OF_DATA_SPACE)
3805 DMEMIT("out_of_data_space ");
3806 else if (pool->pf.mode == PM_READ_ONLY)
Joe Thornbere49e5822012-07-27 15:08:16 +01003807 DMEMIT("ro ");
3808 else
3809 DMEMIT("rw ");
3810
Mike Snitzer018debe2012-12-21 20:23:32 +00003811 if (!pool->pf.discard_enabled)
Mike Snitzer787a996c2013-12-06 16:21:43 -05003812 DMEMIT("ignore_discard ");
Mike Snitzer018debe2012-12-21 20:23:32 +00003813 else if (pool->pf.discard_passdown)
Mike Snitzer787a996c2013-12-06 16:21:43 -05003814 DMEMIT("discard_passdown ");
Joe Thornbere49e5822012-07-27 15:08:16 +01003815 else
Mike Snitzer787a996c2013-12-06 16:21:43 -05003816 DMEMIT("no_discard_passdown ");
3817
3818 if (pool->pf.error_if_no_space)
3819 DMEMIT("error_if_no_space ");
3820 else
3821 DMEMIT("queue_if_no_space ");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003822
3823 break;
3824
3825 case STATUSTYPE_TABLE:
3826 DMEMIT("%s %s %lu %llu ",
3827 format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
3828 format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
3829 (unsigned long)pool->sectors_per_block,
3830 (unsigned long long)pt->low_water_blocks);
Mike Snitzer0424caa2012-09-26 23:45:47 +01003831 emit_flags(&pt->requested_pf, result, sz, maxlen);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003832 break;
3833 }
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003834 return;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003835
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003836err:
3837 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003838}
3839
3840static int pool_iterate_devices(struct dm_target *ti,
3841 iterate_devices_callout_fn fn, void *data)
3842{
3843 struct pool_c *pt = ti->private;
3844
3845 return fn(ti, pt->data_dev, 0, ti->len, data);
3846}
3847
3848static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
3849 struct bio_vec *biovec, int max_size)
3850{
3851 struct pool_c *pt = ti->private;
3852 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
3853
3854 if (!q->merge_bvec_fn)
3855 return max_size;
3856
3857 bvm->bi_bdev = pt->data_dev->bdev;
3858
3859 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
3860}
3861
3862static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
3863{
3864 struct pool_c *pt = ti->private;
3865 struct pool *pool = pt->pool;
Mike Snitzer604ea902014-10-09 18:43:25 -04003866 sector_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
3867
3868 /*
Mike Snitzerd200c302014-11-20 18:07:43 -05003869 * If max_sectors is smaller than pool->sectors_per_block adjust it
3870 * to the highest possible power-of-2 factor of pool->sectors_per_block.
3871 * This is especially beneficial when the pool's data device is a RAID
3872 * device that has a full stripe width that matches pool->sectors_per_block
3873 * -- because even though partial RAID stripe-sized IOs will be issued to a
3874 * single RAID stripe; when aggregated they will end on a full RAID stripe
3875 * boundary.. which avoids additional partial RAID stripe writes cascading
Mike Snitzer604ea902014-10-09 18:43:25 -04003876 */
Mike Snitzer604ea902014-10-09 18:43:25 -04003877 if (limits->max_sectors < pool->sectors_per_block) {
3878 while (!is_factor(pool->sectors_per_block, limits->max_sectors)) {
3879 if ((limits->max_sectors & (limits->max_sectors - 1)) == 0)
3880 limits->max_sectors--;
3881 limits->max_sectors = rounddown_pow_of_two(limits->max_sectors);
3882 }
Mike Snitzer604ea902014-10-09 18:43:25 -04003883 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003884
Mike Snitzer0cc67cd2013-08-20 15:02:41 -04003885 /*
3886 * If the system-determined stacked limits are compatible with the
3887 * pool's blocksize (io_opt is a factor) do not override them.
3888 */
3889 if (io_opt_sectors < pool->sectors_per_block ||
Mike Snitzer604ea902014-10-09 18:43:25 -04003890 !is_factor(io_opt_sectors, pool->sectors_per_block)) {
3891 if (is_factor(pool->sectors_per_block, limits->max_sectors))
3892 blk_limits_io_min(limits, limits->max_sectors << SECTOR_SHIFT);
3893 else
3894 blk_limits_io_min(limits, pool->sectors_per_block << SECTOR_SHIFT);
Mike Snitzer0cc67cd2013-08-20 15:02:41 -04003895 blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
3896 }
Mike Snitzer0424caa2012-09-26 23:45:47 +01003897
3898 /*
3899 * pt->adjusted_pf is a staging area for the actual features to use.
3900 * They get transferred to the live pool in bind_control_target()
3901 * called from pool_preresume().
3902 */
Mike Snitzerb60ab992013-09-19 18:49:11 -04003903 if (!pt->adjusted_pf.discard_enabled) {
3904 /*
3905 * Must explicitly disallow stacking discard limits otherwise the
3906 * block layer will stack them if pool's data device has support.
3907 * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the
3908 * user to see that, so make sure to set all discard limits to 0.
3909 */
3910 limits->discard_granularity = 0;
Mike Snitzer0424caa2012-09-26 23:45:47 +01003911 return;
Mike Snitzerb60ab992013-09-19 18:49:11 -04003912 }
Mike Snitzer0424caa2012-09-26 23:45:47 +01003913
3914 disable_passdown_if_not_supported(pt);
3915
Joe Thornber34fbcf62015-04-16 12:58:35 +01003916 /*
3917 * The pool uses the same discard limits as the underlying data
3918 * device. DM core has already set this up.
3919 */
Joe Thornber991d9fa2011-10-31 20:21:18 +00003920}
3921
3922static struct target_type pool_target = {
3923 .name = "thin-pool",
3924 .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
3925 DM_TARGET_IMMUTABLE,
Joe Thornber34fbcf62015-04-16 12:58:35 +01003926 .version = {1, 15, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00003927 .module = THIS_MODULE,
3928 .ctr = pool_ctr,
3929 .dtr = pool_dtr,
3930 .map = pool_map,
Mike Snitzer80e96c52014-11-07 15:09:46 -05003931 .presuspend = pool_presuspend,
3932 .presuspend_undo = pool_presuspend_undo,
Joe Thornber991d9fa2011-10-31 20:21:18 +00003933 .postsuspend = pool_postsuspend,
3934 .preresume = pool_preresume,
3935 .resume = pool_resume,
3936 .message = pool_message,
3937 .status = pool_status,
3938 .merge = pool_merge,
3939 .iterate_devices = pool_iterate_devices,
3940 .io_hints = pool_io_hints,
3941};
3942
3943/*----------------------------------------------------------------
3944 * Thin target methods
3945 *--------------------------------------------------------------*/
Joe Thornberb10ebd32014-04-08 11:29:01 +01003946static void thin_get(struct thin_c *tc)
3947{
3948 atomic_inc(&tc->refcount);
3949}
3950
3951static void thin_put(struct thin_c *tc)
3952{
3953 if (atomic_dec_and_test(&tc->refcount))
3954 complete(&tc->can_destroy);
3955}
3956
Joe Thornber991d9fa2011-10-31 20:21:18 +00003957static void thin_dtr(struct dm_target *ti)
3958{
3959 struct thin_c *tc = ti->private;
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003960 unsigned long flags;
3961
3962 spin_lock_irqsave(&tc->pool->lock, flags);
3963 list_del_rcu(&tc->list);
3964 spin_unlock_irqrestore(&tc->pool->lock, flags);
3965 synchronize_rcu();
Joe Thornber991d9fa2011-10-31 20:21:18 +00003966
Mikulas Patocka17181fb2014-11-05 17:00:13 -05003967 thin_put(tc);
3968 wait_for_completion(&tc->can_destroy);
3969
Joe Thornber991d9fa2011-10-31 20:21:18 +00003970 mutex_lock(&dm_thin_pool_table.mutex);
3971
3972 __pool_dec(tc->pool);
3973 dm_pool_close_thin_device(tc->td);
3974 dm_put_device(ti, tc->pool_dev);
Joe Thornber2dd9c252012-03-28 18:41:28 +01003975 if (tc->origin_dev)
3976 dm_put_device(ti, tc->origin_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003977 kfree(tc);
3978
3979 mutex_unlock(&dm_thin_pool_table.mutex);
3980}
3981
3982/*
3983 * Thin target parameters:
3984 *
Joe Thornber2dd9c252012-03-28 18:41:28 +01003985 * <pool_dev> <dev_id> [origin_dev]
Joe Thornber991d9fa2011-10-31 20:21:18 +00003986 *
3987 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
3988 * dev_id: the internal device identifier
Joe Thornber2dd9c252012-03-28 18:41:28 +01003989 * origin_dev: a device external to the pool that should act as the origin
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003990 *
3991 * If the pool device has discards disabled, they get disabled for the thin
3992 * device as well.
Joe Thornber991d9fa2011-10-31 20:21:18 +00003993 */
3994static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
3995{
3996 int r;
3997 struct thin_c *tc;
Joe Thornber2dd9c252012-03-28 18:41:28 +01003998 struct dm_dev *pool_dev, *origin_dev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003999 struct mapped_device *pool_md;
Joe Thornber5e3283e2014-04-08 11:08:41 +01004000 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004001
4002 mutex_lock(&dm_thin_pool_table.mutex);
4003
Joe Thornber2dd9c252012-03-28 18:41:28 +01004004 if (argc != 2 && argc != 3) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00004005 ti->error = "Invalid argument count";
4006 r = -EINVAL;
4007 goto out_unlock;
4008 }
4009
4010 tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
4011 if (!tc) {
4012 ti->error = "Out of memory";
4013 r = -ENOMEM;
4014 goto out_unlock;
4015 }
Mike Snitzer583024d2014-10-28 20:58:45 -04004016 tc->thin_md = dm_table_get_md(ti->table);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004017 spin_lock_init(&tc->lock);
Joe Thornbera374bb22014-10-10 13:43:14 +01004018 INIT_LIST_HEAD(&tc->deferred_cells);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004019 bio_list_init(&tc->deferred_bio_list);
4020 bio_list_init(&tc->retry_on_resume_list);
Mike Snitzer67324ea2014-03-21 18:33:41 -04004021 tc->sort_bio_list = RB_ROOT;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004022
Joe Thornber2dd9c252012-03-28 18:41:28 +01004023 if (argc == 3) {
4024 r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
4025 if (r) {
4026 ti->error = "Error opening origin device";
4027 goto bad_origin_dev;
4028 }
4029 tc->origin_dev = origin_dev;
4030 }
4031
Joe Thornber991d9fa2011-10-31 20:21:18 +00004032 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
4033 if (r) {
4034 ti->error = "Error opening pool device";
4035 goto bad_pool_dev;
4036 }
4037 tc->pool_dev = pool_dev;
4038
4039 if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
4040 ti->error = "Invalid device id";
4041 r = -EINVAL;
4042 goto bad_common;
4043 }
4044
4045 pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
4046 if (!pool_md) {
4047 ti->error = "Couldn't get pool mapped device";
4048 r = -EINVAL;
4049 goto bad_common;
4050 }
4051
4052 tc->pool = __pool_table_lookup(pool_md);
4053 if (!tc->pool) {
4054 ti->error = "Couldn't find pool object";
4055 r = -EINVAL;
4056 goto bad_pool_lookup;
4057 }
4058 __pool_inc(tc->pool);
4059
Joe Thornbere49e5822012-07-27 15:08:16 +01004060 if (get_pool_mode(tc->pool) == PM_FAIL) {
4061 ti->error = "Couldn't open thin device, Pool is in fail mode";
Mike Snitzer1acacc02014-02-19 20:32:33 -05004062 r = -EINVAL;
Mike Snitzer80e96c52014-11-07 15:09:46 -05004063 goto bad_pool;
Joe Thornbere49e5822012-07-27 15:08:16 +01004064 }
4065
Joe Thornber991d9fa2011-10-31 20:21:18 +00004066 r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
4067 if (r) {
4068 ti->error = "Couldn't open thin internal device";
Mike Snitzer80e96c52014-11-07 15:09:46 -05004069 goto bad_pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004070 }
4071
Mike Snitzer542f9032012-07-27 15:08:00 +01004072 r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
4073 if (r)
Mike Snitzer80e96c52014-11-07 15:09:46 -05004074 goto bad;
Mike Snitzer542f9032012-07-27 15:08:00 +01004075
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00004076 ti->num_flush_bios = 1;
Joe Thornber16ad3d12012-07-27 15:08:07 +01004077 ti->flush_supported = true;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00004078 ti->per_bio_data_size = sizeof(struct dm_thin_endio_hook);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01004079
4080 /* In case the pool supports discards, pass them on. */
Mike Snitzerb60ab992013-09-19 18:49:11 -04004081 ti->discard_zeroes_data_unsupported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01004082 if (tc->pool->pf.discard_enabled) {
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01004083 ti->discards_supported = true;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00004084 ti->num_discard_bios = 1;
Joe Thornber34fbcf62015-04-16 12:58:35 +01004085 ti->split_discard_bios = false;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01004086 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00004087
Joe Thornber991d9fa2011-10-31 20:21:18 +00004088 mutex_unlock(&dm_thin_pool_table.mutex);
4089
Joe Thornber5e3283e2014-04-08 11:08:41 +01004090 spin_lock_irqsave(&tc->pool->lock, flags);
Mike Snitzer80e96c52014-11-07 15:09:46 -05004091 if (tc->pool->suspended) {
4092 spin_unlock_irqrestore(&tc->pool->lock, flags);
4093 mutex_lock(&dm_thin_pool_table.mutex); /* reacquire for __pool_dec */
4094 ti->error = "Unable to activate thin device while pool is suspended";
4095 r = -EINVAL;
4096 goto bad;
4097 }
Marc Dionne2b94e892014-12-17 07:59:59 -05004098 atomic_set(&tc->refcount, 1);
4099 init_completion(&tc->can_destroy);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004100 list_add_tail_rcu(&tc->list, &tc->pool->active_thins);
Joe Thornber5e3283e2014-04-08 11:08:41 +01004101 spin_unlock_irqrestore(&tc->pool->lock, flags);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004102 /*
4103 * This synchronize_rcu() call is needed here otherwise we risk a
4104 * wake_worker() call finding no bios to process (because the newly
4105 * added tc isn't yet visible). So this reduces latency since we
4106 * aren't then dependent on the periodic commit to wake_worker().
4107 */
4108 synchronize_rcu();
4109
Mike Snitzer80e96c52014-11-07 15:09:46 -05004110 dm_put(pool_md);
4111
Joe Thornber991d9fa2011-10-31 20:21:18 +00004112 return 0;
4113
Mike Snitzer80e96c52014-11-07 15:09:46 -05004114bad:
Mike Snitzer1acacc02014-02-19 20:32:33 -05004115 dm_pool_close_thin_device(tc->td);
Mike Snitzer80e96c52014-11-07 15:09:46 -05004116bad_pool:
Joe Thornber991d9fa2011-10-31 20:21:18 +00004117 __pool_dec(tc->pool);
4118bad_pool_lookup:
4119 dm_put(pool_md);
4120bad_common:
4121 dm_put_device(ti, tc->pool_dev);
4122bad_pool_dev:
Joe Thornber2dd9c252012-03-28 18:41:28 +01004123 if (tc->origin_dev)
4124 dm_put_device(ti, tc->origin_dev);
4125bad_origin_dev:
Joe Thornber991d9fa2011-10-31 20:21:18 +00004126 kfree(tc);
4127out_unlock:
4128 mutex_unlock(&dm_thin_pool_table.mutex);
4129
4130 return r;
4131}
4132
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00004133static int thin_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00004134{
Kent Overstreet4f024f32013-10-11 15:44:27 -07004135 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004136
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00004137 return thin_bio_map(ti, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004138}
4139
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00004140static int thin_endio(struct dm_target *ti, struct bio *bio, int err)
Joe Thornbereb2aa482012-03-28 18:41:28 +01004141{
4142 unsigned long flags;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00004143 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01004144 struct list_head work;
Mike Snitzera24c2562012-06-03 00:30:00 +01004145 struct dm_thin_new_mapping *m, *tmp;
Joe Thornbereb2aa482012-03-28 18:41:28 +01004146 struct pool *pool = h->tc->pool;
4147
4148 if (h->shared_read_entry) {
4149 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01004150 dm_deferred_entry_dec(h->shared_read_entry, &work);
Joe Thornbereb2aa482012-03-28 18:41:28 +01004151
4152 spin_lock_irqsave(&pool->lock, flags);
4153 list_for_each_entry_safe(m, tmp, &work, list) {
4154 list_del(&m->list);
Joe Thornber50f3c3e2014-06-13 13:57:09 +01004155 __complete_mapping_preparation(m);
Joe Thornbereb2aa482012-03-28 18:41:28 +01004156 }
4157 spin_unlock_irqrestore(&pool->lock, flags);
4158 }
4159
Joe Thornber104655f2012-03-28 18:41:28 +01004160 if (h->all_io_entry) {
4161 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01004162 dm_deferred_entry_dec(h->all_io_entry, &work);
Joe Thornber563af182012-12-21 20:23:31 +00004163 if (!list_empty(&work)) {
4164 spin_lock_irqsave(&pool->lock, flags);
4165 list_for_each_entry_safe(m, tmp, &work, list)
Mike Snitzerdaec3382013-12-11 14:01:20 -05004166 list_add_tail(&m->list, &pool->prepared_discards);
Joe Thornber563af182012-12-21 20:23:31 +00004167 spin_unlock_irqrestore(&pool->lock, flags);
4168 wake_worker(pool);
4169 }
Joe Thornber104655f2012-03-28 18:41:28 +01004170 }
4171
Joe Thornber34fbcf62015-04-16 12:58:35 +01004172 if (h->cell)
4173 cell_defer_no_holder(h->tc, h->cell);
4174
Joe Thornbereb2aa482012-03-28 18:41:28 +01004175 return 0;
4176}
4177
Joe Thornber738211f2014-03-03 15:52:28 +00004178static void thin_presuspend(struct dm_target *ti)
4179{
4180 struct thin_c *tc = ti->private;
4181
4182 if (dm_noflush_suspending(ti))
4183 noflush_work(tc, do_noflush_start);
4184}
4185
Joe Thornber991d9fa2011-10-31 20:21:18 +00004186static void thin_postsuspend(struct dm_target *ti)
4187{
Joe Thornber738211f2014-03-03 15:52:28 +00004188 struct thin_c *tc = ti->private;
4189
4190 /*
4191 * The dm_noflush_suspending flag has been cleared by now, so
4192 * unfortunately we must always run this.
4193 */
4194 noflush_work(tc, do_noflush_stop);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004195}
4196
Joe Thornbere5aea7b2014-06-13 14:47:24 +01004197static int thin_preresume(struct dm_target *ti)
4198{
4199 struct thin_c *tc = ti->private;
4200
4201 if (tc->origin_dev)
4202 tc->origin_size = get_dev_size(tc->origin_dev->bdev);
4203
4204 return 0;
4205}
4206
Joe Thornber991d9fa2011-10-31 20:21:18 +00004207/*
4208 * <nr mapped sectors> <highest mapped sector>
4209 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004210static void thin_status(struct dm_target *ti, status_type_t type,
4211 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00004212{
4213 int r;
4214 ssize_t sz = 0;
4215 dm_block_t mapped, highest;
4216 char buf[BDEVNAME_SIZE];
4217 struct thin_c *tc = ti->private;
4218
Joe Thornbere49e5822012-07-27 15:08:16 +01004219 if (get_pool_mode(tc->pool) == PM_FAIL) {
4220 DMEMIT("Fail");
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004221 return;
Joe Thornbere49e5822012-07-27 15:08:16 +01004222 }
4223
Joe Thornber991d9fa2011-10-31 20:21:18 +00004224 if (!tc->td)
4225 DMEMIT("-");
4226 else {
4227 switch (type) {
4228 case STATUSTYPE_INFO:
4229 r = dm_thin_get_mapped_count(tc->td, &mapped);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004230 if (r) {
4231 DMERR("dm_thin_get_mapped_count returned %d", r);
4232 goto err;
4233 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00004234
4235 r = dm_thin_get_highest_mapped_block(tc->td, &highest);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004236 if (r < 0) {
4237 DMERR("dm_thin_get_highest_mapped_block returned %d", r);
4238 goto err;
4239 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00004240
4241 DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
4242 if (r)
4243 DMEMIT("%llu", ((highest + 1) *
4244 tc->pool->sectors_per_block) - 1);
4245 else
4246 DMEMIT("-");
4247 break;
4248
4249 case STATUSTYPE_TABLE:
4250 DMEMIT("%s %lu",
4251 format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
4252 (unsigned long) tc->dev_id);
Joe Thornber2dd9c252012-03-28 18:41:28 +01004253 if (tc->origin_dev)
4254 DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
Joe Thornber991d9fa2011-10-31 20:21:18 +00004255 break;
4256 }
4257 }
4258
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004259 return;
4260
4261err:
4262 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00004263}
4264
Mike Snitzer36f12ae2014-10-09 15:24:12 -04004265static int thin_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
4266 struct bio_vec *biovec, int max_size)
4267{
4268 struct thin_c *tc = ti->private;
4269 struct request_queue *q = bdev_get_queue(tc->pool_dev->bdev);
4270
4271 if (!q->merge_bvec_fn)
4272 return max_size;
4273
4274 bvm->bi_bdev = tc->pool_dev->bdev;
4275 bvm->bi_sector = dm_target_offset(ti, bvm->bi_sector);
4276
4277 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
4278}
4279
Joe Thornber991d9fa2011-10-31 20:21:18 +00004280static int thin_iterate_devices(struct dm_target *ti,
4281 iterate_devices_callout_fn fn, void *data)
4282{
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004283 sector_t blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004284 struct thin_c *tc = ti->private;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004285 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004286
4287 /*
4288 * We can't call dm_pool_get_data_dev_size() since that blocks. So
4289 * we follow a more convoluted path through to the pool's target.
4290 */
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004291 if (!pool->ti)
Joe Thornber991d9fa2011-10-31 20:21:18 +00004292 return 0; /* nothing is bound */
4293
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004294 blocks = pool->ti->len;
4295 (void) sector_div(blocks, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004296 if (blocks)
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004297 return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004298
4299 return 0;
4300}
4301
Joe Thornber34fbcf62015-04-16 12:58:35 +01004302static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)
4303{
4304 struct thin_c *tc = ti->private;
4305 struct pool *pool = tc->pool;
4306
4307 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
4308 limits->max_discard_sectors = 2048 * 1024 * 16; /* 16G */
4309}
4310
Joe Thornber991d9fa2011-10-31 20:21:18 +00004311static struct target_type thin_target = {
4312 .name = "thin",
Joe Thornber34fbcf62015-04-16 12:58:35 +01004313 .version = {1, 15, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00004314 .module = THIS_MODULE,
4315 .ctr = thin_ctr,
4316 .dtr = thin_dtr,
4317 .map = thin_map,
Joe Thornbereb2aa482012-03-28 18:41:28 +01004318 .end_io = thin_endio,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01004319 .preresume = thin_preresume,
Joe Thornber738211f2014-03-03 15:52:28 +00004320 .presuspend = thin_presuspend,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004321 .postsuspend = thin_postsuspend,
4322 .status = thin_status,
Mike Snitzer36f12ae2014-10-09 15:24:12 -04004323 .merge = thin_merge,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004324 .iterate_devices = thin_iterate_devices,
Joe Thornber34fbcf62015-04-16 12:58:35 +01004325 .io_hints = thin_io_hints,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004326};
4327
4328/*----------------------------------------------------------------*/
4329
4330static int __init dm_thin_init(void)
4331{
4332 int r;
4333
4334 pool_table_init();
4335
4336 r = dm_register_target(&thin_target);
4337 if (r)
4338 return r;
4339
4340 r = dm_register_target(&pool_target);
4341 if (r)
Mike Snitzera24c2562012-06-03 00:30:00 +01004342 goto bad_pool_target;
4343
4344 r = -ENOMEM;
4345
Mike Snitzera24c2562012-06-03 00:30:00 +01004346 _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
4347 if (!_new_mapping_cache)
4348 goto bad_new_mapping_cache;
4349
Mike Snitzera24c2562012-06-03 00:30:00 +01004350 return 0;
4351
Mike Snitzera24c2562012-06-03 00:30:00 +01004352bad_new_mapping_cache:
Mike Snitzera24c2562012-06-03 00:30:00 +01004353 dm_unregister_target(&pool_target);
4354bad_pool_target:
4355 dm_unregister_target(&thin_target);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004356
4357 return r;
4358}
4359
4360static void dm_thin_exit(void)
4361{
4362 dm_unregister_target(&thin_target);
4363 dm_unregister_target(&pool_target);
Mike Snitzera24c2562012-06-03 00:30:00 +01004364
Mike Snitzera24c2562012-06-03 00:30:00 +01004365 kmem_cache_destroy(_new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004366}
4367
4368module_init(dm_thin_init);
4369module_exit(dm_thin_exit);
4370
Mike Snitzer80c57892014-05-20 13:38:33 -04004371module_param_named(no_space_timeout, no_space_timeout_secs, uint, S_IRUGO | S_IWUSR);
4372MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds");
4373
Alasdair G Kergon7cab8bf2012-05-12 01:43:19 +01004374MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
Joe Thornber991d9fa2011-10-31 20:21:18 +00004375MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
4376MODULE_LICENSE("GPL");