blob: d2bbe8cc1e9786b66af798df9d8666d3fb96223c [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 Thornbera822c832015-07-03 10:22:42 +010021#include <linux/vmalloc.h>
Joe Thornberac4c3f32014-10-10 16:42:10 +010022#include <linux/sort.h>
Mike Snitzer67324ea2014-03-21 18:33:41 -040023#include <linux/rbtree.h>
Joe Thornber991d9fa2011-10-31 20:21:18 +000024
25#define DM_MSG_PREFIX "thin"
26
27/*
28 * Tunable constants
29 */
Alasdair G Kergon7768ed32012-07-27 15:07:57 +010030#define ENDIO_HOOK_POOL_SIZE 1024
Joe Thornber991d9fa2011-10-31 20:21:18 +000031#define MAPPING_POOL_SIZE 1024
Joe Thornber905e51b2012-03-28 18:41:27 +010032#define COMMIT_PERIOD HZ
Mike Snitzer80c57892014-05-20 13:38:33 -040033#define NO_SPACE_TIMEOUT_SECS 60
34
35static unsigned no_space_timeout_secs = NO_SPACE_TIMEOUT_SECS;
Joe Thornber991d9fa2011-10-31 20:21:18 +000036
Mikulas Patockadf5d2e92013-03-01 22:45:49 +000037DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
38 "A percentage of time allocated for copy on write");
39
Joe Thornber991d9fa2011-10-31 20:21:18 +000040/*
41 * The block size of the device holding pool data must be
42 * between 64KB and 1GB.
43 */
44#define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
45#define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
46
47/*
Joe Thornber991d9fa2011-10-31 20:21:18 +000048 * Device id is restricted to 24 bits.
49 */
50#define MAX_DEV_ID ((1 << 24) - 1)
51
52/*
53 * How do we handle breaking sharing of data blocks?
54 * =================================================
55 *
56 * We use a standard copy-on-write btree to store the mappings for the
57 * devices (note I'm talking about copy-on-write of the metadata here, not
58 * the data). When you take an internal snapshot you clone the root node
59 * of the origin btree. After this there is no concept of an origin or a
60 * snapshot. They are just two device trees that happen to point to the
61 * same data blocks.
62 *
63 * When we get a write in we decide if it's to a shared data block using
64 * some timestamp magic. If it is, we have to break sharing.
65 *
66 * Let's say we write to a shared block in what was the origin. The
67 * steps are:
68 *
69 * i) plug io further to this physical block. (see bio_prison code).
70 *
71 * ii) quiesce any read io to that shared data block. Obviously
Mike Snitzer44feb382012-10-12 21:02:10 +010072 * including all devices that share this block. (see dm_deferred_set code)
Joe Thornber991d9fa2011-10-31 20:21:18 +000073 *
74 * iii) copy the data block to a newly allocate block. This step can be
75 * missed out if the io covers the block. (schedule_copy).
76 *
77 * iv) insert the new mapping into the origin's btree
Joe Thornberfe878f32012-03-28 18:41:24 +010078 * (process_prepared_mapping). This act of inserting breaks some
Joe Thornber991d9fa2011-10-31 20:21:18 +000079 * sharing of btree nodes between the two devices. Breaking sharing only
80 * effects the btree of that specific device. Btrees for the other
81 * devices that share the block never change. The btree for the origin
82 * device as it was after the last commit is untouched, ie. we're using
83 * persistent data structures in the functional programming sense.
84 *
85 * v) unplug io to this physical block, including the io that triggered
86 * the breaking of sharing.
87 *
88 * Steps (ii) and (iii) occur in parallel.
89 *
90 * The metadata _doesn't_ need to be committed before the io continues. We
91 * get away with this because the io is always written to a _new_ block.
92 * If there's a crash, then:
93 *
94 * - The origin mapping will point to the old origin block (the shared
95 * one). This will contain the data as it was before the io that triggered
96 * the breaking of sharing came in.
97 *
98 * - The snap mapping still points to the old block. As it would after
99 * the commit.
100 *
101 * The downside of this scheme is the timestamp magic isn't perfect, and
102 * will continue to think that data block in the snapshot device is shared
103 * even after the write to the origin has broken sharing. I suspect data
104 * blocks will typically be shared by many different devices, so we're
105 * breaking sharing n + 1 times, rather than n, where n is the number of
106 * devices that reference this data block. At the moment I think the
107 * benefits far, far outweigh the disadvantages.
108 */
109
110/*----------------------------------------------------------------*/
111
112/*
Joe Thornber991d9fa2011-10-31 20:21:18 +0000113 * Key building.
114 */
Joe Thornber34fbcf62015-04-16 12:58:35 +0100115enum lock_space {
116 VIRTUAL,
117 PHYSICAL
118};
119
120static void build_key(struct dm_thin_device *td, enum lock_space ls,
121 dm_block_t b, dm_block_t e, struct dm_cell_key *key)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000122{
Joe Thornber34fbcf62015-04-16 12:58:35 +0100123 key->virtual = (ls == VIRTUAL);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000124 key->dev = dm_thin_dev_id(td);
Joe Thornber5f274d82014-09-17 10:17:39 +0100125 key->block_begin = b;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100126 key->block_end = e;
127}
128
129static void build_data_key(struct dm_thin_device *td, dm_block_t b,
130 struct dm_cell_key *key)
131{
132 build_key(td, PHYSICAL, b, b + 1llu, key);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000133}
134
135static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
Mike Snitzer44feb382012-10-12 21:02:10 +0100136 struct dm_cell_key *key)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000137{
Joe Thornber34fbcf62015-04-16 12:58:35 +0100138 build_key(td, VIRTUAL, b, b + 1llu, key);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000139}
140
141/*----------------------------------------------------------------*/
142
Joe Thornber7d327fe2014-10-06 15:45:59 +0100143#define THROTTLE_THRESHOLD (1 * HZ)
144
145struct throttle {
146 struct rw_semaphore lock;
147 unsigned long threshold;
148 bool throttle_applied;
149};
150
151static void throttle_init(struct throttle *t)
152{
153 init_rwsem(&t->lock);
154 t->throttle_applied = false;
155}
156
157static void throttle_work_start(struct throttle *t)
158{
159 t->threshold = jiffies + THROTTLE_THRESHOLD;
160}
161
162static void throttle_work_update(struct throttle *t)
163{
164 if (!t->throttle_applied && jiffies > t->threshold) {
165 down_write(&t->lock);
166 t->throttle_applied = true;
167 }
168}
169
170static void throttle_work_complete(struct throttle *t)
171{
172 if (t->throttle_applied) {
173 t->throttle_applied = false;
174 up_write(&t->lock);
175 }
176}
177
178static void throttle_lock(struct throttle *t)
179{
180 down_read(&t->lock);
181}
182
183static void throttle_unlock(struct throttle *t)
184{
185 up_read(&t->lock);
186}
187
188/*----------------------------------------------------------------*/
189
Joe Thornber991d9fa2011-10-31 20:21:18 +0000190/*
191 * A pool device ties together a metadata device and a data device. It
192 * also provides the interface for creating and destroying internal
193 * devices.
194 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100195struct dm_thin_new_mapping;
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100196
Joe Thornbere49e5822012-07-27 15:08:16 +0100197/*
Joe Thornber3e1a0692014-03-03 16:03:26 +0000198 * The pool runs in 4 modes. Ordered in degraded order for comparisons.
Joe Thornbere49e5822012-07-27 15:08:16 +0100199 */
200enum pool_mode {
201 PM_WRITE, /* metadata may be changed */
Joe Thornber3e1a0692014-03-03 16:03:26 +0000202 PM_OUT_OF_DATA_SPACE, /* metadata may be changed, though data may not be allocated */
Joe Thornbere49e5822012-07-27 15:08:16 +0100203 PM_READ_ONLY, /* metadata may not be changed */
204 PM_FAIL, /* all I/O fails */
205};
206
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100207struct pool_features {
Joe Thornbere49e5822012-07-27 15:08:16 +0100208 enum pool_mode mode;
209
Mike Snitzer9bc142d2012-09-26 23:45:46 +0100210 bool zero_new_blocks:1;
211 bool discard_enabled:1;
212 bool discard_passdown:1;
Mike Snitzer787a996c2013-12-06 16:21:43 -0500213 bool error_if_no_space:1;
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100214};
215
Joe Thornbere49e5822012-07-27 15:08:16 +0100216struct thin_c;
217typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio);
Joe Thornbera374bb22014-10-10 13:43:14 +0100218typedef void (*process_cell_fn)(struct thin_c *tc, struct dm_bio_prison_cell *cell);
Joe Thornbere49e5822012-07-27 15:08:16 +0100219typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m);
220
Joe Thornberac4c3f32014-10-10 16:42:10 +0100221#define CELL_SORT_ARRAY_SIZE 8192
222
Joe Thornber991d9fa2011-10-31 20:21:18 +0000223struct pool {
224 struct list_head list;
225 struct dm_target *ti; /* Only set if a pool target is bound */
226
227 struct mapped_device *pool_md;
228 struct block_device *md_dev;
229 struct dm_pool_metadata *pmd;
230
Joe Thornber991d9fa2011-10-31 20:21:18 +0000231 dm_block_t low_water_blocks;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100232 uint32_t sectors_per_block;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100233 int sectors_per_block_shift;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000234
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100235 struct pool_features pf;
Joe Thornber88a66212013-12-04 20:16:12 -0500236 bool low_water_triggered:1; /* A dm event has been sent */
Mike Snitzer80e96c52014-11-07 15:09:46 -0500237 bool suspended:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000238
Mike Snitzer44feb382012-10-12 21:02:10 +0100239 struct dm_bio_prison *prison;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000240 struct dm_kcopyd_client *copier;
241
242 struct workqueue_struct *wq;
Joe Thornber7d327fe2014-10-06 15:45:59 +0100243 struct throttle throttle;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000244 struct work_struct worker;
Joe Thornber905e51b2012-03-28 18:41:27 +0100245 struct delayed_work waker;
Joe Thornber85ad643b2014-05-09 15:59:38 +0100246 struct delayed_work no_space_timeout;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000247
Joe Thornber905e51b2012-03-28 18:41:27 +0100248 unsigned long last_commit_jiffies;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100249 unsigned ref_count;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000250
251 spinlock_t lock;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000252 struct bio_list deferred_flush_bios;
253 struct list_head prepared_mappings;
Joe Thornber104655f2012-03-28 18:41:28 +0100254 struct list_head prepared_discards;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400255 struct list_head active_thins;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000256
Mike Snitzer44feb382012-10-12 21:02:10 +0100257 struct dm_deferred_set *shared_read_ds;
258 struct dm_deferred_set *all_io_ds;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000259
Mike Snitzera24c2562012-06-03 00:30:00 +0100260 struct dm_thin_new_mapping *next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000261 mempool_t *mapping_pool;
Joe Thornbere49e5822012-07-27 15:08:16 +0100262
263 process_bio_fn process_bio;
264 process_bio_fn process_discard;
265
Joe Thornbera374bb22014-10-10 13:43:14 +0100266 process_cell_fn process_cell;
267 process_cell_fn process_discard_cell;
268
Joe Thornbere49e5822012-07-27 15:08:16 +0100269 process_mapping_fn process_prepared_mapping;
270 process_mapping_fn process_prepared_discard;
Joe Thornberac4c3f32014-10-10 16:42:10 +0100271
Joe Thornbera822c832015-07-03 10:22:42 +0100272 struct dm_bio_prison_cell **cell_sort_array;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000273};
274
Joe Thornbere49e5822012-07-27 15:08:16 +0100275static enum pool_mode get_pool_mode(struct pool *pool);
Joe Thornberb5330652013-12-04 19:51:33 -0500276static void metadata_operation_failed(struct pool *pool, const char *op, int r);
Joe Thornbere49e5822012-07-27 15:08:16 +0100277
Joe Thornber991d9fa2011-10-31 20:21:18 +0000278/*
279 * Target context for a pool.
280 */
281struct pool_c {
282 struct dm_target *ti;
283 struct pool *pool;
284 struct dm_dev *data_dev;
285 struct dm_dev *metadata_dev;
286 struct dm_target_callbacks callbacks;
287
288 dm_block_t low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +0100289 struct pool_features requested_pf; /* Features requested during table load */
290 struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000291};
292
293/*
294 * Target context for a thin.
295 */
296struct thin_c {
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400297 struct list_head list;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000298 struct dm_dev *pool_dev;
Joe Thornber2dd9c252012-03-28 18:41:28 +0100299 struct dm_dev *origin_dev;
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100300 sector_t origin_size;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000301 dm_thin_id dev_id;
302
303 struct pool *pool;
304 struct dm_thin_device *td;
Mike Snitzer583024d2014-10-28 20:58:45 -0400305 struct mapped_device *thin_md;
306
Joe Thornber738211f2014-03-03 15:52:28 +0000307 bool requeue_mode:1;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400308 spinlock_t lock;
Joe Thornbera374bb22014-10-10 13:43:14 +0100309 struct list_head deferred_cells;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400310 struct bio_list deferred_bio_list;
311 struct bio_list retry_on_resume_list;
Mike Snitzer67324ea2014-03-21 18:33:41 -0400312 struct rb_root sort_bio_list; /* sorted list of deferred bios */
Joe Thornberb10ebd32014-04-08 11:29:01 +0100313
314 /*
315 * Ensures the thin is not destroyed until the worker has finished
316 * iterating the active_thins list.
317 */
318 atomic_t refcount;
319 struct completion can_destroy;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000320};
321
322/*----------------------------------------------------------------*/
323
Joe Thornber34fbcf62015-04-16 12:58:35 +0100324/**
325 * __blkdev_issue_discard_async - queue a discard with async completion
326 * @bdev: blockdev to issue discard for
327 * @sector: start sector
328 * @nr_sects: number of sectors to discard
329 * @gfp_mask: memory allocation flags (for bio_alloc)
330 * @flags: BLKDEV_IFL_* flags to control behaviour
331 * @parent_bio: parent discard bio that all sub discards get chained to
332 *
333 * Description:
334 * Asynchronously issue a discard request for the sectors in question.
335 * NOTE: this variant of blk-core's blkdev_issue_discard() is a stop-gap
336 * that is being kept local to DM thinp until the block changes to allow
337 * late bio splitting land upstream.
338 */
339static int __blkdev_issue_discard_async(struct block_device *bdev, sector_t sector,
340 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags,
341 struct bio *parent_bio)
342{
343 struct request_queue *q = bdev_get_queue(bdev);
344 int type = REQ_WRITE | REQ_DISCARD;
345 unsigned int max_discard_sectors, granularity;
346 int alignment;
347 struct bio *bio;
348 int ret = 0;
349 struct blk_plug plug;
350
351 if (!q)
352 return -ENXIO;
353
354 if (!blk_queue_discard(q))
355 return -EOPNOTSUPP;
356
357 /* Zero-sector (unknown) and one-sector granularities are the same. */
358 granularity = max(q->limits.discard_granularity >> 9, 1U);
359 alignment = (bdev_discard_alignment(bdev) >> 9) % granularity;
360
361 /*
362 * Ensure that max_discard_sectors is of the proper
363 * granularity, so that requests stay aligned after a split.
364 */
365 max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9);
366 max_discard_sectors -= max_discard_sectors % granularity;
367 if (unlikely(!max_discard_sectors)) {
368 /* Avoid infinite loop below. Being cautious never hurts. */
369 return -EOPNOTSUPP;
370 }
371
372 if (flags & BLKDEV_DISCARD_SECURE) {
373 if (!blk_queue_secdiscard(q))
374 return -EOPNOTSUPP;
375 type |= REQ_SECURE;
376 }
377
378 blk_start_plug(&plug);
379 while (nr_sects) {
380 unsigned int req_sects;
381 sector_t end_sect, tmp;
382
383 /*
384 * Required bio_put occurs in bio_endio thanks to bio_chain below
385 */
386 bio = bio_alloc(gfp_mask, 1);
387 if (!bio) {
388 ret = -ENOMEM;
389 break;
390 }
391
392 req_sects = min_t(sector_t, nr_sects, max_discard_sectors);
393
394 /*
395 * If splitting a request, and the next starting sector would be
396 * misaligned, stop the discard at the previous aligned sector.
397 */
398 end_sect = sector + req_sects;
399 tmp = end_sect;
400 if (req_sects < nr_sects &&
401 sector_div(tmp, granularity) != alignment) {
402 end_sect = end_sect - alignment;
403 sector_div(end_sect, granularity);
404 end_sect = end_sect * granularity + alignment;
405 req_sects = end_sect - sector;
406 }
407
408 bio_chain(bio, parent_bio);
409
410 bio->bi_iter.bi_sector = sector;
411 bio->bi_bdev = bdev;
412
413 bio->bi_iter.bi_size = req_sects << 9;
414 nr_sects -= req_sects;
415 sector = end_sect;
416
417 submit_bio(type, bio);
418
419 /*
420 * We can loop for a long time in here, if someone does
421 * full device discards (like mkfs). Be nice and allow
422 * us to schedule out to avoid softlocking if preempt
423 * is disabled.
424 */
425 cond_resched();
426 }
427 blk_finish_plug(&plug);
428
429 return ret;
430}
431
432static bool block_size_is_power_of_two(struct pool *pool)
433{
434 return pool->sectors_per_block_shift >= 0;
435}
436
437static sector_t block_to_sectors(struct pool *pool, dm_block_t b)
438{
439 return block_size_is_power_of_two(pool) ?
440 (b << pool->sectors_per_block_shift) :
441 (b * pool->sectors_per_block);
442}
443
444static int issue_discard(struct thin_c *tc, dm_block_t data_b, dm_block_t data_e,
445 struct bio *parent_bio)
446{
447 sector_t s = block_to_sectors(tc->pool, data_b);
448 sector_t len = block_to_sectors(tc->pool, data_e - data_b);
449
450 return __blkdev_issue_discard_async(tc->pool_dev->bdev, s, len,
451 GFP_NOWAIT, 0, parent_bio);
452}
453
454/*----------------------------------------------------------------*/
455
Joe Thornber025b9682013-03-01 22:45:50 +0000456/*
457 * wake_worker() is used when new work is queued and when pool_resume is
458 * ready to continue deferred IO processing.
459 */
460static void wake_worker(struct pool *pool)
461{
462 queue_work(pool->wq, &pool->worker);
463}
464
465/*----------------------------------------------------------------*/
466
Joe Thornber6beca5e2013-03-01 22:45:50 +0000467static int bio_detain(struct pool *pool, struct dm_cell_key *key, struct bio *bio,
468 struct dm_bio_prison_cell **cell_result)
469{
470 int r;
471 struct dm_bio_prison_cell *cell_prealloc;
472
473 /*
474 * Allocate a cell from the prison's mempool.
475 * This might block but it can't fail.
476 */
477 cell_prealloc = dm_bio_prison_alloc_cell(pool->prison, GFP_NOIO);
478
479 r = dm_bio_detain(pool->prison, key, bio, cell_prealloc, cell_result);
480 if (r)
481 /*
482 * We reused an old cell; we can get rid of
483 * the new one.
484 */
485 dm_bio_prison_free_cell(pool->prison, cell_prealloc);
486
487 return r;
488}
489
490static void cell_release(struct pool *pool,
491 struct dm_bio_prison_cell *cell,
492 struct bio_list *bios)
493{
494 dm_cell_release(pool->prison, cell, bios);
495 dm_bio_prison_free_cell(pool->prison, cell);
496}
497
Joe Thornber2d759a42014-10-10 15:27:16 +0100498static void cell_visit_release(struct pool *pool,
499 void (*fn)(void *, struct dm_bio_prison_cell *),
500 void *context,
501 struct dm_bio_prison_cell *cell)
502{
503 dm_cell_visit_release(pool->prison, fn, context, cell);
504 dm_bio_prison_free_cell(pool->prison, cell);
505}
506
Joe Thornber6beca5e2013-03-01 22:45:50 +0000507static void cell_release_no_holder(struct pool *pool,
508 struct dm_bio_prison_cell *cell,
509 struct bio_list *bios)
510{
511 dm_cell_release_no_holder(pool->prison, cell, bios);
512 dm_bio_prison_free_cell(pool->prison, cell);
513}
514
Mike Snitzeraf918052014-05-22 14:32:51 -0400515static void cell_error_with_code(struct pool *pool,
516 struct dm_bio_prison_cell *cell, int error_code)
Joe Thornber6beca5e2013-03-01 22:45:50 +0000517{
Mike Snitzeraf918052014-05-22 14:32:51 -0400518 dm_cell_error(pool->prison, cell, error_code);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000519 dm_bio_prison_free_cell(pool->prison, cell);
520}
521
Mike Snitzeraf918052014-05-22 14:32:51 -0400522static void cell_error(struct pool *pool, struct dm_bio_prison_cell *cell)
523{
524 cell_error_with_code(pool, cell, -EIO);
525}
526
Joe Thornbera374bb22014-10-10 13:43:14 +0100527static void cell_success(struct pool *pool, struct dm_bio_prison_cell *cell)
528{
529 cell_error_with_code(pool, cell, 0);
530}
531
532static void cell_requeue(struct pool *pool, struct dm_bio_prison_cell *cell)
533{
534 cell_error_with_code(pool, cell, DM_ENDIO_REQUEUE);
535}
536
Joe Thornber6beca5e2013-03-01 22:45:50 +0000537/*----------------------------------------------------------------*/
538
Joe Thornber991d9fa2011-10-31 20:21:18 +0000539/*
540 * A global list of pools that uses a struct mapped_device as a key.
541 */
542static struct dm_thin_pool_table {
543 struct mutex mutex;
544 struct list_head pools;
545} dm_thin_pool_table;
546
547static void pool_table_init(void)
548{
549 mutex_init(&dm_thin_pool_table.mutex);
550 INIT_LIST_HEAD(&dm_thin_pool_table.pools);
551}
552
553static void __pool_table_insert(struct pool *pool)
554{
555 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
556 list_add(&pool->list, &dm_thin_pool_table.pools);
557}
558
559static void __pool_table_remove(struct pool *pool)
560{
561 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
562 list_del(&pool->list);
563}
564
565static struct pool *__pool_table_lookup(struct mapped_device *md)
566{
567 struct pool *pool = NULL, *tmp;
568
569 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
570
571 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
572 if (tmp->pool_md == md) {
573 pool = tmp;
574 break;
575 }
576 }
577
578 return pool;
579}
580
581static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
582{
583 struct pool *pool = NULL, *tmp;
584
585 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
586
587 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
588 if (tmp->md_dev == md_dev) {
589 pool = tmp;
590 break;
591 }
592 }
593
594 return pool;
595}
596
597/*----------------------------------------------------------------*/
598
Mike Snitzera24c2562012-06-03 00:30:00 +0100599struct dm_thin_endio_hook {
Joe Thornbereb2aa482012-03-28 18:41:28 +0100600 struct thin_c *tc;
Mike Snitzer44feb382012-10-12 21:02:10 +0100601 struct dm_deferred_entry *shared_read_entry;
602 struct dm_deferred_entry *all_io_entry;
Mike Snitzera24c2562012-06-03 00:30:00 +0100603 struct dm_thin_new_mapping *overwrite_mapping;
Mike Snitzer67324ea2014-03-21 18:33:41 -0400604 struct rb_node rb_node;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100605 struct dm_bio_prison_cell *cell;
Joe Thornbereb2aa482012-03-28 18:41:28 +0100606};
607
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400608static void __merge_bio_list(struct bio_list *bios, struct bio_list *master)
609{
610 bio_list_merge(bios, master);
611 bio_list_init(master);
612}
613
614static void error_bio_list(struct bio_list *bios, int error)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000615{
616 struct bio *bio;
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400617
618 while ((bio = bio_list_pop(bios)))
619 bio_endio(bio, error);
620}
621
622static void error_thin_bio_list(struct thin_c *tc, struct bio_list *master, int error)
623{
Joe Thornber991d9fa2011-10-31 20:21:18 +0000624 struct bio_list bios;
Joe Thornber18adc572014-03-03 15:46:42 +0000625 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000626
627 bio_list_init(&bios);
Joe Thornber18adc572014-03-03 15:46:42 +0000628
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400629 spin_lock_irqsave(&tc->lock, flags);
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400630 __merge_bio_list(&bios, master);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400631 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000632
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400633 error_bio_list(&bios, error);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000634}
635
Joe Thornbera374bb22014-10-10 13:43:14 +0100636static void requeue_deferred_cells(struct thin_c *tc)
637{
638 struct pool *pool = tc->pool;
639 unsigned long flags;
640 struct list_head cells;
641 struct dm_bio_prison_cell *cell, *tmp;
642
643 INIT_LIST_HEAD(&cells);
644
645 spin_lock_irqsave(&tc->lock, flags);
646 list_splice_init(&tc->deferred_cells, &cells);
647 spin_unlock_irqrestore(&tc->lock, flags);
648
649 list_for_each_entry_safe(cell, tmp, &cells, user_list)
650 cell_requeue(pool, cell);
651}
652
Joe Thornber991d9fa2011-10-31 20:21:18 +0000653static void requeue_io(struct thin_c *tc)
654{
Joe Thornber3e1a0692014-03-03 16:03:26 +0000655 struct bio_list bios;
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400656 unsigned long flags;
Joe Thornber3e1a0692014-03-03 16:03:26 +0000657
658 bio_list_init(&bios);
659
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400660 spin_lock_irqsave(&tc->lock, flags);
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400661 __merge_bio_list(&bios, &tc->deferred_bio_list);
662 __merge_bio_list(&bios, &tc->retry_on_resume_list);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400663 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber3e1a0692014-03-03 16:03:26 +0000664
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400665 error_bio_list(&bios, DM_ENDIO_REQUEUE);
666 requeue_deferred_cells(tc);
Joe Thornber3e1a0692014-03-03 16:03:26 +0000667}
668
Mike Snitzer0a927c22015-07-21 13:20:46 -0400669static void error_retry_list_with_code(struct pool *pool, int error)
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400670{
671 struct thin_c *tc;
672
673 rcu_read_lock();
674 list_for_each_entry_rcu(tc, &pool->active_thins, list)
Mike Snitzer0a927c22015-07-21 13:20:46 -0400675 error_thin_bio_list(tc, &tc->retry_on_resume_list, error);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400676 rcu_read_unlock();
677}
678
Mike Snitzer0a927c22015-07-21 13:20:46 -0400679static void error_retry_list(struct pool *pool)
680{
681 return error_retry_list_with_code(pool, -EIO);
682}
683
Joe Thornber991d9fa2011-10-31 20:21:18 +0000684/*
685 * This section of code contains the logic for processing a thin device's IO.
686 * Much of the code depends on pool object resources (lists, workqueues, etc)
687 * but most is exclusively called from the thin target rather than the thin-pool
688 * target.
689 */
690
691static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
692{
Mike Snitzer58f77a22013-03-01 22:45:45 +0000693 struct pool *pool = tc->pool;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700694 sector_t block_nr = bio->bi_iter.bi_sector;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100695
Mike Snitzer58f77a22013-03-01 22:45:45 +0000696 if (block_size_is_power_of_two(pool))
697 block_nr >>= pool->sectors_per_block_shift;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100698 else
Mike Snitzer58f77a22013-03-01 22:45:45 +0000699 (void) sector_div(block_nr, pool->sectors_per_block);
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100700
701 return block_nr;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000702}
703
Joe Thornber34fbcf62015-04-16 12:58:35 +0100704/*
705 * Returns the _complete_ blocks that this bio covers.
706 */
707static void get_bio_block_range(struct thin_c *tc, struct bio *bio,
708 dm_block_t *begin, dm_block_t *end)
709{
710 struct pool *pool = tc->pool;
711 sector_t b = bio->bi_iter.bi_sector;
712 sector_t e = b + (bio->bi_iter.bi_size >> SECTOR_SHIFT);
713
714 b += pool->sectors_per_block - 1ull; /* so we round up */
715
716 if (block_size_is_power_of_two(pool)) {
717 b >>= pool->sectors_per_block_shift;
718 e >>= pool->sectors_per_block_shift;
719 } else {
720 (void) sector_div(b, pool->sectors_per_block);
721 (void) sector_div(e, pool->sectors_per_block);
722 }
723
724 if (e < b)
725 /* Can happen if the bio is within a single block. */
726 e = b;
727
728 *begin = b;
729 *end = e;
730}
731
Joe Thornber991d9fa2011-10-31 20:21:18 +0000732static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
733{
734 struct pool *pool = tc->pool;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700735 sector_t bi_sector = bio->bi_iter.bi_sector;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000736
737 bio->bi_bdev = tc->pool_dev->bdev;
Mike Snitzer58f77a22013-03-01 22:45:45 +0000738 if (block_size_is_power_of_two(pool))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700739 bio->bi_iter.bi_sector =
740 (block << pool->sectors_per_block_shift) |
741 (bi_sector & (pool->sectors_per_block - 1));
Mike Snitzer58f77a22013-03-01 22:45:45 +0000742 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700743 bio->bi_iter.bi_sector = (block * pool->sectors_per_block) +
Mike Snitzer58f77a22013-03-01 22:45:45 +0000744 sector_div(bi_sector, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000745}
746
Joe Thornber2dd9c252012-03-28 18:41:28 +0100747static void remap_to_origin(struct thin_c *tc, struct bio *bio)
748{
749 bio->bi_bdev = tc->origin_dev->bdev;
750}
751
Joe Thornber4afdd682012-07-27 15:08:14 +0100752static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
753{
754 return (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) &&
755 dm_thin_changed_this_transaction(tc->td);
756}
757
Joe Thornbere8088072012-12-21 20:23:31 +0000758static void inc_all_io_entry(struct pool *pool, struct bio *bio)
759{
760 struct dm_thin_endio_hook *h;
761
762 if (bio->bi_rw & REQ_DISCARD)
763 return;
764
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000765 h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbere8088072012-12-21 20:23:31 +0000766 h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
767}
768
Joe Thornber2dd9c252012-03-28 18:41:28 +0100769static void issue(struct thin_c *tc, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000770{
771 struct pool *pool = tc->pool;
772 unsigned long flags;
773
Joe Thornbere49e5822012-07-27 15:08:16 +0100774 if (!bio_triggers_commit(tc, bio)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000775 generic_make_request(bio);
Joe Thornbere49e5822012-07-27 15:08:16 +0100776 return;
777 }
778
779 /*
780 * Complete bio with an error if earlier I/O caused changes to
781 * the metadata that can't be committed e.g, due to I/O errors
782 * on the metadata device.
783 */
784 if (dm_thin_aborted_changes(tc->td)) {
785 bio_io_error(bio);
786 return;
787 }
788
789 /*
790 * Batch together any bios that trigger commits and then issue a
791 * single commit for them in process_deferred_bios().
792 */
793 spin_lock_irqsave(&pool->lock, flags);
794 bio_list_add(&pool->deferred_flush_bios, bio);
795 spin_unlock_irqrestore(&pool->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000796}
797
Joe Thornber2dd9c252012-03-28 18:41:28 +0100798static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
799{
800 remap_to_origin(tc, bio);
801 issue(tc, bio);
802}
803
804static void remap_and_issue(struct thin_c *tc, struct bio *bio,
805 dm_block_t block)
806{
807 remap(tc, bio, block);
808 issue(tc, bio);
809}
810
Joe Thornber991d9fa2011-10-31 20:21:18 +0000811/*----------------------------------------------------------------*/
812
813/*
814 * Bio endio functions.
815 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100816struct dm_thin_new_mapping {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000817 struct list_head list;
818
Mike Snitzer7f214662013-12-17 13:43:31 -0500819 bool pass_discard:1;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100820 bool maybe_shared:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000821
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100822 /*
823 * Track quiescing, copying and zeroing preparation actions. When this
824 * counter hits zero the block is prepared and can be inserted into the
825 * btree.
826 */
827 atomic_t prepare_actions;
828
Mike Snitzer7f214662013-12-17 13:43:31 -0500829 int err;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000830 struct thin_c *tc;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100831 dm_block_t virt_begin, virt_end;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000832 dm_block_t data_block;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100833 struct dm_bio_prison_cell *cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000834
835 /*
836 * If the bio covers the whole area of a block then we can avoid
837 * zeroing or copying. Instead this bio is hooked. The bio will
838 * still be in the cell, so care has to be taken to avoid issuing
839 * the bio twice.
840 */
841 struct bio *bio;
842 bio_end_io_t *saved_bi_end_io;
843};
844
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100845static void __complete_mapping_preparation(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000846{
847 struct pool *pool = m->tc->pool;
848
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100849 if (atomic_dec_and_test(&m->prepare_actions)) {
Mike Snitzerdaec3382013-12-11 14:01:20 -0500850 list_add_tail(&m->list, &pool->prepared_mappings);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000851 wake_worker(pool);
852 }
853}
854
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100855static void complete_mapping_preparation(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000856{
857 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000858 struct pool *pool = m->tc->pool;
859
Joe Thornber991d9fa2011-10-31 20:21:18 +0000860 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100861 __complete_mapping_preparation(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000862 spin_unlock_irqrestore(&pool->lock, flags);
863}
864
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100865static void copy_complete(int read_err, unsigned long write_err, void *context)
866{
867 struct dm_thin_new_mapping *m = context;
868
869 m->err = read_err || write_err ? -EIO : 0;
870 complete_mapping_preparation(m);
871}
872
Joe Thornber991d9fa2011-10-31 20:21:18 +0000873static void overwrite_endio(struct bio *bio, int err)
874{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000875 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Mike Snitzera24c2562012-06-03 00:30:00 +0100876 struct dm_thin_new_mapping *m = h->overwrite_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000877
Mike Snitzer8b908f82015-05-13 17:53:13 -0400878 bio->bi_end_io = m->saved_bi_end_io;
879
Joe Thornber991d9fa2011-10-31 20:21:18 +0000880 m->err = err;
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100881 complete_mapping_preparation(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000882}
883
Joe Thornber991d9fa2011-10-31 20:21:18 +0000884/*----------------------------------------------------------------*/
885
886/*
887 * Workqueue.
888 */
889
890/*
891 * Prepared mapping jobs.
892 */
893
894/*
Joe Thornber2d759a42014-10-10 15:27:16 +0100895 * This sends the bios in the cell, except the original holder, back
896 * to the deferred_bios list.
Joe Thornber991d9fa2011-10-31 20:21:18 +0000897 */
Joe Thornberf286ba02012-12-21 20:23:33 +0000898static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000899{
Joe Thornber991d9fa2011-10-31 20:21:18 +0000900 struct pool *pool = tc->pool;
901 unsigned long flags;
902
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400903 spin_lock_irqsave(&tc->lock, flags);
904 cell_release_no_holder(pool, cell, &tc->deferred_bio_list);
905 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000906
907 wake_worker(pool);
908}
909
Joe Thornbera374bb22014-10-10 13:43:14 +0100910static void thin_defer_bio(struct thin_c *tc, struct bio *bio);
911
Joe Thornber2d759a42014-10-10 15:27:16 +0100912struct remap_info {
913 struct thin_c *tc;
914 struct bio_list defer_bios;
915 struct bio_list issue_bios;
916};
917
918static void __inc_remap_and_issue_cell(void *context,
919 struct dm_bio_prison_cell *cell)
920{
921 struct remap_info *info = context;
922 struct bio *bio;
923
924 while ((bio = bio_list_pop(&cell->bios))) {
925 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA))
926 bio_list_add(&info->defer_bios, bio);
927 else {
928 inc_all_io_entry(info->tc->pool, bio);
929
930 /*
931 * We can't issue the bios with the bio prison lock
932 * held, so we add them to a list to issue on
933 * return from this function.
934 */
935 bio_list_add(&info->issue_bios, bio);
936 }
937 }
938}
939
Joe Thornbera374bb22014-10-10 13:43:14 +0100940static void inc_remap_and_issue_cell(struct thin_c *tc,
941 struct dm_bio_prison_cell *cell,
942 dm_block_t block)
943{
944 struct bio *bio;
Joe Thornber2d759a42014-10-10 15:27:16 +0100945 struct remap_info info;
Joe Thornbera374bb22014-10-10 13:43:14 +0100946
Joe Thornber2d759a42014-10-10 15:27:16 +0100947 info.tc = tc;
948 bio_list_init(&info.defer_bios);
949 bio_list_init(&info.issue_bios);
Joe Thornbera374bb22014-10-10 13:43:14 +0100950
Joe Thornber2d759a42014-10-10 15:27:16 +0100951 /*
952 * We have to be careful to inc any bios we're about to issue
953 * before the cell is released, and avoid a race with new bios
954 * being added to the cell.
955 */
956 cell_visit_release(tc->pool, __inc_remap_and_issue_cell,
957 &info, cell);
958
959 while ((bio = bio_list_pop(&info.defer_bios)))
960 thin_defer_bio(tc, bio);
961
962 while ((bio = bio_list_pop(&info.issue_bios)))
963 remap_and_issue(info.tc, bio, block);
Joe Thornbera374bb22014-10-10 13:43:14 +0100964}
965
Joe Thornbere49e5822012-07-27 15:08:16 +0100966static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
967{
Joe Thornber6beca5e2013-03-01 22:45:50 +0000968 cell_error(m->tc->pool, m->cell);
Joe Thornbere49e5822012-07-27 15:08:16 +0100969 list_del(&m->list);
970 mempool_free(m, m->tc->pool->mapping_pool);
971}
Joe Thornber025b9682013-03-01 22:45:50 +0000972
Mike Snitzera24c2562012-06-03 00:30:00 +0100973static void process_prepared_mapping(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000974{
975 struct thin_c *tc = m->tc;
Joe Thornber6beca5e2013-03-01 22:45:50 +0000976 struct pool *pool = tc->pool;
Mike Snitzer8b908f82015-05-13 17:53:13 -0400977 struct bio *bio = m->bio;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000978 int r;
979
Joe Thornber991d9fa2011-10-31 20:21:18 +0000980 if (m->err) {
Joe Thornber6beca5e2013-03-01 22:45:50 +0000981 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100982 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000983 }
984
985 /*
986 * Commit the prepared block into the mapping btree.
987 * Any I/O for this block arriving after this point will get
988 * remapped to it directly.
989 */
Joe Thornber34fbcf62015-04-16 12:58:35 +0100990 r = dm_thin_insert_block(tc->td, m->virt_begin, m->data_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000991 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -0500992 metadata_operation_failed(pool, "dm_thin_insert_block", r);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000993 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100994 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000995 }
996
997 /*
998 * Release any bios held while the block was being provisioned.
999 * If we are processing a write bio that completely covers the block,
1000 * we already processed it so can ignore it now when processing
1001 * the bios in the cell.
1002 */
1003 if (bio) {
Joe Thornber2d759a42014-10-10 15:27:16 +01001004 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001005 bio_endio(bio, 0);
Joe Thornber2d759a42014-10-10 15:27:16 +01001006 } else {
1007 inc_all_io_entry(tc->pool, m->cell->holder);
1008 remap_and_issue(tc, m->cell->holder, m->data_block);
1009 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
1010 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001011
Joe Thornber905386f2012-07-27 15:08:05 +01001012out:
Joe Thornber991d9fa2011-10-31 20:21:18 +00001013 list_del(&m->list);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001014 mempool_free(m, pool->mapping_pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001015}
1016
Joe Thornber34fbcf62015-04-16 12:58:35 +01001017/*----------------------------------------------------------------*/
1018
1019static void free_discard_mapping(struct dm_thin_new_mapping *m)
1020{
1021 struct thin_c *tc = m->tc;
1022 if (m->cell)
1023 cell_defer_no_holder(tc, m->cell);
1024 mempool_free(m, tc->pool->mapping_pool);
1025}
1026
Joe Thornbere49e5822012-07-27 15:08:16 +01001027static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
Joe Thornber104655f2012-03-28 18:41:28 +01001028{
Joe Thornbere49e5822012-07-27 15:08:16 +01001029 bio_io_error(m->bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001030 free_discard_mapping(m);
Joe Thornbere49e5822012-07-27 15:08:16 +01001031}
Joe Thornber104655f2012-03-28 18:41:28 +01001032
Joe Thornber34fbcf62015-04-16 12:58:35 +01001033static void process_prepared_discard_success(struct dm_thin_new_mapping *m)
Joe Thornbere49e5822012-07-27 15:08:16 +01001034{
Joe Thornber34fbcf62015-04-16 12:58:35 +01001035 bio_endio(m->bio, 0);
1036 free_discard_mapping(m);
Joe Thornber104655f2012-03-28 18:41:28 +01001037}
1038
Joe Thornber34fbcf62015-04-16 12:58:35 +01001039static void process_prepared_discard_no_passdown(struct dm_thin_new_mapping *m)
Joe Thornbere49e5822012-07-27 15:08:16 +01001040{
1041 int r;
1042 struct thin_c *tc = m->tc;
1043
Joe Thornber34fbcf62015-04-16 12:58:35 +01001044 r = dm_thin_remove_range(tc->td, m->cell->key.block_begin, m->cell->key.block_end);
1045 if (r) {
1046 metadata_operation_failed(tc->pool, "dm_thin_remove_range", r);
1047 bio_io_error(m->bio);
1048 } else
1049 bio_endio(m->bio, 0);
Joe Thornbere49e5822012-07-27 15:08:16 +01001050
Joe Thornber34fbcf62015-04-16 12:58:35 +01001051 cell_defer_no_holder(tc, m->cell);
1052 mempool_free(m, tc->pool->mapping_pool);
1053}
1054
1055static int passdown_double_checking_shared_status(struct dm_thin_new_mapping *m)
1056{
1057 /*
1058 * We've already unmapped this range of blocks, but before we
1059 * passdown we have to check that these blocks are now unused.
1060 */
1061 int r;
1062 bool used = true;
1063 struct thin_c *tc = m->tc;
1064 struct pool *pool = tc->pool;
1065 dm_block_t b = m->data_block, e, end = m->data_block + m->virt_end - m->virt_begin;
1066
1067 while (b != end) {
1068 /* find start of unmapped run */
1069 for (; b < end; b++) {
1070 r = dm_pool_block_is_used(pool->pmd, b, &used);
1071 if (r)
1072 return r;
1073
1074 if (!used)
1075 break;
1076 }
1077
1078 if (b == end)
1079 break;
1080
1081 /* find end of run */
1082 for (e = b + 1; e != end; e++) {
1083 r = dm_pool_block_is_used(pool->pmd, e, &used);
1084 if (r)
1085 return r;
1086
1087 if (used)
1088 break;
1089 }
1090
1091 r = issue_discard(tc, b, e, m->bio);
1092 if (r)
1093 return r;
1094
1095 b = e;
1096 }
1097
1098 return 0;
1099}
1100
1101static void process_prepared_discard_passdown(struct dm_thin_new_mapping *m)
1102{
1103 int r;
1104 struct thin_c *tc = m->tc;
1105 struct pool *pool = tc->pool;
1106
1107 r = dm_thin_remove_range(tc->td, m->virt_begin, m->virt_end);
1108 if (r)
1109 metadata_operation_failed(pool, "dm_thin_remove_range", r);
1110
1111 else if (m->maybe_shared)
1112 r = passdown_double_checking_shared_status(m);
1113 else
1114 r = issue_discard(tc, m->data_block, m->data_block + (m->virt_end - m->virt_begin), m->bio);
1115
1116 /*
1117 * Even if r is set, there could be sub discards in flight that we
1118 * need to wait for.
1119 */
1120 bio_endio(m->bio, r);
1121 cell_defer_no_holder(tc, m->cell);
1122 mempool_free(m, pool->mapping_pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01001123}
1124
Joe Thornber104655f2012-03-28 18:41:28 +01001125static void process_prepared(struct pool *pool, struct list_head *head,
Joe Thornbere49e5822012-07-27 15:08:16 +01001126 process_mapping_fn *fn)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001127{
1128 unsigned long flags;
1129 struct list_head maps;
Mike Snitzera24c2562012-06-03 00:30:00 +01001130 struct dm_thin_new_mapping *m, *tmp;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001131
1132 INIT_LIST_HEAD(&maps);
1133 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +01001134 list_splice_init(head, &maps);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001135 spin_unlock_irqrestore(&pool->lock, flags);
1136
1137 list_for_each_entry_safe(m, tmp, &maps, list)
Joe Thornbere49e5822012-07-27 15:08:16 +01001138 (*fn)(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001139}
1140
1141/*
1142 * Deferred bio jobs.
1143 */
Joe Thornber104655f2012-03-28 18:41:28 +01001144static int io_overlaps_block(struct pool *pool, struct bio *bio)
1145{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001146 return bio->bi_iter.bi_size ==
1147 (pool->sectors_per_block << SECTOR_SHIFT);
Joe Thornber104655f2012-03-28 18:41:28 +01001148}
1149
Joe Thornber991d9fa2011-10-31 20:21:18 +00001150static int io_overwrites_block(struct pool *pool, struct bio *bio)
1151{
Joe Thornber104655f2012-03-28 18:41:28 +01001152 return (bio_data_dir(bio) == WRITE) &&
1153 io_overlaps_block(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001154}
1155
1156static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
1157 bio_end_io_t *fn)
1158{
1159 *save = bio->bi_end_io;
1160 bio->bi_end_io = fn;
1161}
1162
1163static int ensure_next_mapping(struct pool *pool)
1164{
1165 if (pool->next_mapping)
1166 return 0;
1167
1168 pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
1169
1170 return pool->next_mapping ? 0 : -ENOMEM;
1171}
1172
Mike Snitzera24c2562012-06-03 00:30:00 +01001173static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001174{
Mike Snitzer16961b02013-12-17 13:19:11 -05001175 struct dm_thin_new_mapping *m = pool->next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001176
1177 BUG_ON(!pool->next_mapping);
1178
Mike Snitzer16961b02013-12-17 13:19:11 -05001179 memset(m, 0, sizeof(struct dm_thin_new_mapping));
1180 INIT_LIST_HEAD(&m->list);
1181 m->bio = NULL;
1182
Joe Thornber991d9fa2011-10-31 20:21:18 +00001183 pool->next_mapping = NULL;
1184
Mike Snitzer16961b02013-12-17 13:19:11 -05001185 return m;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001186}
1187
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001188static void ll_zero(struct thin_c *tc, struct dm_thin_new_mapping *m,
1189 sector_t begin, sector_t end)
1190{
1191 int r;
1192 struct dm_io_region to;
1193
1194 to.bdev = tc->pool_dev->bdev;
1195 to.sector = begin;
1196 to.count = end - begin;
1197
1198 r = dm_kcopyd_zero(tc->pool->copier, 1, &to, 0, copy_complete, m);
1199 if (r < 0) {
1200 DMERR_LIMIT("dm_kcopyd_zero() failed");
1201 copy_complete(1, 1, m);
1202 }
1203}
1204
Mike Snitzer452d7a62014-10-09 19:20:21 -04001205static void remap_and_issue_overwrite(struct thin_c *tc, struct bio *bio,
Joe Thornber34fbcf62015-04-16 12:58:35 +01001206 dm_block_t data_begin,
Mike Snitzer452d7a62014-10-09 19:20:21 -04001207 struct dm_thin_new_mapping *m)
1208{
1209 struct pool *pool = tc->pool;
1210 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1211
1212 h->overwrite_mapping = m;
1213 m->bio = bio;
1214 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
1215 inc_all_io_entry(pool, bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001216 remap_and_issue(tc, bio, data_begin);
Mike Snitzer452d7a62014-10-09 19:20:21 -04001217}
1218
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001219/*
1220 * A partial copy also needs to zero the uncopied region.
1221 */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001222static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
Joe Thornber2dd9c252012-03-28 18:41:28 +01001223 struct dm_dev *origin, dm_block_t data_origin,
1224 dm_block_t data_dest,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001225 struct dm_bio_prison_cell *cell, struct bio *bio,
1226 sector_t len)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001227{
1228 int r;
1229 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +01001230 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001231
Joe Thornber991d9fa2011-10-31 20:21:18 +00001232 m->tc = tc;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001233 m->virt_begin = virt_block;
1234 m->virt_end = virt_block + 1u;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001235 m->data_block = data_dest;
1236 m->cell = cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001237
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001238 /*
1239 * quiesce action + copy action + an extra reference held for the
1240 * duration of this function (we may need to inc later for a
1241 * partial zero).
1242 */
1243 atomic_set(&m->prepare_actions, 3);
1244
Mike Snitzer44feb382012-10-12 21:02:10 +01001245 if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001246 complete_mapping_preparation(m); /* already quiesced */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001247
1248 /*
1249 * IO to pool_dev remaps to the pool target's data_dev.
1250 *
1251 * If the whole block of data is being overwritten, we can issue the
1252 * bio immediately. Otherwise we use kcopyd to clone the data first.
1253 */
Mike Snitzer452d7a62014-10-09 19:20:21 -04001254 if (io_overwrites_block(pool, bio))
1255 remap_and_issue_overwrite(tc, bio, data_dest, m);
1256 else {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001257 struct dm_io_region from, to;
1258
Joe Thornber2dd9c252012-03-28 18:41:28 +01001259 from.bdev = origin->bdev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001260 from.sector = data_origin * pool->sectors_per_block;
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001261 from.count = len;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001262
1263 to.bdev = tc->pool_dev->bdev;
1264 to.sector = data_dest * pool->sectors_per_block;
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001265 to.count = len;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001266
1267 r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
1268 0, copy_complete, m);
1269 if (r < 0) {
Mike Snitzerc3977412012-12-21 20:23:34 +00001270 DMERR_LIMIT("dm_kcopyd_copy() failed");
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001271 copy_complete(1, 1, m);
1272
1273 /*
1274 * We allow the zero to be issued, to simplify the
1275 * error path. Otherwise we'd need to start
1276 * worrying about decrementing the prepare_actions
1277 * counter.
1278 */
1279 }
1280
1281 /*
1282 * Do we need to zero a tail region?
1283 */
1284 if (len < pool->sectors_per_block && pool->pf.zero_new_blocks) {
1285 atomic_inc(&m->prepare_actions);
1286 ll_zero(tc, m,
1287 data_dest * pool->sectors_per_block + len,
1288 (data_dest + 1) * pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001289 }
1290 }
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001291
1292 complete_mapping_preparation(m); /* drop our ref */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001293}
1294
Joe Thornber2dd9c252012-03-28 18:41:28 +01001295static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
1296 dm_block_t data_origin, dm_block_t data_dest,
Mike Snitzera24c2562012-06-03 00:30:00 +01001297 struct dm_bio_prison_cell *cell, struct bio *bio)
Joe Thornber2dd9c252012-03-28 18:41:28 +01001298{
1299 schedule_copy(tc, virt_block, tc->pool_dev,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001300 data_origin, data_dest, cell, bio,
1301 tc->pool->sectors_per_block);
Joe Thornber2dd9c252012-03-28 18:41:28 +01001302}
1303
Joe Thornber991d9fa2011-10-31 20:21:18 +00001304static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
Mike Snitzera24c2562012-06-03 00:30:00 +01001305 dm_block_t data_block, struct dm_bio_prison_cell *cell,
Joe Thornber991d9fa2011-10-31 20:21:18 +00001306 struct bio *bio)
1307{
1308 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +01001309 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001310
Joe Thornber50f3c3e2014-06-13 13:57:09 +01001311 atomic_set(&m->prepare_actions, 1); /* no need to quiesce */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001312 m->tc = tc;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001313 m->virt_begin = virt_block;
1314 m->virt_end = virt_block + 1u;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001315 m->data_block = data_block;
1316 m->cell = cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001317
1318 /*
1319 * If the whole block of data is being overwritten or we are not
1320 * zeroing pre-existing data, we can issue the bio immediately.
1321 * Otherwise we use kcopyd to zero the data first.
1322 */
Mike Snitzerf8ae7522015-05-14 11:28:37 -04001323 if (pool->pf.zero_new_blocks) {
1324 if (io_overwrites_block(pool, bio))
1325 remap_and_issue_overwrite(tc, bio, data_block, m);
1326 else
1327 ll_zero(tc, m, data_block * pool->sectors_per_block,
1328 (data_block + 1) * pool->sectors_per_block);
1329 } else
Joe Thornber991d9fa2011-10-31 20:21:18 +00001330 process_prepared_mapping(m);
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001331}
Joe Thornber991d9fa2011-10-31 20:21:18 +00001332
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001333static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
1334 dm_block_t data_dest,
1335 struct dm_bio_prison_cell *cell, struct bio *bio)
1336{
1337 struct pool *pool = tc->pool;
1338 sector_t virt_block_begin = virt_block * pool->sectors_per_block;
1339 sector_t virt_block_end = (virt_block + 1) * pool->sectors_per_block;
1340
1341 if (virt_block_end <= tc->origin_size)
1342 schedule_copy(tc, virt_block, tc->origin_dev,
1343 virt_block, data_dest, cell, bio,
1344 pool->sectors_per_block);
1345
1346 else if (virt_block_begin < tc->origin_size)
1347 schedule_copy(tc, virt_block, tc->origin_dev,
1348 virt_block, data_dest, cell, bio,
1349 tc->origin_size - virt_block_begin);
1350
1351 else
1352 schedule_zero(tc, virt_block, data_dest, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001353}
1354
Joe Thornber2c43fd22014-12-11 11:12:19 +00001355static void set_pool_mode(struct pool *pool, enum pool_mode new_mode);
1356
1357static void check_for_space(struct pool *pool)
1358{
1359 int r;
1360 dm_block_t nr_free;
1361
1362 if (get_pool_mode(pool) != PM_OUT_OF_DATA_SPACE)
1363 return;
1364
1365 r = dm_pool_get_free_block_count(pool->pmd, &nr_free);
1366 if (r)
1367 return;
1368
1369 if (nr_free)
1370 set_pool_mode(pool, PM_WRITE);
1371}
1372
Joe Thornbere49e5822012-07-27 15:08:16 +01001373/*
1374 * A non-zero return indicates read_only or fail_io mode.
1375 * Many callers don't care about the return value.
1376 */
Joe Thornber020cc3b2013-12-04 15:05:36 -05001377static int commit(struct pool *pool)
Joe Thornbere49e5822012-07-27 15:08:16 +01001378{
1379 int r;
1380
Joe Thornber8d07e8a2014-05-06 16:28:14 +01001381 if (get_pool_mode(pool) >= PM_READ_ONLY)
Joe Thornbere49e5822012-07-27 15:08:16 +01001382 return -EINVAL;
1383
Joe Thornber020cc3b2013-12-04 15:05:36 -05001384 r = dm_pool_commit_metadata(pool->pmd);
Joe Thornberb5330652013-12-04 19:51:33 -05001385 if (r)
1386 metadata_operation_failed(pool, "dm_pool_commit_metadata", r);
Joe Thornber2c43fd22014-12-11 11:12:19 +00001387 else
1388 check_for_space(pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01001389
1390 return r;
1391}
1392
Joe Thornber88a66212013-12-04 20:16:12 -05001393static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks)
1394{
1395 unsigned long flags;
1396
1397 if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
1398 DMWARN("%s: reached low water mark for data device: sending event.",
1399 dm_device_name(pool->pool_md));
1400 spin_lock_irqsave(&pool->lock, flags);
1401 pool->low_water_triggered = true;
1402 spin_unlock_irqrestore(&pool->lock, flags);
1403 dm_table_event(pool->ti->table);
1404 }
1405}
1406
Joe Thornber991d9fa2011-10-31 20:21:18 +00001407static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
1408{
1409 int r;
1410 dm_block_t free_blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001411 struct pool *pool = tc->pool;
1412
Joe Thornber3e1a0692014-03-03 16:03:26 +00001413 if (WARN_ON(get_pool_mode(pool) != PM_WRITE))
Joe Thornber8d30abf2013-12-04 19:16:11 -05001414 return -EINVAL;
1415
Joe Thornber991d9fa2011-10-31 20:21:18 +00001416 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
Joe Thornberb5330652013-12-04 19:51:33 -05001417 if (r) {
1418 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001419 return r;
Joe Thornberb5330652013-12-04 19:51:33 -05001420 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001421
Joe Thornber88a66212013-12-04 20:16:12 -05001422 check_low_water_mark(pool, free_blocks);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001423
1424 if (!free_blocks) {
Mike Snitzer94563ba2013-08-22 09:56:18 -04001425 /*
1426 * Try to commit to see if that will free up some
1427 * more space.
1428 */
Joe Thornber020cc3b2013-12-04 15:05:36 -05001429 r = commit(pool);
1430 if (r)
1431 return r;
Mike Snitzer94563ba2013-08-22 09:56:18 -04001432
1433 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
Joe Thornberb5330652013-12-04 19:51:33 -05001434 if (r) {
1435 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
Mike Snitzer94563ba2013-08-22 09:56:18 -04001436 return r;
Joe Thornberb5330652013-12-04 19:51:33 -05001437 }
Mike Snitzer94563ba2013-08-22 09:56:18 -04001438
Mike Snitzer94563ba2013-08-22 09:56:18 -04001439 if (!free_blocks) {
Joe Thornber3e1a0692014-03-03 16:03:26 +00001440 set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001441 return -ENOSPC;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001442 }
1443 }
1444
1445 r = dm_pool_alloc_data_block(pool->pmd, result);
Mike Snitzer4a02b342013-12-03 12:20:57 -05001446 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05001447 metadata_operation_failed(pool, "dm_pool_alloc_data_block", r);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001448 return r;
Mike Snitzer4a02b342013-12-03 12:20:57 -05001449 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001450
1451 return 0;
1452}
1453
1454/*
1455 * If we have run out of space, queue bios until the device is
1456 * resumed, presumably after having been reloaded with more space.
1457 */
1458static void retry_on_resume(struct bio *bio)
1459{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001460 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01001461 struct thin_c *tc = h->tc;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001462 unsigned long flags;
1463
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001464 spin_lock_irqsave(&tc->lock, flags);
1465 bio_list_add(&tc->retry_on_resume_list, bio);
1466 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001467}
1468
Mike Snitzeraf918052014-05-22 14:32:51 -04001469static int should_error_unserviceable_bio(struct pool *pool)
Joe Thornber3e1a0692014-03-03 16:03:26 +00001470{
1471 enum pool_mode m = get_pool_mode(pool);
1472
1473 switch (m) {
1474 case PM_WRITE:
1475 /* Shouldn't get here */
1476 DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
Mike Snitzeraf918052014-05-22 14:32:51 -04001477 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001478
1479 case PM_OUT_OF_DATA_SPACE:
Mike Snitzeraf918052014-05-22 14:32:51 -04001480 return pool->pf.error_if_no_space ? -ENOSPC : 0;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001481
1482 case PM_READ_ONLY:
1483 case PM_FAIL:
Mike Snitzeraf918052014-05-22 14:32:51 -04001484 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001485 default:
1486 /* Shouldn't get here */
1487 DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
Mike Snitzeraf918052014-05-22 14:32:51 -04001488 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001489 }
1490}
1491
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001492static void handle_unserviceable_bio(struct pool *pool, struct bio *bio)
1493{
Mike Snitzeraf918052014-05-22 14:32:51 -04001494 int error = should_error_unserviceable_bio(pool);
1495
1496 if (error)
1497 bio_endio(bio, error);
Mike Snitzer6d162022013-12-20 18:09:02 -05001498 else
1499 retry_on_resume(bio);
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001500}
1501
Mike Snitzer399cadd2013-12-05 16:03:33 -05001502static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001503{
1504 struct bio *bio;
1505 struct bio_list bios;
Mike Snitzeraf918052014-05-22 14:32:51 -04001506 int error;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001507
Mike Snitzeraf918052014-05-22 14:32:51 -04001508 error = should_error_unserviceable_bio(pool);
1509 if (error) {
1510 cell_error_with_code(pool, cell, error);
Joe Thornber3e1a0692014-03-03 16:03:26 +00001511 return;
1512 }
1513
Joe Thornber991d9fa2011-10-31 20:21:18 +00001514 bio_list_init(&bios);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001515 cell_release(pool, cell, &bios);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001516
Mike Snitzer9d094ee2014-10-19 08:23:09 -04001517 while ((bio = bio_list_pop(&bios)))
1518 retry_on_resume(bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001519}
1520
Joe Thornber34fbcf62015-04-16 12:58:35 +01001521static void process_discard_cell_no_passdown(struct thin_c *tc,
1522 struct dm_bio_prison_cell *virt_cell)
Joe Thornber104655f2012-03-28 18:41:28 +01001523{
Joe Thornber104655f2012-03-28 18:41:28 +01001524 struct pool *pool = tc->pool;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001525 struct dm_thin_new_mapping *m = get_next_mapping(pool);
1526
1527 /*
1528 * We don't need to lock the data blocks, since there's no
1529 * passdown. We only lock data blocks for allocation and breaking sharing.
1530 */
1531 m->tc = tc;
1532 m->virt_begin = virt_cell->key.block_begin;
1533 m->virt_end = virt_cell->key.block_end;
1534 m->cell = virt_cell;
1535 m->bio = virt_cell->holder;
1536
1537 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
1538 pool->process_prepared_discard(m);
1539}
1540
1541/*
1542 * FIXME: DM local hack to defer parent bios's end_io until we
1543 * _know_ all chained sub range discard bios have completed.
1544 * Will go away once late bio splitting lands upstream!
1545 */
1546static inline void __bio_inc_remaining(struct bio *bio)
1547{
1548 bio->bi_flags |= (1 << BIO_CHAIN);
1549 smp_mb__before_atomic();
1550 atomic_inc(&bio->__bi_remaining);
1551}
1552
1553static void break_up_discard_bio(struct thin_c *tc, dm_block_t begin, dm_block_t end,
1554 struct bio *bio)
1555{
1556 struct pool *pool = tc->pool;
1557
1558 int r;
1559 bool maybe_shared;
1560 struct dm_cell_key data_key;
1561 struct dm_bio_prison_cell *data_cell;
Mike Snitzera24c2562012-06-03 00:30:00 +01001562 struct dm_thin_new_mapping *m;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001563 dm_block_t virt_begin, virt_end, data_begin;
Joe Thornber104655f2012-03-28 18:41:28 +01001564
Joe Thornber34fbcf62015-04-16 12:58:35 +01001565 while (begin != end) {
1566 r = ensure_next_mapping(pool);
1567 if (r)
1568 /* we did our best */
1569 return;
Joe Thornber104655f2012-03-28 18:41:28 +01001570
Joe Thornber34fbcf62015-04-16 12:58:35 +01001571 r = dm_thin_find_mapped_range(tc->td, begin, end, &virt_begin, &virt_end,
1572 &data_begin, &maybe_shared);
1573 if (r)
1574 /*
1575 * Silently fail, letting any mappings we've
1576 * created complete.
1577 */
Joe Thornber104655f2012-03-28 18:41:28 +01001578 break;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001579
1580 build_key(tc->td, PHYSICAL, data_begin, data_begin + (virt_end - virt_begin), &data_key);
1581 if (bio_detain(tc->pool, &data_key, NULL, &data_cell)) {
1582 /* contention, we'll give up with this range */
1583 begin = virt_end;
1584 continue;
Joe Thornber104655f2012-03-28 18:41:28 +01001585 }
1586
Joe Thornber104655f2012-03-28 18:41:28 +01001587 /*
Joe Thornber34fbcf62015-04-16 12:58:35 +01001588 * IO may still be going to the destination block. We must
1589 * quiesce before we can do the removal.
Joe Thornber104655f2012-03-28 18:41:28 +01001590 */
Joe Thornber34fbcf62015-04-16 12:58:35 +01001591 m = get_next_mapping(pool);
1592 m->tc = tc;
1593 m->maybe_shared = maybe_shared;
1594 m->virt_begin = virt_begin;
1595 m->virt_end = virt_end;
1596 m->data_block = data_begin;
1597 m->cell = data_cell;
1598 m->bio = bio;
Joe Thornber104655f2012-03-28 18:41:28 +01001599
Joe Thornber34fbcf62015-04-16 12:58:35 +01001600 /*
1601 * The parent bio must not complete before sub discard bios are
1602 * chained to it (see __blkdev_issue_discard_async's bio_chain)!
1603 *
1604 * This per-mapping bi_remaining increment is paired with
1605 * the implicit decrement that occurs via bio_endio() in
1606 * process_prepared_discard_{passdown,no_passdown}.
1607 */
1608 __bio_inc_remaining(bio);
1609 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
1610 pool->process_prepared_discard(m);
1611
1612 begin = virt_end;
Joe Thornber104655f2012-03-28 18:41:28 +01001613 }
1614}
1615
Joe Thornber34fbcf62015-04-16 12:58:35 +01001616static void process_discard_cell_passdown(struct thin_c *tc, struct dm_bio_prison_cell *virt_cell)
1617{
1618 struct bio *bio = virt_cell->holder;
1619 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1620
1621 /*
1622 * The virt_cell will only get freed once the origin bio completes.
1623 * This means it will remain locked while all the individual
1624 * passdown bios are in flight.
1625 */
1626 h->cell = virt_cell;
1627 break_up_discard_bio(tc, virt_cell->key.block_begin, virt_cell->key.block_end, bio);
1628
1629 /*
1630 * We complete the bio now, knowing that the bi_remaining field
1631 * will prevent completion until the sub range discards have
1632 * completed.
1633 */
1634 bio_endio(bio, 0);
1635}
1636
Joe Thornbera374bb22014-10-10 13:43:14 +01001637static void process_discard_bio(struct thin_c *tc, struct bio *bio)
1638{
Joe Thornber34fbcf62015-04-16 12:58:35 +01001639 dm_block_t begin, end;
1640 struct dm_cell_key virt_key;
1641 struct dm_bio_prison_cell *virt_cell;
Joe Thornbera374bb22014-10-10 13:43:14 +01001642
Joe Thornber34fbcf62015-04-16 12:58:35 +01001643 get_bio_block_range(tc, bio, &begin, &end);
1644 if (begin == end) {
1645 /*
1646 * The discard covers less than a block.
1647 */
1648 bio_endio(bio, 0);
1649 return;
1650 }
1651
1652 build_key(tc->td, VIRTUAL, begin, end, &virt_key);
1653 if (bio_detain(tc->pool, &virt_key, bio, &virt_cell))
1654 /*
1655 * Potential starvation issue: We're relying on the
1656 * fs/application being well behaved, and not trying to
1657 * send IO to a region at the same time as discarding it.
1658 * If they do this persistently then it's possible this
1659 * cell will never be granted.
1660 */
Joe Thornbera374bb22014-10-10 13:43:14 +01001661 return;
1662
Joe Thornber34fbcf62015-04-16 12:58:35 +01001663 tc->pool->process_discard_cell(tc, virt_cell);
Joe Thornbera374bb22014-10-10 13:43:14 +01001664}
1665
Joe Thornber991d9fa2011-10-31 20:21:18 +00001666static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzer44feb382012-10-12 21:02:10 +01001667 struct dm_cell_key *key,
Joe Thornber991d9fa2011-10-31 20:21:18 +00001668 struct dm_thin_lookup_result *lookup_result,
Mike Snitzera24c2562012-06-03 00:30:00 +01001669 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001670{
1671 int r;
1672 dm_block_t data_block;
Mike Snitzerd6fc2042013-08-21 17:40:11 -04001673 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001674
1675 r = alloc_data_block(tc, &data_block);
1676 switch (r) {
1677 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001678 schedule_internal_copy(tc, block, lookup_result->block,
1679 data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001680 break;
1681
1682 case -ENOSPC:
Mike Snitzer399cadd2013-12-05 16:03:33 -05001683 retry_bios_on_resume(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001684 break;
1685
1686 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001687 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1688 __func__, r);
Mike Snitzerd6fc2042013-08-21 17:40:11 -04001689 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001690 break;
1691 }
1692}
1693
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001694static void __remap_and_issue_shared_cell(void *context,
1695 struct dm_bio_prison_cell *cell)
1696{
1697 struct remap_info *info = context;
1698 struct bio *bio;
1699
1700 while ((bio = bio_list_pop(&cell->bios))) {
1701 if ((bio_data_dir(bio) == WRITE) ||
1702 (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)))
1703 bio_list_add(&info->defer_bios, bio);
1704 else {
1705 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));;
1706
1707 h->shared_read_entry = dm_deferred_entry_inc(info->tc->pool->shared_read_ds);
1708 inc_all_io_entry(info->tc->pool, bio);
1709 bio_list_add(&info->issue_bios, bio);
1710 }
1711 }
1712}
1713
1714static void remap_and_issue_shared_cell(struct thin_c *tc,
1715 struct dm_bio_prison_cell *cell,
1716 dm_block_t block)
1717{
1718 struct bio *bio;
1719 struct remap_info info;
1720
1721 info.tc = tc;
1722 bio_list_init(&info.defer_bios);
1723 bio_list_init(&info.issue_bios);
1724
1725 cell_visit_release(tc->pool, __remap_and_issue_shared_cell,
1726 &info, cell);
1727
1728 while ((bio = bio_list_pop(&info.defer_bios)))
1729 thin_defer_bio(tc, bio);
1730
1731 while ((bio = bio_list_pop(&info.issue_bios)))
1732 remap_and_issue(tc, bio, block);
1733}
1734
Joe Thornber991d9fa2011-10-31 20:21:18 +00001735static void process_shared_bio(struct thin_c *tc, struct bio *bio,
1736 dm_block_t block,
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001737 struct dm_thin_lookup_result *lookup_result,
1738 struct dm_bio_prison_cell *virt_cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001739{
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001740 struct dm_bio_prison_cell *data_cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001741 struct pool *pool = tc->pool;
Mike Snitzer44feb382012-10-12 21:02:10 +01001742 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001743
1744 /*
1745 * If cell is already occupied, then sharing is already in the process
1746 * of being broken so we have nothing further to do here.
1747 */
1748 build_data_key(tc->td, lookup_result->block, &key);
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001749 if (bio_detain(pool, &key, bio, &data_cell)) {
1750 cell_defer_no_holder(tc, virt_cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001751 return;
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001752 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001753
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001754 if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size) {
1755 break_sharing(tc, bio, block, &key, lookup_result, data_cell);
1756 cell_defer_no_holder(tc, virt_cell);
1757 } else {
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001758 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornber991d9fa2011-10-31 20:21:18 +00001759
Mike Snitzer44feb382012-10-12 21:02:10 +01001760 h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
Joe Thornbere8088072012-12-21 20:23:31 +00001761 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001762 remap_and_issue(tc, bio, lookup_result->block);
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001763
1764 remap_and_issue_shared_cell(tc, data_cell, lookup_result->block);
1765 remap_and_issue_shared_cell(tc, virt_cell, lookup_result->block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001766 }
1767}
1768
1769static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzera24c2562012-06-03 00:30:00 +01001770 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001771{
1772 int r;
1773 dm_block_t data_block;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001774 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001775
1776 /*
1777 * Remap empty bios (flushes) immediately, without provisioning.
1778 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07001779 if (!bio->bi_iter.bi_size) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001780 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001781 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001782
Joe Thornber991d9fa2011-10-31 20:21:18 +00001783 remap_and_issue(tc, bio, 0);
1784 return;
1785 }
1786
1787 /*
1788 * Fill read bios with zeroes and complete them immediately.
1789 */
1790 if (bio_data_dir(bio) == READ) {
1791 zero_fill_bio(bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001792 cell_defer_no_holder(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001793 bio_endio(bio, 0);
1794 return;
1795 }
1796
1797 r = alloc_data_block(tc, &data_block);
1798 switch (r) {
1799 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001800 if (tc->origin_dev)
1801 schedule_external_copy(tc, block, data_block, cell, bio);
1802 else
1803 schedule_zero(tc, block, data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001804 break;
1805
1806 case -ENOSPC:
Mike Snitzer399cadd2013-12-05 16:03:33 -05001807 retry_bios_on_resume(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001808 break;
1809
1810 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001811 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1812 __func__, r);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001813 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001814 break;
1815 }
1816}
1817
Joe Thornbera374bb22014-10-10 13:43:14 +01001818static void process_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001819{
1820 int r;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001821 struct pool *pool = tc->pool;
Joe Thornbera374bb22014-10-10 13:43:14 +01001822 struct bio *bio = cell->holder;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001823 dm_block_t block = get_bio_block(tc, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001824 struct dm_thin_lookup_result lookup_result;
1825
Joe Thornbera374bb22014-10-10 13:43:14 +01001826 if (tc->requeue_mode) {
1827 cell_requeue(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001828 return;
Joe Thornbera374bb22014-10-10 13:43:14 +01001829 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001830
1831 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1832 switch (r) {
1833 case 0:
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001834 if (lookup_result.shared)
1835 process_shared_bio(tc, bio, block, &lookup_result, cell);
1836 else {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001837 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001838 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbera374bb22014-10-10 13:43:14 +01001839 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001840 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001841 break;
1842
1843 case -ENODATA:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001844 if (bio_data_dir(bio) == READ && tc->origin_dev) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001845 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001846 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001847
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001848 if (bio_end_sector(bio) <= tc->origin_size)
1849 remap_to_origin_and_issue(tc, bio);
1850
1851 else if (bio->bi_iter.bi_sector < tc->origin_size) {
1852 zero_fill_bio(bio);
1853 bio->bi_iter.bi_size = (tc->origin_size - bio->bi_iter.bi_sector) << SECTOR_SHIFT;
1854 remap_to_origin_and_issue(tc, bio);
1855
1856 } else {
1857 zero_fill_bio(bio);
1858 bio_endio(bio, 0);
1859 }
Joe Thornber2dd9c252012-03-28 18:41:28 +01001860 } else
1861 provision_block(tc, bio, block, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001862 break;
1863
1864 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001865 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1866 __func__, r);
Joe Thornberf286ba02012-12-21 20:23:33 +00001867 cell_defer_no_holder(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001868 bio_io_error(bio);
1869 break;
1870 }
1871}
1872
Joe Thornbera374bb22014-10-10 13:43:14 +01001873static void process_bio(struct thin_c *tc, struct bio *bio)
1874{
1875 struct pool *pool = tc->pool;
1876 dm_block_t block = get_bio_block(tc, bio);
1877 struct dm_bio_prison_cell *cell;
1878 struct dm_cell_key key;
1879
1880 /*
1881 * If cell is already occupied, then the block is already
1882 * being provisioned so we have nothing further to do here.
1883 */
1884 build_virtual_key(tc->td, block, &key);
1885 if (bio_detain(pool, &key, bio, &cell))
1886 return;
1887
1888 process_cell(tc, cell);
1889}
1890
1891static void __process_bio_read_only(struct thin_c *tc, struct bio *bio,
1892 struct dm_bio_prison_cell *cell)
Joe Thornbere49e5822012-07-27 15:08:16 +01001893{
1894 int r;
1895 int rw = bio_data_dir(bio);
1896 dm_block_t block = get_bio_block(tc, bio);
1897 struct dm_thin_lookup_result lookup_result;
1898
1899 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1900 switch (r) {
1901 case 0:
Joe Thornbera374bb22014-10-10 13:43:14 +01001902 if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size) {
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001903 handle_unserviceable_bio(tc->pool, bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01001904 if (cell)
1905 cell_defer_no_holder(tc, cell);
1906 } else {
Joe Thornbere8088072012-12-21 20:23:31 +00001907 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001908 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbera374bb22014-10-10 13:43:14 +01001909 if (cell)
1910 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001911 }
Joe Thornbere49e5822012-07-27 15:08:16 +01001912 break;
1913
1914 case -ENODATA:
Joe Thornbera374bb22014-10-10 13:43:14 +01001915 if (cell)
1916 cell_defer_no_holder(tc, cell);
Joe Thornbere49e5822012-07-27 15:08:16 +01001917 if (rw != READ) {
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001918 handle_unserviceable_bio(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001919 break;
1920 }
1921
1922 if (tc->origin_dev) {
Joe Thornbere8088072012-12-21 20:23:31 +00001923 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001924 remap_to_origin_and_issue(tc, bio);
1925 break;
1926 }
1927
1928 zero_fill_bio(bio);
1929 bio_endio(bio, 0);
1930 break;
1931
1932 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001933 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1934 __func__, r);
Joe Thornbera374bb22014-10-10 13:43:14 +01001935 if (cell)
1936 cell_defer_no_holder(tc, cell);
Joe Thornbere49e5822012-07-27 15:08:16 +01001937 bio_io_error(bio);
1938 break;
1939 }
1940}
1941
Joe Thornbera374bb22014-10-10 13:43:14 +01001942static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
1943{
1944 __process_bio_read_only(tc, bio, NULL);
1945}
1946
1947static void process_cell_read_only(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1948{
1949 __process_bio_read_only(tc, cell->holder, cell);
1950}
1951
Joe Thornber3e1a0692014-03-03 16:03:26 +00001952static void process_bio_success(struct thin_c *tc, struct bio *bio)
1953{
1954 bio_endio(bio, 0);
1955}
1956
Joe Thornbere49e5822012-07-27 15:08:16 +01001957static void process_bio_fail(struct thin_c *tc, struct bio *bio)
1958{
1959 bio_io_error(bio);
1960}
1961
Joe Thornbera374bb22014-10-10 13:43:14 +01001962static void process_cell_success(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1963{
1964 cell_success(tc->pool, cell);
1965}
1966
1967static void process_cell_fail(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1968{
1969 cell_error(tc->pool, cell);
1970}
1971
Joe Thornberac8c3f32013-05-10 14:37:21 +01001972/*
1973 * FIXME: should we also commit due to size of transaction, measured in
1974 * metadata blocks?
1975 */
Joe Thornber905e51b2012-03-28 18:41:27 +01001976static int need_commit_due_to_time(struct pool *pool)
1977{
Manuel Schölling0f30af92014-05-22 22:42:37 +02001978 return !time_in_range(jiffies, pool->last_commit_jiffies,
1979 pool->last_commit_jiffies + COMMIT_PERIOD);
Joe Thornber905e51b2012-03-28 18:41:27 +01001980}
1981
Mike Snitzer67324ea2014-03-21 18:33:41 -04001982#define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node)
1983#define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook))
1984
1985static void __thin_bio_rb_add(struct thin_c *tc, struct bio *bio)
1986{
1987 struct rb_node **rbp, *parent;
1988 struct dm_thin_endio_hook *pbd;
1989 sector_t bi_sector = bio->bi_iter.bi_sector;
1990
1991 rbp = &tc->sort_bio_list.rb_node;
1992 parent = NULL;
1993 while (*rbp) {
1994 parent = *rbp;
1995 pbd = thin_pbd(parent);
1996
1997 if (bi_sector < thin_bio(pbd)->bi_iter.bi_sector)
1998 rbp = &(*rbp)->rb_left;
1999 else
2000 rbp = &(*rbp)->rb_right;
2001 }
2002
2003 pbd = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
2004 rb_link_node(&pbd->rb_node, parent, rbp);
2005 rb_insert_color(&pbd->rb_node, &tc->sort_bio_list);
2006}
2007
2008static void __extract_sorted_bios(struct thin_c *tc)
2009{
2010 struct rb_node *node;
2011 struct dm_thin_endio_hook *pbd;
2012 struct bio *bio;
2013
2014 for (node = rb_first(&tc->sort_bio_list); node; node = rb_next(node)) {
2015 pbd = thin_pbd(node);
2016 bio = thin_bio(pbd);
2017
2018 bio_list_add(&tc->deferred_bio_list, bio);
2019 rb_erase(&pbd->rb_node, &tc->sort_bio_list);
2020 }
2021
2022 WARN_ON(!RB_EMPTY_ROOT(&tc->sort_bio_list));
2023}
2024
2025static void __sort_thin_deferred_bios(struct thin_c *tc)
2026{
2027 struct bio *bio;
2028 struct bio_list bios;
2029
2030 bio_list_init(&bios);
2031 bio_list_merge(&bios, &tc->deferred_bio_list);
2032 bio_list_init(&tc->deferred_bio_list);
2033
2034 /* Sort deferred_bio_list using rb-tree */
2035 while ((bio = bio_list_pop(&bios)))
2036 __thin_bio_rb_add(tc, bio);
2037
2038 /*
2039 * Transfer the sorted bios in sort_bio_list back to
2040 * deferred_bio_list to allow lockless submission of
2041 * all bios.
2042 */
2043 __extract_sorted_bios(tc);
2044}
2045
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002046static void process_thin_deferred_bios(struct thin_c *tc)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002047{
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002048 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002049 unsigned long flags;
2050 struct bio *bio;
2051 struct bio_list bios;
Mike Snitzer67324ea2014-03-21 18:33:41 -04002052 struct blk_plug plug;
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002053 unsigned count = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002054
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002055 if (tc->requeue_mode) {
Mike Snitzer42d6a8c2014-10-19 07:52:44 -04002056 error_thin_bio_list(tc, &tc->deferred_bio_list, DM_ENDIO_REQUEUE);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002057 return;
2058 }
2059
Joe Thornber991d9fa2011-10-31 20:21:18 +00002060 bio_list_init(&bios);
2061
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002062 spin_lock_irqsave(&tc->lock, flags);
Mike Snitzer67324ea2014-03-21 18:33:41 -04002063
2064 if (bio_list_empty(&tc->deferred_bio_list)) {
2065 spin_unlock_irqrestore(&tc->lock, flags);
2066 return;
2067 }
2068
2069 __sort_thin_deferred_bios(tc);
2070
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002071 bio_list_merge(&bios, &tc->deferred_bio_list);
2072 bio_list_init(&tc->deferred_bio_list);
Mike Snitzer67324ea2014-03-21 18:33:41 -04002073
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002074 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002075
Mike Snitzer67324ea2014-03-21 18:33:41 -04002076 blk_start_plug(&plug);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002077 while ((bio = bio_list_pop(&bios))) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00002078 /*
2079 * If we've got no free new_mapping structs, and processing
2080 * this bio might require one, we pause until there are some
2081 * prepared mappings to process.
2082 */
2083 if (ensure_next_mapping(pool)) {
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002084 spin_lock_irqsave(&tc->lock, flags);
2085 bio_list_add(&tc->deferred_bio_list, bio);
2086 bio_list_merge(&tc->deferred_bio_list, &bios);
2087 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002088 break;
2089 }
Joe Thornber104655f2012-03-28 18:41:28 +01002090
2091 if (bio->bi_rw & REQ_DISCARD)
Joe Thornbere49e5822012-07-27 15:08:16 +01002092 pool->process_discard(tc, bio);
Joe Thornber104655f2012-03-28 18:41:28 +01002093 else
Joe Thornbere49e5822012-07-27 15:08:16 +01002094 pool->process_bio(tc, bio);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002095
2096 if ((count++ & 127) == 0) {
Joe Thornber7d327fe2014-10-06 15:45:59 +01002097 throttle_work_update(&pool->throttle);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002098 dm_pool_issue_prefetches(pool->pmd);
2099 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002100 }
Mike Snitzer67324ea2014-03-21 18:33:41 -04002101 blk_finish_plug(&plug);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002102}
2103
Joe Thornberac4c3f32014-10-10 16:42:10 +01002104static int cmp_cells(const void *lhs, const void *rhs)
2105{
2106 struct dm_bio_prison_cell *lhs_cell = *((struct dm_bio_prison_cell **) lhs);
2107 struct dm_bio_prison_cell *rhs_cell = *((struct dm_bio_prison_cell **) rhs);
2108
2109 BUG_ON(!lhs_cell->holder);
2110 BUG_ON(!rhs_cell->holder);
2111
2112 if (lhs_cell->holder->bi_iter.bi_sector < rhs_cell->holder->bi_iter.bi_sector)
2113 return -1;
2114
2115 if (lhs_cell->holder->bi_iter.bi_sector > rhs_cell->holder->bi_iter.bi_sector)
2116 return 1;
2117
2118 return 0;
2119}
2120
2121static unsigned sort_cells(struct pool *pool, struct list_head *cells)
2122{
2123 unsigned count = 0;
2124 struct dm_bio_prison_cell *cell, *tmp;
2125
2126 list_for_each_entry_safe(cell, tmp, cells, user_list) {
2127 if (count >= CELL_SORT_ARRAY_SIZE)
2128 break;
2129
2130 pool->cell_sort_array[count++] = cell;
2131 list_del(&cell->user_list);
2132 }
2133
2134 sort(pool->cell_sort_array, count, sizeof(cell), cmp_cells, NULL);
2135
2136 return count;
2137}
2138
Joe Thornbera374bb22014-10-10 13:43:14 +01002139static void process_thin_deferred_cells(struct thin_c *tc)
2140{
2141 struct pool *pool = tc->pool;
2142 unsigned long flags;
2143 struct list_head cells;
Joe Thornberac4c3f32014-10-10 16:42:10 +01002144 struct dm_bio_prison_cell *cell;
2145 unsigned i, j, count;
Joe Thornbera374bb22014-10-10 13:43:14 +01002146
2147 INIT_LIST_HEAD(&cells);
2148
2149 spin_lock_irqsave(&tc->lock, flags);
2150 list_splice_init(&tc->deferred_cells, &cells);
2151 spin_unlock_irqrestore(&tc->lock, flags);
2152
2153 if (list_empty(&cells))
2154 return;
2155
Joe Thornberac4c3f32014-10-10 16:42:10 +01002156 do {
2157 count = sort_cells(tc->pool, &cells);
Joe Thornbera374bb22014-10-10 13:43:14 +01002158
Joe Thornberac4c3f32014-10-10 16:42:10 +01002159 for (i = 0; i < count; i++) {
2160 cell = pool->cell_sort_array[i];
2161 BUG_ON(!cell->holder);
2162
2163 /*
2164 * If we've got no free new_mapping structs, and processing
2165 * this bio might require one, we pause until there are some
2166 * prepared mappings to process.
2167 */
2168 if (ensure_next_mapping(pool)) {
2169 for (j = i; j < count; j++)
2170 list_add(&pool->cell_sort_array[j]->user_list, &cells);
2171
2172 spin_lock_irqsave(&tc->lock, flags);
2173 list_splice(&cells, &tc->deferred_cells);
2174 spin_unlock_irqrestore(&tc->lock, flags);
2175 return;
2176 }
2177
2178 if (cell->holder->bi_rw & REQ_DISCARD)
2179 pool->process_discard_cell(tc, cell);
2180 else
2181 pool->process_cell(tc, cell);
Joe Thornbera374bb22014-10-10 13:43:14 +01002182 }
Joe Thornberac4c3f32014-10-10 16:42:10 +01002183 } while (!list_empty(&cells));
Joe Thornbera374bb22014-10-10 13:43:14 +01002184}
2185
Joe Thornberb10ebd32014-04-08 11:29:01 +01002186static void thin_get(struct thin_c *tc);
2187static void thin_put(struct thin_c *tc);
2188
2189/*
2190 * We can't hold rcu_read_lock() around code that can block. So we
2191 * find a thin with the rcu lock held; bump a refcount; then drop
2192 * the lock.
2193 */
2194static struct thin_c *get_first_thin(struct pool *pool)
2195{
2196 struct thin_c *tc = NULL;
2197
2198 rcu_read_lock();
2199 if (!list_empty(&pool->active_thins)) {
2200 tc = list_entry_rcu(pool->active_thins.next, struct thin_c, list);
2201 thin_get(tc);
2202 }
2203 rcu_read_unlock();
2204
2205 return tc;
2206}
2207
2208static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc)
2209{
2210 struct thin_c *old_tc = tc;
2211
2212 rcu_read_lock();
2213 list_for_each_entry_continue_rcu(tc, &pool->active_thins, list) {
2214 thin_get(tc);
2215 thin_put(old_tc);
2216 rcu_read_unlock();
2217 return tc;
2218 }
2219 thin_put(old_tc);
2220 rcu_read_unlock();
2221
2222 return NULL;
2223}
2224
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002225static void process_deferred_bios(struct pool *pool)
2226{
2227 unsigned long flags;
2228 struct bio *bio;
2229 struct bio_list bios;
2230 struct thin_c *tc;
2231
Joe Thornberb10ebd32014-04-08 11:29:01 +01002232 tc = get_first_thin(pool);
2233 while (tc) {
Joe Thornbera374bb22014-10-10 13:43:14 +01002234 process_thin_deferred_cells(tc);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002235 process_thin_deferred_bios(tc);
Joe Thornberb10ebd32014-04-08 11:29:01 +01002236 tc = get_next_thin(pool, tc);
2237 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002238
2239 /*
2240 * If there are any deferred flush bios, we must commit
2241 * the metadata before issuing them.
2242 */
2243 bio_list_init(&bios);
2244 spin_lock_irqsave(&pool->lock, flags);
2245 bio_list_merge(&bios, &pool->deferred_flush_bios);
2246 bio_list_init(&pool->deferred_flush_bios);
2247 spin_unlock_irqrestore(&pool->lock, flags);
2248
Mike Snitzer4d1662a2014-02-06 06:08:56 -05002249 if (bio_list_empty(&bios) &&
2250 !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool)))
Joe Thornber991d9fa2011-10-31 20:21:18 +00002251 return;
2252
Joe Thornber020cc3b2013-12-04 15:05:36 -05002253 if (commit(pool)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00002254 while ((bio = bio_list_pop(&bios)))
2255 bio_io_error(bio);
2256 return;
2257 }
Joe Thornber905e51b2012-03-28 18:41:27 +01002258 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002259
2260 while ((bio = bio_list_pop(&bios)))
2261 generic_make_request(bio);
2262}
2263
2264static void do_worker(struct work_struct *ws)
2265{
2266 struct pool *pool = container_of(ws, struct pool, worker);
2267
Joe Thornber7d327fe2014-10-06 15:45:59 +01002268 throttle_work_start(&pool->throttle);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002269 dm_pool_issue_prefetches(pool->pmd);
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_mappings, &pool->process_prepared_mapping);
Joe Thornber7d327fe2014-10-06 15:45:59 +01002272 throttle_work_update(&pool->throttle);
Joe Thornbere49e5822012-07-27 15:08:16 +01002273 process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
Joe Thornber7d327fe2014-10-06 15:45:59 +01002274 throttle_work_update(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002275 process_deferred_bios(pool);
Joe Thornber7d327fe2014-10-06 15:45:59 +01002276 throttle_work_complete(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002277}
2278
Joe Thornber905e51b2012-03-28 18:41:27 +01002279/*
2280 * We want to commit periodically so that not too much
2281 * unwritten data builds up.
2282 */
2283static void do_waker(struct work_struct *ws)
2284{
2285 struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
2286 wake_worker(pool);
2287 queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
2288}
2289
Mike Snitzerbcc696f2015-07-15 16:52:04 -04002290static void notify_of_pool_mode_change_to_oods(struct pool *pool);
2291
Joe Thornber85ad643b2014-05-09 15:59:38 +01002292/*
2293 * We're holding onto IO to allow userland time to react. After the
2294 * timeout either the pool will have been resized (and thus back in
Mike Snitzerbcc696f2015-07-15 16:52:04 -04002295 * PM_WRITE mode), or we degrade to PM_OUT_OF_DATA_SPACE w/ error_if_no_space.
Joe Thornber85ad643b2014-05-09 15:59:38 +01002296 */
2297static void do_no_space_timeout(struct work_struct *ws)
2298{
2299 struct pool *pool = container_of(to_delayed_work(ws), struct pool,
2300 no_space_timeout);
2301
Mike Snitzerbcc696f2015-07-15 16:52:04 -04002302 if (get_pool_mode(pool) == PM_OUT_OF_DATA_SPACE && !pool->pf.error_if_no_space) {
2303 pool->pf.error_if_no_space = true;
2304 notify_of_pool_mode_change_to_oods(pool);
Mike Snitzer0a927c22015-07-21 13:20:46 -04002305 error_retry_list_with_code(pool, -ENOSPC);
Mike Snitzerbcc696f2015-07-15 16:52:04 -04002306 }
Joe Thornber85ad643b2014-05-09 15:59:38 +01002307}
2308
Joe Thornber991d9fa2011-10-31 20:21:18 +00002309/*----------------------------------------------------------------*/
2310
Joe Thornbere7a3e872014-05-13 16:14:14 -04002311struct pool_work {
Joe Thornber738211f2014-03-03 15:52:28 +00002312 struct work_struct worker;
Joe Thornbere7a3e872014-05-13 16:14:14 -04002313 struct completion complete;
Joe Thornber738211f2014-03-03 15:52:28 +00002314};
2315
Joe Thornbere7a3e872014-05-13 16:14:14 -04002316static struct pool_work *to_pool_work(struct work_struct *ws)
Joe Thornber738211f2014-03-03 15:52:28 +00002317{
Joe Thornbere7a3e872014-05-13 16:14:14 -04002318 return container_of(ws, struct pool_work, worker);
2319}
2320
2321static void pool_work_complete(struct pool_work *pw)
2322{
2323 complete(&pw->complete);
2324}
2325
2326static void pool_work_wait(struct pool_work *pw, struct pool *pool,
2327 void (*fn)(struct work_struct *))
2328{
2329 INIT_WORK_ONSTACK(&pw->worker, fn);
2330 init_completion(&pw->complete);
2331 queue_work(pool->wq, &pw->worker);
2332 wait_for_completion(&pw->complete);
2333}
2334
2335/*----------------------------------------------------------------*/
2336
2337struct noflush_work {
2338 struct pool_work pw;
2339 struct thin_c *tc;
2340};
2341
2342static struct noflush_work *to_noflush(struct work_struct *ws)
2343{
2344 return container_of(to_pool_work(ws), struct noflush_work, pw);
Joe Thornber738211f2014-03-03 15:52:28 +00002345}
2346
2347static void do_noflush_start(struct work_struct *ws)
2348{
Joe Thornbere7a3e872014-05-13 16:14:14 -04002349 struct noflush_work *w = to_noflush(ws);
Joe Thornber738211f2014-03-03 15:52:28 +00002350 w->tc->requeue_mode = true;
2351 requeue_io(w->tc);
Joe Thornbere7a3e872014-05-13 16:14:14 -04002352 pool_work_complete(&w->pw);
Joe Thornber738211f2014-03-03 15:52:28 +00002353}
2354
2355static void do_noflush_stop(struct work_struct *ws)
2356{
Joe Thornbere7a3e872014-05-13 16:14:14 -04002357 struct noflush_work *w = to_noflush(ws);
Joe Thornber738211f2014-03-03 15:52:28 +00002358 w->tc->requeue_mode = false;
Joe Thornbere7a3e872014-05-13 16:14:14 -04002359 pool_work_complete(&w->pw);
Joe Thornber738211f2014-03-03 15:52:28 +00002360}
2361
2362static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *))
2363{
2364 struct noflush_work w;
2365
Joe Thornber738211f2014-03-03 15:52:28 +00002366 w.tc = tc;
Joe Thornbere7a3e872014-05-13 16:14:14 -04002367 pool_work_wait(&w.pw, tc->pool, fn);
Joe Thornber738211f2014-03-03 15:52:28 +00002368}
2369
2370/*----------------------------------------------------------------*/
2371
Joe Thornbere49e5822012-07-27 15:08:16 +01002372static enum pool_mode get_pool_mode(struct pool *pool)
2373{
2374 return pool->pf.mode;
2375}
2376
Joe Thornber3e1a0692014-03-03 16:03:26 +00002377static void notify_of_pool_mode_change(struct pool *pool, const char *new_mode)
2378{
2379 dm_table_event(pool->ti->table);
2380 DMINFO("%s: switching pool to %s mode",
2381 dm_device_name(pool->pool_md), new_mode);
2382}
2383
Mike Snitzerbcc696f2015-07-15 16:52:04 -04002384static void notify_of_pool_mode_change_to_oods(struct pool *pool)
2385{
2386 if (!pool->pf.error_if_no_space)
2387 notify_of_pool_mode_change(pool, "out-of-data-space (queue IO)");
2388 else
2389 notify_of_pool_mode_change(pool, "out-of-data-space (error IO)");
2390}
2391
Joe Thornber34fbcf62015-04-16 12:58:35 +01002392static bool passdown_enabled(struct pool_c *pt)
2393{
2394 return pt->adjusted_pf.discard_passdown;
2395}
2396
2397static void set_discard_callbacks(struct pool *pool)
2398{
2399 struct pool_c *pt = pool->ti->private;
2400
2401 if (passdown_enabled(pt)) {
2402 pool->process_discard_cell = process_discard_cell_passdown;
2403 pool->process_prepared_discard = process_prepared_discard_passdown;
2404 } else {
2405 pool->process_discard_cell = process_discard_cell_no_passdown;
2406 pool->process_prepared_discard = process_prepared_discard_no_passdown;
2407 }
2408}
2409
Mike Snitzer8b64e882013-12-20 14:27:28 -05002410static void set_pool_mode(struct pool *pool, enum pool_mode new_mode)
Joe Thornbere49e5822012-07-27 15:08:16 +01002411{
Mike Snitzercdc2b412014-02-14 18:10:55 -05002412 struct pool_c *pt = pool->ti->private;
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002413 bool needs_check = dm_pool_metadata_needs_check(pool->pmd);
2414 enum pool_mode old_mode = get_pool_mode(pool);
Mike Snitzer80c57892014-05-20 13:38:33 -04002415 unsigned long no_space_timeout = ACCESS_ONCE(no_space_timeout_secs) * HZ;
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002416
2417 /*
2418 * Never allow the pool to transition to PM_WRITE mode if user
2419 * intervention is required to verify metadata and data consistency.
2420 */
2421 if (new_mode == PM_WRITE && needs_check) {
2422 DMERR("%s: unable to switch pool to write mode until repaired.",
2423 dm_device_name(pool->pool_md));
2424 if (old_mode != new_mode)
2425 new_mode = old_mode;
2426 else
2427 new_mode = PM_READ_ONLY;
2428 }
2429 /*
2430 * If we were in PM_FAIL mode, rollback of metadata failed. We're
2431 * not going to recover without a thin_repair. So we never let the
2432 * pool move out of the old mode.
2433 */
2434 if (old_mode == PM_FAIL)
2435 new_mode = old_mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002436
Mike Snitzer8b64e882013-12-20 14:27:28 -05002437 switch (new_mode) {
Joe Thornbere49e5822012-07-27 15:08:16 +01002438 case PM_FAIL:
Mike Snitzer8b64e882013-12-20 14:27:28 -05002439 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00002440 notify_of_pool_mode_change(pool, "failure");
Joe Thornber5383ef32013-12-04 16:30:01 -05002441 dm_pool_metadata_read_only(pool->pmd);
Joe Thornbere49e5822012-07-27 15:08:16 +01002442 pool->process_bio = process_bio_fail;
2443 pool->process_discard = process_bio_fail;
Joe Thornbera374bb22014-10-10 13:43:14 +01002444 pool->process_cell = process_cell_fail;
2445 pool->process_discard_cell = process_cell_fail;
Joe Thornbere49e5822012-07-27 15:08:16 +01002446 pool->process_prepared_mapping = process_prepared_mapping_fail;
2447 pool->process_prepared_discard = process_prepared_discard_fail;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002448
2449 error_retry_list(pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01002450 break;
2451
2452 case PM_READ_ONLY:
Mike Snitzer8b64e882013-12-20 14:27:28 -05002453 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00002454 notify_of_pool_mode_change(pool, "read-only");
2455 dm_pool_metadata_read_only(pool->pmd);
2456 pool->process_bio = process_bio_read_only;
2457 pool->process_discard = process_bio_success;
Joe Thornbera374bb22014-10-10 13:43:14 +01002458 pool->process_cell = process_cell_read_only;
2459 pool->process_discard_cell = process_cell_success;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002460 pool->process_prepared_mapping = process_prepared_mapping_fail;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002461 pool->process_prepared_discard = process_prepared_discard_success;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002462
2463 error_retry_list(pool);
2464 break;
2465
2466 case PM_OUT_OF_DATA_SPACE:
2467 /*
2468 * Ideally we'd never hit this state; the low water mark
2469 * would trigger userland to extend the pool before we
2470 * completely run out of data space. However, many small
2471 * IOs to unprovisioned space can consume data space at an
2472 * alarming rate. Adjust your low water mark if you're
2473 * frequently seeing this mode.
2474 */
2475 if (old_mode != new_mode)
Mike Snitzerbcc696f2015-07-15 16:52:04 -04002476 notify_of_pool_mode_change_to_oods(pool);
Joe Thornber3e1a0692014-03-03 16:03:26 +00002477 pool->process_bio = process_bio_read_only;
Joe Thornbera374bb22014-10-10 13:43:14 +01002478 pool->process_discard = process_discard_bio;
2479 pool->process_cell = process_cell_read_only;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002480 pool->process_prepared_mapping = process_prepared_mapping;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002481 set_discard_callbacks(pool);
Joe Thornber85ad643b2014-05-09 15:59:38 +01002482
Mike Snitzer80c57892014-05-20 13:38:33 -04002483 if (!pool->pf.error_if_no_space && no_space_timeout)
2484 queue_delayed_work(pool->wq, &pool->no_space_timeout, no_space_timeout);
Joe Thornbere49e5822012-07-27 15:08:16 +01002485 break;
2486
2487 case PM_WRITE:
Mike Snitzer8b64e882013-12-20 14:27:28 -05002488 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00002489 notify_of_pool_mode_change(pool, "write");
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002490 dm_pool_metadata_read_write(pool->pmd);
Joe Thornbere49e5822012-07-27 15:08:16 +01002491 pool->process_bio = process_bio;
Joe Thornbera374bb22014-10-10 13:43:14 +01002492 pool->process_discard = process_discard_bio;
2493 pool->process_cell = process_cell;
Joe Thornbere49e5822012-07-27 15:08:16 +01002494 pool->process_prepared_mapping = process_prepared_mapping;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002495 set_discard_callbacks(pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01002496 break;
2497 }
Mike Snitzer8b64e882013-12-20 14:27:28 -05002498
2499 pool->pf.mode = new_mode;
Mike Snitzercdc2b412014-02-14 18:10:55 -05002500 /*
2501 * The pool mode may have changed, sync it so bind_control_target()
2502 * doesn't cause an unexpected mode transition on resume.
2503 */
2504 pt->adjusted_pf.mode = new_mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002505}
2506
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002507static void abort_transaction(struct pool *pool)
2508{
2509 const char *dev_name = dm_device_name(pool->pool_md);
2510
2511 DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
2512 if (dm_pool_abort_metadata(pool->pmd)) {
2513 DMERR("%s: failed to abort metadata transaction", dev_name);
2514 set_pool_mode(pool, PM_FAIL);
2515 }
2516
2517 if (dm_pool_metadata_set_needs_check(pool->pmd)) {
2518 DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
2519 set_pool_mode(pool, PM_FAIL);
2520 }
2521}
2522
Joe Thornberb5330652013-12-04 19:51:33 -05002523static void metadata_operation_failed(struct pool *pool, const char *op, int r)
2524{
2525 DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
2526 dm_device_name(pool->pool_md), op, r);
2527
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002528 abort_transaction(pool);
Joe Thornberb5330652013-12-04 19:51:33 -05002529 set_pool_mode(pool, PM_READ_ONLY);
2530}
2531
Joe Thornbere49e5822012-07-27 15:08:16 +01002532/*----------------------------------------------------------------*/
2533
Joe Thornber991d9fa2011-10-31 20:21:18 +00002534/*
2535 * Mapping functions.
2536 */
2537
2538/*
2539 * Called only while mapping a thin bio to hand it over to the workqueue.
2540 */
2541static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
2542{
2543 unsigned long flags;
2544 struct pool *pool = tc->pool;
2545
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002546 spin_lock_irqsave(&tc->lock, flags);
2547 bio_list_add(&tc->deferred_bio_list, bio);
2548 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002549
2550 wake_worker(pool);
2551}
2552
Joe Thornber7d327fe2014-10-06 15:45:59 +01002553static void thin_defer_bio_with_throttle(struct thin_c *tc, struct bio *bio)
2554{
2555 struct pool *pool = tc->pool;
2556
2557 throttle_lock(&pool->throttle);
2558 thin_defer_bio(tc, bio);
2559 throttle_unlock(&pool->throttle);
2560}
2561
Joe Thornbera374bb22014-10-10 13:43:14 +01002562static void thin_defer_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
2563{
2564 unsigned long flags;
2565 struct pool *pool = tc->pool;
2566
2567 throttle_lock(&pool->throttle);
2568 spin_lock_irqsave(&tc->lock, flags);
2569 list_add_tail(&cell->user_list, &tc->deferred_cells);
2570 spin_unlock_irqrestore(&tc->lock, flags);
2571 throttle_unlock(&pool->throttle);
2572
2573 wake_worker(pool);
2574}
2575
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002576static void thin_hook_bio(struct thin_c *tc, struct bio *bio)
Joe Thornbereb2aa482012-03-28 18:41:28 +01002577{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002578 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01002579
2580 h->tc = tc;
2581 h->shared_read_entry = NULL;
Joe Thornbere8088072012-12-21 20:23:31 +00002582 h->all_io_entry = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01002583 h->overwrite_mapping = NULL;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002584 h->cell = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01002585}
2586
Joe Thornber991d9fa2011-10-31 20:21:18 +00002587/*
2588 * Non-blocking function called from the thin target's map function.
2589 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002590static int thin_bio_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002591{
2592 int r;
2593 struct thin_c *tc = ti->private;
2594 dm_block_t block = get_bio_block(tc, bio);
2595 struct dm_thin_device *td = tc->td;
2596 struct dm_thin_lookup_result result;
Joe Thornbera374bb22014-10-10 13:43:14 +01002597 struct dm_bio_prison_cell *virt_cell, *data_cell;
Joe Thornbere8088072012-12-21 20:23:31 +00002598 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002599
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002600 thin_hook_bio(tc, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01002601
Joe Thornber738211f2014-03-03 15:52:28 +00002602 if (tc->requeue_mode) {
2603 bio_endio(bio, DM_ENDIO_REQUEUE);
2604 return DM_MAPIO_SUBMITTED;
2605 }
2606
Joe Thornbere49e5822012-07-27 15:08:16 +01002607 if (get_pool_mode(tc->pool) == PM_FAIL) {
2608 bio_io_error(bio);
2609 return DM_MAPIO_SUBMITTED;
2610 }
2611
Joe Thornber104655f2012-03-28 18:41:28 +01002612 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) {
Joe Thornber7d327fe2014-10-06 15:45:59 +01002613 thin_defer_bio_with_throttle(tc, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002614 return DM_MAPIO_SUBMITTED;
2615 }
2616
Joe Thornberc822ed92014-10-10 09:41:09 +01002617 /*
2618 * We must hold the virtual cell before doing the lookup, otherwise
2619 * there's a race with discard.
2620 */
2621 build_virtual_key(tc->td, block, &key);
Joe Thornbera374bb22014-10-10 13:43:14 +01002622 if (bio_detain(tc->pool, &key, bio, &virt_cell))
Joe Thornberc822ed92014-10-10 09:41:09 +01002623 return DM_MAPIO_SUBMITTED;
2624
Joe Thornber991d9fa2011-10-31 20:21:18 +00002625 r = dm_thin_find_block(td, block, 0, &result);
2626
2627 /*
2628 * Note that we defer readahead too.
2629 */
2630 switch (r) {
2631 case 0:
2632 if (unlikely(result.shared)) {
2633 /*
2634 * We have a race condition here between the
2635 * result.shared value returned by the lookup and
2636 * snapshot creation, which may cause new
2637 * sharing.
2638 *
2639 * To avoid this always quiesce the origin before
2640 * taking the snap. You want to do this anyway to
2641 * ensure a consistent application view
2642 * (i.e. lockfs).
2643 *
2644 * More distant ancestors are irrelevant. The
2645 * shared flag will be set in their case.
2646 */
Joe Thornbera374bb22014-10-10 13:43:14 +01002647 thin_defer_cell(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002648 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002649 }
Joe Thornbere8088072012-12-21 20:23:31 +00002650
Joe Thornbere8088072012-12-21 20:23:31 +00002651 build_data_key(tc->td, result.block, &key);
Joe Thornbera374bb22014-10-10 13:43:14 +01002652 if (bio_detain(tc->pool, &key, bio, &data_cell)) {
2653 cell_defer_no_holder(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002654 return DM_MAPIO_SUBMITTED;
2655 }
2656
2657 inc_all_io_entry(tc->pool, bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01002658 cell_defer_no_holder(tc, data_cell);
2659 cell_defer_no_holder(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002660
2661 remap(tc, bio, result.block);
2662 return DM_MAPIO_REMAPPED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002663
2664 case -ENODATA:
Joe Thornbere49e5822012-07-27 15:08:16 +01002665 case -EWOULDBLOCK:
Joe Thornbera374bb22014-10-10 13:43:14 +01002666 thin_defer_cell(tc, virt_cell);
Joe Thornber2aab3852012-12-21 20:23:33 +00002667 return DM_MAPIO_SUBMITTED;
Joe Thornbere49e5822012-07-27 15:08:16 +01002668
2669 default:
2670 /*
2671 * Must always call bio_io_error on failure.
2672 * dm_thin_find_block can fail with -EINVAL if the
2673 * pool is switched to fail-io mode.
2674 */
2675 bio_io_error(bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01002676 cell_defer_no_holder(tc, virt_cell);
Joe Thornber2aab3852012-12-21 20:23:33 +00002677 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002678 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002679}
2680
2681static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
2682{
Joe Thornber991d9fa2011-10-31 20:21:18 +00002683 struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
Mike Snitzer760fe672014-03-20 08:36:47 -04002684 struct request_queue *q;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002685
Mike Snitzer760fe672014-03-20 08:36:47 -04002686 if (get_pool_mode(pt->pool) == PM_OUT_OF_DATA_SPACE)
2687 return 1;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002688
Mike Snitzer760fe672014-03-20 08:36:47 -04002689 q = bdev_get_queue(pt->data_dev->bdev);
2690 return bdi_congested(&q->backing_dev_info, bdi_bits);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002691}
2692
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002693static void requeue_bios(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002694{
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002695 unsigned long flags;
2696 struct thin_c *tc;
2697
2698 rcu_read_lock();
2699 list_for_each_entry_rcu(tc, &pool->active_thins, list) {
2700 spin_lock_irqsave(&tc->lock, flags);
2701 bio_list_merge(&tc->deferred_bio_list, &tc->retry_on_resume_list);
2702 bio_list_init(&tc->retry_on_resume_list);
2703 spin_unlock_irqrestore(&tc->lock, flags);
2704 }
2705 rcu_read_unlock();
Joe Thornber991d9fa2011-10-31 20:21:18 +00002706}
2707
2708/*----------------------------------------------------------------
2709 * Binding of control targets to a pool object
2710 *--------------------------------------------------------------*/
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002711static bool data_dev_supports_discard(struct pool_c *pt)
2712{
2713 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
2714
2715 return q && blk_queue_discard(q);
2716}
2717
Joe Thornber58051b92013-03-20 17:21:25 +00002718static bool is_factor(sector_t block_size, uint32_t n)
2719{
2720 return !sector_div(block_size, n);
2721}
2722
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002723/*
2724 * If discard_passdown was enabled verify that the data device
Mike Snitzer0424caa2012-09-26 23:45:47 +01002725 * supports discards. Disable discard_passdown if not.
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002726 */
Mike Snitzer0424caa2012-09-26 23:45:47 +01002727static void disable_passdown_if_not_supported(struct pool_c *pt)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002728{
Mike Snitzer0424caa2012-09-26 23:45:47 +01002729 struct pool *pool = pt->pool;
2730 struct block_device *data_bdev = pt->data_dev->bdev;
2731 struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
Mike Snitzer0424caa2012-09-26 23:45:47 +01002732 const char *reason = NULL;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002733 char buf[BDEVNAME_SIZE];
2734
Mike Snitzer0424caa2012-09-26 23:45:47 +01002735 if (!pt->adjusted_pf.discard_passdown)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002736 return;
2737
Mike Snitzer0424caa2012-09-26 23:45:47 +01002738 if (!data_dev_supports_discard(pt))
2739 reason = "discard unsupported";
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002740
Mike Snitzer0424caa2012-09-26 23:45:47 +01002741 else if (data_limits->max_discard_sectors < pool->sectors_per_block)
2742 reason = "max discard sectors smaller than a block";
2743
Mike Snitzer0424caa2012-09-26 23:45:47 +01002744 if (reason) {
2745 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
2746 pt->adjusted_pf.discard_passdown = false;
2747 }
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002748}
2749
Joe Thornber991d9fa2011-10-31 20:21:18 +00002750static int bind_control_target(struct pool *pool, struct dm_target *ti)
2751{
2752 struct pool_c *pt = ti->private;
2753
Joe Thornbere49e5822012-07-27 15:08:16 +01002754 /*
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002755 * We want to make sure that a pool in PM_FAIL mode is never upgraded.
Joe Thornbere49e5822012-07-27 15:08:16 +01002756 */
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002757 enum pool_mode old_mode = get_pool_mode(pool);
Mike Snitzer0424caa2012-09-26 23:45:47 +01002758 enum pool_mode new_mode = pt->adjusted_pf.mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002759
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002760 /*
Mike Snitzer8b64e882013-12-20 14:27:28 -05002761 * Don't change the pool's mode until set_pool_mode() below.
2762 * Otherwise the pool's process_* function pointers may
2763 * not match the desired pool mode.
2764 */
2765 pt->adjusted_pf.mode = old_mode;
2766
2767 pool->ti = ti;
2768 pool->pf = pt->adjusted_pf;
2769 pool->low_water_blocks = pt->low_water_blocks;
2770
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002771 set_pool_mode(pool, new_mode);
Mike Snitzerf4026932012-05-19 01:01:01 +01002772
Joe Thornber991d9fa2011-10-31 20:21:18 +00002773 return 0;
2774}
2775
2776static void unbind_control_target(struct pool *pool, struct dm_target *ti)
2777{
2778 if (pool->ti == ti)
2779 pool->ti = NULL;
2780}
2781
2782/*----------------------------------------------------------------
2783 * Pool creation
2784 *--------------------------------------------------------------*/
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002785/* Initialize pool features. */
2786static void pool_features_init(struct pool_features *pf)
2787{
Joe Thornbere49e5822012-07-27 15:08:16 +01002788 pf->mode = PM_WRITE;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002789 pf->zero_new_blocks = true;
2790 pf->discard_enabled = true;
2791 pf->discard_passdown = true;
Mike Snitzer787a996c2013-12-06 16:21:43 -05002792 pf->error_if_no_space = false;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002793}
2794
Joe Thornber991d9fa2011-10-31 20:21:18 +00002795static void __pool_destroy(struct pool *pool)
2796{
2797 __pool_table_remove(pool);
2798
Joe Thornbera822c832015-07-03 10:22:42 +01002799 vfree(pool->cell_sort_array);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002800 if (dm_pool_metadata_close(pool->pmd) < 0)
2801 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2802
Mike Snitzer44feb382012-10-12 21:02:10 +01002803 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002804 dm_kcopyd_client_destroy(pool->copier);
2805
2806 if (pool->wq)
2807 destroy_workqueue(pool->wq);
2808
2809 if (pool->next_mapping)
2810 mempool_free(pool->next_mapping, pool->mapping_pool);
2811 mempool_destroy(pool->mapping_pool);
Mike Snitzer44feb382012-10-12 21:02:10 +01002812 dm_deferred_set_destroy(pool->shared_read_ds);
2813 dm_deferred_set_destroy(pool->all_io_ds);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002814 kfree(pool);
2815}
2816
Mike Snitzera24c2562012-06-03 00:30:00 +01002817static struct kmem_cache *_new_mapping_cache;
Mike Snitzera24c2562012-06-03 00:30:00 +01002818
Joe Thornber991d9fa2011-10-31 20:21:18 +00002819static struct pool *pool_create(struct mapped_device *pool_md,
2820 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002821 unsigned long block_size,
2822 int read_only, char **error)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002823{
2824 int r;
2825 void *err_p;
2826 struct pool *pool;
2827 struct dm_pool_metadata *pmd;
Joe Thornbere49e5822012-07-27 15:08:16 +01002828 bool format_device = read_only ? false : true;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002829
Joe Thornbere49e5822012-07-27 15:08:16 +01002830 pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002831 if (IS_ERR(pmd)) {
2832 *error = "Error creating metadata object";
2833 return (struct pool *)pmd;
2834 }
2835
2836 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
2837 if (!pool) {
2838 *error = "Error allocating memory for pool";
2839 err_p = ERR_PTR(-ENOMEM);
2840 goto bad_pool;
2841 }
2842
2843 pool->pmd = pmd;
2844 pool->sectors_per_block = block_size;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +01002845 if (block_size & (block_size - 1))
2846 pool->sectors_per_block_shift = -1;
2847 else
2848 pool->sectors_per_block_shift = __ffs(block_size);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002849 pool->low_water_blocks = 0;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002850 pool_features_init(&pool->pf);
Joe Thornbera195db22014-10-06 16:30:06 -04002851 pool->prison = dm_bio_prison_create();
Joe Thornber991d9fa2011-10-31 20:21:18 +00002852 if (!pool->prison) {
2853 *error = "Error creating pool's bio prison";
2854 err_p = ERR_PTR(-ENOMEM);
2855 goto bad_prison;
2856 }
2857
Mikulas Patockadf5d2e92013-03-01 22:45:49 +00002858 pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002859 if (IS_ERR(pool->copier)) {
2860 r = PTR_ERR(pool->copier);
2861 *error = "Error creating pool's kcopyd client";
2862 err_p = ERR_PTR(r);
2863 goto bad_kcopyd_client;
2864 }
2865
2866 /*
2867 * Create singlethreaded workqueue that will service all devices
2868 * that use this metadata.
2869 */
2870 pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2871 if (!pool->wq) {
2872 *error = "Error creating pool's workqueue";
2873 err_p = ERR_PTR(-ENOMEM);
2874 goto bad_wq;
2875 }
2876
Joe Thornber7d327fe2014-10-06 15:45:59 +01002877 throttle_init(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002878 INIT_WORK(&pool->worker, do_worker);
Joe Thornber905e51b2012-03-28 18:41:27 +01002879 INIT_DELAYED_WORK(&pool->waker, do_waker);
Joe Thornber85ad643b2014-05-09 15:59:38 +01002880 INIT_DELAYED_WORK(&pool->no_space_timeout, do_no_space_timeout);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002881 spin_lock_init(&pool->lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002882 bio_list_init(&pool->deferred_flush_bios);
2883 INIT_LIST_HEAD(&pool->prepared_mappings);
Joe Thornber104655f2012-03-28 18:41:28 +01002884 INIT_LIST_HEAD(&pool->prepared_discards);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002885 INIT_LIST_HEAD(&pool->active_thins);
Joe Thornber88a66212013-12-04 20:16:12 -05002886 pool->low_water_triggered = false;
Mike Snitzer80e96c52014-11-07 15:09:46 -05002887 pool->suspended = true;
Mike Snitzer44feb382012-10-12 21:02:10 +01002888
2889 pool->shared_read_ds = dm_deferred_set_create();
2890 if (!pool->shared_read_ds) {
2891 *error = "Error creating pool's shared read deferred set";
2892 err_p = ERR_PTR(-ENOMEM);
2893 goto bad_shared_read_ds;
2894 }
2895
2896 pool->all_io_ds = dm_deferred_set_create();
2897 if (!pool->all_io_ds) {
2898 *error = "Error creating pool's all io deferred set";
2899 err_p = ERR_PTR(-ENOMEM);
2900 goto bad_all_io_ds;
2901 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002902
2903 pool->next_mapping = NULL;
Mike Snitzera24c2562012-06-03 00:30:00 +01002904 pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
2905 _new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002906 if (!pool->mapping_pool) {
2907 *error = "Error creating pool's mapping mempool";
2908 err_p = ERR_PTR(-ENOMEM);
2909 goto bad_mapping_pool;
2910 }
2911
Joe Thornbera822c832015-07-03 10:22:42 +01002912 pool->cell_sort_array = vmalloc(sizeof(*pool->cell_sort_array) * CELL_SORT_ARRAY_SIZE);
2913 if (!pool->cell_sort_array) {
2914 *error = "Error allocating cell sort array";
2915 err_p = ERR_PTR(-ENOMEM);
2916 goto bad_sort_array;
2917 }
2918
Joe Thornber991d9fa2011-10-31 20:21:18 +00002919 pool->ref_count = 1;
Joe Thornber905e51b2012-03-28 18:41:27 +01002920 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002921 pool->pool_md = pool_md;
2922 pool->md_dev = metadata_dev;
2923 __pool_table_insert(pool);
2924
2925 return pool;
2926
Joe Thornbera822c832015-07-03 10:22:42 +01002927bad_sort_array:
2928 mempool_destroy(pool->mapping_pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002929bad_mapping_pool:
Mike Snitzer44feb382012-10-12 21:02:10 +01002930 dm_deferred_set_destroy(pool->all_io_ds);
2931bad_all_io_ds:
2932 dm_deferred_set_destroy(pool->shared_read_ds);
2933bad_shared_read_ds:
Joe Thornber991d9fa2011-10-31 20:21:18 +00002934 destroy_workqueue(pool->wq);
2935bad_wq:
2936 dm_kcopyd_client_destroy(pool->copier);
2937bad_kcopyd_client:
Mike Snitzer44feb382012-10-12 21:02:10 +01002938 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002939bad_prison:
2940 kfree(pool);
2941bad_pool:
2942 if (dm_pool_metadata_close(pmd))
2943 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2944
2945 return err_p;
2946}
2947
2948static void __pool_inc(struct pool *pool)
2949{
2950 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2951 pool->ref_count++;
2952}
2953
2954static void __pool_dec(struct pool *pool)
2955{
2956 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2957 BUG_ON(!pool->ref_count);
2958 if (!--pool->ref_count)
2959 __pool_destroy(pool);
2960}
2961
2962static struct pool *__pool_find(struct mapped_device *pool_md,
2963 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002964 unsigned long block_size, int read_only,
2965 char **error, int *created)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002966{
2967 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
2968
2969 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01002970 if (pool->pool_md != pool_md) {
2971 *error = "metadata device already in use by a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00002972 return ERR_PTR(-EBUSY);
Mike Snitzerf09996c2012-07-27 15:07:59 +01002973 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002974 __pool_inc(pool);
2975
2976 } else {
2977 pool = __pool_table_lookup(pool_md);
2978 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01002979 if (pool->md_dev != metadata_dev) {
2980 *error = "different pool cannot replace a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00002981 return ERR_PTR(-EINVAL);
Mike Snitzerf09996c2012-07-27 15:07:59 +01002982 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002983 __pool_inc(pool);
2984
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002985 } else {
Joe Thornbere49e5822012-07-27 15:08:16 +01002986 pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002987 *created = 1;
2988 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002989 }
2990
2991 return pool;
2992}
2993
2994/*----------------------------------------------------------------
2995 * Pool target methods
2996 *--------------------------------------------------------------*/
2997static void pool_dtr(struct dm_target *ti)
2998{
2999 struct pool_c *pt = ti->private;
3000
3001 mutex_lock(&dm_thin_pool_table.mutex);
3002
3003 unbind_control_target(pt->pool, ti);
3004 __pool_dec(pt->pool);
3005 dm_put_device(ti, pt->metadata_dev);
3006 dm_put_device(ti, pt->data_dev);
3007 kfree(pt);
3008
3009 mutex_unlock(&dm_thin_pool_table.mutex);
3010}
3011
Joe Thornber991d9fa2011-10-31 20:21:18 +00003012static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
3013 struct dm_target *ti)
3014{
3015 int r;
3016 unsigned argc;
3017 const char *arg_name;
3018
3019 static struct dm_arg _args[] = {
Mike Snitzer74aa45c2014-01-15 19:07:58 -05003020 {0, 4, "Invalid number of pool feature arguments"},
Joe Thornber991d9fa2011-10-31 20:21:18 +00003021 };
3022
3023 /*
3024 * No feature arguments supplied.
3025 */
3026 if (!as->argc)
3027 return 0;
3028
3029 r = dm_read_arg_group(_args, as, &argc, &ti->error);
3030 if (r)
3031 return -EINVAL;
3032
3033 while (argc && !r) {
3034 arg_name = dm_shift_arg(as);
3035 argc--;
3036
Joe Thornbere49e5822012-07-27 15:08:16 +01003037 if (!strcasecmp(arg_name, "skip_block_zeroing"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003038 pf->zero_new_blocks = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003039
Joe Thornbere49e5822012-07-27 15:08:16 +01003040 else if (!strcasecmp(arg_name, "ignore_discard"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003041 pf->discard_enabled = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01003042
3043 else if (!strcasecmp(arg_name, "no_discard_passdown"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003044 pf->discard_passdown = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01003045
3046 else if (!strcasecmp(arg_name, "read_only"))
3047 pf->mode = PM_READ_ONLY;
3048
Mike Snitzer787a996c2013-12-06 16:21:43 -05003049 else if (!strcasecmp(arg_name, "error_if_no_space"))
3050 pf->error_if_no_space = true;
3051
Joe Thornbere49e5822012-07-27 15:08:16 +01003052 else {
3053 ti->error = "Unrecognised pool feature requested";
3054 r = -EINVAL;
3055 break;
3056 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003057 }
3058
3059 return r;
3060}
3061
Joe Thornberac8c3f32013-05-10 14:37:21 +01003062static void metadata_low_callback(void *context)
3063{
3064 struct pool *pool = context;
3065
3066 DMWARN("%s: reached low water mark for metadata device: sending event.",
3067 dm_device_name(pool->pool_md));
3068
3069 dm_table_event(pool->ti->table);
3070}
3071
Mike Snitzer7d489352014-02-12 23:58:15 -05003072static sector_t get_dev_size(struct block_device *bdev)
Joe Thornberb17446d2013-05-10 14:37:18 +01003073{
Mike Snitzer7d489352014-02-12 23:58:15 -05003074 return i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
3075}
3076
3077static void warn_if_metadata_device_too_big(struct block_device *bdev)
3078{
3079 sector_t metadata_dev_size = get_dev_size(bdev);
Joe Thornberb17446d2013-05-10 14:37:18 +01003080 char buffer[BDEVNAME_SIZE];
3081
Mike Snitzer7d489352014-02-12 23:58:15 -05003082 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
Joe Thornberb17446d2013-05-10 14:37:18 +01003083 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
3084 bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS);
Mike Snitzer7d489352014-02-12 23:58:15 -05003085}
3086
3087static sector_t get_metadata_dev_size(struct block_device *bdev)
3088{
3089 sector_t metadata_dev_size = get_dev_size(bdev);
3090
3091 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS)
3092 metadata_dev_size = THIN_METADATA_MAX_SECTORS;
Joe Thornberb17446d2013-05-10 14:37:18 +01003093
3094 return metadata_dev_size;
3095}
3096
Joe Thornber24347e92013-05-10 14:37:19 +01003097static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev)
3098{
3099 sector_t metadata_dev_size = get_metadata_dev_size(bdev);
3100
Mike Snitzer7d489352014-02-12 23:58:15 -05003101 sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE);
Joe Thornber24347e92013-05-10 14:37:19 +01003102
3103 return metadata_dev_size;
3104}
3105
Joe Thornber991d9fa2011-10-31 20:21:18 +00003106/*
Joe Thornberac8c3f32013-05-10 14:37:21 +01003107 * When a metadata threshold is crossed a dm event is triggered, and
3108 * userland should respond by growing the metadata device. We could let
3109 * userland set the threshold, like we do with the data threshold, but I'm
3110 * not sure they know enough to do this well.
3111 */
3112static dm_block_t calc_metadata_threshold(struct pool_c *pt)
3113{
3114 /*
3115 * 4M is ample for all ops with the possible exception of thin
3116 * device deletion which is harmless if it fails (just retry the
3117 * delete after you've grown the device).
3118 */
3119 dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4;
3120 return min((dm_block_t)1024ULL /* 4M */, quarter);
3121}
3122
3123/*
Joe Thornber991d9fa2011-10-31 20:21:18 +00003124 * thin-pool <metadata dev> <data dev>
3125 * <data block size (sectors)>
3126 * <low water mark (blocks)>
3127 * [<#feature args> [<arg>]*]
3128 *
3129 * Optional feature arguments are:
3130 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003131 * ignore_discard: disable discard
3132 * no_discard_passdown: don't pass discards down to the data device
Mike Snitzer787a996c2013-12-06 16:21:43 -05003133 * read_only: Don't allow any changes to be made to the pool metadata.
3134 * error_if_no_space: error IOs, instead of queueing, if no space.
Joe Thornber991d9fa2011-10-31 20:21:18 +00003135 */
3136static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
3137{
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003138 int r, pool_created = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003139 struct pool_c *pt;
3140 struct pool *pool;
3141 struct pool_features pf;
3142 struct dm_arg_set as;
3143 struct dm_dev *data_dev;
3144 unsigned long block_size;
3145 dm_block_t low_water_blocks;
3146 struct dm_dev *metadata_dev;
Joe Thornber5d0db962013-05-10 14:37:19 +01003147 fmode_t metadata_mode;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003148
3149 /*
3150 * FIXME Remove validation from scope of lock.
3151 */
3152 mutex_lock(&dm_thin_pool_table.mutex);
3153
3154 if (argc < 4) {
3155 ti->error = "Invalid argument count";
3156 r = -EINVAL;
3157 goto out_unlock;
3158 }
Joe Thornber5d0db962013-05-10 14:37:19 +01003159
Joe Thornber991d9fa2011-10-31 20:21:18 +00003160 as.argc = argc;
3161 as.argv = argv;
3162
Joe Thornber5d0db962013-05-10 14:37:19 +01003163 /*
3164 * Set default pool features.
3165 */
3166 pool_features_init(&pf);
3167
3168 dm_consume_args(&as, 4);
3169 r = parse_pool_features(&as, &pf, ti);
3170 if (r)
3171 goto out_unlock;
3172
3173 metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE);
3174 r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003175 if (r) {
3176 ti->error = "Error opening metadata block device";
3177 goto out_unlock;
3178 }
Mike Snitzer7d489352014-02-12 23:58:15 -05003179 warn_if_metadata_device_too_big(metadata_dev->bdev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003180
3181 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
3182 if (r) {
3183 ti->error = "Error getting data device";
3184 goto out_metadata;
3185 }
3186
3187 if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
3188 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
3189 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01003190 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00003191 ti->error = "Invalid block size";
3192 r = -EINVAL;
3193 goto out;
3194 }
3195
3196 if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
3197 ti->error = "Invalid low water mark";
3198 r = -EINVAL;
3199 goto out;
3200 }
3201
Joe Thornber991d9fa2011-10-31 20:21:18 +00003202 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
3203 if (!pt) {
3204 r = -ENOMEM;
3205 goto out;
3206 }
3207
3208 pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
Joe Thornbere49e5822012-07-27 15:08:16 +01003209 block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003210 if (IS_ERR(pool)) {
3211 r = PTR_ERR(pool);
3212 goto out_free_pt;
3213 }
3214
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003215 /*
3216 * 'pool_created' reflects whether this is the first table load.
3217 * Top level discard support is not allowed to be changed after
3218 * initial load. This would require a pool reload to trigger thin
3219 * device changes.
3220 */
3221 if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
3222 ti->error = "Discard support cannot be disabled once enabled";
3223 r = -EINVAL;
3224 goto out_flags_changed;
3225 }
3226
Joe Thornber991d9fa2011-10-31 20:21:18 +00003227 pt->pool = pool;
3228 pt->ti = ti;
3229 pt->metadata_dev = metadata_dev;
3230 pt->data_dev = data_dev;
3231 pt->low_water_blocks = low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +01003232 pt->adjusted_pf = pt->requested_pf = pf;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003233 ti->num_flush_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003234
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003235 /*
3236 * Only need to enable discards if the pool should pass
3237 * them down to the data device. The thin device's discard
3238 * processing will cause mappings to be removed from the btree.
3239 */
Mike Snitzerb60ab992013-09-19 18:49:11 -04003240 ti->discard_zeroes_data_unsupported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003241 if (pf.discard_enabled && pf.discard_passdown) {
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003242 ti->num_discard_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003243
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003244 /*
3245 * Setting 'discards_supported' circumvents the normal
3246 * stacking of discard limits (this keeps the pool and
3247 * thin devices' discard limits consistent).
3248 */
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01003249 ti->discards_supported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003250 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003251 ti->private = pt;
3252
Joe Thornberac8c3f32013-05-10 14:37:21 +01003253 r = dm_pool_register_metadata_threshold(pt->pool->pmd,
3254 calc_metadata_threshold(pt),
3255 metadata_low_callback,
3256 pool);
3257 if (r)
3258 goto out_free_pt;
3259
Joe Thornber991d9fa2011-10-31 20:21:18 +00003260 pt->callbacks.congested_fn = pool_is_congested;
3261 dm_table_add_target_callbacks(ti->table, &pt->callbacks);
3262
3263 mutex_unlock(&dm_thin_pool_table.mutex);
3264
3265 return 0;
3266
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003267out_flags_changed:
3268 __pool_dec(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003269out_free_pt:
3270 kfree(pt);
3271out:
3272 dm_put_device(ti, data_dev);
3273out_metadata:
3274 dm_put_device(ti, metadata_dev);
3275out_unlock:
3276 mutex_unlock(&dm_thin_pool_table.mutex);
3277
3278 return r;
3279}
3280
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003281static int pool_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003282{
3283 int r;
3284 struct pool_c *pt = ti->private;
3285 struct pool *pool = pt->pool;
3286 unsigned long flags;
3287
3288 /*
3289 * As this is a singleton target, ti->begin is always zero.
3290 */
3291 spin_lock_irqsave(&pool->lock, flags);
3292 bio->bi_bdev = pt->data_dev->bdev;
3293 r = DM_MAPIO_REMAPPED;
3294 spin_unlock_irqrestore(&pool->lock, flags);
3295
3296 return r;
3297}
3298
Joe Thornberb17446d2013-05-10 14:37:18 +01003299static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit)
3300{
3301 int r;
3302 struct pool_c *pt = ti->private;
3303 struct pool *pool = pt->pool;
3304 sector_t data_size = ti->len;
3305 dm_block_t sb_data_size;
3306
3307 *need_commit = false;
3308
3309 (void) sector_div(data_size, pool->sectors_per_block);
3310
3311 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
3312 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003313 DMERR("%s: failed to retrieve data device size",
3314 dm_device_name(pool->pool_md));
Joe Thornberb17446d2013-05-10 14:37:18 +01003315 return r;
3316 }
3317
3318 if (data_size < sb_data_size) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003319 DMERR("%s: pool target (%llu blocks) too small: expected %llu",
3320 dm_device_name(pool->pool_md),
Joe Thornberb17446d2013-05-10 14:37:18 +01003321 (unsigned long long)data_size, sb_data_size);
3322 return -EINVAL;
3323
3324 } else if (data_size > sb_data_size) {
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05003325 if (dm_pool_metadata_needs_check(pool->pmd)) {
3326 DMERR("%s: unable to grow the data device until repaired.",
3327 dm_device_name(pool->pool_md));
3328 return 0;
3329 }
3330
Mike Snitzer6f7f51d2013-12-04 10:25:53 -05003331 if (sb_data_size)
3332 DMINFO("%s: growing the data device from %llu to %llu blocks",
3333 dm_device_name(pool->pool_md),
3334 sb_data_size, (unsigned long long)data_size);
Joe Thornberb17446d2013-05-10 14:37:18 +01003335 r = dm_pool_resize_data_dev(pool->pmd, data_size);
3336 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05003337 metadata_operation_failed(pool, "dm_pool_resize_data_dev", r);
Joe Thornberb17446d2013-05-10 14:37:18 +01003338 return r;
3339 }
3340
3341 *need_commit = true;
3342 }
3343
3344 return 0;
3345}
3346
Joe Thornber24347e92013-05-10 14:37:19 +01003347static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit)
3348{
3349 int r;
3350 struct pool_c *pt = ti->private;
3351 struct pool *pool = pt->pool;
3352 dm_block_t metadata_dev_size, sb_metadata_dev_size;
3353
3354 *need_commit = false;
3355
Alasdair G Kergon610bba82013-05-19 18:57:50 +01003356 metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev);
Joe Thornber24347e92013-05-10 14:37:19 +01003357
3358 r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size);
3359 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003360 DMERR("%s: failed to retrieve metadata device size",
3361 dm_device_name(pool->pool_md));
Joe Thornber24347e92013-05-10 14:37:19 +01003362 return r;
3363 }
3364
3365 if (metadata_dev_size < sb_metadata_dev_size) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003366 DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
3367 dm_device_name(pool->pool_md),
Joe Thornber24347e92013-05-10 14:37:19 +01003368 metadata_dev_size, sb_metadata_dev_size);
3369 return -EINVAL;
3370
3371 } else if (metadata_dev_size > sb_metadata_dev_size) {
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05003372 if (dm_pool_metadata_needs_check(pool->pmd)) {
3373 DMERR("%s: unable to grow the metadata device until repaired.",
3374 dm_device_name(pool->pool_md));
3375 return 0;
3376 }
3377
Mike Snitzer7d489352014-02-12 23:58:15 -05003378 warn_if_metadata_device_too_big(pool->md_dev);
Mike Snitzer6f7f51d2013-12-04 10:25:53 -05003379 DMINFO("%s: growing the metadata device from %llu to %llu blocks",
3380 dm_device_name(pool->pool_md),
3381 sb_metadata_dev_size, metadata_dev_size);
Joe Thornber24347e92013-05-10 14:37:19 +01003382 r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size);
3383 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05003384 metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r);
Joe Thornber24347e92013-05-10 14:37:19 +01003385 return r;
3386 }
3387
3388 *need_commit = true;
3389 }
3390
3391 return 0;
3392}
3393
Joe Thornber991d9fa2011-10-31 20:21:18 +00003394/*
3395 * Retrieves the number of blocks of the data device from
3396 * the superblock and compares it to the actual device size,
3397 * thus resizing the data device in case it has grown.
3398 *
3399 * This both copes with opening preallocated data devices in the ctr
3400 * being followed by a resume
3401 * -and-
3402 * calling the resume method individually after userspace has
3403 * grown the data device in reaction to a table event.
3404 */
3405static int pool_preresume(struct dm_target *ti)
3406{
3407 int r;
Joe Thornber24347e92013-05-10 14:37:19 +01003408 bool need_commit1, need_commit2;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003409 struct pool_c *pt = ti->private;
3410 struct pool *pool = pt->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003411
3412 /*
3413 * Take control of the pool object.
3414 */
3415 r = bind_control_target(pool, ti);
3416 if (r)
3417 return r;
3418
Joe Thornberb17446d2013-05-10 14:37:18 +01003419 r = maybe_resize_data_dev(ti, &need_commit1);
3420 if (r)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003421 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003422
Joe Thornber24347e92013-05-10 14:37:19 +01003423 r = maybe_resize_metadata_dev(ti, &need_commit2);
3424 if (r)
3425 return r;
3426
3427 if (need_commit1 || need_commit2)
Joe Thornber020cc3b2013-12-04 15:05:36 -05003428 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003429
3430 return 0;
3431}
3432
Mike Snitzer583024d2014-10-28 20:58:45 -04003433static void pool_suspend_active_thins(struct pool *pool)
3434{
3435 struct thin_c *tc;
3436
3437 /* Suspend all active thin devices */
3438 tc = get_first_thin(pool);
3439 while (tc) {
3440 dm_internal_suspend_noflush(tc->thin_md);
3441 tc = get_next_thin(pool, tc);
3442 }
3443}
3444
3445static void pool_resume_active_thins(struct pool *pool)
3446{
3447 struct thin_c *tc;
3448
3449 /* Resume all active thin devices */
3450 tc = get_first_thin(pool);
3451 while (tc) {
3452 dm_internal_resume(tc->thin_md);
3453 tc = get_next_thin(pool, tc);
3454 }
3455}
3456
Joe Thornber991d9fa2011-10-31 20:21:18 +00003457static void pool_resume(struct dm_target *ti)
3458{
3459 struct pool_c *pt = ti->private;
3460 struct pool *pool = pt->pool;
3461 unsigned long flags;
3462
Mike Snitzer583024d2014-10-28 20:58:45 -04003463 /*
3464 * Must requeue active_thins' bios and then resume
3465 * active_thins _before_ clearing 'suspend' flag.
3466 */
3467 requeue_bios(pool);
3468 pool_resume_active_thins(pool);
3469
Joe Thornber991d9fa2011-10-31 20:21:18 +00003470 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber88a66212013-12-04 20:16:12 -05003471 pool->low_water_triggered = false;
Mike Snitzer80e96c52014-11-07 15:09:46 -05003472 pool->suspended = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003473 spin_unlock_irqrestore(&pool->lock, flags);
Mike Snitzer80e96c52014-11-07 15:09:46 -05003474
Joe Thornber905e51b2012-03-28 18:41:27 +01003475 do_waker(&pool->waker.work);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003476}
3477
Mike Snitzer80e96c52014-11-07 15:09:46 -05003478static void pool_presuspend(struct dm_target *ti)
3479{
3480 struct pool_c *pt = ti->private;
3481 struct pool *pool = pt->pool;
3482 unsigned long flags;
3483
3484 spin_lock_irqsave(&pool->lock, flags);
3485 pool->suspended = true;
3486 spin_unlock_irqrestore(&pool->lock, flags);
Mike Snitzer583024d2014-10-28 20:58:45 -04003487
3488 pool_suspend_active_thins(pool);
Mike Snitzer80e96c52014-11-07 15:09:46 -05003489}
3490
3491static void pool_presuspend_undo(struct dm_target *ti)
3492{
3493 struct pool_c *pt = ti->private;
3494 struct pool *pool = pt->pool;
3495 unsigned long flags;
3496
Mike Snitzer583024d2014-10-28 20:58:45 -04003497 pool_resume_active_thins(pool);
3498
Mike Snitzer80e96c52014-11-07 15:09:46 -05003499 spin_lock_irqsave(&pool->lock, flags);
3500 pool->suspended = false;
3501 spin_unlock_irqrestore(&pool->lock, flags);
3502}
3503
Joe Thornber991d9fa2011-10-31 20:21:18 +00003504static void pool_postsuspend(struct dm_target *ti)
3505{
Joe Thornber991d9fa2011-10-31 20:21:18 +00003506 struct pool_c *pt = ti->private;
3507 struct pool *pool = pt->pool;
3508
Joe Thornber905e51b2012-03-28 18:41:27 +01003509 cancel_delayed_work(&pool->waker);
Joe Thornber85ad643b2014-05-09 15:59:38 +01003510 cancel_delayed_work(&pool->no_space_timeout);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003511 flush_workqueue(pool->wq);
Joe Thornber020cc3b2013-12-04 15:05:36 -05003512 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003513}
3514
3515static int check_arg_count(unsigned argc, unsigned args_required)
3516{
3517 if (argc != args_required) {
3518 DMWARN("Message received with %u arguments instead of %u.",
3519 argc, args_required);
3520 return -EINVAL;
3521 }
3522
3523 return 0;
3524}
3525
3526static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
3527{
3528 if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
3529 *dev_id <= MAX_DEV_ID)
3530 return 0;
3531
3532 if (warning)
3533 DMWARN("Message received with invalid device id: %s", arg);
3534
3535 return -EINVAL;
3536}
3537
3538static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
3539{
3540 dm_thin_id dev_id;
3541 int r;
3542
3543 r = check_arg_count(argc, 2);
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 = dm_pool_create_thin(pool->pmd, dev_id);
3552 if (r) {
3553 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
3554 argv[1]);
3555 return r;
3556 }
3557
3558 return 0;
3559}
3560
3561static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3562{
3563 dm_thin_id dev_id;
3564 dm_thin_id origin_dev_id;
3565 int r;
3566
3567 r = check_arg_count(argc, 3);
3568 if (r)
3569 return r;
3570
3571 r = read_dev_id(argv[1], &dev_id, 1);
3572 if (r)
3573 return r;
3574
3575 r = read_dev_id(argv[2], &origin_dev_id, 1);
3576 if (r)
3577 return r;
3578
3579 r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
3580 if (r) {
3581 DMWARN("Creation of new snapshot %s of device %s failed.",
3582 argv[1], argv[2]);
3583 return r;
3584 }
3585
3586 return 0;
3587}
3588
3589static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
3590{
3591 dm_thin_id dev_id;
3592 int r;
3593
3594 r = check_arg_count(argc, 2);
3595 if (r)
3596 return r;
3597
3598 r = read_dev_id(argv[1], &dev_id, 1);
3599 if (r)
3600 return r;
3601
3602 r = dm_pool_delete_thin_device(pool->pmd, dev_id);
3603 if (r)
3604 DMWARN("Deletion of thin device %s failed.", argv[1]);
3605
3606 return r;
3607}
3608
3609static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
3610{
3611 dm_thin_id old_id, new_id;
3612 int r;
3613
3614 r = check_arg_count(argc, 3);
3615 if (r)
3616 return r;
3617
3618 if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
3619 DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
3620 return -EINVAL;
3621 }
3622
3623 if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
3624 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
3625 return -EINVAL;
3626 }
3627
3628 r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
3629 if (r) {
3630 DMWARN("Failed to change transaction id from %s to %s.",
3631 argv[1], argv[2]);
3632 return r;
3633 }
3634
3635 return 0;
3636}
3637
Joe Thornbercc8394d2012-06-03 00:30:01 +01003638static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3639{
3640 int r;
3641
3642 r = check_arg_count(argc, 1);
3643 if (r)
3644 return r;
3645
Joe Thornber020cc3b2013-12-04 15:05:36 -05003646 (void) commit(pool);
Joe Thornber0d200ae2012-07-03 12:55:31 +01003647
Joe Thornbercc8394d2012-06-03 00:30:01 +01003648 r = dm_pool_reserve_metadata_snap(pool->pmd);
3649 if (r)
3650 DMWARN("reserve_metadata_snap message failed.");
3651
3652 return r;
3653}
3654
3655static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3656{
3657 int r;
3658
3659 r = check_arg_count(argc, 1);
3660 if (r)
3661 return r;
3662
3663 r = dm_pool_release_metadata_snap(pool->pmd);
3664 if (r)
3665 DMWARN("release_metadata_snap message failed.");
3666
3667 return r;
3668}
3669
Joe Thornber991d9fa2011-10-31 20:21:18 +00003670/*
3671 * Messages supported:
3672 * create_thin <dev_id>
3673 * create_snap <dev_id> <origin_id>
3674 * delete <dev_id>
Joe Thornber991d9fa2011-10-31 20:21:18 +00003675 * set_transaction_id <current_trans_id> <new_trans_id>
Joe Thornbercc8394d2012-06-03 00:30:01 +01003676 * reserve_metadata_snap
3677 * release_metadata_snap
Joe Thornber991d9fa2011-10-31 20:21:18 +00003678 */
3679static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
3680{
3681 int r = -EINVAL;
3682 struct pool_c *pt = ti->private;
3683 struct pool *pool = pt->pool;
3684
Joe Thornber2a7eaea2015-01-26 11:38:21 +00003685 if (get_pool_mode(pool) >= PM_READ_ONLY) {
3686 DMERR("%s: unable to service pool target messages in READ_ONLY or FAIL mode",
3687 dm_device_name(pool->pool_md));
Mike Snitzerfd467692015-06-09 12:31:26 -04003688 return -EOPNOTSUPP;
Joe Thornber2a7eaea2015-01-26 11:38:21 +00003689 }
3690
Joe Thornber991d9fa2011-10-31 20:21:18 +00003691 if (!strcasecmp(argv[0], "create_thin"))
3692 r = process_create_thin_mesg(argc, argv, pool);
3693
3694 else if (!strcasecmp(argv[0], "create_snap"))
3695 r = process_create_snap_mesg(argc, argv, pool);
3696
3697 else if (!strcasecmp(argv[0], "delete"))
3698 r = process_delete_mesg(argc, argv, pool);
3699
3700 else if (!strcasecmp(argv[0], "set_transaction_id"))
3701 r = process_set_transaction_id_mesg(argc, argv, pool);
3702
Joe Thornbercc8394d2012-06-03 00:30:01 +01003703 else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
3704 r = process_reserve_metadata_snap_mesg(argc, argv, pool);
3705
3706 else if (!strcasecmp(argv[0], "release_metadata_snap"))
3707 r = process_release_metadata_snap_mesg(argc, argv, pool);
3708
Joe Thornber991d9fa2011-10-31 20:21:18 +00003709 else
3710 DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
3711
Joe Thornbere49e5822012-07-27 15:08:16 +01003712 if (!r)
Joe Thornber020cc3b2013-12-04 15:05:36 -05003713 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003714
3715 return r;
3716}
3717
Joe Thornbere49e5822012-07-27 15:08:16 +01003718static void emit_flags(struct pool_features *pf, char *result,
3719 unsigned sz, unsigned maxlen)
3720{
3721 unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
Mike Snitzer787a996c2013-12-06 16:21:43 -05003722 !pf->discard_passdown + (pf->mode == PM_READ_ONLY) +
3723 pf->error_if_no_space;
Joe Thornbere49e5822012-07-27 15:08:16 +01003724 DMEMIT("%u ", count);
3725
3726 if (!pf->zero_new_blocks)
3727 DMEMIT("skip_block_zeroing ");
3728
3729 if (!pf->discard_enabled)
3730 DMEMIT("ignore_discard ");
3731
3732 if (!pf->discard_passdown)
3733 DMEMIT("no_discard_passdown ");
3734
3735 if (pf->mode == PM_READ_ONLY)
3736 DMEMIT("read_only ");
Mike Snitzer787a996c2013-12-06 16:21:43 -05003737
3738 if (pf->error_if_no_space)
3739 DMEMIT("error_if_no_space ");
Joe Thornbere49e5822012-07-27 15:08:16 +01003740}
3741
Joe Thornber991d9fa2011-10-31 20:21:18 +00003742/*
3743 * Status line is:
3744 * <transaction id> <used metadata sectors>/<total metadata sectors>
3745 * <used data sectors>/<total data sectors> <held metadata root>
Mike Snitzere4c78e22015-07-15 11:40:24 -04003746 * <pool mode> <discard config> <no space config> <needs_check>
Joe Thornber991d9fa2011-10-31 20:21:18 +00003747 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003748static void pool_status(struct dm_target *ti, status_type_t type,
3749 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003750{
Joe Thornbere49e5822012-07-27 15:08:16 +01003751 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003752 unsigned sz = 0;
3753 uint64_t transaction_id;
3754 dm_block_t nr_free_blocks_data;
3755 dm_block_t nr_free_blocks_metadata;
3756 dm_block_t nr_blocks_data;
3757 dm_block_t nr_blocks_metadata;
3758 dm_block_t held_root;
3759 char buf[BDEVNAME_SIZE];
3760 char buf2[BDEVNAME_SIZE];
3761 struct pool_c *pt = ti->private;
3762 struct pool *pool = pt->pool;
3763
3764 switch (type) {
3765 case STATUSTYPE_INFO:
Joe Thornbere49e5822012-07-27 15:08:16 +01003766 if (get_pool_mode(pool) == PM_FAIL) {
3767 DMEMIT("Fail");
3768 break;
3769 }
3770
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01003771 /* Commit to ensure statistics aren't out-of-date */
3772 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
Joe Thornber020cc3b2013-12-04 15:05:36 -05003773 (void) commit(pool);
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01003774
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003775 r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id);
3776 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003777 DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
3778 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003779 goto err;
3780 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003781
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003782 r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata);
3783 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003784 DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
3785 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003786 goto err;
3787 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003788
3789 r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003790 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003791 DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
3792 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003793 goto err;
3794 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003795
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003796 r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data);
3797 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003798 DMERR("%s: dm_pool_get_free_block_count returned %d",
3799 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003800 goto err;
3801 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003802
3803 r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003804 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003805 DMERR("%s: dm_pool_get_data_dev_size returned %d",
3806 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003807 goto err;
3808 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003809
Joe Thornbercc8394d2012-06-03 00:30:01 +01003810 r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003811 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003812 DMERR("%s: dm_pool_get_metadata_snap returned %d",
3813 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003814 goto err;
3815 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003816
3817 DMEMIT("%llu %llu/%llu %llu/%llu ",
3818 (unsigned long long)transaction_id,
3819 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
3820 (unsigned long long)nr_blocks_metadata,
3821 (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
3822 (unsigned long long)nr_blocks_data);
3823
3824 if (held_root)
Joe Thornbere49e5822012-07-27 15:08:16 +01003825 DMEMIT("%llu ", held_root);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003826 else
Joe Thornbere49e5822012-07-27 15:08:16 +01003827 DMEMIT("- ");
3828
Joe Thornber3e1a0692014-03-03 16:03:26 +00003829 if (pool->pf.mode == PM_OUT_OF_DATA_SPACE)
3830 DMEMIT("out_of_data_space ");
3831 else if (pool->pf.mode == PM_READ_ONLY)
Joe Thornbere49e5822012-07-27 15:08:16 +01003832 DMEMIT("ro ");
3833 else
3834 DMEMIT("rw ");
3835
Mike Snitzer018debe2012-12-21 20:23:32 +00003836 if (!pool->pf.discard_enabled)
Mike Snitzer787a996c2013-12-06 16:21:43 -05003837 DMEMIT("ignore_discard ");
Mike Snitzer018debe2012-12-21 20:23:32 +00003838 else if (pool->pf.discard_passdown)
Mike Snitzer787a996c2013-12-06 16:21:43 -05003839 DMEMIT("discard_passdown ");
Joe Thornbere49e5822012-07-27 15:08:16 +01003840 else
Mike Snitzer787a996c2013-12-06 16:21:43 -05003841 DMEMIT("no_discard_passdown ");
3842
3843 if (pool->pf.error_if_no_space)
3844 DMEMIT("error_if_no_space ");
3845 else
3846 DMEMIT("queue_if_no_space ");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003847
Mike Snitzere4c78e22015-07-15 11:40:24 -04003848 if (dm_pool_metadata_needs_check(pool->pmd))
3849 DMEMIT("needs_check ");
3850 else
3851 DMEMIT("- ");
3852
Joe Thornber991d9fa2011-10-31 20:21:18 +00003853 break;
3854
3855 case STATUSTYPE_TABLE:
3856 DMEMIT("%s %s %lu %llu ",
3857 format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
3858 format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
3859 (unsigned long)pool->sectors_per_block,
3860 (unsigned long long)pt->low_water_blocks);
Mike Snitzer0424caa2012-09-26 23:45:47 +01003861 emit_flags(&pt->requested_pf, result, sz, maxlen);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003862 break;
3863 }
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003864 return;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003865
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003866err:
3867 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003868}
3869
3870static int pool_iterate_devices(struct dm_target *ti,
3871 iterate_devices_callout_fn fn, void *data)
3872{
3873 struct pool_c *pt = ti->private;
3874
3875 return fn(ti, pt->data_dev, 0, ti->len, data);
3876}
3877
3878static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
3879 struct bio_vec *biovec, int max_size)
3880{
3881 struct pool_c *pt = ti->private;
3882 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
3883
3884 if (!q->merge_bvec_fn)
3885 return max_size;
3886
3887 bvm->bi_bdev = pt->data_dev->bdev;
3888
3889 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
3890}
3891
3892static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
3893{
3894 struct pool_c *pt = ti->private;
3895 struct pool *pool = pt->pool;
Mike Snitzer604ea902014-10-09 18:43:25 -04003896 sector_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
3897
3898 /*
Mike Snitzerd200c302014-11-20 18:07:43 -05003899 * If max_sectors is smaller than pool->sectors_per_block adjust it
3900 * to the highest possible power-of-2 factor of pool->sectors_per_block.
3901 * This is especially beneficial when the pool's data device is a RAID
3902 * device that has a full stripe width that matches pool->sectors_per_block
3903 * -- because even though partial RAID stripe-sized IOs will be issued to a
3904 * single RAID stripe; when aggregated they will end on a full RAID stripe
3905 * boundary.. which avoids additional partial RAID stripe writes cascading
Mike Snitzer604ea902014-10-09 18:43:25 -04003906 */
Mike Snitzer604ea902014-10-09 18:43:25 -04003907 if (limits->max_sectors < pool->sectors_per_block) {
3908 while (!is_factor(pool->sectors_per_block, limits->max_sectors)) {
3909 if ((limits->max_sectors & (limits->max_sectors - 1)) == 0)
3910 limits->max_sectors--;
3911 limits->max_sectors = rounddown_pow_of_two(limits->max_sectors);
3912 }
Mike Snitzer604ea902014-10-09 18:43:25 -04003913 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003914
Mike Snitzer0cc67cd2013-08-20 15:02:41 -04003915 /*
3916 * If the system-determined stacked limits are compatible with the
3917 * pool's blocksize (io_opt is a factor) do not override them.
3918 */
3919 if (io_opt_sectors < pool->sectors_per_block ||
Mike Snitzer604ea902014-10-09 18:43:25 -04003920 !is_factor(io_opt_sectors, pool->sectors_per_block)) {
3921 if (is_factor(pool->sectors_per_block, limits->max_sectors))
3922 blk_limits_io_min(limits, limits->max_sectors << SECTOR_SHIFT);
3923 else
3924 blk_limits_io_min(limits, pool->sectors_per_block << SECTOR_SHIFT);
Mike Snitzer0cc67cd2013-08-20 15:02:41 -04003925 blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
3926 }
Mike Snitzer0424caa2012-09-26 23:45:47 +01003927
3928 /*
3929 * pt->adjusted_pf is a staging area for the actual features to use.
3930 * They get transferred to the live pool in bind_control_target()
3931 * called from pool_preresume().
3932 */
Mike Snitzerb60ab992013-09-19 18:49:11 -04003933 if (!pt->adjusted_pf.discard_enabled) {
3934 /*
3935 * Must explicitly disallow stacking discard limits otherwise the
3936 * block layer will stack them if pool's data device has support.
3937 * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the
3938 * user to see that, so make sure to set all discard limits to 0.
3939 */
3940 limits->discard_granularity = 0;
Mike Snitzer0424caa2012-09-26 23:45:47 +01003941 return;
Mike Snitzerb60ab992013-09-19 18:49:11 -04003942 }
Mike Snitzer0424caa2012-09-26 23:45:47 +01003943
3944 disable_passdown_if_not_supported(pt);
3945
Joe Thornber34fbcf62015-04-16 12:58:35 +01003946 /*
3947 * The pool uses the same discard limits as the underlying data
3948 * device. DM core has already set this up.
3949 */
Joe Thornber991d9fa2011-10-31 20:21:18 +00003950}
3951
3952static struct target_type pool_target = {
3953 .name = "thin-pool",
3954 .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
3955 DM_TARGET_IMMUTABLE,
Mike Snitzere4c78e22015-07-15 11:40:24 -04003956 .version = {1, 16, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00003957 .module = THIS_MODULE,
3958 .ctr = pool_ctr,
3959 .dtr = pool_dtr,
3960 .map = pool_map,
Mike Snitzer80e96c52014-11-07 15:09:46 -05003961 .presuspend = pool_presuspend,
3962 .presuspend_undo = pool_presuspend_undo,
Joe Thornber991d9fa2011-10-31 20:21:18 +00003963 .postsuspend = pool_postsuspend,
3964 .preresume = pool_preresume,
3965 .resume = pool_resume,
3966 .message = pool_message,
3967 .status = pool_status,
3968 .merge = pool_merge,
3969 .iterate_devices = pool_iterate_devices,
3970 .io_hints = pool_io_hints,
3971};
3972
3973/*----------------------------------------------------------------
3974 * Thin target methods
3975 *--------------------------------------------------------------*/
Joe Thornberb10ebd32014-04-08 11:29:01 +01003976static void thin_get(struct thin_c *tc)
3977{
3978 atomic_inc(&tc->refcount);
3979}
3980
3981static void thin_put(struct thin_c *tc)
3982{
3983 if (atomic_dec_and_test(&tc->refcount))
3984 complete(&tc->can_destroy);
3985}
3986
Joe Thornber991d9fa2011-10-31 20:21:18 +00003987static void thin_dtr(struct dm_target *ti)
3988{
3989 struct thin_c *tc = ti->private;
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003990 unsigned long flags;
3991
3992 spin_lock_irqsave(&tc->pool->lock, flags);
3993 list_del_rcu(&tc->list);
3994 spin_unlock_irqrestore(&tc->pool->lock, flags);
3995 synchronize_rcu();
Joe Thornber991d9fa2011-10-31 20:21:18 +00003996
Mikulas Patocka17181fb2014-11-05 17:00:13 -05003997 thin_put(tc);
3998 wait_for_completion(&tc->can_destroy);
3999
Joe Thornber991d9fa2011-10-31 20:21:18 +00004000 mutex_lock(&dm_thin_pool_table.mutex);
4001
4002 __pool_dec(tc->pool);
4003 dm_pool_close_thin_device(tc->td);
4004 dm_put_device(ti, tc->pool_dev);
Joe Thornber2dd9c252012-03-28 18:41:28 +01004005 if (tc->origin_dev)
4006 dm_put_device(ti, tc->origin_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004007 kfree(tc);
4008
4009 mutex_unlock(&dm_thin_pool_table.mutex);
4010}
4011
4012/*
4013 * Thin target parameters:
4014 *
Joe Thornber2dd9c252012-03-28 18:41:28 +01004015 * <pool_dev> <dev_id> [origin_dev]
Joe Thornber991d9fa2011-10-31 20:21:18 +00004016 *
4017 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
4018 * dev_id: the internal device identifier
Joe Thornber2dd9c252012-03-28 18:41:28 +01004019 * origin_dev: a device external to the pool that should act as the origin
Joe Thornber67e2e2b2012-03-28 18:41:29 +01004020 *
4021 * If the pool device has discards disabled, they get disabled for the thin
4022 * device as well.
Joe Thornber991d9fa2011-10-31 20:21:18 +00004023 */
4024static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
4025{
4026 int r;
4027 struct thin_c *tc;
Joe Thornber2dd9c252012-03-28 18:41:28 +01004028 struct dm_dev *pool_dev, *origin_dev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004029 struct mapped_device *pool_md;
Joe Thornber5e3283e2014-04-08 11:08:41 +01004030 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004031
4032 mutex_lock(&dm_thin_pool_table.mutex);
4033
Joe Thornber2dd9c252012-03-28 18:41:28 +01004034 if (argc != 2 && argc != 3) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00004035 ti->error = "Invalid argument count";
4036 r = -EINVAL;
4037 goto out_unlock;
4038 }
4039
4040 tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
4041 if (!tc) {
4042 ti->error = "Out of memory";
4043 r = -ENOMEM;
4044 goto out_unlock;
4045 }
Mike Snitzer583024d2014-10-28 20:58:45 -04004046 tc->thin_md = dm_table_get_md(ti->table);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004047 spin_lock_init(&tc->lock);
Joe Thornbera374bb22014-10-10 13:43:14 +01004048 INIT_LIST_HEAD(&tc->deferred_cells);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004049 bio_list_init(&tc->deferred_bio_list);
4050 bio_list_init(&tc->retry_on_resume_list);
Mike Snitzer67324ea2014-03-21 18:33:41 -04004051 tc->sort_bio_list = RB_ROOT;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004052
Joe Thornber2dd9c252012-03-28 18:41:28 +01004053 if (argc == 3) {
4054 r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
4055 if (r) {
4056 ti->error = "Error opening origin device";
4057 goto bad_origin_dev;
4058 }
4059 tc->origin_dev = origin_dev;
4060 }
4061
Joe Thornber991d9fa2011-10-31 20:21:18 +00004062 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
4063 if (r) {
4064 ti->error = "Error opening pool device";
4065 goto bad_pool_dev;
4066 }
4067 tc->pool_dev = pool_dev;
4068
4069 if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
4070 ti->error = "Invalid device id";
4071 r = -EINVAL;
4072 goto bad_common;
4073 }
4074
4075 pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
4076 if (!pool_md) {
4077 ti->error = "Couldn't get pool mapped device";
4078 r = -EINVAL;
4079 goto bad_common;
4080 }
4081
4082 tc->pool = __pool_table_lookup(pool_md);
4083 if (!tc->pool) {
4084 ti->error = "Couldn't find pool object";
4085 r = -EINVAL;
4086 goto bad_pool_lookup;
4087 }
4088 __pool_inc(tc->pool);
4089
Joe Thornbere49e5822012-07-27 15:08:16 +01004090 if (get_pool_mode(tc->pool) == PM_FAIL) {
4091 ti->error = "Couldn't open thin device, Pool is in fail mode";
Mike Snitzer1acacc02014-02-19 20:32:33 -05004092 r = -EINVAL;
Mike Snitzer80e96c52014-11-07 15:09:46 -05004093 goto bad_pool;
Joe Thornbere49e5822012-07-27 15:08:16 +01004094 }
4095
Joe Thornber991d9fa2011-10-31 20:21:18 +00004096 r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
4097 if (r) {
4098 ti->error = "Couldn't open thin internal device";
Mike Snitzer80e96c52014-11-07 15:09:46 -05004099 goto bad_pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004100 }
4101
Mike Snitzer542f9032012-07-27 15:08:00 +01004102 r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
4103 if (r)
Mike Snitzer80e96c52014-11-07 15:09:46 -05004104 goto bad;
Mike Snitzer542f9032012-07-27 15:08:00 +01004105
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00004106 ti->num_flush_bios = 1;
Joe Thornber16ad3d12012-07-27 15:08:07 +01004107 ti->flush_supported = true;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00004108 ti->per_bio_data_size = sizeof(struct dm_thin_endio_hook);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01004109
4110 /* In case the pool supports discards, pass them on. */
Mike Snitzerb60ab992013-09-19 18:49:11 -04004111 ti->discard_zeroes_data_unsupported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01004112 if (tc->pool->pf.discard_enabled) {
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01004113 ti->discards_supported = true;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00004114 ti->num_discard_bios = 1;
Joe Thornber34fbcf62015-04-16 12:58:35 +01004115 ti->split_discard_bios = false;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01004116 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00004117
Joe Thornber991d9fa2011-10-31 20:21:18 +00004118 mutex_unlock(&dm_thin_pool_table.mutex);
4119
Joe Thornber5e3283e2014-04-08 11:08:41 +01004120 spin_lock_irqsave(&tc->pool->lock, flags);
Mike Snitzer80e96c52014-11-07 15:09:46 -05004121 if (tc->pool->suspended) {
4122 spin_unlock_irqrestore(&tc->pool->lock, flags);
4123 mutex_lock(&dm_thin_pool_table.mutex); /* reacquire for __pool_dec */
4124 ti->error = "Unable to activate thin device while pool is suspended";
4125 r = -EINVAL;
4126 goto bad;
4127 }
Marc Dionne2b94e892014-12-17 07:59:59 -05004128 atomic_set(&tc->refcount, 1);
4129 init_completion(&tc->can_destroy);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004130 list_add_tail_rcu(&tc->list, &tc->pool->active_thins);
Joe Thornber5e3283e2014-04-08 11:08:41 +01004131 spin_unlock_irqrestore(&tc->pool->lock, flags);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004132 /*
4133 * This synchronize_rcu() call is needed here otherwise we risk a
4134 * wake_worker() call finding no bios to process (because the newly
4135 * added tc isn't yet visible). So this reduces latency since we
4136 * aren't then dependent on the periodic commit to wake_worker().
4137 */
4138 synchronize_rcu();
4139
Mike Snitzer80e96c52014-11-07 15:09:46 -05004140 dm_put(pool_md);
4141
Joe Thornber991d9fa2011-10-31 20:21:18 +00004142 return 0;
4143
Mike Snitzer80e96c52014-11-07 15:09:46 -05004144bad:
Mike Snitzer1acacc02014-02-19 20:32:33 -05004145 dm_pool_close_thin_device(tc->td);
Mike Snitzer80e96c52014-11-07 15:09:46 -05004146bad_pool:
Joe Thornber991d9fa2011-10-31 20:21:18 +00004147 __pool_dec(tc->pool);
4148bad_pool_lookup:
4149 dm_put(pool_md);
4150bad_common:
4151 dm_put_device(ti, tc->pool_dev);
4152bad_pool_dev:
Joe Thornber2dd9c252012-03-28 18:41:28 +01004153 if (tc->origin_dev)
4154 dm_put_device(ti, tc->origin_dev);
4155bad_origin_dev:
Joe Thornber991d9fa2011-10-31 20:21:18 +00004156 kfree(tc);
4157out_unlock:
4158 mutex_unlock(&dm_thin_pool_table.mutex);
4159
4160 return r;
4161}
4162
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00004163static int thin_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00004164{
Kent Overstreet4f024f32013-10-11 15:44:27 -07004165 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004166
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00004167 return thin_bio_map(ti, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004168}
4169
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00004170static int thin_endio(struct dm_target *ti, struct bio *bio, int err)
Joe Thornbereb2aa482012-03-28 18:41:28 +01004171{
4172 unsigned long flags;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00004173 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01004174 struct list_head work;
Mike Snitzera24c2562012-06-03 00:30:00 +01004175 struct dm_thin_new_mapping *m, *tmp;
Joe Thornbereb2aa482012-03-28 18:41:28 +01004176 struct pool *pool = h->tc->pool;
4177
4178 if (h->shared_read_entry) {
4179 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01004180 dm_deferred_entry_dec(h->shared_read_entry, &work);
Joe Thornbereb2aa482012-03-28 18:41:28 +01004181
4182 spin_lock_irqsave(&pool->lock, flags);
4183 list_for_each_entry_safe(m, tmp, &work, list) {
4184 list_del(&m->list);
Joe Thornber50f3c3e2014-06-13 13:57:09 +01004185 __complete_mapping_preparation(m);
Joe Thornbereb2aa482012-03-28 18:41:28 +01004186 }
4187 spin_unlock_irqrestore(&pool->lock, flags);
4188 }
4189
Joe Thornber104655f2012-03-28 18:41:28 +01004190 if (h->all_io_entry) {
4191 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01004192 dm_deferred_entry_dec(h->all_io_entry, &work);
Joe Thornber563af182012-12-21 20:23:31 +00004193 if (!list_empty(&work)) {
4194 spin_lock_irqsave(&pool->lock, flags);
4195 list_for_each_entry_safe(m, tmp, &work, list)
Mike Snitzerdaec3382013-12-11 14:01:20 -05004196 list_add_tail(&m->list, &pool->prepared_discards);
Joe Thornber563af182012-12-21 20:23:31 +00004197 spin_unlock_irqrestore(&pool->lock, flags);
4198 wake_worker(pool);
4199 }
Joe Thornber104655f2012-03-28 18:41:28 +01004200 }
4201
Joe Thornber34fbcf62015-04-16 12:58:35 +01004202 if (h->cell)
4203 cell_defer_no_holder(h->tc, h->cell);
4204
Joe Thornbereb2aa482012-03-28 18:41:28 +01004205 return 0;
4206}
4207
Joe Thornber738211f2014-03-03 15:52:28 +00004208static void thin_presuspend(struct dm_target *ti)
4209{
4210 struct thin_c *tc = ti->private;
4211
4212 if (dm_noflush_suspending(ti))
4213 noflush_work(tc, do_noflush_start);
4214}
4215
Joe Thornber991d9fa2011-10-31 20:21:18 +00004216static void thin_postsuspend(struct dm_target *ti)
4217{
Joe Thornber738211f2014-03-03 15:52:28 +00004218 struct thin_c *tc = ti->private;
4219
4220 /*
4221 * The dm_noflush_suspending flag has been cleared by now, so
4222 * unfortunately we must always run this.
4223 */
4224 noflush_work(tc, do_noflush_stop);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004225}
4226
Joe Thornbere5aea7b2014-06-13 14:47:24 +01004227static int thin_preresume(struct dm_target *ti)
4228{
4229 struct thin_c *tc = ti->private;
4230
4231 if (tc->origin_dev)
4232 tc->origin_size = get_dev_size(tc->origin_dev->bdev);
4233
4234 return 0;
4235}
4236
Joe Thornber991d9fa2011-10-31 20:21:18 +00004237/*
4238 * <nr mapped sectors> <highest mapped sector>
4239 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004240static void thin_status(struct dm_target *ti, status_type_t type,
4241 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00004242{
4243 int r;
4244 ssize_t sz = 0;
4245 dm_block_t mapped, highest;
4246 char buf[BDEVNAME_SIZE];
4247 struct thin_c *tc = ti->private;
4248
Joe Thornbere49e5822012-07-27 15:08:16 +01004249 if (get_pool_mode(tc->pool) == PM_FAIL) {
4250 DMEMIT("Fail");
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004251 return;
Joe Thornbere49e5822012-07-27 15:08:16 +01004252 }
4253
Joe Thornber991d9fa2011-10-31 20:21:18 +00004254 if (!tc->td)
4255 DMEMIT("-");
4256 else {
4257 switch (type) {
4258 case STATUSTYPE_INFO:
4259 r = dm_thin_get_mapped_count(tc->td, &mapped);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004260 if (r) {
4261 DMERR("dm_thin_get_mapped_count returned %d", r);
4262 goto err;
4263 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00004264
4265 r = dm_thin_get_highest_mapped_block(tc->td, &highest);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004266 if (r < 0) {
4267 DMERR("dm_thin_get_highest_mapped_block returned %d", r);
4268 goto err;
4269 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00004270
4271 DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
4272 if (r)
4273 DMEMIT("%llu", ((highest + 1) *
4274 tc->pool->sectors_per_block) - 1);
4275 else
4276 DMEMIT("-");
4277 break;
4278
4279 case STATUSTYPE_TABLE:
4280 DMEMIT("%s %lu",
4281 format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
4282 (unsigned long) tc->dev_id);
Joe Thornber2dd9c252012-03-28 18:41:28 +01004283 if (tc->origin_dev)
4284 DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
Joe Thornber991d9fa2011-10-31 20:21:18 +00004285 break;
4286 }
4287 }
4288
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004289 return;
4290
4291err:
4292 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00004293}
4294
Mike Snitzer36f12ae2014-10-09 15:24:12 -04004295static int thin_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
4296 struct bio_vec *biovec, int max_size)
4297{
4298 struct thin_c *tc = ti->private;
4299 struct request_queue *q = bdev_get_queue(tc->pool_dev->bdev);
4300
4301 if (!q->merge_bvec_fn)
4302 return max_size;
4303
4304 bvm->bi_bdev = tc->pool_dev->bdev;
4305 bvm->bi_sector = dm_target_offset(ti, bvm->bi_sector);
4306
4307 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
4308}
4309
Joe Thornber991d9fa2011-10-31 20:21:18 +00004310static int thin_iterate_devices(struct dm_target *ti,
4311 iterate_devices_callout_fn fn, void *data)
4312{
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004313 sector_t blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004314 struct thin_c *tc = ti->private;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004315 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004316
4317 /*
4318 * We can't call dm_pool_get_data_dev_size() since that blocks. So
4319 * we follow a more convoluted path through to the pool's target.
4320 */
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004321 if (!pool->ti)
Joe Thornber991d9fa2011-10-31 20:21:18 +00004322 return 0; /* nothing is bound */
4323
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004324 blocks = pool->ti->len;
4325 (void) sector_div(blocks, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004326 if (blocks)
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004327 return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004328
4329 return 0;
4330}
4331
Joe Thornber34fbcf62015-04-16 12:58:35 +01004332static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)
4333{
4334 struct thin_c *tc = ti->private;
4335 struct pool *pool = tc->pool;
4336
4337 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
4338 limits->max_discard_sectors = 2048 * 1024 * 16; /* 16G */
4339}
4340
Joe Thornber991d9fa2011-10-31 20:21:18 +00004341static struct target_type thin_target = {
4342 .name = "thin",
Mike Snitzere4c78e22015-07-15 11:40:24 -04004343 .version = {1, 16, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00004344 .module = THIS_MODULE,
4345 .ctr = thin_ctr,
4346 .dtr = thin_dtr,
4347 .map = thin_map,
Joe Thornbereb2aa482012-03-28 18:41:28 +01004348 .end_io = thin_endio,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01004349 .preresume = thin_preresume,
Joe Thornber738211f2014-03-03 15:52:28 +00004350 .presuspend = thin_presuspend,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004351 .postsuspend = thin_postsuspend,
4352 .status = thin_status,
Mike Snitzer36f12ae2014-10-09 15:24:12 -04004353 .merge = thin_merge,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004354 .iterate_devices = thin_iterate_devices,
Joe Thornber34fbcf62015-04-16 12:58:35 +01004355 .io_hints = thin_io_hints,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004356};
4357
4358/*----------------------------------------------------------------*/
4359
4360static int __init dm_thin_init(void)
4361{
4362 int r;
4363
4364 pool_table_init();
4365
4366 r = dm_register_target(&thin_target);
4367 if (r)
4368 return r;
4369
4370 r = dm_register_target(&pool_target);
4371 if (r)
Mike Snitzera24c2562012-06-03 00:30:00 +01004372 goto bad_pool_target;
4373
4374 r = -ENOMEM;
4375
Mike Snitzera24c2562012-06-03 00:30:00 +01004376 _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
4377 if (!_new_mapping_cache)
4378 goto bad_new_mapping_cache;
4379
Mike Snitzera24c2562012-06-03 00:30:00 +01004380 return 0;
4381
Mike Snitzera24c2562012-06-03 00:30:00 +01004382bad_new_mapping_cache:
Mike Snitzera24c2562012-06-03 00:30:00 +01004383 dm_unregister_target(&pool_target);
4384bad_pool_target:
4385 dm_unregister_target(&thin_target);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004386
4387 return r;
4388}
4389
4390static void dm_thin_exit(void)
4391{
4392 dm_unregister_target(&thin_target);
4393 dm_unregister_target(&pool_target);
Mike Snitzera24c2562012-06-03 00:30:00 +01004394
Mike Snitzera24c2562012-06-03 00:30:00 +01004395 kmem_cache_destroy(_new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004396}
4397
4398module_init(dm_thin_init);
4399module_exit(dm_thin_exit);
4400
Mike Snitzer80c57892014-05-20 13:38:33 -04004401module_param_named(no_space_timeout, no_space_timeout_secs, uint, S_IRUGO | S_IWUSR);
4402MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds");
4403
Alasdair G Kergon7cab8bf2012-05-12 01:43:19 +01004404MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
Joe Thornber991d9fa2011-10-31 20:21:18 +00004405MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
4406MODULE_LICENSE("GPL");