blob: 81309d7836c5d31dfce67079e81725ac08519c89 [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 Thornbera9537db2018-09-10 16:50:09 +0100203
204 /*
205 * Like READ_ONLY, except may switch back to WRITE on metadata resize. Reported as READ_ONLY.
206 */
207 PM_OUT_OF_METADATA_SPACE,
Joe Thornbere49e5822012-07-27 15:08:16 +0100208 PM_READ_ONLY, /* metadata may not be changed */
Joe Thornbera9537db2018-09-10 16:50:09 +0100209
Joe Thornbere49e5822012-07-27 15:08:16 +0100210 PM_FAIL, /* all I/O fails */
211};
212
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100213struct pool_features {
Joe Thornbere49e5822012-07-27 15:08:16 +0100214 enum pool_mode mode;
215
Mike Snitzer9bc142d2012-09-26 23:45:46 +0100216 bool zero_new_blocks:1;
217 bool discard_enabled:1;
218 bool discard_passdown:1;
Mike Snitzer787a996c2013-12-06 16:21:43 -0500219 bool error_if_no_space:1;
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100220};
221
Joe Thornbere49e5822012-07-27 15:08:16 +0100222struct thin_c;
223typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio);
Joe Thornbera374bb22014-10-10 13:43:14 +0100224typedef void (*process_cell_fn)(struct thin_c *tc, struct dm_bio_prison_cell *cell);
Joe Thornbere49e5822012-07-27 15:08:16 +0100225typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m);
226
Joe Thornberac4c3f32014-10-10 16:42:10 +0100227#define CELL_SORT_ARRAY_SIZE 8192
228
Joe Thornber991d9fa2011-10-31 20:21:18 +0000229struct pool {
230 struct list_head list;
231 struct dm_target *ti; /* Only set if a pool target is bound */
232
233 struct mapped_device *pool_md;
234 struct block_device *md_dev;
235 struct dm_pool_metadata *pmd;
236
Joe Thornber991d9fa2011-10-31 20:21:18 +0000237 dm_block_t low_water_blocks;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100238 uint32_t sectors_per_block;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100239 int sectors_per_block_shift;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000240
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100241 struct pool_features pf;
Joe Thornber88a66212013-12-04 20:16:12 -0500242 bool low_water_triggered:1; /* A dm event has been sent */
Mike Snitzer80e96c52014-11-07 15:09:46 -0500243 bool suspended:1;
Mike Snitzerc3667cc2016-03-10 11:31:35 -0500244 bool out_of_data_space:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000245
Mike Snitzer44feb382012-10-12 21:02:10 +0100246 struct dm_bio_prison *prison;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000247 struct dm_kcopyd_client *copier;
248
249 struct workqueue_struct *wq;
Joe Thornber7d327fe2014-10-06 15:45:59 +0100250 struct throttle throttle;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000251 struct work_struct worker;
Joe Thornber905e51b2012-03-28 18:41:27 +0100252 struct delayed_work waker;
Joe Thornber85ad643b2014-05-09 15:59:38 +0100253 struct delayed_work no_space_timeout;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000254
Joe Thornber905e51b2012-03-28 18:41:27 +0100255 unsigned long last_commit_jiffies;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100256 unsigned ref_count;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000257
258 spinlock_t lock;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000259 struct bio_list deferred_flush_bios;
260 struct list_head prepared_mappings;
Joe Thornber104655f2012-03-28 18:41:28 +0100261 struct list_head prepared_discards;
Joe Thornber2a0fbff2016-07-01 14:00:02 +0100262 struct list_head prepared_discards_pt2;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400263 struct list_head active_thins;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000264
Mike Snitzer44feb382012-10-12 21:02:10 +0100265 struct dm_deferred_set *shared_read_ds;
266 struct dm_deferred_set *all_io_ds;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000267
Mike Snitzera24c2562012-06-03 00:30:00 +0100268 struct dm_thin_new_mapping *next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000269 mempool_t *mapping_pool;
Joe Thornbere49e5822012-07-27 15:08:16 +0100270
271 process_bio_fn process_bio;
272 process_bio_fn process_discard;
273
Joe Thornbera374bb22014-10-10 13:43:14 +0100274 process_cell_fn process_cell;
275 process_cell_fn process_discard_cell;
276
Joe Thornbere49e5822012-07-27 15:08:16 +0100277 process_mapping_fn process_prepared_mapping;
278 process_mapping_fn process_prepared_discard;
Joe Thornber2a0fbff2016-07-01 14:00:02 +0100279 process_mapping_fn process_prepared_discard_pt2;
Joe Thornberac4c3f32014-10-10 16:42:10 +0100280
Joe Thornbera822c832015-07-03 10:22:42 +0100281 struct dm_bio_prison_cell **cell_sort_array;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000282};
283
Joe Thornbere49e5822012-07-27 15:08:16 +0100284static enum pool_mode get_pool_mode(struct pool *pool);
Joe Thornberb5330652013-12-04 19:51:33 -0500285static void metadata_operation_failed(struct pool *pool, const char *op, int r);
Joe Thornbere49e5822012-07-27 15:08:16 +0100286
Joe Thornber991d9fa2011-10-31 20:21:18 +0000287/*
288 * Target context for a pool.
289 */
290struct pool_c {
291 struct dm_target *ti;
292 struct pool *pool;
293 struct dm_dev *data_dev;
294 struct dm_dev *metadata_dev;
295 struct dm_target_callbacks callbacks;
296
297 dm_block_t low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +0100298 struct pool_features requested_pf; /* Features requested during table load */
299 struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000300};
301
302/*
303 * Target context for a thin.
304 */
305struct thin_c {
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400306 struct list_head list;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000307 struct dm_dev *pool_dev;
Joe Thornber2dd9c252012-03-28 18:41:28 +0100308 struct dm_dev *origin_dev;
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100309 sector_t origin_size;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000310 dm_thin_id dev_id;
311
312 struct pool *pool;
313 struct dm_thin_device *td;
Mike Snitzer583024d2014-10-28 20:58:45 -0400314 struct mapped_device *thin_md;
315
Joe Thornber738211f2014-03-03 15:52:28 +0000316 bool requeue_mode:1;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400317 spinlock_t lock;
Joe Thornbera374bb22014-10-10 13:43:14 +0100318 struct list_head deferred_cells;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400319 struct bio_list deferred_bio_list;
320 struct bio_list retry_on_resume_list;
Mike Snitzer67324ea2014-03-21 18:33:41 -0400321 struct rb_root sort_bio_list; /* sorted list of deferred bios */
Joe Thornberb10ebd32014-04-08 11:29:01 +0100322
323 /*
324 * Ensures the thin is not destroyed until the worker has finished
325 * iterating the active_thins list.
326 */
327 atomic_t refcount;
328 struct completion can_destroy;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000329};
330
331/*----------------------------------------------------------------*/
332
Joe Thornber34fbcf62015-04-16 12:58:35 +0100333static bool block_size_is_power_of_two(struct pool *pool)
334{
335 return pool->sectors_per_block_shift >= 0;
336}
337
338static sector_t block_to_sectors(struct pool *pool, dm_block_t b)
339{
340 return block_size_is_power_of_two(pool) ?
341 (b << pool->sectors_per_block_shift) :
342 (b * pool->sectors_per_block);
343}
344
Joe Thornber202bae52016-05-04 14:12:42 -0400345/*----------------------------------------------------------------*/
346
347struct discard_op {
348 struct thin_c *tc;
349 struct blk_plug plug;
350 struct bio *parent_bio;
351 struct bio *bio;
352};
353
354static void begin_discard(struct discard_op *op, struct thin_c *tc, struct bio *parent)
Joe Thornber34fbcf62015-04-16 12:58:35 +0100355{
Joe Thornber202bae52016-05-04 14:12:42 -0400356 BUG_ON(!parent);
357
358 op->tc = tc;
359 blk_start_plug(&op->plug);
360 op->parent_bio = parent;
361 op->bio = NULL;
362}
363
364static int issue_discard(struct discard_op *op, dm_block_t data_b, dm_block_t data_e)
365{
366 struct thin_c *tc = op->tc;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100367 sector_t s = block_to_sectors(tc->pool, data_b);
368 sector_t len = block_to_sectors(tc->pool, data_e - data_b);
369
Joe Thornber202bae52016-05-04 14:12:42 -0400370 return __blkdev_issue_discard(tc->pool_dev->bdev, s, len,
Mike Christie469e3212016-06-05 14:31:49 -0500371 GFP_NOWAIT, 0, &op->bio);
Joe Thornber202bae52016-05-04 14:12:42 -0400372}
373
374static void end_discard(struct discard_op *op, int r)
375{
376 if (op->bio) {
377 /*
378 * Even if one of the calls to issue_discard failed, we
379 * need to wait for the chain to complete.
380 */
381 bio_chain(op->bio, op->parent_bio);
Mike Christiee6047142016-06-05 14:32:04 -0500382 bio_set_op_attrs(op->bio, REQ_OP_DISCARD, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -0500383 submit_bio(op->bio);
Mike Snitzer3dba53a92016-05-02 20:16:21 -0400384 }
Mike Snitzer3dba53a92016-05-02 20:16:21 -0400385
Joe Thornber202bae52016-05-04 14:12:42 -0400386 blk_finish_plug(&op->plug);
387
388 /*
389 * Even if r is set, there could be sub discards in flight that we
390 * need to wait for.
391 */
392 if (r && !op->parent_bio->bi_error)
393 op->parent_bio->bi_error = r;
394 bio_endio(op->parent_bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +0100395}
396
397/*----------------------------------------------------------------*/
398
Joe Thornber025b9682013-03-01 22:45:50 +0000399/*
400 * wake_worker() is used when new work is queued and when pool_resume is
401 * ready to continue deferred IO processing.
402 */
403static void wake_worker(struct pool *pool)
404{
405 queue_work(pool->wq, &pool->worker);
406}
407
408/*----------------------------------------------------------------*/
409
Joe Thornber6beca5e2013-03-01 22:45:50 +0000410static int bio_detain(struct pool *pool, struct dm_cell_key *key, struct bio *bio,
411 struct dm_bio_prison_cell **cell_result)
412{
413 int r;
414 struct dm_bio_prison_cell *cell_prealloc;
415
416 /*
417 * Allocate a cell from the prison's mempool.
418 * This might block but it can't fail.
419 */
420 cell_prealloc = dm_bio_prison_alloc_cell(pool->prison, GFP_NOIO);
421
422 r = dm_bio_detain(pool->prison, key, bio, cell_prealloc, cell_result);
423 if (r)
424 /*
425 * We reused an old cell; we can get rid of
426 * the new one.
427 */
428 dm_bio_prison_free_cell(pool->prison, cell_prealloc);
429
430 return r;
431}
432
433static void cell_release(struct pool *pool,
434 struct dm_bio_prison_cell *cell,
435 struct bio_list *bios)
436{
437 dm_cell_release(pool->prison, cell, bios);
438 dm_bio_prison_free_cell(pool->prison, cell);
439}
440
Joe Thornber2d759a42014-10-10 15:27:16 +0100441static void cell_visit_release(struct pool *pool,
442 void (*fn)(void *, struct dm_bio_prison_cell *),
443 void *context,
444 struct dm_bio_prison_cell *cell)
445{
446 dm_cell_visit_release(pool->prison, fn, context, cell);
447 dm_bio_prison_free_cell(pool->prison, cell);
448}
449
Joe Thornber6beca5e2013-03-01 22:45:50 +0000450static void cell_release_no_holder(struct pool *pool,
451 struct dm_bio_prison_cell *cell,
452 struct bio_list *bios)
453{
454 dm_cell_release_no_holder(pool->prison, cell, bios);
455 dm_bio_prison_free_cell(pool->prison, cell);
456}
457
Mike Snitzeraf918052014-05-22 14:32:51 -0400458static void cell_error_with_code(struct pool *pool,
459 struct dm_bio_prison_cell *cell, int error_code)
Joe Thornber6beca5e2013-03-01 22:45:50 +0000460{
Mike Snitzeraf918052014-05-22 14:32:51 -0400461 dm_cell_error(pool->prison, cell, error_code);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000462 dm_bio_prison_free_cell(pool->prison, cell);
463}
464
Mike Snitzerc3667cc2016-03-10 11:31:35 -0500465static int get_pool_io_error_code(struct pool *pool)
466{
467 return pool->out_of_data_space ? -ENOSPC : -EIO;
468}
469
Mike Snitzeraf918052014-05-22 14:32:51 -0400470static void cell_error(struct pool *pool, struct dm_bio_prison_cell *cell)
471{
Mike Snitzerc3667cc2016-03-10 11:31:35 -0500472 int error = get_pool_io_error_code(pool);
473
474 cell_error_with_code(pool, cell, error);
Mike Snitzeraf918052014-05-22 14:32:51 -0400475}
476
Joe Thornbera374bb22014-10-10 13:43:14 +0100477static void cell_success(struct pool *pool, struct dm_bio_prison_cell *cell)
478{
479 cell_error_with_code(pool, cell, 0);
480}
481
482static void cell_requeue(struct pool *pool, struct dm_bio_prison_cell *cell)
483{
484 cell_error_with_code(pool, cell, DM_ENDIO_REQUEUE);
485}
486
Joe Thornber6beca5e2013-03-01 22:45:50 +0000487/*----------------------------------------------------------------*/
488
Joe Thornber991d9fa2011-10-31 20:21:18 +0000489/*
490 * A global list of pools that uses a struct mapped_device as a key.
491 */
492static struct dm_thin_pool_table {
493 struct mutex mutex;
494 struct list_head pools;
495} dm_thin_pool_table;
496
497static void pool_table_init(void)
498{
499 mutex_init(&dm_thin_pool_table.mutex);
500 INIT_LIST_HEAD(&dm_thin_pool_table.pools);
501}
502
503static void __pool_table_insert(struct pool *pool)
504{
505 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
506 list_add(&pool->list, &dm_thin_pool_table.pools);
507}
508
509static void __pool_table_remove(struct pool *pool)
510{
511 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
512 list_del(&pool->list);
513}
514
515static struct pool *__pool_table_lookup(struct mapped_device *md)
516{
517 struct pool *pool = NULL, *tmp;
518
519 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
520
521 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
522 if (tmp->pool_md == md) {
523 pool = tmp;
524 break;
525 }
526 }
527
528 return pool;
529}
530
531static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
532{
533 struct pool *pool = NULL, *tmp;
534
535 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
536
537 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
538 if (tmp->md_dev == md_dev) {
539 pool = tmp;
540 break;
541 }
542 }
543
544 return pool;
545}
546
547/*----------------------------------------------------------------*/
548
Mike Snitzera24c2562012-06-03 00:30:00 +0100549struct dm_thin_endio_hook {
Joe Thornbereb2aa482012-03-28 18:41:28 +0100550 struct thin_c *tc;
Mike Snitzer44feb382012-10-12 21:02:10 +0100551 struct dm_deferred_entry *shared_read_entry;
552 struct dm_deferred_entry *all_io_entry;
Mike Snitzera24c2562012-06-03 00:30:00 +0100553 struct dm_thin_new_mapping *overwrite_mapping;
Mike Snitzer67324ea2014-03-21 18:33:41 -0400554 struct rb_node rb_node;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100555 struct dm_bio_prison_cell *cell;
Joe Thornbereb2aa482012-03-28 18:41:28 +0100556};
557
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400558static void __merge_bio_list(struct bio_list *bios, struct bio_list *master)
559{
560 bio_list_merge(bios, master);
561 bio_list_init(master);
562}
563
564static void error_bio_list(struct bio_list *bios, int error)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000565{
566 struct bio *bio;
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400567
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200568 while ((bio = bio_list_pop(bios))) {
569 bio->bi_error = error;
570 bio_endio(bio);
571 }
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400572}
573
574static void error_thin_bio_list(struct thin_c *tc, struct bio_list *master, int error)
575{
Joe Thornber991d9fa2011-10-31 20:21:18 +0000576 struct bio_list bios;
Joe Thornber18adc572014-03-03 15:46:42 +0000577 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000578
579 bio_list_init(&bios);
Joe Thornber18adc572014-03-03 15:46:42 +0000580
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400581 spin_lock_irqsave(&tc->lock, flags);
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400582 __merge_bio_list(&bios, master);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400583 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000584
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400585 error_bio_list(&bios, error);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000586}
587
Joe Thornbera374bb22014-10-10 13:43:14 +0100588static void requeue_deferred_cells(struct thin_c *tc)
589{
590 struct pool *pool = tc->pool;
591 unsigned long flags;
592 struct list_head cells;
593 struct dm_bio_prison_cell *cell, *tmp;
594
595 INIT_LIST_HEAD(&cells);
596
597 spin_lock_irqsave(&tc->lock, flags);
598 list_splice_init(&tc->deferred_cells, &cells);
599 spin_unlock_irqrestore(&tc->lock, flags);
600
601 list_for_each_entry_safe(cell, tmp, &cells, user_list)
602 cell_requeue(pool, cell);
603}
604
Joe Thornber991d9fa2011-10-31 20:21:18 +0000605static void requeue_io(struct thin_c *tc)
606{
Joe Thornber3e1a0692014-03-03 16:03:26 +0000607 struct bio_list bios;
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400608 unsigned long flags;
Joe Thornber3e1a0692014-03-03 16:03:26 +0000609
610 bio_list_init(&bios);
611
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400612 spin_lock_irqsave(&tc->lock, flags);
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400613 __merge_bio_list(&bios, &tc->deferred_bio_list);
614 __merge_bio_list(&bios, &tc->retry_on_resume_list);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400615 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber3e1a0692014-03-03 16:03:26 +0000616
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400617 error_bio_list(&bios, DM_ENDIO_REQUEUE);
618 requeue_deferred_cells(tc);
Joe Thornber3e1a0692014-03-03 16:03:26 +0000619}
620
Mike Snitzer0a927c22015-07-21 13:20:46 -0400621static void error_retry_list_with_code(struct pool *pool, int error)
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400622{
623 struct thin_c *tc;
624
625 rcu_read_lock();
626 list_for_each_entry_rcu(tc, &pool->active_thins, list)
Mike Snitzer0a927c22015-07-21 13:20:46 -0400627 error_thin_bio_list(tc, &tc->retry_on_resume_list, error);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400628 rcu_read_unlock();
629}
630
Mike Snitzer0a927c22015-07-21 13:20:46 -0400631static void error_retry_list(struct pool *pool)
632{
Mike Snitzerc3667cc2016-03-10 11:31:35 -0500633 int error = get_pool_io_error_code(pool);
634
Amitoj Kaur Chawla813923b2016-04-11 11:37:08 +0530635 error_retry_list_with_code(pool, error);
Mike Snitzer0a927c22015-07-21 13:20:46 -0400636}
637
Joe Thornber991d9fa2011-10-31 20:21:18 +0000638/*
639 * This section of code contains the logic for processing a thin device's IO.
640 * Much of the code depends on pool object resources (lists, workqueues, etc)
641 * but most is exclusively called from the thin target rather than the thin-pool
642 * target.
643 */
644
645static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
646{
Mike Snitzer58f77a22013-03-01 22:45:45 +0000647 struct pool *pool = tc->pool;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700648 sector_t block_nr = bio->bi_iter.bi_sector;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100649
Mike Snitzer58f77a22013-03-01 22:45:45 +0000650 if (block_size_is_power_of_two(pool))
651 block_nr >>= pool->sectors_per_block_shift;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100652 else
Mike Snitzer58f77a22013-03-01 22:45:45 +0000653 (void) sector_div(block_nr, pool->sectors_per_block);
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100654
655 return block_nr;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000656}
657
Joe Thornber34fbcf62015-04-16 12:58:35 +0100658/*
659 * Returns the _complete_ blocks that this bio covers.
660 */
661static void get_bio_block_range(struct thin_c *tc, struct bio *bio,
662 dm_block_t *begin, dm_block_t *end)
663{
664 struct pool *pool = tc->pool;
665 sector_t b = bio->bi_iter.bi_sector;
666 sector_t e = b + (bio->bi_iter.bi_size >> SECTOR_SHIFT);
667
668 b += pool->sectors_per_block - 1ull; /* so we round up */
669
670 if (block_size_is_power_of_two(pool)) {
671 b >>= pool->sectors_per_block_shift;
672 e >>= pool->sectors_per_block_shift;
673 } else {
674 (void) sector_div(b, pool->sectors_per_block);
675 (void) sector_div(e, pool->sectors_per_block);
676 }
677
678 if (e < b)
679 /* Can happen if the bio is within a single block. */
680 e = b;
681
682 *begin = b;
683 *end = e;
684}
685
Joe Thornber991d9fa2011-10-31 20:21:18 +0000686static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
687{
688 struct pool *pool = tc->pool;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700689 sector_t bi_sector = bio->bi_iter.bi_sector;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000690
691 bio->bi_bdev = tc->pool_dev->bdev;
Mike Snitzer58f77a22013-03-01 22:45:45 +0000692 if (block_size_is_power_of_two(pool))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700693 bio->bi_iter.bi_sector =
694 (block << pool->sectors_per_block_shift) |
695 (bi_sector & (pool->sectors_per_block - 1));
Mike Snitzer58f77a22013-03-01 22:45:45 +0000696 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700697 bio->bi_iter.bi_sector = (block * pool->sectors_per_block) +
Mike Snitzer58f77a22013-03-01 22:45:45 +0000698 sector_div(bi_sector, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000699}
700
Joe Thornber2dd9c252012-03-28 18:41:28 +0100701static void remap_to_origin(struct thin_c *tc, struct bio *bio)
702{
703 bio->bi_bdev = tc->origin_dev->bdev;
704}
705
Joe Thornber4afdd682012-07-27 15:08:14 +0100706static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
707{
Jens Axboe1eff9d32016-08-05 15:35:16 -0600708 return (bio->bi_opf & (REQ_PREFLUSH | REQ_FUA)) &&
Joe Thornber4afdd682012-07-27 15:08:14 +0100709 dm_thin_changed_this_transaction(tc->td);
710}
711
Joe Thornbere8088072012-12-21 20:23:31 +0000712static void inc_all_io_entry(struct pool *pool, struct bio *bio)
713{
714 struct dm_thin_endio_hook *h;
715
Mike Christiee6047142016-06-05 14:32:04 -0500716 if (bio_op(bio) == REQ_OP_DISCARD)
Joe Thornbere8088072012-12-21 20:23:31 +0000717 return;
718
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000719 h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbere8088072012-12-21 20:23:31 +0000720 h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
721}
722
Joe Thornber2dd9c252012-03-28 18:41:28 +0100723static void issue(struct thin_c *tc, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000724{
725 struct pool *pool = tc->pool;
726 unsigned long flags;
727
Joe Thornbere49e5822012-07-27 15:08:16 +0100728 if (!bio_triggers_commit(tc, bio)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000729 generic_make_request(bio);
Joe Thornbere49e5822012-07-27 15:08:16 +0100730 return;
731 }
732
733 /*
734 * Complete bio with an error if earlier I/O caused changes to
735 * the metadata that can't be committed e.g, due to I/O errors
736 * on the metadata device.
737 */
738 if (dm_thin_aborted_changes(tc->td)) {
739 bio_io_error(bio);
740 return;
741 }
742
743 /*
744 * Batch together any bios that trigger commits and then issue a
745 * single commit for them in process_deferred_bios().
746 */
747 spin_lock_irqsave(&pool->lock, flags);
748 bio_list_add(&pool->deferred_flush_bios, bio);
749 spin_unlock_irqrestore(&pool->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000750}
751
Joe Thornber2dd9c252012-03-28 18:41:28 +0100752static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
753{
754 remap_to_origin(tc, bio);
755 issue(tc, bio);
756}
757
758static void remap_and_issue(struct thin_c *tc, struct bio *bio,
759 dm_block_t block)
760{
761 remap(tc, bio, block);
762 issue(tc, bio);
763}
764
Joe Thornber991d9fa2011-10-31 20:21:18 +0000765/*----------------------------------------------------------------*/
766
767/*
768 * Bio endio functions.
769 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100770struct dm_thin_new_mapping {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000771 struct list_head list;
772
Mike Snitzer7f214662013-12-17 13:43:31 -0500773 bool pass_discard:1;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100774 bool maybe_shared:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000775
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100776 /*
777 * Track quiescing, copying and zeroing preparation actions. When this
778 * counter hits zero the block is prepared and can be inserted into the
779 * btree.
780 */
781 atomic_t prepare_actions;
782
Mike Snitzer7f214662013-12-17 13:43:31 -0500783 int err;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000784 struct thin_c *tc;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100785 dm_block_t virt_begin, virt_end;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000786 dm_block_t data_block;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100787 struct dm_bio_prison_cell *cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000788
789 /*
790 * If the bio covers the whole area of a block then we can avoid
791 * zeroing or copying. Instead this bio is hooked. The bio will
792 * still be in the cell, so care has to be taken to avoid issuing
793 * the bio twice.
794 */
795 struct bio *bio;
796 bio_end_io_t *saved_bi_end_io;
797};
798
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100799static void __complete_mapping_preparation(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000800{
801 struct pool *pool = m->tc->pool;
802
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100803 if (atomic_dec_and_test(&m->prepare_actions)) {
Mike Snitzerdaec3382013-12-11 14:01:20 -0500804 list_add_tail(&m->list, &pool->prepared_mappings);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000805 wake_worker(pool);
806 }
807}
808
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100809static void complete_mapping_preparation(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000810{
811 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000812 struct pool *pool = m->tc->pool;
813
Joe Thornber991d9fa2011-10-31 20:21:18 +0000814 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100815 __complete_mapping_preparation(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000816 spin_unlock_irqrestore(&pool->lock, flags);
817}
818
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100819static void copy_complete(int read_err, unsigned long write_err, void *context)
820{
821 struct dm_thin_new_mapping *m = context;
822
823 m->err = read_err || write_err ? -EIO : 0;
824 complete_mapping_preparation(m);
825}
826
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200827static void overwrite_endio(struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000828{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000829 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Mike Snitzera24c2562012-06-03 00:30:00 +0100830 struct dm_thin_new_mapping *m = h->overwrite_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000831
Mike Snitzer8b908f82015-05-13 17:53:13 -0400832 bio->bi_end_io = m->saved_bi_end_io;
833
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200834 m->err = bio->bi_error;
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100835 complete_mapping_preparation(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000836}
837
Joe Thornber991d9fa2011-10-31 20:21:18 +0000838/*----------------------------------------------------------------*/
839
840/*
841 * Workqueue.
842 */
843
844/*
845 * Prepared mapping jobs.
846 */
847
848/*
Joe Thornber2d759a42014-10-10 15:27:16 +0100849 * This sends the bios in the cell, except the original holder, back
850 * to the deferred_bios list.
Joe Thornber991d9fa2011-10-31 20:21:18 +0000851 */
Joe Thornberf286ba02012-12-21 20:23:33 +0000852static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000853{
Joe Thornber991d9fa2011-10-31 20:21:18 +0000854 struct pool *pool = tc->pool;
855 unsigned long flags;
856
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400857 spin_lock_irqsave(&tc->lock, flags);
858 cell_release_no_holder(pool, cell, &tc->deferred_bio_list);
859 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000860
861 wake_worker(pool);
862}
863
Joe Thornbera374bb22014-10-10 13:43:14 +0100864static void thin_defer_bio(struct thin_c *tc, struct bio *bio);
865
Joe Thornber2d759a42014-10-10 15:27:16 +0100866struct remap_info {
867 struct thin_c *tc;
868 struct bio_list defer_bios;
869 struct bio_list issue_bios;
870};
871
872static void __inc_remap_and_issue_cell(void *context,
873 struct dm_bio_prison_cell *cell)
874{
875 struct remap_info *info = context;
876 struct bio *bio;
877
878 while ((bio = bio_list_pop(&cell->bios))) {
Jens Axboe1eff9d32016-08-05 15:35:16 -0600879 if (bio->bi_opf & (REQ_PREFLUSH | REQ_FUA) ||
Mike Christiee6047142016-06-05 14:32:04 -0500880 bio_op(bio) == REQ_OP_DISCARD)
Joe Thornber2d759a42014-10-10 15:27:16 +0100881 bio_list_add(&info->defer_bios, bio);
882 else {
883 inc_all_io_entry(info->tc->pool, bio);
884
885 /*
886 * We can't issue the bios with the bio prison lock
887 * held, so we add them to a list to issue on
888 * return from this function.
889 */
890 bio_list_add(&info->issue_bios, bio);
891 }
892 }
893}
894
Joe Thornbera374bb22014-10-10 13:43:14 +0100895static void inc_remap_and_issue_cell(struct thin_c *tc,
896 struct dm_bio_prison_cell *cell,
897 dm_block_t block)
898{
899 struct bio *bio;
Joe Thornber2d759a42014-10-10 15:27:16 +0100900 struct remap_info info;
Joe Thornbera374bb22014-10-10 13:43:14 +0100901
Joe Thornber2d759a42014-10-10 15:27:16 +0100902 info.tc = tc;
903 bio_list_init(&info.defer_bios);
904 bio_list_init(&info.issue_bios);
Joe Thornbera374bb22014-10-10 13:43:14 +0100905
Joe Thornber2d759a42014-10-10 15:27:16 +0100906 /*
907 * We have to be careful to inc any bios we're about to issue
908 * before the cell is released, and avoid a race with new bios
909 * being added to the cell.
910 */
911 cell_visit_release(tc->pool, __inc_remap_and_issue_cell,
912 &info, cell);
913
914 while ((bio = bio_list_pop(&info.defer_bios)))
915 thin_defer_bio(tc, bio);
916
917 while ((bio = bio_list_pop(&info.issue_bios)))
918 remap_and_issue(info.tc, bio, block);
Joe Thornbera374bb22014-10-10 13:43:14 +0100919}
920
Joe Thornbere49e5822012-07-27 15:08:16 +0100921static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
922{
Joe Thornber6beca5e2013-03-01 22:45:50 +0000923 cell_error(m->tc->pool, m->cell);
Joe Thornbere49e5822012-07-27 15:08:16 +0100924 list_del(&m->list);
925 mempool_free(m, m->tc->pool->mapping_pool);
926}
Joe Thornber025b9682013-03-01 22:45:50 +0000927
Mike Snitzera24c2562012-06-03 00:30:00 +0100928static void process_prepared_mapping(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000929{
930 struct thin_c *tc = m->tc;
Joe Thornber6beca5e2013-03-01 22:45:50 +0000931 struct pool *pool = tc->pool;
Mike Snitzer8b908f82015-05-13 17:53:13 -0400932 struct bio *bio = m->bio;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000933 int r;
934
Joe Thornber991d9fa2011-10-31 20:21:18 +0000935 if (m->err) {
Joe Thornber6beca5e2013-03-01 22:45:50 +0000936 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100937 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000938 }
939
940 /*
941 * Commit the prepared block into the mapping btree.
942 * Any I/O for this block arriving after this point will get
943 * remapped to it directly.
944 */
Joe Thornber34fbcf62015-04-16 12:58:35 +0100945 r = dm_thin_insert_block(tc->td, m->virt_begin, m->data_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000946 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -0500947 metadata_operation_failed(pool, "dm_thin_insert_block", r);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000948 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100949 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000950 }
951
952 /*
953 * Release any bios held while the block was being provisioned.
954 * If we are processing a write bio that completely covers the block,
955 * we already processed it so can ignore it now when processing
956 * the bios in the cell.
957 */
958 if (bio) {
Joe Thornber2d759a42014-10-10 15:27:16 +0100959 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200960 bio_endio(bio);
Joe Thornber2d759a42014-10-10 15:27:16 +0100961 } else {
962 inc_all_io_entry(tc->pool, m->cell->holder);
963 remap_and_issue(tc, m->cell->holder, m->data_block);
964 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
965 }
Joe Thornber991d9fa2011-10-31 20:21:18 +0000966
Joe Thornber905386f2012-07-27 15:08:05 +0100967out:
Joe Thornber991d9fa2011-10-31 20:21:18 +0000968 list_del(&m->list);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000969 mempool_free(m, pool->mapping_pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000970}
971
Joe Thornber34fbcf62015-04-16 12:58:35 +0100972/*----------------------------------------------------------------*/
973
974static void free_discard_mapping(struct dm_thin_new_mapping *m)
975{
976 struct thin_c *tc = m->tc;
977 if (m->cell)
978 cell_defer_no_holder(tc, m->cell);
979 mempool_free(m, tc->pool->mapping_pool);
980}
981
Joe Thornbere49e5822012-07-27 15:08:16 +0100982static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
Joe Thornber104655f2012-03-28 18:41:28 +0100983{
Joe Thornbere49e5822012-07-27 15:08:16 +0100984 bio_io_error(m->bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +0100985 free_discard_mapping(m);
Joe Thornbere49e5822012-07-27 15:08:16 +0100986}
Joe Thornber104655f2012-03-28 18:41:28 +0100987
Joe Thornber34fbcf62015-04-16 12:58:35 +0100988static void process_prepared_discard_success(struct dm_thin_new_mapping *m)
Joe Thornbere49e5822012-07-27 15:08:16 +0100989{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200990 bio_endio(m->bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +0100991 free_discard_mapping(m);
Joe Thornber104655f2012-03-28 18:41:28 +0100992}
993
Joe Thornber34fbcf62015-04-16 12:58:35 +0100994static void process_prepared_discard_no_passdown(struct dm_thin_new_mapping *m)
Joe Thornbere49e5822012-07-27 15:08:16 +0100995{
996 int r;
997 struct thin_c *tc = m->tc;
998
Joe Thornber34fbcf62015-04-16 12:58:35 +0100999 r = dm_thin_remove_range(tc->td, m->cell->key.block_begin, m->cell->key.block_end);
1000 if (r) {
1001 metadata_operation_failed(tc->pool, "dm_thin_remove_range", r);
1002 bio_io_error(m->bio);
1003 } else
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001004 bio_endio(m->bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001005
Joe Thornber34fbcf62015-04-16 12:58:35 +01001006 cell_defer_no_holder(tc, m->cell);
1007 mempool_free(m, tc->pool->mapping_pool);
1008}
1009
Joe Thornber202bae52016-05-04 14:12:42 -04001010/*----------------------------------------------------------------*/
1011
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001012static void passdown_double_checking_shared_status(struct dm_thin_new_mapping *m,
1013 struct bio *discard_parent)
Joe Thornber34fbcf62015-04-16 12:58:35 +01001014{
1015 /*
1016 * We've already unmapped this range of blocks, but before we
1017 * passdown we have to check that these blocks are now unused.
1018 */
Joe Thornber202bae52016-05-04 14:12:42 -04001019 int r = 0;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001020 bool used = true;
1021 struct thin_c *tc = m->tc;
1022 struct pool *pool = tc->pool;
1023 dm_block_t b = m->data_block, e, end = m->data_block + m->virt_end - m->virt_begin;
Joe Thornber202bae52016-05-04 14:12:42 -04001024 struct discard_op op;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001025
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001026 begin_discard(&op, tc, discard_parent);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001027 while (b != end) {
1028 /* find start of unmapped run */
1029 for (; b < end; b++) {
1030 r = dm_pool_block_is_used(pool->pmd, b, &used);
1031 if (r)
Joe Thornber202bae52016-05-04 14:12:42 -04001032 goto out;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001033
1034 if (!used)
1035 break;
1036 }
1037
1038 if (b == end)
1039 break;
1040
1041 /* find end of run */
1042 for (e = b + 1; e != end; e++) {
1043 r = dm_pool_block_is_used(pool->pmd, e, &used);
1044 if (r)
Joe Thornber202bae52016-05-04 14:12:42 -04001045 goto out;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001046
1047 if (used)
1048 break;
1049 }
1050
Joe Thornber202bae52016-05-04 14:12:42 -04001051 r = issue_discard(&op, b, e);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001052 if (r)
Joe Thornber202bae52016-05-04 14:12:42 -04001053 goto out;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001054
1055 b = e;
1056 }
Joe Thornber202bae52016-05-04 14:12:42 -04001057out:
1058 end_discard(&op, r);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001059}
1060
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001061static void queue_passdown_pt2(struct dm_thin_new_mapping *m)
1062{
1063 unsigned long flags;
1064 struct pool *pool = m->tc->pool;
1065
1066 spin_lock_irqsave(&pool->lock, flags);
1067 list_add_tail(&m->list, &pool->prepared_discards_pt2);
1068 spin_unlock_irqrestore(&pool->lock, flags);
1069 wake_worker(pool);
1070}
1071
1072static void passdown_endio(struct bio *bio)
1073{
1074 /*
1075 * It doesn't matter if the passdown discard failed, we still want
1076 * to unmap (we ignore err).
1077 */
1078 queue_passdown_pt2(bio->bi_private);
Dennis Yang17731312017-04-18 15:27:06 +08001079 bio_put(bio);
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001080}
1081
1082static void process_prepared_discard_passdown_pt1(struct dm_thin_new_mapping *m)
1083{
1084 int r;
1085 struct thin_c *tc = m->tc;
1086 struct pool *pool = tc->pool;
1087 struct bio *discard_parent;
1088 dm_block_t data_end = m->data_block + (m->virt_end - m->virt_begin);
1089
1090 /*
1091 * Only this thread allocates blocks, so we can be sure that the
1092 * newly unmapped blocks will not be allocated before the end of
1093 * the function.
1094 */
1095 r = dm_thin_remove_range(tc->td, m->virt_begin, m->virt_end);
1096 if (r) {
1097 metadata_operation_failed(pool, "dm_thin_remove_range", r);
1098 bio_io_error(m->bio);
1099 cell_defer_no_holder(tc, m->cell);
1100 mempool_free(m, pool->mapping_pool);
1101 return;
1102 }
1103
Vallish Vaidyeshwara1c0fa382017-06-23 18:53:06 +00001104 /*
1105 * Increment the unmapped blocks. This prevents a race between the
1106 * passdown io and reallocation of freed blocks.
1107 */
1108 r = dm_pool_inc_data_range(pool->pmd, m->data_block, data_end);
1109 if (r) {
1110 metadata_operation_failed(pool, "dm_pool_inc_data_range", r);
1111 bio_io_error(m->bio);
1112 cell_defer_no_holder(tc, m->cell);
1113 mempool_free(m, pool->mapping_pool);
1114 return;
1115 }
1116
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001117 discard_parent = bio_alloc(GFP_NOIO, 1);
1118 if (!discard_parent) {
1119 DMWARN("%s: unable to allocate top level discard bio for passdown. Skipping passdown.",
1120 dm_device_name(tc->pool->pool_md));
1121 queue_passdown_pt2(m);
1122
1123 } else {
1124 discard_parent->bi_end_io = passdown_endio;
1125 discard_parent->bi_private = m;
1126
1127 if (m->maybe_shared)
1128 passdown_double_checking_shared_status(m, discard_parent);
1129 else {
1130 struct discard_op op;
1131
1132 begin_discard(&op, tc, discard_parent);
1133 r = issue_discard(&op, m->data_block, data_end);
1134 end_discard(&op, r);
1135 }
1136 }
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001137}
1138
1139static void process_prepared_discard_passdown_pt2(struct dm_thin_new_mapping *m)
Joe Thornber34fbcf62015-04-16 12:58:35 +01001140{
1141 int r;
1142 struct thin_c *tc = m->tc;
1143 struct pool *pool = tc->pool;
1144
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001145 /*
1146 * The passdown has completed, so now we can decrement all those
1147 * unmapped blocks.
1148 */
1149 r = dm_pool_dec_data_range(pool->pmd, m->data_block,
1150 m->data_block + (m->virt_end - m->virt_begin));
Joe Thornber202bae52016-05-04 14:12:42 -04001151 if (r) {
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001152 metadata_operation_failed(pool, "dm_pool_dec_data_range", r);
Joe Thornber202bae52016-05-04 14:12:42 -04001153 bio_io_error(m->bio);
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001154 } else
1155 bio_endio(m->bio);
Joe Thornber202bae52016-05-04 14:12:42 -04001156
Joe Thornber34fbcf62015-04-16 12:58:35 +01001157 cell_defer_no_holder(tc, m->cell);
1158 mempool_free(m, pool->mapping_pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01001159}
1160
Joe Thornber104655f2012-03-28 18:41:28 +01001161static void process_prepared(struct pool *pool, struct list_head *head,
Joe Thornbere49e5822012-07-27 15:08:16 +01001162 process_mapping_fn *fn)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001163{
1164 unsigned long flags;
1165 struct list_head maps;
Mike Snitzera24c2562012-06-03 00:30:00 +01001166 struct dm_thin_new_mapping *m, *tmp;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001167
1168 INIT_LIST_HEAD(&maps);
1169 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +01001170 list_splice_init(head, &maps);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001171 spin_unlock_irqrestore(&pool->lock, flags);
1172
1173 list_for_each_entry_safe(m, tmp, &maps, list)
Joe Thornbere49e5822012-07-27 15:08:16 +01001174 (*fn)(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001175}
1176
1177/*
1178 * Deferred bio jobs.
1179 */
Joe Thornber104655f2012-03-28 18:41:28 +01001180static int io_overlaps_block(struct pool *pool, struct bio *bio)
1181{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001182 return bio->bi_iter.bi_size ==
1183 (pool->sectors_per_block << SECTOR_SHIFT);
Joe Thornber104655f2012-03-28 18:41:28 +01001184}
1185
Joe Thornber991d9fa2011-10-31 20:21:18 +00001186static int io_overwrites_block(struct pool *pool, struct bio *bio)
1187{
Joe Thornber104655f2012-03-28 18:41:28 +01001188 return (bio_data_dir(bio) == WRITE) &&
1189 io_overlaps_block(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001190}
1191
1192static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
1193 bio_end_io_t *fn)
1194{
1195 *save = bio->bi_end_io;
1196 bio->bi_end_io = fn;
1197}
1198
1199static int ensure_next_mapping(struct pool *pool)
1200{
1201 if (pool->next_mapping)
1202 return 0;
1203
1204 pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
1205
1206 return pool->next_mapping ? 0 : -ENOMEM;
1207}
1208
Mike Snitzera24c2562012-06-03 00:30:00 +01001209static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001210{
Mike Snitzer16961b02013-12-17 13:19:11 -05001211 struct dm_thin_new_mapping *m = pool->next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001212
1213 BUG_ON(!pool->next_mapping);
1214
Mike Snitzer16961b02013-12-17 13:19:11 -05001215 memset(m, 0, sizeof(struct dm_thin_new_mapping));
1216 INIT_LIST_HEAD(&m->list);
1217 m->bio = NULL;
1218
Joe Thornber991d9fa2011-10-31 20:21:18 +00001219 pool->next_mapping = NULL;
1220
Mike Snitzer16961b02013-12-17 13:19:11 -05001221 return m;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001222}
1223
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001224static void ll_zero(struct thin_c *tc, struct dm_thin_new_mapping *m,
1225 sector_t begin, sector_t end)
1226{
1227 int r;
1228 struct dm_io_region to;
1229
1230 to.bdev = tc->pool_dev->bdev;
1231 to.sector = begin;
1232 to.count = end - begin;
1233
1234 r = dm_kcopyd_zero(tc->pool->copier, 1, &to, 0, copy_complete, m);
1235 if (r < 0) {
1236 DMERR_LIMIT("dm_kcopyd_zero() failed");
1237 copy_complete(1, 1, m);
1238 }
1239}
1240
Mike Snitzer452d7a62014-10-09 19:20:21 -04001241static void remap_and_issue_overwrite(struct thin_c *tc, struct bio *bio,
Joe Thornber34fbcf62015-04-16 12:58:35 +01001242 dm_block_t data_begin,
Mike Snitzer452d7a62014-10-09 19:20:21 -04001243 struct dm_thin_new_mapping *m)
1244{
1245 struct pool *pool = tc->pool;
1246 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1247
1248 h->overwrite_mapping = m;
1249 m->bio = bio;
1250 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
1251 inc_all_io_entry(pool, bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001252 remap_and_issue(tc, bio, data_begin);
Mike Snitzer452d7a62014-10-09 19:20:21 -04001253}
1254
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001255/*
1256 * A partial copy also needs to zero the uncopied region.
1257 */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001258static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
Joe Thornber2dd9c252012-03-28 18:41:28 +01001259 struct dm_dev *origin, dm_block_t data_origin,
1260 dm_block_t data_dest,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001261 struct dm_bio_prison_cell *cell, struct bio *bio,
1262 sector_t len)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001263{
1264 int r;
1265 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +01001266 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001267
Joe Thornber991d9fa2011-10-31 20:21:18 +00001268 m->tc = tc;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001269 m->virt_begin = virt_block;
1270 m->virt_end = virt_block + 1u;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001271 m->data_block = data_dest;
1272 m->cell = cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001273
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001274 /*
1275 * quiesce action + copy action + an extra reference held for the
1276 * duration of this function (we may need to inc later for a
1277 * partial zero).
1278 */
1279 atomic_set(&m->prepare_actions, 3);
1280
Mike Snitzer44feb382012-10-12 21:02:10 +01001281 if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001282 complete_mapping_preparation(m); /* already quiesced */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001283
1284 /*
1285 * IO to pool_dev remaps to the pool target's data_dev.
1286 *
1287 * If the whole block of data is being overwritten, we can issue the
1288 * bio immediately. Otherwise we use kcopyd to clone the data first.
1289 */
Mike Snitzer452d7a62014-10-09 19:20:21 -04001290 if (io_overwrites_block(pool, bio))
1291 remap_and_issue_overwrite(tc, bio, data_dest, m);
1292 else {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001293 struct dm_io_region from, to;
1294
Joe Thornber2dd9c252012-03-28 18:41:28 +01001295 from.bdev = origin->bdev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001296 from.sector = data_origin * pool->sectors_per_block;
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001297 from.count = len;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001298
1299 to.bdev = tc->pool_dev->bdev;
1300 to.sector = data_dest * pool->sectors_per_block;
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001301 to.count = len;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001302
1303 r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
1304 0, copy_complete, m);
1305 if (r < 0) {
Mike Snitzerc3977412012-12-21 20:23:34 +00001306 DMERR_LIMIT("dm_kcopyd_copy() failed");
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001307 copy_complete(1, 1, m);
1308
1309 /*
1310 * We allow the zero to be issued, to simplify the
1311 * error path. Otherwise we'd need to start
1312 * worrying about decrementing the prepare_actions
1313 * counter.
1314 */
1315 }
1316
1317 /*
1318 * Do we need to zero a tail region?
1319 */
1320 if (len < pool->sectors_per_block && pool->pf.zero_new_blocks) {
1321 atomic_inc(&m->prepare_actions);
1322 ll_zero(tc, m,
1323 data_dest * pool->sectors_per_block + len,
1324 (data_dest + 1) * pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001325 }
1326 }
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001327
1328 complete_mapping_preparation(m); /* drop our ref */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001329}
1330
Joe Thornber2dd9c252012-03-28 18:41:28 +01001331static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
1332 dm_block_t data_origin, dm_block_t data_dest,
Mike Snitzera24c2562012-06-03 00:30:00 +01001333 struct dm_bio_prison_cell *cell, struct bio *bio)
Joe Thornber2dd9c252012-03-28 18:41:28 +01001334{
1335 schedule_copy(tc, virt_block, tc->pool_dev,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001336 data_origin, data_dest, cell, bio,
1337 tc->pool->sectors_per_block);
Joe Thornber2dd9c252012-03-28 18:41:28 +01001338}
1339
Joe Thornber991d9fa2011-10-31 20:21:18 +00001340static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
Mike Snitzera24c2562012-06-03 00:30:00 +01001341 dm_block_t data_block, struct dm_bio_prison_cell *cell,
Joe Thornber991d9fa2011-10-31 20:21:18 +00001342 struct bio *bio)
1343{
1344 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +01001345 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001346
Joe Thornber50f3c3e2014-06-13 13:57:09 +01001347 atomic_set(&m->prepare_actions, 1); /* no need to quiesce */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001348 m->tc = tc;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001349 m->virt_begin = virt_block;
1350 m->virt_end = virt_block + 1u;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001351 m->data_block = data_block;
1352 m->cell = cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001353
1354 /*
1355 * If the whole block of data is being overwritten or we are not
1356 * zeroing pre-existing data, we can issue the bio immediately.
1357 * Otherwise we use kcopyd to zero the data first.
1358 */
Mike Snitzerf8ae7522015-05-14 11:28:37 -04001359 if (pool->pf.zero_new_blocks) {
1360 if (io_overwrites_block(pool, bio))
1361 remap_and_issue_overwrite(tc, bio, data_block, m);
1362 else
1363 ll_zero(tc, m, data_block * pool->sectors_per_block,
1364 (data_block + 1) * pool->sectors_per_block);
1365 } else
Joe Thornber991d9fa2011-10-31 20:21:18 +00001366 process_prepared_mapping(m);
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001367}
Joe Thornber991d9fa2011-10-31 20:21:18 +00001368
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001369static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
1370 dm_block_t data_dest,
1371 struct dm_bio_prison_cell *cell, struct bio *bio)
1372{
1373 struct pool *pool = tc->pool;
1374 sector_t virt_block_begin = virt_block * pool->sectors_per_block;
1375 sector_t virt_block_end = (virt_block + 1) * pool->sectors_per_block;
1376
1377 if (virt_block_end <= tc->origin_size)
1378 schedule_copy(tc, virt_block, tc->origin_dev,
1379 virt_block, data_dest, cell, bio,
1380 pool->sectors_per_block);
1381
1382 else if (virt_block_begin < tc->origin_size)
1383 schedule_copy(tc, virt_block, tc->origin_dev,
1384 virt_block, data_dest, cell, bio,
1385 tc->origin_size - virt_block_begin);
1386
1387 else
1388 schedule_zero(tc, virt_block, data_dest, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001389}
1390
Joe Thornber2c43fd22014-12-11 11:12:19 +00001391static void set_pool_mode(struct pool *pool, enum pool_mode new_mode);
1392
Mike Snitzerf2bc5d12018-06-26 12:04:23 -04001393static void requeue_bios(struct pool *pool);
1394
Joe Thornbera9537db2018-09-10 16:50:09 +01001395static bool is_read_only_pool_mode(enum pool_mode mode)
1396{
1397 return (mode == PM_OUT_OF_METADATA_SPACE || mode == PM_READ_ONLY);
1398}
1399
1400static bool is_read_only(struct pool *pool)
1401{
1402 return is_read_only_pool_mode(get_pool_mode(pool));
1403}
1404
1405static void check_for_metadata_space(struct pool *pool)
1406{
1407 int r;
1408 const char *ooms_reason = NULL;
1409 dm_block_t nr_free;
1410
1411 r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free);
1412 if (r)
1413 ooms_reason = "Could not get free metadata blocks";
1414 else if (!nr_free)
1415 ooms_reason = "No free metadata blocks";
1416
1417 if (ooms_reason && !is_read_only(pool)) {
1418 DMERR("%s", ooms_reason);
1419 set_pool_mode(pool, PM_OUT_OF_METADATA_SPACE);
1420 }
1421}
1422
1423static void check_for_data_space(struct pool *pool)
Joe Thornber2c43fd22014-12-11 11:12:19 +00001424{
1425 int r;
1426 dm_block_t nr_free;
1427
1428 if (get_pool_mode(pool) != PM_OUT_OF_DATA_SPACE)
1429 return;
1430
1431 r = dm_pool_get_free_block_count(pool->pmd, &nr_free);
1432 if (r)
1433 return;
1434
Mike Snitzerf2bc5d12018-06-26 12:04:23 -04001435 if (nr_free) {
Joe Thornber2c43fd22014-12-11 11:12:19 +00001436 set_pool_mode(pool, PM_WRITE);
Mike Snitzerf2bc5d12018-06-26 12:04:23 -04001437 requeue_bios(pool);
1438 }
Joe Thornber2c43fd22014-12-11 11:12:19 +00001439}
1440
Joe Thornbere49e5822012-07-27 15:08:16 +01001441/*
1442 * A non-zero return indicates read_only or fail_io mode.
1443 * Many callers don't care about the return value.
1444 */
Joe Thornber020cc3b2013-12-04 15:05:36 -05001445static int commit(struct pool *pool)
Joe Thornbere49e5822012-07-27 15:08:16 +01001446{
1447 int r;
1448
Joe Thornbera9537db2018-09-10 16:50:09 +01001449 if (get_pool_mode(pool) >= PM_OUT_OF_METADATA_SPACE)
Joe Thornbere49e5822012-07-27 15:08:16 +01001450 return -EINVAL;
1451
Joe Thornber020cc3b2013-12-04 15:05:36 -05001452 r = dm_pool_commit_metadata(pool->pmd);
Joe Thornberb5330652013-12-04 19:51:33 -05001453 if (r)
1454 metadata_operation_failed(pool, "dm_pool_commit_metadata", r);
Joe Thornbera9537db2018-09-10 16:50:09 +01001455 else {
1456 check_for_metadata_space(pool);
1457 check_for_data_space(pool);
1458 }
Joe Thornbere49e5822012-07-27 15:08:16 +01001459
1460 return r;
1461}
1462
Joe Thornber88a66212013-12-04 20:16:12 -05001463static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks)
1464{
1465 unsigned long flags;
1466
1467 if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
1468 DMWARN("%s: reached low water mark for data device: sending event.",
1469 dm_device_name(pool->pool_md));
1470 spin_lock_irqsave(&pool->lock, flags);
1471 pool->low_water_triggered = true;
1472 spin_unlock_irqrestore(&pool->lock, flags);
1473 dm_table_event(pool->ti->table);
1474 }
1475}
1476
Joe Thornber991d9fa2011-10-31 20:21:18 +00001477static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
1478{
1479 int r;
1480 dm_block_t free_blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001481 struct pool *pool = tc->pool;
1482
Joe Thornber3e1a0692014-03-03 16:03:26 +00001483 if (WARN_ON(get_pool_mode(pool) != PM_WRITE))
Joe Thornber8d30abf2013-12-04 19:16:11 -05001484 return -EINVAL;
1485
Joe Thornber991d9fa2011-10-31 20:21:18 +00001486 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
Joe Thornberb5330652013-12-04 19:51:33 -05001487 if (r) {
1488 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001489 return r;
Joe Thornberb5330652013-12-04 19:51:33 -05001490 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001491
Joe Thornber88a66212013-12-04 20:16:12 -05001492 check_low_water_mark(pool, free_blocks);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001493
1494 if (!free_blocks) {
Mike Snitzer94563ba2013-08-22 09:56:18 -04001495 /*
1496 * Try to commit to see if that will free up some
1497 * more space.
1498 */
Joe Thornber020cc3b2013-12-04 15:05:36 -05001499 r = commit(pool);
1500 if (r)
1501 return r;
Mike Snitzer94563ba2013-08-22 09:56:18 -04001502
1503 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
Joe Thornberb5330652013-12-04 19:51:33 -05001504 if (r) {
1505 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
Mike Snitzer94563ba2013-08-22 09:56:18 -04001506 return r;
Joe Thornberb5330652013-12-04 19:51:33 -05001507 }
Mike Snitzer94563ba2013-08-22 09:56:18 -04001508
Mike Snitzer94563ba2013-08-22 09:56:18 -04001509 if (!free_blocks) {
Joe Thornber3e1a0692014-03-03 16:03:26 +00001510 set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001511 return -ENOSPC;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001512 }
1513 }
1514
1515 r = dm_pool_alloc_data_block(pool->pmd, result);
Mike Snitzer4a02b342013-12-03 12:20:57 -05001516 if (r) {
Mike Snitzerf2bc5d12018-06-26 12:04:23 -04001517 if (r == -ENOSPC)
1518 set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
1519 else
1520 metadata_operation_failed(pool, "dm_pool_alloc_data_block", r);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001521 return r;
Mike Snitzer4a02b342013-12-03 12:20:57 -05001522 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001523
Joe Thornbera9537db2018-09-10 16:50:09 +01001524 r = dm_pool_get_free_metadata_block_count(pool->pmd, &free_blocks);
1525 if (r) {
1526 metadata_operation_failed(pool, "dm_pool_get_free_metadata_block_count", r);
1527 return r;
1528 }
1529
1530 if (!free_blocks) {
1531 /* Let's commit before we use up the metadata reserve. */
1532 r = commit(pool);
1533 if (r)
1534 return r;
1535 }
1536
Joe Thornber991d9fa2011-10-31 20:21:18 +00001537 return 0;
1538}
1539
1540/*
1541 * If we have run out of space, queue bios until the device is
1542 * resumed, presumably after having been reloaded with more space.
1543 */
1544static void retry_on_resume(struct bio *bio)
1545{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001546 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01001547 struct thin_c *tc = h->tc;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001548 unsigned long flags;
1549
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001550 spin_lock_irqsave(&tc->lock, flags);
1551 bio_list_add(&tc->retry_on_resume_list, bio);
1552 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001553}
1554
Mike Snitzeraf918052014-05-22 14:32:51 -04001555static int should_error_unserviceable_bio(struct pool *pool)
Joe Thornber3e1a0692014-03-03 16:03:26 +00001556{
1557 enum pool_mode m = get_pool_mode(pool);
1558
1559 switch (m) {
1560 case PM_WRITE:
1561 /* Shouldn't get here */
1562 DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
Mike Snitzeraf918052014-05-22 14:32:51 -04001563 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001564
1565 case PM_OUT_OF_DATA_SPACE:
Mike Snitzeraf918052014-05-22 14:32:51 -04001566 return pool->pf.error_if_no_space ? -ENOSPC : 0;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001567
Joe Thornbera9537db2018-09-10 16:50:09 +01001568 case PM_OUT_OF_METADATA_SPACE:
Joe Thornber3e1a0692014-03-03 16:03:26 +00001569 case PM_READ_ONLY:
1570 case PM_FAIL:
Mike Snitzeraf918052014-05-22 14:32:51 -04001571 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001572 default:
1573 /* Shouldn't get here */
1574 DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
Mike Snitzeraf918052014-05-22 14:32:51 -04001575 return -EIO;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001576 }
1577}
1578
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001579static void handle_unserviceable_bio(struct pool *pool, struct bio *bio)
1580{
Mike Snitzeraf918052014-05-22 14:32:51 -04001581 int error = should_error_unserviceable_bio(pool);
1582
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001583 if (error) {
1584 bio->bi_error = error;
1585 bio_endio(bio);
1586 } else
Mike Snitzer6d162022013-12-20 18:09:02 -05001587 retry_on_resume(bio);
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001588}
1589
Mike Snitzer399cadd2013-12-05 16:03:33 -05001590static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001591{
1592 struct bio *bio;
1593 struct bio_list bios;
Mike Snitzeraf918052014-05-22 14:32:51 -04001594 int error;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001595
Mike Snitzeraf918052014-05-22 14:32:51 -04001596 error = should_error_unserviceable_bio(pool);
1597 if (error) {
1598 cell_error_with_code(pool, cell, error);
Joe Thornber3e1a0692014-03-03 16:03:26 +00001599 return;
1600 }
1601
Joe Thornber991d9fa2011-10-31 20:21:18 +00001602 bio_list_init(&bios);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001603 cell_release(pool, cell, &bios);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001604
Mike Snitzer9d094ee2014-10-19 08:23:09 -04001605 while ((bio = bio_list_pop(&bios)))
1606 retry_on_resume(bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001607}
1608
Joe Thornber34fbcf62015-04-16 12:58:35 +01001609static void process_discard_cell_no_passdown(struct thin_c *tc,
1610 struct dm_bio_prison_cell *virt_cell)
Joe Thornber104655f2012-03-28 18:41:28 +01001611{
Joe Thornber104655f2012-03-28 18:41:28 +01001612 struct pool *pool = tc->pool;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001613 struct dm_thin_new_mapping *m = get_next_mapping(pool);
1614
1615 /*
1616 * We don't need to lock the data blocks, since there's no
1617 * passdown. We only lock data blocks for allocation and breaking sharing.
1618 */
1619 m->tc = tc;
1620 m->virt_begin = virt_cell->key.block_begin;
1621 m->virt_end = virt_cell->key.block_end;
1622 m->cell = virt_cell;
1623 m->bio = virt_cell->holder;
1624
1625 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
1626 pool->process_prepared_discard(m);
1627}
1628
Joe Thornber34fbcf62015-04-16 12:58:35 +01001629static void break_up_discard_bio(struct thin_c *tc, dm_block_t begin, dm_block_t end,
1630 struct bio *bio)
1631{
1632 struct pool *pool = tc->pool;
1633
1634 int r;
1635 bool maybe_shared;
1636 struct dm_cell_key data_key;
1637 struct dm_bio_prison_cell *data_cell;
Mike Snitzera24c2562012-06-03 00:30:00 +01001638 struct dm_thin_new_mapping *m;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001639 dm_block_t virt_begin, virt_end, data_begin;
Joe Thornber104655f2012-03-28 18:41:28 +01001640
Joe Thornber34fbcf62015-04-16 12:58:35 +01001641 while (begin != end) {
1642 r = ensure_next_mapping(pool);
1643 if (r)
1644 /* we did our best */
1645 return;
Joe Thornber104655f2012-03-28 18:41:28 +01001646
Joe Thornber34fbcf62015-04-16 12:58:35 +01001647 r = dm_thin_find_mapped_range(tc->td, begin, end, &virt_begin, &virt_end,
1648 &data_begin, &maybe_shared);
1649 if (r)
1650 /*
1651 * Silently fail, letting any mappings we've
1652 * created complete.
1653 */
Joe Thornber104655f2012-03-28 18:41:28 +01001654 break;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001655
1656 build_key(tc->td, PHYSICAL, data_begin, data_begin + (virt_end - virt_begin), &data_key);
1657 if (bio_detain(tc->pool, &data_key, NULL, &data_cell)) {
1658 /* contention, we'll give up with this range */
1659 begin = virt_end;
1660 continue;
Joe Thornber104655f2012-03-28 18:41:28 +01001661 }
1662
Joe Thornber104655f2012-03-28 18:41:28 +01001663 /*
Joe Thornber34fbcf62015-04-16 12:58:35 +01001664 * IO may still be going to the destination block. We must
1665 * quiesce before we can do the removal.
Joe Thornber104655f2012-03-28 18:41:28 +01001666 */
Joe Thornber34fbcf62015-04-16 12:58:35 +01001667 m = get_next_mapping(pool);
1668 m->tc = tc;
1669 m->maybe_shared = maybe_shared;
1670 m->virt_begin = virt_begin;
1671 m->virt_end = virt_end;
1672 m->data_block = data_begin;
1673 m->cell = data_cell;
1674 m->bio = bio;
Joe Thornber104655f2012-03-28 18:41:28 +01001675
Joe Thornber34fbcf62015-04-16 12:58:35 +01001676 /*
1677 * The parent bio must not complete before sub discard bios are
Joe Thornber202bae52016-05-04 14:12:42 -04001678 * chained to it (see end_discard's bio_chain)!
Joe Thornber34fbcf62015-04-16 12:58:35 +01001679 *
1680 * This per-mapping bi_remaining increment is paired with
1681 * the implicit decrement that occurs via bio_endio() in
Joe Thornber202bae52016-05-04 14:12:42 -04001682 * end_discard().
Joe Thornber34fbcf62015-04-16 12:58:35 +01001683 */
Mike Snitzer13e4f8a2016-05-04 15:05:44 -04001684 bio_inc_remaining(bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001685 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
1686 pool->process_prepared_discard(m);
1687
1688 begin = virt_end;
Joe Thornber104655f2012-03-28 18:41:28 +01001689 }
1690}
1691
Joe Thornber34fbcf62015-04-16 12:58:35 +01001692static void process_discard_cell_passdown(struct thin_c *tc, struct dm_bio_prison_cell *virt_cell)
1693{
1694 struct bio *bio = virt_cell->holder;
1695 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1696
1697 /*
1698 * The virt_cell will only get freed once the origin bio completes.
1699 * This means it will remain locked while all the individual
1700 * passdown bios are in flight.
1701 */
1702 h->cell = virt_cell;
1703 break_up_discard_bio(tc, virt_cell->key.block_begin, virt_cell->key.block_end, bio);
1704
1705 /*
1706 * We complete the bio now, knowing that the bi_remaining field
1707 * will prevent completion until the sub range discards have
1708 * completed.
1709 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001710 bio_endio(bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001711}
1712
Joe Thornbera374bb22014-10-10 13:43:14 +01001713static void process_discard_bio(struct thin_c *tc, struct bio *bio)
1714{
Joe Thornber34fbcf62015-04-16 12:58:35 +01001715 dm_block_t begin, end;
1716 struct dm_cell_key virt_key;
1717 struct dm_bio_prison_cell *virt_cell;
Joe Thornbera374bb22014-10-10 13:43:14 +01001718
Joe Thornber34fbcf62015-04-16 12:58:35 +01001719 get_bio_block_range(tc, bio, &begin, &end);
1720 if (begin == end) {
1721 /*
1722 * The discard covers less than a block.
1723 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001724 bio_endio(bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001725 return;
1726 }
1727
1728 build_key(tc->td, VIRTUAL, begin, end, &virt_key);
1729 if (bio_detain(tc->pool, &virt_key, bio, &virt_cell))
1730 /*
1731 * Potential starvation issue: We're relying on the
1732 * fs/application being well behaved, and not trying to
1733 * send IO to a region at the same time as discarding it.
1734 * If they do this persistently then it's possible this
1735 * cell will never be granted.
1736 */
Joe Thornbera374bb22014-10-10 13:43:14 +01001737 return;
1738
Joe Thornber34fbcf62015-04-16 12:58:35 +01001739 tc->pool->process_discard_cell(tc, virt_cell);
Joe Thornbera374bb22014-10-10 13:43:14 +01001740}
1741
Joe Thornber991d9fa2011-10-31 20:21:18 +00001742static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzer44feb382012-10-12 21:02:10 +01001743 struct dm_cell_key *key,
Joe Thornber991d9fa2011-10-31 20:21:18 +00001744 struct dm_thin_lookup_result *lookup_result,
Mike Snitzera24c2562012-06-03 00:30:00 +01001745 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001746{
1747 int r;
1748 dm_block_t data_block;
Mike Snitzerd6fc2042013-08-21 17:40:11 -04001749 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001750
1751 r = alloc_data_block(tc, &data_block);
1752 switch (r) {
1753 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001754 schedule_internal_copy(tc, block, lookup_result->block,
1755 data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001756 break;
1757
1758 case -ENOSPC:
Mike Snitzer399cadd2013-12-05 16:03:33 -05001759 retry_bios_on_resume(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001760 break;
1761
1762 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001763 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1764 __func__, r);
Mike Snitzerd6fc2042013-08-21 17:40:11 -04001765 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001766 break;
1767 }
1768}
1769
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001770static void __remap_and_issue_shared_cell(void *context,
1771 struct dm_bio_prison_cell *cell)
1772{
1773 struct remap_info *info = context;
1774 struct bio *bio;
1775
1776 while ((bio = bio_list_pop(&cell->bios))) {
1777 if ((bio_data_dir(bio) == WRITE) ||
Jens Axboe1eff9d32016-08-05 15:35:16 -06001778 (bio->bi_opf & (REQ_PREFLUSH | REQ_FUA) ||
Mike Christiee6047142016-06-05 14:32:04 -05001779 bio_op(bio) == REQ_OP_DISCARD))
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001780 bio_list_add(&info->defer_bios, bio);
1781 else {
1782 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));;
1783
1784 h->shared_read_entry = dm_deferred_entry_inc(info->tc->pool->shared_read_ds);
1785 inc_all_io_entry(info->tc->pool, bio);
1786 bio_list_add(&info->issue_bios, bio);
1787 }
1788 }
1789}
1790
1791static void remap_and_issue_shared_cell(struct thin_c *tc,
1792 struct dm_bio_prison_cell *cell,
1793 dm_block_t block)
1794{
1795 struct bio *bio;
1796 struct remap_info info;
1797
1798 info.tc = tc;
1799 bio_list_init(&info.defer_bios);
1800 bio_list_init(&info.issue_bios);
1801
1802 cell_visit_release(tc->pool, __remap_and_issue_shared_cell,
1803 &info, cell);
1804
1805 while ((bio = bio_list_pop(&info.defer_bios)))
1806 thin_defer_bio(tc, bio);
1807
1808 while ((bio = bio_list_pop(&info.issue_bios)))
1809 remap_and_issue(tc, bio, block);
1810}
1811
Joe Thornber991d9fa2011-10-31 20:21:18 +00001812static void process_shared_bio(struct thin_c *tc, struct bio *bio,
1813 dm_block_t block,
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001814 struct dm_thin_lookup_result *lookup_result,
1815 struct dm_bio_prison_cell *virt_cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001816{
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001817 struct dm_bio_prison_cell *data_cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001818 struct pool *pool = tc->pool;
Mike Snitzer44feb382012-10-12 21:02:10 +01001819 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001820
1821 /*
1822 * If cell is already occupied, then sharing is already in the process
1823 * of being broken so we have nothing further to do here.
1824 */
1825 build_data_key(tc->td, lookup_result->block, &key);
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001826 if (bio_detain(pool, &key, bio, &data_cell)) {
1827 cell_defer_no_holder(tc, virt_cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001828 return;
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001829 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001830
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001831 if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size) {
1832 break_sharing(tc, bio, block, &key, lookup_result, data_cell);
1833 cell_defer_no_holder(tc, virt_cell);
1834 } else {
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001835 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornber991d9fa2011-10-31 20:21:18 +00001836
Mike Snitzer44feb382012-10-12 21:02:10 +01001837 h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
Joe Thornbere8088072012-12-21 20:23:31 +00001838 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001839 remap_and_issue(tc, bio, lookup_result->block);
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001840
1841 remap_and_issue_shared_cell(tc, data_cell, lookup_result->block);
1842 remap_and_issue_shared_cell(tc, virt_cell, lookup_result->block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001843 }
1844}
1845
1846static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzera24c2562012-06-03 00:30:00 +01001847 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001848{
1849 int r;
1850 dm_block_t data_block;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001851 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001852
1853 /*
1854 * Remap empty bios (flushes) immediately, without provisioning.
1855 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07001856 if (!bio->bi_iter.bi_size) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001857 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001858 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001859
Joe Thornber991d9fa2011-10-31 20:21:18 +00001860 remap_and_issue(tc, bio, 0);
1861 return;
1862 }
1863
1864 /*
1865 * Fill read bios with zeroes and complete them immediately.
1866 */
1867 if (bio_data_dir(bio) == READ) {
1868 zero_fill_bio(bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001869 cell_defer_no_holder(tc, cell);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001870 bio_endio(bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001871 return;
1872 }
1873
1874 r = alloc_data_block(tc, &data_block);
1875 switch (r) {
1876 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001877 if (tc->origin_dev)
1878 schedule_external_copy(tc, block, data_block, cell, bio);
1879 else
1880 schedule_zero(tc, block, data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001881 break;
1882
1883 case -ENOSPC:
Mike Snitzer399cadd2013-12-05 16:03:33 -05001884 retry_bios_on_resume(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001885 break;
1886
1887 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001888 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1889 __func__, r);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001890 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001891 break;
1892 }
1893}
1894
Joe Thornbera374bb22014-10-10 13:43:14 +01001895static void process_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001896{
1897 int r;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001898 struct pool *pool = tc->pool;
Joe Thornbera374bb22014-10-10 13:43:14 +01001899 struct bio *bio = cell->holder;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001900 dm_block_t block = get_bio_block(tc, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001901 struct dm_thin_lookup_result lookup_result;
1902
Joe Thornbera374bb22014-10-10 13:43:14 +01001903 if (tc->requeue_mode) {
1904 cell_requeue(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001905 return;
Joe Thornbera374bb22014-10-10 13:43:14 +01001906 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001907
1908 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1909 switch (r) {
1910 case 0:
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001911 if (lookup_result.shared)
1912 process_shared_bio(tc, bio, block, &lookup_result, cell);
1913 else {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001914 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001915 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbera374bb22014-10-10 13:43:14 +01001916 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001917 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001918 break;
1919
1920 case -ENODATA:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001921 if (bio_data_dir(bio) == READ && tc->origin_dev) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001922 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001923 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001924
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001925 if (bio_end_sector(bio) <= tc->origin_size)
1926 remap_to_origin_and_issue(tc, bio);
1927
1928 else if (bio->bi_iter.bi_sector < tc->origin_size) {
1929 zero_fill_bio(bio);
1930 bio->bi_iter.bi_size = (tc->origin_size - bio->bi_iter.bi_sector) << SECTOR_SHIFT;
1931 remap_to_origin_and_issue(tc, bio);
1932
1933 } else {
1934 zero_fill_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001935 bio_endio(bio);
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001936 }
Joe Thornber2dd9c252012-03-28 18:41:28 +01001937 } else
1938 provision_block(tc, bio, block, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001939 break;
1940
1941 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001942 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1943 __func__, r);
Joe Thornberf286ba02012-12-21 20:23:33 +00001944 cell_defer_no_holder(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001945 bio_io_error(bio);
1946 break;
1947 }
1948}
1949
Joe Thornbera374bb22014-10-10 13:43:14 +01001950static void process_bio(struct thin_c *tc, struct bio *bio)
1951{
1952 struct pool *pool = tc->pool;
1953 dm_block_t block = get_bio_block(tc, bio);
1954 struct dm_bio_prison_cell *cell;
1955 struct dm_cell_key key;
1956
1957 /*
1958 * If cell is already occupied, then the block is already
1959 * being provisioned so we have nothing further to do here.
1960 */
1961 build_virtual_key(tc->td, block, &key);
1962 if (bio_detain(pool, &key, bio, &cell))
1963 return;
1964
1965 process_cell(tc, cell);
1966}
1967
1968static void __process_bio_read_only(struct thin_c *tc, struct bio *bio,
1969 struct dm_bio_prison_cell *cell)
Joe Thornbere49e5822012-07-27 15:08:16 +01001970{
1971 int r;
1972 int rw = bio_data_dir(bio);
1973 dm_block_t block = get_bio_block(tc, bio);
1974 struct dm_thin_lookup_result lookup_result;
1975
1976 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1977 switch (r) {
1978 case 0:
Joe Thornbera374bb22014-10-10 13:43:14 +01001979 if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size) {
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001980 handle_unserviceable_bio(tc->pool, bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01001981 if (cell)
1982 cell_defer_no_holder(tc, cell);
1983 } else {
Joe Thornbere8088072012-12-21 20:23:31 +00001984 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001985 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbera374bb22014-10-10 13:43:14 +01001986 if (cell)
1987 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001988 }
Joe Thornbere49e5822012-07-27 15:08:16 +01001989 break;
1990
1991 case -ENODATA:
Joe Thornbera374bb22014-10-10 13:43:14 +01001992 if (cell)
1993 cell_defer_no_holder(tc, cell);
Joe Thornbere49e5822012-07-27 15:08:16 +01001994 if (rw != READ) {
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001995 handle_unserviceable_bio(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001996 break;
1997 }
1998
1999 if (tc->origin_dev) {
Joe Thornbere8088072012-12-21 20:23:31 +00002000 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01002001 remap_to_origin_and_issue(tc, bio);
2002 break;
2003 }
2004
2005 zero_fill_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002006 bio_endio(bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01002007 break;
2008
2009 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00002010 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
2011 __func__, r);
Joe Thornbera374bb22014-10-10 13:43:14 +01002012 if (cell)
2013 cell_defer_no_holder(tc, cell);
Joe Thornbere49e5822012-07-27 15:08:16 +01002014 bio_io_error(bio);
2015 break;
2016 }
2017}
2018
Joe Thornbera374bb22014-10-10 13:43:14 +01002019static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
2020{
2021 __process_bio_read_only(tc, bio, NULL);
2022}
2023
2024static void process_cell_read_only(struct thin_c *tc, struct dm_bio_prison_cell *cell)
2025{
2026 __process_bio_read_only(tc, cell->holder, cell);
2027}
2028
Joe Thornber3e1a0692014-03-03 16:03:26 +00002029static void process_bio_success(struct thin_c *tc, struct bio *bio)
2030{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002031 bio_endio(bio);
Joe Thornber3e1a0692014-03-03 16:03:26 +00002032}
2033
Joe Thornbere49e5822012-07-27 15:08:16 +01002034static void process_bio_fail(struct thin_c *tc, struct bio *bio)
2035{
2036 bio_io_error(bio);
2037}
2038
Joe Thornbera374bb22014-10-10 13:43:14 +01002039static void process_cell_success(struct thin_c *tc, struct dm_bio_prison_cell *cell)
2040{
2041 cell_success(tc->pool, cell);
2042}
2043
2044static void process_cell_fail(struct thin_c *tc, struct dm_bio_prison_cell *cell)
2045{
2046 cell_error(tc->pool, cell);
2047}
2048
Joe Thornberac8c3f32013-05-10 14:37:21 +01002049/*
2050 * FIXME: should we also commit due to size of transaction, measured in
2051 * metadata blocks?
2052 */
Joe Thornber905e51b2012-03-28 18:41:27 +01002053static int need_commit_due_to_time(struct pool *pool)
2054{
Manuel Schölling0f30af92014-05-22 22:42:37 +02002055 return !time_in_range(jiffies, pool->last_commit_jiffies,
2056 pool->last_commit_jiffies + COMMIT_PERIOD);
Joe Thornber905e51b2012-03-28 18:41:27 +01002057}
2058
Mike Snitzer67324ea2014-03-21 18:33:41 -04002059#define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node)
2060#define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook))
2061
2062static void __thin_bio_rb_add(struct thin_c *tc, struct bio *bio)
2063{
2064 struct rb_node **rbp, *parent;
2065 struct dm_thin_endio_hook *pbd;
2066 sector_t bi_sector = bio->bi_iter.bi_sector;
2067
2068 rbp = &tc->sort_bio_list.rb_node;
2069 parent = NULL;
2070 while (*rbp) {
2071 parent = *rbp;
2072 pbd = thin_pbd(parent);
2073
2074 if (bi_sector < thin_bio(pbd)->bi_iter.bi_sector)
2075 rbp = &(*rbp)->rb_left;
2076 else
2077 rbp = &(*rbp)->rb_right;
2078 }
2079
2080 pbd = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
2081 rb_link_node(&pbd->rb_node, parent, rbp);
2082 rb_insert_color(&pbd->rb_node, &tc->sort_bio_list);
2083}
2084
2085static void __extract_sorted_bios(struct thin_c *tc)
2086{
2087 struct rb_node *node;
2088 struct dm_thin_endio_hook *pbd;
2089 struct bio *bio;
2090
2091 for (node = rb_first(&tc->sort_bio_list); node; node = rb_next(node)) {
2092 pbd = thin_pbd(node);
2093 bio = thin_bio(pbd);
2094
2095 bio_list_add(&tc->deferred_bio_list, bio);
2096 rb_erase(&pbd->rb_node, &tc->sort_bio_list);
2097 }
2098
2099 WARN_ON(!RB_EMPTY_ROOT(&tc->sort_bio_list));
2100}
2101
2102static void __sort_thin_deferred_bios(struct thin_c *tc)
2103{
2104 struct bio *bio;
2105 struct bio_list bios;
2106
2107 bio_list_init(&bios);
2108 bio_list_merge(&bios, &tc->deferred_bio_list);
2109 bio_list_init(&tc->deferred_bio_list);
2110
2111 /* Sort deferred_bio_list using rb-tree */
2112 while ((bio = bio_list_pop(&bios)))
2113 __thin_bio_rb_add(tc, bio);
2114
2115 /*
2116 * Transfer the sorted bios in sort_bio_list back to
2117 * deferred_bio_list to allow lockless submission of
2118 * all bios.
2119 */
2120 __extract_sorted_bios(tc);
2121}
2122
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002123static void process_thin_deferred_bios(struct thin_c *tc)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002124{
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002125 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002126 unsigned long flags;
2127 struct bio *bio;
2128 struct bio_list bios;
Mike Snitzer67324ea2014-03-21 18:33:41 -04002129 struct blk_plug plug;
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002130 unsigned count = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002131
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002132 if (tc->requeue_mode) {
Mike Snitzer42d6a8c2014-10-19 07:52:44 -04002133 error_thin_bio_list(tc, &tc->deferred_bio_list, DM_ENDIO_REQUEUE);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002134 return;
2135 }
2136
Joe Thornber991d9fa2011-10-31 20:21:18 +00002137 bio_list_init(&bios);
2138
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002139 spin_lock_irqsave(&tc->lock, flags);
Mike Snitzer67324ea2014-03-21 18:33:41 -04002140
2141 if (bio_list_empty(&tc->deferred_bio_list)) {
2142 spin_unlock_irqrestore(&tc->lock, flags);
2143 return;
2144 }
2145
2146 __sort_thin_deferred_bios(tc);
2147
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002148 bio_list_merge(&bios, &tc->deferred_bio_list);
2149 bio_list_init(&tc->deferred_bio_list);
Mike Snitzer67324ea2014-03-21 18:33:41 -04002150
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002151 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002152
Mike Snitzer67324ea2014-03-21 18:33:41 -04002153 blk_start_plug(&plug);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002154 while ((bio = bio_list_pop(&bios))) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00002155 /*
2156 * If we've got no free new_mapping structs, and processing
2157 * this bio might require one, we pause until there are some
2158 * prepared mappings to process.
2159 */
2160 if (ensure_next_mapping(pool)) {
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002161 spin_lock_irqsave(&tc->lock, flags);
2162 bio_list_add(&tc->deferred_bio_list, bio);
2163 bio_list_merge(&tc->deferred_bio_list, &bios);
2164 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002165 break;
2166 }
Joe Thornber104655f2012-03-28 18:41:28 +01002167
Mike Christiee6047142016-06-05 14:32:04 -05002168 if (bio_op(bio) == REQ_OP_DISCARD)
Joe Thornbere49e5822012-07-27 15:08:16 +01002169 pool->process_discard(tc, bio);
Joe Thornber104655f2012-03-28 18:41:28 +01002170 else
Joe Thornbere49e5822012-07-27 15:08:16 +01002171 pool->process_bio(tc, bio);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002172
2173 if ((count++ & 127) == 0) {
Joe Thornber7d327fe2014-10-06 15:45:59 +01002174 throttle_work_update(&pool->throttle);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002175 dm_pool_issue_prefetches(pool->pmd);
2176 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002177 }
Mike Snitzer67324ea2014-03-21 18:33:41 -04002178 blk_finish_plug(&plug);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002179}
2180
Joe Thornberac4c3f32014-10-10 16:42:10 +01002181static int cmp_cells(const void *lhs, const void *rhs)
2182{
2183 struct dm_bio_prison_cell *lhs_cell = *((struct dm_bio_prison_cell **) lhs);
2184 struct dm_bio_prison_cell *rhs_cell = *((struct dm_bio_prison_cell **) rhs);
2185
2186 BUG_ON(!lhs_cell->holder);
2187 BUG_ON(!rhs_cell->holder);
2188
2189 if (lhs_cell->holder->bi_iter.bi_sector < rhs_cell->holder->bi_iter.bi_sector)
2190 return -1;
2191
2192 if (lhs_cell->holder->bi_iter.bi_sector > rhs_cell->holder->bi_iter.bi_sector)
2193 return 1;
2194
2195 return 0;
2196}
2197
2198static unsigned sort_cells(struct pool *pool, struct list_head *cells)
2199{
2200 unsigned count = 0;
2201 struct dm_bio_prison_cell *cell, *tmp;
2202
2203 list_for_each_entry_safe(cell, tmp, cells, user_list) {
2204 if (count >= CELL_SORT_ARRAY_SIZE)
2205 break;
2206
2207 pool->cell_sort_array[count++] = cell;
2208 list_del(&cell->user_list);
2209 }
2210
2211 sort(pool->cell_sort_array, count, sizeof(cell), cmp_cells, NULL);
2212
2213 return count;
2214}
2215
Joe Thornbera374bb22014-10-10 13:43:14 +01002216static void process_thin_deferred_cells(struct thin_c *tc)
2217{
2218 struct pool *pool = tc->pool;
2219 unsigned long flags;
2220 struct list_head cells;
Joe Thornberac4c3f32014-10-10 16:42:10 +01002221 struct dm_bio_prison_cell *cell;
2222 unsigned i, j, count;
Joe Thornbera374bb22014-10-10 13:43:14 +01002223
2224 INIT_LIST_HEAD(&cells);
2225
2226 spin_lock_irqsave(&tc->lock, flags);
2227 list_splice_init(&tc->deferred_cells, &cells);
2228 spin_unlock_irqrestore(&tc->lock, flags);
2229
2230 if (list_empty(&cells))
2231 return;
2232
Joe Thornberac4c3f32014-10-10 16:42:10 +01002233 do {
2234 count = sort_cells(tc->pool, &cells);
Joe Thornbera374bb22014-10-10 13:43:14 +01002235
Joe Thornberac4c3f32014-10-10 16:42:10 +01002236 for (i = 0; i < count; i++) {
2237 cell = pool->cell_sort_array[i];
2238 BUG_ON(!cell->holder);
2239
2240 /*
2241 * If we've got no free new_mapping structs, and processing
2242 * this bio might require one, we pause until there are some
2243 * prepared mappings to process.
2244 */
2245 if (ensure_next_mapping(pool)) {
2246 for (j = i; j < count; j++)
2247 list_add(&pool->cell_sort_array[j]->user_list, &cells);
2248
2249 spin_lock_irqsave(&tc->lock, flags);
2250 list_splice(&cells, &tc->deferred_cells);
2251 spin_unlock_irqrestore(&tc->lock, flags);
2252 return;
2253 }
2254
Mike Christiee6047142016-06-05 14:32:04 -05002255 if (bio_op(cell->holder) == REQ_OP_DISCARD)
Joe Thornberac4c3f32014-10-10 16:42:10 +01002256 pool->process_discard_cell(tc, cell);
2257 else
2258 pool->process_cell(tc, cell);
Joe Thornbera374bb22014-10-10 13:43:14 +01002259 }
Joe Thornberac4c3f32014-10-10 16:42:10 +01002260 } while (!list_empty(&cells));
Joe Thornbera374bb22014-10-10 13:43:14 +01002261}
2262
Joe Thornberb10ebd32014-04-08 11:29:01 +01002263static void thin_get(struct thin_c *tc);
2264static void thin_put(struct thin_c *tc);
2265
2266/*
2267 * We can't hold rcu_read_lock() around code that can block. So we
2268 * find a thin with the rcu lock held; bump a refcount; then drop
2269 * the lock.
2270 */
2271static struct thin_c *get_first_thin(struct pool *pool)
2272{
2273 struct thin_c *tc = NULL;
2274
2275 rcu_read_lock();
2276 if (!list_empty(&pool->active_thins)) {
2277 tc = list_entry_rcu(pool->active_thins.next, struct thin_c, list);
2278 thin_get(tc);
2279 }
2280 rcu_read_unlock();
2281
2282 return tc;
2283}
2284
2285static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc)
2286{
2287 struct thin_c *old_tc = tc;
2288
2289 rcu_read_lock();
2290 list_for_each_entry_continue_rcu(tc, &pool->active_thins, list) {
2291 thin_get(tc);
2292 thin_put(old_tc);
2293 rcu_read_unlock();
2294 return tc;
2295 }
2296 thin_put(old_tc);
2297 rcu_read_unlock();
2298
2299 return NULL;
2300}
2301
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002302static void process_deferred_bios(struct pool *pool)
2303{
2304 unsigned long flags;
2305 struct bio *bio;
2306 struct bio_list bios;
2307 struct thin_c *tc;
2308
Joe Thornberb10ebd32014-04-08 11:29:01 +01002309 tc = get_first_thin(pool);
2310 while (tc) {
Joe Thornbera374bb22014-10-10 13:43:14 +01002311 process_thin_deferred_cells(tc);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002312 process_thin_deferred_bios(tc);
Joe Thornberb10ebd32014-04-08 11:29:01 +01002313 tc = get_next_thin(pool, tc);
2314 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002315
2316 /*
2317 * If there are any deferred flush bios, we must commit
2318 * the metadata before issuing them.
2319 */
2320 bio_list_init(&bios);
2321 spin_lock_irqsave(&pool->lock, flags);
2322 bio_list_merge(&bios, &pool->deferred_flush_bios);
2323 bio_list_init(&pool->deferred_flush_bios);
2324 spin_unlock_irqrestore(&pool->lock, flags);
2325
Mike Snitzer4d1662a2014-02-06 06:08:56 -05002326 if (bio_list_empty(&bios) &&
2327 !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool)))
Joe Thornber991d9fa2011-10-31 20:21:18 +00002328 return;
2329
Joe Thornber020cc3b2013-12-04 15:05:36 -05002330 if (commit(pool)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00002331 while ((bio = bio_list_pop(&bios)))
2332 bio_io_error(bio);
2333 return;
2334 }
Joe Thornber905e51b2012-03-28 18:41:27 +01002335 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002336
2337 while ((bio = bio_list_pop(&bios)))
2338 generic_make_request(bio);
2339}
2340
2341static void do_worker(struct work_struct *ws)
2342{
2343 struct pool *pool = container_of(ws, struct pool, worker);
2344
Joe Thornber7d327fe2014-10-06 15:45:59 +01002345 throttle_work_start(&pool->throttle);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002346 dm_pool_issue_prefetches(pool->pmd);
Joe Thornber7d327fe2014-10-06 15:45:59 +01002347 throttle_work_update(&pool->throttle);
Joe Thornbere49e5822012-07-27 15:08:16 +01002348 process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
Joe Thornber7d327fe2014-10-06 15:45:59 +01002349 throttle_work_update(&pool->throttle);
Joe Thornbere49e5822012-07-27 15:08:16 +01002350 process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
Joe Thornber7d327fe2014-10-06 15:45:59 +01002351 throttle_work_update(&pool->throttle);
Joe Thornber2a0fbff2016-07-01 14:00:02 +01002352 process_prepared(pool, &pool->prepared_discards_pt2, &pool->process_prepared_discard_pt2);
2353 throttle_work_update(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002354 process_deferred_bios(pool);
Joe Thornber7d327fe2014-10-06 15:45:59 +01002355 throttle_work_complete(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002356}
2357
Joe Thornber905e51b2012-03-28 18:41:27 +01002358/*
2359 * We want to commit periodically so that not too much
2360 * unwritten data builds up.
2361 */
2362static void do_waker(struct work_struct *ws)
2363{
2364 struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
2365 wake_worker(pool);
2366 queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
2367}
2368
Mike Snitzerbcc696f2015-07-15 16:52:04 -04002369static void notify_of_pool_mode_change_to_oods(struct pool *pool);
2370
Joe Thornber85ad643b2014-05-09 15:59:38 +01002371/*
2372 * We're holding onto IO to allow userland time to react. After the
2373 * timeout either the pool will have been resized (and thus back in
Mike Snitzerbcc696f2015-07-15 16:52:04 -04002374 * PM_WRITE mode), or we degrade to PM_OUT_OF_DATA_SPACE w/ error_if_no_space.
Joe Thornber85ad643b2014-05-09 15:59:38 +01002375 */
2376static void do_no_space_timeout(struct work_struct *ws)
2377{
2378 struct pool *pool = container_of(to_delayed_work(ws), struct pool,
2379 no_space_timeout);
2380
Mike Snitzerbcc696f2015-07-15 16:52:04 -04002381 if (get_pool_mode(pool) == PM_OUT_OF_DATA_SPACE && !pool->pf.error_if_no_space) {
2382 pool->pf.error_if_no_space = true;
2383 notify_of_pool_mode_change_to_oods(pool);
Mike Snitzer0a927c22015-07-21 13:20:46 -04002384 error_retry_list_with_code(pool, -ENOSPC);
Mike Snitzerbcc696f2015-07-15 16:52:04 -04002385 }
Joe Thornber85ad643b2014-05-09 15:59:38 +01002386}
2387
Joe Thornber991d9fa2011-10-31 20:21:18 +00002388/*----------------------------------------------------------------*/
2389
Joe Thornbere7a3e872014-05-13 16:14:14 -04002390struct pool_work {
Joe Thornber738211f2014-03-03 15:52:28 +00002391 struct work_struct worker;
Joe Thornbere7a3e872014-05-13 16:14:14 -04002392 struct completion complete;
Joe Thornber738211f2014-03-03 15:52:28 +00002393};
2394
Joe Thornbere7a3e872014-05-13 16:14:14 -04002395static struct pool_work *to_pool_work(struct work_struct *ws)
Joe Thornber738211f2014-03-03 15:52:28 +00002396{
Joe Thornbere7a3e872014-05-13 16:14:14 -04002397 return container_of(ws, struct pool_work, worker);
2398}
2399
2400static void pool_work_complete(struct pool_work *pw)
2401{
2402 complete(&pw->complete);
2403}
2404
2405static void pool_work_wait(struct pool_work *pw, struct pool *pool,
2406 void (*fn)(struct work_struct *))
2407{
2408 INIT_WORK_ONSTACK(&pw->worker, fn);
2409 init_completion(&pw->complete);
2410 queue_work(pool->wq, &pw->worker);
2411 wait_for_completion(&pw->complete);
2412}
2413
2414/*----------------------------------------------------------------*/
2415
2416struct noflush_work {
2417 struct pool_work pw;
2418 struct thin_c *tc;
2419};
2420
2421static struct noflush_work *to_noflush(struct work_struct *ws)
2422{
2423 return container_of(to_pool_work(ws), struct noflush_work, pw);
Joe Thornber738211f2014-03-03 15:52:28 +00002424}
2425
2426static void do_noflush_start(struct work_struct *ws)
2427{
Joe Thornbere7a3e872014-05-13 16:14:14 -04002428 struct noflush_work *w = to_noflush(ws);
Joe Thornber738211f2014-03-03 15:52:28 +00002429 w->tc->requeue_mode = true;
2430 requeue_io(w->tc);
Joe Thornbere7a3e872014-05-13 16:14:14 -04002431 pool_work_complete(&w->pw);
Joe Thornber738211f2014-03-03 15:52:28 +00002432}
2433
2434static void do_noflush_stop(struct work_struct *ws)
2435{
Joe Thornbere7a3e872014-05-13 16:14:14 -04002436 struct noflush_work *w = to_noflush(ws);
Joe Thornber738211f2014-03-03 15:52:28 +00002437 w->tc->requeue_mode = false;
Joe Thornbere7a3e872014-05-13 16:14:14 -04002438 pool_work_complete(&w->pw);
Joe Thornber738211f2014-03-03 15:52:28 +00002439}
2440
2441static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *))
2442{
2443 struct noflush_work w;
2444
Joe Thornber738211f2014-03-03 15:52:28 +00002445 w.tc = tc;
Joe Thornbere7a3e872014-05-13 16:14:14 -04002446 pool_work_wait(&w.pw, tc->pool, fn);
Joe Thornber738211f2014-03-03 15:52:28 +00002447}
2448
2449/*----------------------------------------------------------------*/
2450
Joe Thornbere49e5822012-07-27 15:08:16 +01002451static enum pool_mode get_pool_mode(struct pool *pool)
2452{
2453 return pool->pf.mode;
2454}
2455
Joe Thornber3e1a0692014-03-03 16:03:26 +00002456static void notify_of_pool_mode_change(struct pool *pool, const char *new_mode)
2457{
2458 dm_table_event(pool->ti->table);
2459 DMINFO("%s: switching pool to %s mode",
2460 dm_device_name(pool->pool_md), new_mode);
2461}
2462
Mike Snitzerbcc696f2015-07-15 16:52:04 -04002463static void notify_of_pool_mode_change_to_oods(struct pool *pool)
2464{
2465 if (!pool->pf.error_if_no_space)
2466 notify_of_pool_mode_change(pool, "out-of-data-space (queue IO)");
2467 else
2468 notify_of_pool_mode_change(pool, "out-of-data-space (error IO)");
2469}
2470
Joe Thornber34fbcf62015-04-16 12:58:35 +01002471static bool passdown_enabled(struct pool_c *pt)
2472{
2473 return pt->adjusted_pf.discard_passdown;
2474}
2475
2476static void set_discard_callbacks(struct pool *pool)
2477{
2478 struct pool_c *pt = pool->ti->private;
2479
2480 if (passdown_enabled(pt)) {
2481 pool->process_discard_cell = process_discard_cell_passdown;
Joe Thornber2a0fbff2016-07-01 14:00:02 +01002482 pool->process_prepared_discard = process_prepared_discard_passdown_pt1;
2483 pool->process_prepared_discard_pt2 = process_prepared_discard_passdown_pt2;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002484 } else {
2485 pool->process_discard_cell = process_discard_cell_no_passdown;
2486 pool->process_prepared_discard = process_prepared_discard_no_passdown;
2487 }
2488}
2489
Mike Snitzer8b64e882013-12-20 14:27:28 -05002490static void set_pool_mode(struct pool *pool, enum pool_mode new_mode)
Joe Thornbere49e5822012-07-27 15:08:16 +01002491{
Mike Snitzercdc2b412014-02-14 18:10:55 -05002492 struct pool_c *pt = pool->ti->private;
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002493 bool needs_check = dm_pool_metadata_needs_check(pool->pmd);
2494 enum pool_mode old_mode = get_pool_mode(pool);
Mike Snitzer80c57892014-05-20 13:38:33 -04002495 unsigned long no_space_timeout = ACCESS_ONCE(no_space_timeout_secs) * HZ;
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002496
2497 /*
2498 * Never allow the pool to transition to PM_WRITE mode if user
2499 * intervention is required to verify metadata and data consistency.
2500 */
2501 if (new_mode == PM_WRITE && needs_check) {
2502 DMERR("%s: unable to switch pool to write mode until repaired.",
2503 dm_device_name(pool->pool_md));
2504 if (old_mode != new_mode)
2505 new_mode = old_mode;
2506 else
2507 new_mode = PM_READ_ONLY;
2508 }
2509 /*
2510 * If we were in PM_FAIL mode, rollback of metadata failed. We're
2511 * not going to recover without a thin_repair. So we never let the
2512 * pool move out of the old mode.
2513 */
2514 if (old_mode == PM_FAIL)
2515 new_mode = old_mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002516
Mike Snitzer8b64e882013-12-20 14:27:28 -05002517 switch (new_mode) {
Joe Thornbere49e5822012-07-27 15:08:16 +01002518 case PM_FAIL:
Mike Snitzer8b64e882013-12-20 14:27:28 -05002519 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00002520 notify_of_pool_mode_change(pool, "failure");
Joe Thornber5383ef32013-12-04 16:30:01 -05002521 dm_pool_metadata_read_only(pool->pmd);
Joe Thornbere49e5822012-07-27 15:08:16 +01002522 pool->process_bio = process_bio_fail;
2523 pool->process_discard = process_bio_fail;
Joe Thornbera374bb22014-10-10 13:43:14 +01002524 pool->process_cell = process_cell_fail;
2525 pool->process_discard_cell = process_cell_fail;
Joe Thornbere49e5822012-07-27 15:08:16 +01002526 pool->process_prepared_mapping = process_prepared_mapping_fail;
2527 pool->process_prepared_discard = process_prepared_discard_fail;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002528
2529 error_retry_list(pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01002530 break;
2531
Joe Thornbera9537db2018-09-10 16:50:09 +01002532 case PM_OUT_OF_METADATA_SPACE:
Joe Thornbere49e5822012-07-27 15:08:16 +01002533 case PM_READ_ONLY:
Joe Thornbera9537db2018-09-10 16:50:09 +01002534 if (!is_read_only_pool_mode(old_mode))
Joe Thornber3e1a0692014-03-03 16:03:26 +00002535 notify_of_pool_mode_change(pool, "read-only");
2536 dm_pool_metadata_read_only(pool->pmd);
2537 pool->process_bio = process_bio_read_only;
2538 pool->process_discard = process_bio_success;
Joe Thornbera374bb22014-10-10 13:43:14 +01002539 pool->process_cell = process_cell_read_only;
2540 pool->process_discard_cell = process_cell_success;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002541 pool->process_prepared_mapping = process_prepared_mapping_fail;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002542 pool->process_prepared_discard = process_prepared_discard_success;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002543
2544 error_retry_list(pool);
2545 break;
2546
2547 case PM_OUT_OF_DATA_SPACE:
2548 /*
2549 * Ideally we'd never hit this state; the low water mark
2550 * would trigger userland to extend the pool before we
2551 * completely run out of data space. However, many small
2552 * IOs to unprovisioned space can consume data space at an
2553 * alarming rate. Adjust your low water mark if you're
2554 * frequently seeing this mode.
2555 */
2556 if (old_mode != new_mode)
Mike Snitzerbcc696f2015-07-15 16:52:04 -04002557 notify_of_pool_mode_change_to_oods(pool);
Mike Snitzerc3667cc2016-03-10 11:31:35 -05002558 pool->out_of_data_space = true;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002559 pool->process_bio = process_bio_read_only;
Joe Thornbera374bb22014-10-10 13:43:14 +01002560 pool->process_discard = process_discard_bio;
2561 pool->process_cell = process_cell_read_only;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002562 pool->process_prepared_mapping = process_prepared_mapping;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002563 set_discard_callbacks(pool);
Joe Thornber85ad643b2014-05-09 15:59:38 +01002564
Mike Snitzer80c57892014-05-20 13:38:33 -04002565 if (!pool->pf.error_if_no_space && no_space_timeout)
2566 queue_delayed_work(pool->wq, &pool->no_space_timeout, no_space_timeout);
Joe Thornbere49e5822012-07-27 15:08:16 +01002567 break;
2568
2569 case PM_WRITE:
Mike Snitzer8b64e882013-12-20 14:27:28 -05002570 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00002571 notify_of_pool_mode_change(pool, "write");
Hou Tao9a3f8fd52018-08-02 16:18:24 +08002572 if (old_mode == PM_OUT_OF_DATA_SPACE)
2573 cancel_delayed_work_sync(&pool->no_space_timeout);
Mike Snitzerc3667cc2016-03-10 11:31:35 -05002574 pool->out_of_data_space = false;
Mike Snitzer172c2382015-11-06 10:53:01 -05002575 pool->pf.error_if_no_space = pt->requested_pf.error_if_no_space;
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002576 dm_pool_metadata_read_write(pool->pmd);
Joe Thornbere49e5822012-07-27 15:08:16 +01002577 pool->process_bio = process_bio;
Joe Thornbera374bb22014-10-10 13:43:14 +01002578 pool->process_discard = process_discard_bio;
2579 pool->process_cell = process_cell;
Joe Thornbere49e5822012-07-27 15:08:16 +01002580 pool->process_prepared_mapping = process_prepared_mapping;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002581 set_discard_callbacks(pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01002582 break;
2583 }
Mike Snitzer8b64e882013-12-20 14:27:28 -05002584
2585 pool->pf.mode = new_mode;
Mike Snitzercdc2b412014-02-14 18:10:55 -05002586 /*
2587 * The pool mode may have changed, sync it so bind_control_target()
2588 * doesn't cause an unexpected mode transition on resume.
2589 */
2590 pt->adjusted_pf.mode = new_mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002591}
2592
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002593static void abort_transaction(struct pool *pool)
2594{
2595 const char *dev_name = dm_device_name(pool->pool_md);
2596
2597 DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
2598 if (dm_pool_abort_metadata(pool->pmd)) {
2599 DMERR("%s: failed to abort metadata transaction", dev_name);
2600 set_pool_mode(pool, PM_FAIL);
2601 }
2602
2603 if (dm_pool_metadata_set_needs_check(pool->pmd)) {
2604 DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
2605 set_pool_mode(pool, PM_FAIL);
2606 }
2607}
2608
Joe Thornberb5330652013-12-04 19:51:33 -05002609static void metadata_operation_failed(struct pool *pool, const char *op, int r)
2610{
2611 DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
2612 dm_device_name(pool->pool_md), op, r);
2613
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002614 abort_transaction(pool);
Joe Thornberb5330652013-12-04 19:51:33 -05002615 set_pool_mode(pool, PM_READ_ONLY);
2616}
2617
Joe Thornbere49e5822012-07-27 15:08:16 +01002618/*----------------------------------------------------------------*/
2619
Joe Thornber991d9fa2011-10-31 20:21:18 +00002620/*
2621 * Mapping functions.
2622 */
2623
2624/*
2625 * Called only while mapping a thin bio to hand it over to the workqueue.
2626 */
2627static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
2628{
2629 unsigned long flags;
2630 struct pool *pool = tc->pool;
2631
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002632 spin_lock_irqsave(&tc->lock, flags);
2633 bio_list_add(&tc->deferred_bio_list, bio);
2634 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002635
2636 wake_worker(pool);
2637}
2638
Joe Thornber7d327fe2014-10-06 15:45:59 +01002639static void thin_defer_bio_with_throttle(struct thin_c *tc, struct bio *bio)
2640{
2641 struct pool *pool = tc->pool;
2642
2643 throttle_lock(&pool->throttle);
2644 thin_defer_bio(tc, bio);
2645 throttle_unlock(&pool->throttle);
2646}
2647
Joe Thornbera374bb22014-10-10 13:43:14 +01002648static void thin_defer_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
2649{
2650 unsigned long flags;
2651 struct pool *pool = tc->pool;
2652
2653 throttle_lock(&pool->throttle);
2654 spin_lock_irqsave(&tc->lock, flags);
2655 list_add_tail(&cell->user_list, &tc->deferred_cells);
2656 spin_unlock_irqrestore(&tc->lock, flags);
2657 throttle_unlock(&pool->throttle);
2658
2659 wake_worker(pool);
2660}
2661
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002662static void thin_hook_bio(struct thin_c *tc, struct bio *bio)
Joe Thornbereb2aa482012-03-28 18:41:28 +01002663{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002664 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01002665
2666 h->tc = tc;
2667 h->shared_read_entry = NULL;
Joe Thornbere8088072012-12-21 20:23:31 +00002668 h->all_io_entry = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01002669 h->overwrite_mapping = NULL;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002670 h->cell = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01002671}
2672
Joe Thornber991d9fa2011-10-31 20:21:18 +00002673/*
2674 * Non-blocking function called from the thin target's map function.
2675 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002676static int thin_bio_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002677{
2678 int r;
2679 struct thin_c *tc = ti->private;
2680 dm_block_t block = get_bio_block(tc, bio);
2681 struct dm_thin_device *td = tc->td;
2682 struct dm_thin_lookup_result result;
Joe Thornbera374bb22014-10-10 13:43:14 +01002683 struct dm_bio_prison_cell *virt_cell, *data_cell;
Joe Thornbere8088072012-12-21 20:23:31 +00002684 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002685
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002686 thin_hook_bio(tc, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01002687
Joe Thornber738211f2014-03-03 15:52:28 +00002688 if (tc->requeue_mode) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002689 bio->bi_error = DM_ENDIO_REQUEUE;
2690 bio_endio(bio);
Joe Thornber738211f2014-03-03 15:52:28 +00002691 return DM_MAPIO_SUBMITTED;
2692 }
2693
Joe Thornbere49e5822012-07-27 15:08:16 +01002694 if (get_pool_mode(tc->pool) == PM_FAIL) {
2695 bio_io_error(bio);
2696 return DM_MAPIO_SUBMITTED;
2697 }
2698
Jens Axboe1eff9d32016-08-05 15:35:16 -06002699 if (bio->bi_opf & (REQ_PREFLUSH | REQ_FUA) ||
Mike Christiee6047142016-06-05 14:32:04 -05002700 bio_op(bio) == REQ_OP_DISCARD) {
Joe Thornber7d327fe2014-10-06 15:45:59 +01002701 thin_defer_bio_with_throttle(tc, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002702 return DM_MAPIO_SUBMITTED;
2703 }
2704
Joe Thornberc822ed92014-10-10 09:41:09 +01002705 /*
2706 * We must hold the virtual cell before doing the lookup, otherwise
2707 * there's a race with discard.
2708 */
2709 build_virtual_key(tc->td, block, &key);
Joe Thornbera374bb22014-10-10 13:43:14 +01002710 if (bio_detain(tc->pool, &key, bio, &virt_cell))
Joe Thornberc822ed92014-10-10 09:41:09 +01002711 return DM_MAPIO_SUBMITTED;
2712
Joe Thornber991d9fa2011-10-31 20:21:18 +00002713 r = dm_thin_find_block(td, block, 0, &result);
2714
2715 /*
2716 * Note that we defer readahead too.
2717 */
2718 switch (r) {
2719 case 0:
2720 if (unlikely(result.shared)) {
2721 /*
2722 * We have a race condition here between the
2723 * result.shared value returned by the lookup and
2724 * snapshot creation, which may cause new
2725 * sharing.
2726 *
2727 * To avoid this always quiesce the origin before
2728 * taking the snap. You want to do this anyway to
2729 * ensure a consistent application view
2730 * (i.e. lockfs).
2731 *
2732 * More distant ancestors are irrelevant. The
2733 * shared flag will be set in their case.
2734 */
Joe Thornbera374bb22014-10-10 13:43:14 +01002735 thin_defer_cell(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002736 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002737 }
Joe Thornbere8088072012-12-21 20:23:31 +00002738
Joe Thornbere8088072012-12-21 20:23:31 +00002739 build_data_key(tc->td, result.block, &key);
Joe Thornbera374bb22014-10-10 13:43:14 +01002740 if (bio_detain(tc->pool, &key, bio, &data_cell)) {
2741 cell_defer_no_holder(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002742 return DM_MAPIO_SUBMITTED;
2743 }
2744
2745 inc_all_io_entry(tc->pool, bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01002746 cell_defer_no_holder(tc, data_cell);
2747 cell_defer_no_holder(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002748
2749 remap(tc, bio, result.block);
2750 return DM_MAPIO_REMAPPED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002751
2752 case -ENODATA:
Joe Thornbere49e5822012-07-27 15:08:16 +01002753 case -EWOULDBLOCK:
Joe Thornbera374bb22014-10-10 13:43:14 +01002754 thin_defer_cell(tc, virt_cell);
Joe Thornber2aab3852012-12-21 20:23:33 +00002755 return DM_MAPIO_SUBMITTED;
Joe Thornbere49e5822012-07-27 15:08:16 +01002756
2757 default:
2758 /*
2759 * Must always call bio_io_error on failure.
2760 * dm_thin_find_block can fail with -EINVAL if the
2761 * pool is switched to fail-io mode.
2762 */
2763 bio_io_error(bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01002764 cell_defer_no_holder(tc, virt_cell);
Joe Thornber2aab3852012-12-21 20:23:33 +00002765 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002766 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002767}
2768
2769static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
2770{
Joe Thornber991d9fa2011-10-31 20:21:18 +00002771 struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
Mike Snitzer760fe672014-03-20 08:36:47 -04002772 struct request_queue *q;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002773
Mike Snitzer760fe672014-03-20 08:36:47 -04002774 if (get_pool_mode(pt->pool) == PM_OUT_OF_DATA_SPACE)
2775 return 1;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002776
Mike Snitzer760fe672014-03-20 08:36:47 -04002777 q = bdev_get_queue(pt->data_dev->bdev);
2778 return bdi_congested(&q->backing_dev_info, bdi_bits);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002779}
2780
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002781static void requeue_bios(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002782{
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002783 unsigned long flags;
2784 struct thin_c *tc;
2785
2786 rcu_read_lock();
2787 list_for_each_entry_rcu(tc, &pool->active_thins, list) {
2788 spin_lock_irqsave(&tc->lock, flags);
2789 bio_list_merge(&tc->deferred_bio_list, &tc->retry_on_resume_list);
2790 bio_list_init(&tc->retry_on_resume_list);
2791 spin_unlock_irqrestore(&tc->lock, flags);
2792 }
2793 rcu_read_unlock();
Joe Thornber991d9fa2011-10-31 20:21:18 +00002794}
2795
2796/*----------------------------------------------------------------
2797 * Binding of control targets to a pool object
2798 *--------------------------------------------------------------*/
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002799static bool data_dev_supports_discard(struct pool_c *pt)
2800{
2801 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
2802
2803 return q && blk_queue_discard(q);
2804}
2805
Joe Thornber58051b92013-03-20 17:21:25 +00002806static bool is_factor(sector_t block_size, uint32_t n)
2807{
2808 return !sector_div(block_size, n);
2809}
2810
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002811/*
2812 * If discard_passdown was enabled verify that the data device
Mike Snitzer0424caa2012-09-26 23:45:47 +01002813 * supports discards. Disable discard_passdown if not.
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002814 */
Mike Snitzer0424caa2012-09-26 23:45:47 +01002815static void disable_passdown_if_not_supported(struct pool_c *pt)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002816{
Mike Snitzer0424caa2012-09-26 23:45:47 +01002817 struct pool *pool = pt->pool;
2818 struct block_device *data_bdev = pt->data_dev->bdev;
2819 struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
Mike Snitzer0424caa2012-09-26 23:45:47 +01002820 const char *reason = NULL;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002821 char buf[BDEVNAME_SIZE];
2822
Mike Snitzer0424caa2012-09-26 23:45:47 +01002823 if (!pt->adjusted_pf.discard_passdown)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002824 return;
2825
Mike Snitzer0424caa2012-09-26 23:45:47 +01002826 if (!data_dev_supports_discard(pt))
2827 reason = "discard unsupported";
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002828
Mike Snitzer0424caa2012-09-26 23:45:47 +01002829 else if (data_limits->max_discard_sectors < pool->sectors_per_block)
2830 reason = "max discard sectors smaller than a block";
2831
Mike Snitzer0424caa2012-09-26 23:45:47 +01002832 if (reason) {
2833 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
2834 pt->adjusted_pf.discard_passdown = false;
2835 }
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002836}
2837
Joe Thornber991d9fa2011-10-31 20:21:18 +00002838static int bind_control_target(struct pool *pool, struct dm_target *ti)
2839{
2840 struct pool_c *pt = ti->private;
2841
Joe Thornbere49e5822012-07-27 15:08:16 +01002842 /*
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002843 * We want to make sure that a pool in PM_FAIL mode is never upgraded.
Joe Thornbere49e5822012-07-27 15:08:16 +01002844 */
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002845 enum pool_mode old_mode = get_pool_mode(pool);
Mike Snitzer0424caa2012-09-26 23:45:47 +01002846 enum pool_mode new_mode = pt->adjusted_pf.mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002847
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002848 /*
Mike Snitzer8b64e882013-12-20 14:27:28 -05002849 * Don't change the pool's mode until set_pool_mode() below.
2850 * Otherwise the pool's process_* function pointers may
2851 * not match the desired pool mode.
2852 */
2853 pt->adjusted_pf.mode = old_mode;
2854
2855 pool->ti = ti;
2856 pool->pf = pt->adjusted_pf;
2857 pool->low_water_blocks = pt->low_water_blocks;
2858
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002859 set_pool_mode(pool, new_mode);
Mike Snitzerf4026932012-05-19 01:01:01 +01002860
Joe Thornber991d9fa2011-10-31 20:21:18 +00002861 return 0;
2862}
2863
2864static void unbind_control_target(struct pool *pool, struct dm_target *ti)
2865{
2866 if (pool->ti == ti)
2867 pool->ti = NULL;
2868}
2869
2870/*----------------------------------------------------------------
2871 * Pool creation
2872 *--------------------------------------------------------------*/
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002873/* Initialize pool features. */
2874static void pool_features_init(struct pool_features *pf)
2875{
Joe Thornbere49e5822012-07-27 15:08:16 +01002876 pf->mode = PM_WRITE;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002877 pf->zero_new_blocks = true;
2878 pf->discard_enabled = true;
2879 pf->discard_passdown = true;
Mike Snitzer787a996c2013-12-06 16:21:43 -05002880 pf->error_if_no_space = false;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002881}
2882
Joe Thornber991d9fa2011-10-31 20:21:18 +00002883static void __pool_destroy(struct pool *pool)
2884{
2885 __pool_table_remove(pool);
2886
Joe Thornbera822c832015-07-03 10:22:42 +01002887 vfree(pool->cell_sort_array);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002888 if (dm_pool_metadata_close(pool->pmd) < 0)
2889 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2890
Mike Snitzer44feb382012-10-12 21:02:10 +01002891 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002892 dm_kcopyd_client_destroy(pool->copier);
2893
2894 if (pool->wq)
2895 destroy_workqueue(pool->wq);
2896
2897 if (pool->next_mapping)
2898 mempool_free(pool->next_mapping, pool->mapping_pool);
2899 mempool_destroy(pool->mapping_pool);
Mike Snitzer44feb382012-10-12 21:02:10 +01002900 dm_deferred_set_destroy(pool->shared_read_ds);
2901 dm_deferred_set_destroy(pool->all_io_ds);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002902 kfree(pool);
2903}
2904
Mike Snitzera24c2562012-06-03 00:30:00 +01002905static struct kmem_cache *_new_mapping_cache;
Mike Snitzera24c2562012-06-03 00:30:00 +01002906
Joe Thornber991d9fa2011-10-31 20:21:18 +00002907static struct pool *pool_create(struct mapped_device *pool_md,
2908 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002909 unsigned long block_size,
2910 int read_only, char **error)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002911{
2912 int r;
2913 void *err_p;
2914 struct pool *pool;
2915 struct dm_pool_metadata *pmd;
Joe Thornbere49e5822012-07-27 15:08:16 +01002916 bool format_device = read_only ? false : true;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002917
Joe Thornbere49e5822012-07-27 15:08:16 +01002918 pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002919 if (IS_ERR(pmd)) {
2920 *error = "Error creating metadata object";
2921 return (struct pool *)pmd;
2922 }
2923
2924 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
2925 if (!pool) {
2926 *error = "Error allocating memory for pool";
2927 err_p = ERR_PTR(-ENOMEM);
2928 goto bad_pool;
2929 }
2930
2931 pool->pmd = pmd;
2932 pool->sectors_per_block = block_size;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +01002933 if (block_size & (block_size - 1))
2934 pool->sectors_per_block_shift = -1;
2935 else
2936 pool->sectors_per_block_shift = __ffs(block_size);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002937 pool->low_water_blocks = 0;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002938 pool_features_init(&pool->pf);
Joe Thornbera195db22014-10-06 16:30:06 -04002939 pool->prison = dm_bio_prison_create();
Joe Thornber991d9fa2011-10-31 20:21:18 +00002940 if (!pool->prison) {
2941 *error = "Error creating pool's bio prison";
2942 err_p = ERR_PTR(-ENOMEM);
2943 goto bad_prison;
2944 }
2945
Mikulas Patockadf5d2e92013-03-01 22:45:49 +00002946 pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002947 if (IS_ERR(pool->copier)) {
2948 r = PTR_ERR(pool->copier);
2949 *error = "Error creating pool's kcopyd client";
2950 err_p = ERR_PTR(r);
2951 goto bad_kcopyd_client;
2952 }
2953
2954 /*
2955 * Create singlethreaded workqueue that will service all devices
2956 * that use this metadata.
2957 */
2958 pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2959 if (!pool->wq) {
2960 *error = "Error creating pool's workqueue";
2961 err_p = ERR_PTR(-ENOMEM);
2962 goto bad_wq;
2963 }
2964
Joe Thornber7d327fe2014-10-06 15:45:59 +01002965 throttle_init(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002966 INIT_WORK(&pool->worker, do_worker);
Joe Thornber905e51b2012-03-28 18:41:27 +01002967 INIT_DELAYED_WORK(&pool->waker, do_waker);
Joe Thornber85ad643b2014-05-09 15:59:38 +01002968 INIT_DELAYED_WORK(&pool->no_space_timeout, do_no_space_timeout);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002969 spin_lock_init(&pool->lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002970 bio_list_init(&pool->deferred_flush_bios);
2971 INIT_LIST_HEAD(&pool->prepared_mappings);
Joe Thornber104655f2012-03-28 18:41:28 +01002972 INIT_LIST_HEAD(&pool->prepared_discards);
Joe Thornber2a0fbff2016-07-01 14:00:02 +01002973 INIT_LIST_HEAD(&pool->prepared_discards_pt2);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002974 INIT_LIST_HEAD(&pool->active_thins);
Joe Thornber88a66212013-12-04 20:16:12 -05002975 pool->low_water_triggered = false;
Mike Snitzer80e96c52014-11-07 15:09:46 -05002976 pool->suspended = true;
Mike Snitzerc3667cc2016-03-10 11:31:35 -05002977 pool->out_of_data_space = false;
Mike Snitzer44feb382012-10-12 21:02:10 +01002978
2979 pool->shared_read_ds = dm_deferred_set_create();
2980 if (!pool->shared_read_ds) {
2981 *error = "Error creating pool's shared read deferred set";
2982 err_p = ERR_PTR(-ENOMEM);
2983 goto bad_shared_read_ds;
2984 }
2985
2986 pool->all_io_ds = dm_deferred_set_create();
2987 if (!pool->all_io_ds) {
2988 *error = "Error creating pool's all io deferred set";
2989 err_p = ERR_PTR(-ENOMEM);
2990 goto bad_all_io_ds;
2991 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002992
2993 pool->next_mapping = NULL;
Mike Snitzera24c2562012-06-03 00:30:00 +01002994 pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
2995 _new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002996 if (!pool->mapping_pool) {
2997 *error = "Error creating pool's mapping mempool";
2998 err_p = ERR_PTR(-ENOMEM);
2999 goto bad_mapping_pool;
3000 }
3001
Joe Thornbera822c832015-07-03 10:22:42 +01003002 pool->cell_sort_array = vmalloc(sizeof(*pool->cell_sort_array) * CELL_SORT_ARRAY_SIZE);
3003 if (!pool->cell_sort_array) {
3004 *error = "Error allocating cell sort array";
3005 err_p = ERR_PTR(-ENOMEM);
3006 goto bad_sort_array;
3007 }
3008
Joe Thornber991d9fa2011-10-31 20:21:18 +00003009 pool->ref_count = 1;
Joe Thornber905e51b2012-03-28 18:41:27 +01003010 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003011 pool->pool_md = pool_md;
3012 pool->md_dev = metadata_dev;
3013 __pool_table_insert(pool);
3014
3015 return pool;
3016
Joe Thornbera822c832015-07-03 10:22:42 +01003017bad_sort_array:
3018 mempool_destroy(pool->mapping_pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003019bad_mapping_pool:
Mike Snitzer44feb382012-10-12 21:02:10 +01003020 dm_deferred_set_destroy(pool->all_io_ds);
3021bad_all_io_ds:
3022 dm_deferred_set_destroy(pool->shared_read_ds);
3023bad_shared_read_ds:
Joe Thornber991d9fa2011-10-31 20:21:18 +00003024 destroy_workqueue(pool->wq);
3025bad_wq:
3026 dm_kcopyd_client_destroy(pool->copier);
3027bad_kcopyd_client:
Mike Snitzer44feb382012-10-12 21:02:10 +01003028 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003029bad_prison:
3030 kfree(pool);
3031bad_pool:
3032 if (dm_pool_metadata_close(pmd))
3033 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
3034
3035 return err_p;
3036}
3037
3038static void __pool_inc(struct pool *pool)
3039{
3040 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
3041 pool->ref_count++;
3042}
3043
3044static void __pool_dec(struct pool *pool)
3045{
3046 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
3047 BUG_ON(!pool->ref_count);
3048 if (!--pool->ref_count)
3049 __pool_destroy(pool);
3050}
3051
3052static struct pool *__pool_find(struct mapped_device *pool_md,
3053 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01003054 unsigned long block_size, int read_only,
3055 char **error, int *created)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003056{
3057 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
3058
3059 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01003060 if (pool->pool_md != pool_md) {
3061 *error = "metadata device already in use by a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00003062 return ERR_PTR(-EBUSY);
Mike Snitzerf09996c2012-07-27 15:07:59 +01003063 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003064 __pool_inc(pool);
3065
3066 } else {
3067 pool = __pool_table_lookup(pool_md);
3068 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01003069 if (pool->md_dev != metadata_dev) {
3070 *error = "different pool cannot replace a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00003071 return ERR_PTR(-EINVAL);
Mike Snitzerf09996c2012-07-27 15:07:59 +01003072 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003073 __pool_inc(pool);
3074
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003075 } else {
Joe Thornbere49e5822012-07-27 15:08:16 +01003076 pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003077 *created = 1;
3078 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003079 }
3080
3081 return pool;
3082}
3083
3084/*----------------------------------------------------------------
3085 * Pool target methods
3086 *--------------------------------------------------------------*/
3087static void pool_dtr(struct dm_target *ti)
3088{
3089 struct pool_c *pt = ti->private;
3090
3091 mutex_lock(&dm_thin_pool_table.mutex);
3092
3093 unbind_control_target(pt->pool, ti);
3094 __pool_dec(pt->pool);
3095 dm_put_device(ti, pt->metadata_dev);
3096 dm_put_device(ti, pt->data_dev);
3097 kfree(pt);
3098
3099 mutex_unlock(&dm_thin_pool_table.mutex);
3100}
3101
Joe Thornber991d9fa2011-10-31 20:21:18 +00003102static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
3103 struct dm_target *ti)
3104{
3105 int r;
3106 unsigned argc;
3107 const char *arg_name;
3108
3109 static struct dm_arg _args[] = {
Mike Snitzer74aa45c2014-01-15 19:07:58 -05003110 {0, 4, "Invalid number of pool feature arguments"},
Joe Thornber991d9fa2011-10-31 20:21:18 +00003111 };
3112
3113 /*
3114 * No feature arguments supplied.
3115 */
3116 if (!as->argc)
3117 return 0;
3118
3119 r = dm_read_arg_group(_args, as, &argc, &ti->error);
3120 if (r)
3121 return -EINVAL;
3122
3123 while (argc && !r) {
3124 arg_name = dm_shift_arg(as);
3125 argc--;
3126
Joe Thornbere49e5822012-07-27 15:08:16 +01003127 if (!strcasecmp(arg_name, "skip_block_zeroing"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003128 pf->zero_new_blocks = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003129
Joe Thornbere49e5822012-07-27 15:08:16 +01003130 else if (!strcasecmp(arg_name, "ignore_discard"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003131 pf->discard_enabled = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01003132
3133 else if (!strcasecmp(arg_name, "no_discard_passdown"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003134 pf->discard_passdown = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01003135
3136 else if (!strcasecmp(arg_name, "read_only"))
3137 pf->mode = PM_READ_ONLY;
3138
Mike Snitzer787a996c2013-12-06 16:21:43 -05003139 else if (!strcasecmp(arg_name, "error_if_no_space"))
3140 pf->error_if_no_space = true;
3141
Joe Thornbere49e5822012-07-27 15:08:16 +01003142 else {
3143 ti->error = "Unrecognised pool feature requested";
3144 r = -EINVAL;
3145 break;
3146 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003147 }
3148
3149 return r;
3150}
3151
Joe Thornberac8c3f32013-05-10 14:37:21 +01003152static void metadata_low_callback(void *context)
3153{
3154 struct pool *pool = context;
3155
3156 DMWARN("%s: reached low water mark for metadata device: sending event.",
3157 dm_device_name(pool->pool_md));
3158
3159 dm_table_event(pool->ti->table);
3160}
3161
Mike Snitzer7d489352014-02-12 23:58:15 -05003162static sector_t get_dev_size(struct block_device *bdev)
Joe Thornberb17446d2013-05-10 14:37:18 +01003163{
Mike Snitzer7d489352014-02-12 23:58:15 -05003164 return i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
3165}
3166
3167static void warn_if_metadata_device_too_big(struct block_device *bdev)
3168{
3169 sector_t metadata_dev_size = get_dev_size(bdev);
Joe Thornberb17446d2013-05-10 14:37:18 +01003170 char buffer[BDEVNAME_SIZE];
3171
Mike Snitzer7d489352014-02-12 23:58:15 -05003172 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
Joe Thornberb17446d2013-05-10 14:37:18 +01003173 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
3174 bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS);
Mike Snitzer7d489352014-02-12 23:58:15 -05003175}
3176
3177static sector_t get_metadata_dev_size(struct block_device *bdev)
3178{
3179 sector_t metadata_dev_size = get_dev_size(bdev);
3180
3181 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS)
3182 metadata_dev_size = THIN_METADATA_MAX_SECTORS;
Joe Thornberb17446d2013-05-10 14:37:18 +01003183
3184 return metadata_dev_size;
3185}
3186
Joe Thornber24347e92013-05-10 14:37:19 +01003187static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev)
3188{
3189 sector_t metadata_dev_size = get_metadata_dev_size(bdev);
3190
Mike Snitzer7d489352014-02-12 23:58:15 -05003191 sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE);
Joe Thornber24347e92013-05-10 14:37:19 +01003192
3193 return metadata_dev_size;
3194}
3195
Joe Thornber991d9fa2011-10-31 20:21:18 +00003196/*
Joe Thornberac8c3f32013-05-10 14:37:21 +01003197 * When a metadata threshold is crossed a dm event is triggered, and
3198 * userland should respond by growing the metadata device. We could let
3199 * userland set the threshold, like we do with the data threshold, but I'm
3200 * not sure they know enough to do this well.
3201 */
3202static dm_block_t calc_metadata_threshold(struct pool_c *pt)
3203{
3204 /*
3205 * 4M is ample for all ops with the possible exception of thin
3206 * device deletion which is harmless if it fails (just retry the
3207 * delete after you've grown the device).
3208 */
3209 dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4;
3210 return min((dm_block_t)1024ULL /* 4M */, quarter);
3211}
3212
3213/*
Joe Thornber991d9fa2011-10-31 20:21:18 +00003214 * thin-pool <metadata dev> <data dev>
3215 * <data block size (sectors)>
3216 * <low water mark (blocks)>
3217 * [<#feature args> [<arg>]*]
3218 *
3219 * Optional feature arguments are:
3220 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003221 * ignore_discard: disable discard
3222 * no_discard_passdown: don't pass discards down to the data device
Mike Snitzer787a996c2013-12-06 16:21:43 -05003223 * read_only: Don't allow any changes to be made to the pool metadata.
3224 * error_if_no_space: error IOs, instead of queueing, if no space.
Joe Thornber991d9fa2011-10-31 20:21:18 +00003225 */
3226static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
3227{
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003228 int r, pool_created = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003229 struct pool_c *pt;
3230 struct pool *pool;
3231 struct pool_features pf;
3232 struct dm_arg_set as;
3233 struct dm_dev *data_dev;
3234 unsigned long block_size;
3235 dm_block_t low_water_blocks;
3236 struct dm_dev *metadata_dev;
Joe Thornber5d0db962013-05-10 14:37:19 +01003237 fmode_t metadata_mode;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003238
3239 /*
3240 * FIXME Remove validation from scope of lock.
3241 */
3242 mutex_lock(&dm_thin_pool_table.mutex);
3243
3244 if (argc < 4) {
3245 ti->error = "Invalid argument count";
3246 r = -EINVAL;
3247 goto out_unlock;
3248 }
Joe Thornber5d0db962013-05-10 14:37:19 +01003249
Joe Thornber991d9fa2011-10-31 20:21:18 +00003250 as.argc = argc;
3251 as.argv = argv;
3252
Joe Thornber5d0db962013-05-10 14:37:19 +01003253 /*
3254 * Set default pool features.
3255 */
3256 pool_features_init(&pf);
3257
3258 dm_consume_args(&as, 4);
3259 r = parse_pool_features(&as, &pf, ti);
3260 if (r)
3261 goto out_unlock;
3262
3263 metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE);
3264 r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003265 if (r) {
3266 ti->error = "Error opening metadata block device";
3267 goto out_unlock;
3268 }
Mike Snitzer7d489352014-02-12 23:58:15 -05003269 warn_if_metadata_device_too_big(metadata_dev->bdev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003270
3271 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
3272 if (r) {
3273 ti->error = "Error getting data device";
3274 goto out_metadata;
3275 }
3276
3277 if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
3278 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
3279 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01003280 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00003281 ti->error = "Invalid block size";
3282 r = -EINVAL;
3283 goto out;
3284 }
3285
3286 if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
3287 ti->error = "Invalid low water mark";
3288 r = -EINVAL;
3289 goto out;
3290 }
3291
Joe Thornber991d9fa2011-10-31 20:21:18 +00003292 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
3293 if (!pt) {
3294 r = -ENOMEM;
3295 goto out;
3296 }
3297
3298 pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
Joe Thornbere49e5822012-07-27 15:08:16 +01003299 block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003300 if (IS_ERR(pool)) {
3301 r = PTR_ERR(pool);
3302 goto out_free_pt;
3303 }
3304
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003305 /*
3306 * 'pool_created' reflects whether this is the first table load.
3307 * Top level discard support is not allowed to be changed after
3308 * initial load. This would require a pool reload to trigger thin
3309 * device changes.
3310 */
3311 if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
3312 ti->error = "Discard support cannot be disabled once enabled";
3313 r = -EINVAL;
3314 goto out_flags_changed;
3315 }
3316
Joe Thornber991d9fa2011-10-31 20:21:18 +00003317 pt->pool = pool;
3318 pt->ti = ti;
3319 pt->metadata_dev = metadata_dev;
3320 pt->data_dev = data_dev;
3321 pt->low_water_blocks = low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +01003322 pt->adjusted_pf = pt->requested_pf = pf;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003323 ti->num_flush_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003324
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003325 /*
3326 * Only need to enable discards if the pool should pass
3327 * them down to the data device. The thin device's discard
3328 * processing will cause mappings to be removed from the btree.
3329 */
Mike Snitzerb60ab992013-09-19 18:49:11 -04003330 ti->discard_zeroes_data_unsupported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003331 if (pf.discard_enabled && pf.discard_passdown) {
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003332 ti->num_discard_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003333
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003334 /*
3335 * Setting 'discards_supported' circumvents the normal
3336 * stacking of discard limits (this keeps the pool and
3337 * thin devices' discard limits consistent).
3338 */
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01003339 ti->discards_supported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003340 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003341 ti->private = pt;
3342
Joe Thornberac8c3f32013-05-10 14:37:21 +01003343 r = dm_pool_register_metadata_threshold(pt->pool->pmd,
3344 calc_metadata_threshold(pt),
3345 metadata_low_callback,
3346 pool);
3347 if (r)
Mike Snitzerba306702015-10-13 12:04:28 -04003348 goto out_flags_changed;
Joe Thornberac8c3f32013-05-10 14:37:21 +01003349
Joe Thornber991d9fa2011-10-31 20:21:18 +00003350 pt->callbacks.congested_fn = pool_is_congested;
3351 dm_table_add_target_callbacks(ti->table, &pt->callbacks);
3352
3353 mutex_unlock(&dm_thin_pool_table.mutex);
3354
3355 return 0;
3356
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003357out_flags_changed:
3358 __pool_dec(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003359out_free_pt:
3360 kfree(pt);
3361out:
3362 dm_put_device(ti, data_dev);
3363out_metadata:
3364 dm_put_device(ti, metadata_dev);
3365out_unlock:
3366 mutex_unlock(&dm_thin_pool_table.mutex);
3367
3368 return r;
3369}
3370
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003371static int pool_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003372{
3373 int r;
3374 struct pool_c *pt = ti->private;
3375 struct pool *pool = pt->pool;
3376 unsigned long flags;
3377
3378 /*
3379 * As this is a singleton target, ti->begin is always zero.
3380 */
3381 spin_lock_irqsave(&pool->lock, flags);
3382 bio->bi_bdev = pt->data_dev->bdev;
3383 r = DM_MAPIO_REMAPPED;
3384 spin_unlock_irqrestore(&pool->lock, flags);
3385
3386 return r;
3387}
3388
Joe Thornberb17446d2013-05-10 14:37:18 +01003389static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit)
3390{
3391 int r;
3392 struct pool_c *pt = ti->private;
3393 struct pool *pool = pt->pool;
3394 sector_t data_size = ti->len;
3395 dm_block_t sb_data_size;
3396
3397 *need_commit = false;
3398
3399 (void) sector_div(data_size, pool->sectors_per_block);
3400
3401 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
3402 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003403 DMERR("%s: failed to retrieve data device size",
3404 dm_device_name(pool->pool_md));
Joe Thornberb17446d2013-05-10 14:37:18 +01003405 return r;
3406 }
3407
3408 if (data_size < sb_data_size) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003409 DMERR("%s: pool target (%llu blocks) too small: expected %llu",
3410 dm_device_name(pool->pool_md),
Joe Thornberb17446d2013-05-10 14:37:18 +01003411 (unsigned long long)data_size, sb_data_size);
3412 return -EINVAL;
3413
3414 } else if (data_size > sb_data_size) {
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05003415 if (dm_pool_metadata_needs_check(pool->pmd)) {
3416 DMERR("%s: unable to grow the data device until repaired.",
3417 dm_device_name(pool->pool_md));
3418 return 0;
3419 }
3420
Mike Snitzer6f7f51d2013-12-04 10:25:53 -05003421 if (sb_data_size)
3422 DMINFO("%s: growing the data device from %llu to %llu blocks",
3423 dm_device_name(pool->pool_md),
3424 sb_data_size, (unsigned long long)data_size);
Joe Thornberb17446d2013-05-10 14:37:18 +01003425 r = dm_pool_resize_data_dev(pool->pmd, data_size);
3426 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05003427 metadata_operation_failed(pool, "dm_pool_resize_data_dev", r);
Joe Thornberb17446d2013-05-10 14:37:18 +01003428 return r;
3429 }
3430
3431 *need_commit = true;
3432 }
3433
3434 return 0;
3435}
3436
Joe Thornber24347e92013-05-10 14:37:19 +01003437static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit)
3438{
3439 int r;
3440 struct pool_c *pt = ti->private;
3441 struct pool *pool = pt->pool;
3442 dm_block_t metadata_dev_size, sb_metadata_dev_size;
3443
3444 *need_commit = false;
3445
Alasdair G Kergon610bba82013-05-19 18:57:50 +01003446 metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev);
Joe Thornber24347e92013-05-10 14:37:19 +01003447
3448 r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size);
3449 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003450 DMERR("%s: failed to retrieve metadata device size",
3451 dm_device_name(pool->pool_md));
Joe Thornber24347e92013-05-10 14:37:19 +01003452 return r;
3453 }
3454
3455 if (metadata_dev_size < sb_metadata_dev_size) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003456 DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
3457 dm_device_name(pool->pool_md),
Joe Thornber24347e92013-05-10 14:37:19 +01003458 metadata_dev_size, sb_metadata_dev_size);
3459 return -EINVAL;
3460
3461 } else if (metadata_dev_size > sb_metadata_dev_size) {
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05003462 if (dm_pool_metadata_needs_check(pool->pmd)) {
3463 DMERR("%s: unable to grow the metadata device until repaired.",
3464 dm_device_name(pool->pool_md));
3465 return 0;
3466 }
3467
Mike Snitzer7d489352014-02-12 23:58:15 -05003468 warn_if_metadata_device_too_big(pool->md_dev);
Mike Snitzer6f7f51d2013-12-04 10:25:53 -05003469 DMINFO("%s: growing the metadata device from %llu to %llu blocks",
3470 dm_device_name(pool->pool_md),
3471 sb_metadata_dev_size, metadata_dev_size);
Joe Thornbera9537db2018-09-10 16:50:09 +01003472
3473 if (get_pool_mode(pool) == PM_OUT_OF_METADATA_SPACE)
3474 set_pool_mode(pool, PM_WRITE);
3475
Joe Thornber24347e92013-05-10 14:37:19 +01003476 r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size);
3477 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05003478 metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r);
Joe Thornber24347e92013-05-10 14:37:19 +01003479 return r;
3480 }
3481
3482 *need_commit = true;
3483 }
3484
3485 return 0;
3486}
3487
Joe Thornber991d9fa2011-10-31 20:21:18 +00003488/*
3489 * Retrieves the number of blocks of the data device from
3490 * the superblock and compares it to the actual device size,
3491 * thus resizing the data device in case it has grown.
3492 *
3493 * This both copes with opening preallocated data devices in the ctr
3494 * being followed by a resume
3495 * -and-
3496 * calling the resume method individually after userspace has
3497 * grown the data device in reaction to a table event.
3498 */
3499static int pool_preresume(struct dm_target *ti)
3500{
3501 int r;
Joe Thornber24347e92013-05-10 14:37:19 +01003502 bool need_commit1, need_commit2;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003503 struct pool_c *pt = ti->private;
3504 struct pool *pool = pt->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003505
3506 /*
3507 * Take control of the pool object.
3508 */
3509 r = bind_control_target(pool, ti);
3510 if (r)
3511 return r;
3512
Joe Thornberb17446d2013-05-10 14:37:18 +01003513 r = maybe_resize_data_dev(ti, &need_commit1);
3514 if (r)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003515 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003516
Joe Thornber24347e92013-05-10 14:37:19 +01003517 r = maybe_resize_metadata_dev(ti, &need_commit2);
3518 if (r)
3519 return r;
3520
3521 if (need_commit1 || need_commit2)
Joe Thornber020cc3b2013-12-04 15:05:36 -05003522 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003523
3524 return 0;
3525}
3526
Mike Snitzer583024d2014-10-28 20:58:45 -04003527static void pool_suspend_active_thins(struct pool *pool)
3528{
3529 struct thin_c *tc;
3530
3531 /* Suspend all active thin devices */
3532 tc = get_first_thin(pool);
3533 while (tc) {
3534 dm_internal_suspend_noflush(tc->thin_md);
3535 tc = get_next_thin(pool, tc);
3536 }
3537}
3538
3539static void pool_resume_active_thins(struct pool *pool)
3540{
3541 struct thin_c *tc;
3542
3543 /* Resume all active thin devices */
3544 tc = get_first_thin(pool);
3545 while (tc) {
3546 dm_internal_resume(tc->thin_md);
3547 tc = get_next_thin(pool, tc);
3548 }
3549}
3550
Joe Thornber991d9fa2011-10-31 20:21:18 +00003551static void pool_resume(struct dm_target *ti)
3552{
3553 struct pool_c *pt = ti->private;
3554 struct pool *pool = pt->pool;
3555 unsigned long flags;
3556
Mike Snitzer583024d2014-10-28 20:58:45 -04003557 /*
3558 * Must requeue active_thins' bios and then resume
3559 * active_thins _before_ clearing 'suspend' flag.
3560 */
3561 requeue_bios(pool);
3562 pool_resume_active_thins(pool);
3563
Joe Thornber991d9fa2011-10-31 20:21:18 +00003564 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber88a66212013-12-04 20:16:12 -05003565 pool->low_water_triggered = false;
Mike Snitzer80e96c52014-11-07 15:09:46 -05003566 pool->suspended = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003567 spin_unlock_irqrestore(&pool->lock, flags);
Mike Snitzer80e96c52014-11-07 15:09:46 -05003568
Joe Thornber905e51b2012-03-28 18:41:27 +01003569 do_waker(&pool->waker.work);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003570}
3571
Mike Snitzer80e96c52014-11-07 15:09:46 -05003572static void pool_presuspend(struct dm_target *ti)
3573{
3574 struct pool_c *pt = ti->private;
3575 struct pool *pool = pt->pool;
3576 unsigned long flags;
3577
3578 spin_lock_irqsave(&pool->lock, flags);
3579 pool->suspended = true;
3580 spin_unlock_irqrestore(&pool->lock, flags);
Mike Snitzer583024d2014-10-28 20:58:45 -04003581
3582 pool_suspend_active_thins(pool);
Mike Snitzer80e96c52014-11-07 15:09:46 -05003583}
3584
3585static void pool_presuspend_undo(struct dm_target *ti)
3586{
3587 struct pool_c *pt = ti->private;
3588 struct pool *pool = pt->pool;
3589 unsigned long flags;
3590
Mike Snitzer583024d2014-10-28 20:58:45 -04003591 pool_resume_active_thins(pool);
3592
Mike Snitzer80e96c52014-11-07 15:09:46 -05003593 spin_lock_irqsave(&pool->lock, flags);
3594 pool->suspended = false;
3595 spin_unlock_irqrestore(&pool->lock, flags);
3596}
3597
Joe Thornber991d9fa2011-10-31 20:21:18 +00003598static void pool_postsuspend(struct dm_target *ti)
3599{
Joe Thornber991d9fa2011-10-31 20:21:18 +00003600 struct pool_c *pt = ti->private;
3601 struct pool *pool = pt->pool;
3602
Nikolay Borisov18d03e82015-12-17 18:03:35 +02003603 cancel_delayed_work_sync(&pool->waker);
3604 cancel_delayed_work_sync(&pool->no_space_timeout);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003605 flush_workqueue(pool->wq);
Joe Thornber020cc3b2013-12-04 15:05:36 -05003606 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003607}
3608
3609static int check_arg_count(unsigned argc, unsigned args_required)
3610{
3611 if (argc != args_required) {
3612 DMWARN("Message received with %u arguments instead of %u.",
3613 argc, args_required);
3614 return -EINVAL;
3615 }
3616
3617 return 0;
3618}
3619
3620static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
3621{
3622 if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
3623 *dev_id <= MAX_DEV_ID)
3624 return 0;
3625
3626 if (warning)
3627 DMWARN("Message received with invalid device id: %s", arg);
3628
3629 return -EINVAL;
3630}
3631
3632static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
3633{
3634 dm_thin_id dev_id;
3635 int r;
3636
3637 r = check_arg_count(argc, 2);
3638 if (r)
3639 return r;
3640
3641 r = read_dev_id(argv[1], &dev_id, 1);
3642 if (r)
3643 return r;
3644
3645 r = dm_pool_create_thin(pool->pmd, dev_id);
3646 if (r) {
3647 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
3648 argv[1]);
3649 return r;
3650 }
3651
3652 return 0;
3653}
3654
3655static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3656{
3657 dm_thin_id dev_id;
3658 dm_thin_id origin_dev_id;
3659 int r;
3660
3661 r = check_arg_count(argc, 3);
3662 if (r)
3663 return r;
3664
3665 r = read_dev_id(argv[1], &dev_id, 1);
3666 if (r)
3667 return r;
3668
3669 r = read_dev_id(argv[2], &origin_dev_id, 1);
3670 if (r)
3671 return r;
3672
3673 r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
3674 if (r) {
3675 DMWARN("Creation of new snapshot %s of device %s failed.",
3676 argv[1], argv[2]);
3677 return r;
3678 }
3679
3680 return 0;
3681}
3682
3683static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
3684{
3685 dm_thin_id dev_id;
3686 int r;
3687
3688 r = check_arg_count(argc, 2);
3689 if (r)
3690 return r;
3691
3692 r = read_dev_id(argv[1], &dev_id, 1);
3693 if (r)
3694 return r;
3695
3696 r = dm_pool_delete_thin_device(pool->pmd, dev_id);
3697 if (r)
3698 DMWARN("Deletion of thin device %s failed.", argv[1]);
3699
3700 return r;
3701}
3702
3703static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
3704{
3705 dm_thin_id old_id, new_id;
3706 int r;
3707
3708 r = check_arg_count(argc, 3);
3709 if (r)
3710 return r;
3711
3712 if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
3713 DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
3714 return -EINVAL;
3715 }
3716
3717 if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
3718 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
3719 return -EINVAL;
3720 }
3721
3722 r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
3723 if (r) {
3724 DMWARN("Failed to change transaction id from %s to %s.",
3725 argv[1], argv[2]);
3726 return r;
3727 }
3728
3729 return 0;
3730}
3731
Joe Thornbercc8394d2012-06-03 00:30:01 +01003732static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3733{
3734 int r;
3735
3736 r = check_arg_count(argc, 1);
3737 if (r)
3738 return r;
3739
Joe Thornber020cc3b2013-12-04 15:05:36 -05003740 (void) commit(pool);
Joe Thornber0d200ae2012-07-03 12:55:31 +01003741
Joe Thornbercc8394d2012-06-03 00:30:01 +01003742 r = dm_pool_reserve_metadata_snap(pool->pmd);
3743 if (r)
3744 DMWARN("reserve_metadata_snap message failed.");
3745
3746 return r;
3747}
3748
3749static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3750{
3751 int r;
3752
3753 r = check_arg_count(argc, 1);
3754 if (r)
3755 return r;
3756
3757 r = dm_pool_release_metadata_snap(pool->pmd);
3758 if (r)
3759 DMWARN("release_metadata_snap message failed.");
3760
3761 return r;
3762}
3763
Joe Thornber991d9fa2011-10-31 20:21:18 +00003764/*
3765 * Messages supported:
3766 * create_thin <dev_id>
3767 * create_snap <dev_id> <origin_id>
3768 * delete <dev_id>
Joe Thornber991d9fa2011-10-31 20:21:18 +00003769 * set_transaction_id <current_trans_id> <new_trans_id>
Joe Thornbercc8394d2012-06-03 00:30:01 +01003770 * reserve_metadata_snap
3771 * release_metadata_snap
Joe Thornber991d9fa2011-10-31 20:21:18 +00003772 */
3773static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
3774{
3775 int r = -EINVAL;
3776 struct pool_c *pt = ti->private;
3777 struct pool *pool = pt->pool;
3778
Joe Thornbera9537db2018-09-10 16:50:09 +01003779 if (get_pool_mode(pool) >= PM_OUT_OF_METADATA_SPACE) {
Joe Thornber2a7eaea2015-01-26 11:38:21 +00003780 DMERR("%s: unable to service pool target messages in READ_ONLY or FAIL mode",
3781 dm_device_name(pool->pool_md));
Mike Snitzerfd467692015-06-09 12:31:26 -04003782 return -EOPNOTSUPP;
Joe Thornber2a7eaea2015-01-26 11:38:21 +00003783 }
3784
Joe Thornber991d9fa2011-10-31 20:21:18 +00003785 if (!strcasecmp(argv[0], "create_thin"))
3786 r = process_create_thin_mesg(argc, argv, pool);
3787
3788 else if (!strcasecmp(argv[0], "create_snap"))
3789 r = process_create_snap_mesg(argc, argv, pool);
3790
3791 else if (!strcasecmp(argv[0], "delete"))
3792 r = process_delete_mesg(argc, argv, pool);
3793
3794 else if (!strcasecmp(argv[0], "set_transaction_id"))
3795 r = process_set_transaction_id_mesg(argc, argv, pool);
3796
Joe Thornbercc8394d2012-06-03 00:30:01 +01003797 else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
3798 r = process_reserve_metadata_snap_mesg(argc, argv, pool);
3799
3800 else if (!strcasecmp(argv[0], "release_metadata_snap"))
3801 r = process_release_metadata_snap_mesg(argc, argv, pool);
3802
Joe Thornber991d9fa2011-10-31 20:21:18 +00003803 else
3804 DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
3805
Joe Thornbere49e5822012-07-27 15:08:16 +01003806 if (!r)
Joe Thornber020cc3b2013-12-04 15:05:36 -05003807 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003808
3809 return r;
3810}
3811
Joe Thornbere49e5822012-07-27 15:08:16 +01003812static void emit_flags(struct pool_features *pf, char *result,
3813 unsigned sz, unsigned maxlen)
3814{
3815 unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
Mike Snitzer787a996c2013-12-06 16:21:43 -05003816 !pf->discard_passdown + (pf->mode == PM_READ_ONLY) +
3817 pf->error_if_no_space;
Joe Thornbere49e5822012-07-27 15:08:16 +01003818 DMEMIT("%u ", count);
3819
3820 if (!pf->zero_new_blocks)
3821 DMEMIT("skip_block_zeroing ");
3822
3823 if (!pf->discard_enabled)
3824 DMEMIT("ignore_discard ");
3825
3826 if (!pf->discard_passdown)
3827 DMEMIT("no_discard_passdown ");
3828
3829 if (pf->mode == PM_READ_ONLY)
3830 DMEMIT("read_only ");
Mike Snitzer787a996c2013-12-06 16:21:43 -05003831
3832 if (pf->error_if_no_space)
3833 DMEMIT("error_if_no_space ");
Joe Thornbere49e5822012-07-27 15:08:16 +01003834}
3835
Joe Thornber991d9fa2011-10-31 20:21:18 +00003836/*
3837 * Status line is:
3838 * <transaction id> <used metadata sectors>/<total metadata sectors>
3839 * <used data sectors>/<total data sectors> <held metadata root>
Mike Snitzere4c78e22015-07-15 11:40:24 -04003840 * <pool mode> <discard config> <no space config> <needs_check>
Joe Thornber991d9fa2011-10-31 20:21:18 +00003841 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003842static void pool_status(struct dm_target *ti, status_type_t type,
3843 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003844{
Joe Thornbere49e5822012-07-27 15:08:16 +01003845 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003846 unsigned sz = 0;
3847 uint64_t transaction_id;
3848 dm_block_t nr_free_blocks_data;
3849 dm_block_t nr_free_blocks_metadata;
3850 dm_block_t nr_blocks_data;
3851 dm_block_t nr_blocks_metadata;
3852 dm_block_t held_root;
Joe Thornbera9537db2018-09-10 16:50:09 +01003853 enum pool_mode mode;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003854 char buf[BDEVNAME_SIZE];
3855 char buf2[BDEVNAME_SIZE];
3856 struct pool_c *pt = ti->private;
3857 struct pool *pool = pt->pool;
3858
3859 switch (type) {
3860 case STATUSTYPE_INFO:
Joe Thornbere49e5822012-07-27 15:08:16 +01003861 if (get_pool_mode(pool) == PM_FAIL) {
3862 DMEMIT("Fail");
3863 break;
3864 }
3865
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01003866 /* Commit to ensure statistics aren't out-of-date */
3867 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
Joe Thornber020cc3b2013-12-04 15:05:36 -05003868 (void) commit(pool);
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01003869
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003870 r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id);
3871 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003872 DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
3873 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003874 goto err;
3875 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003876
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003877 r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata);
3878 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003879 DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
3880 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003881 goto err;
3882 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003883
3884 r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003885 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003886 DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
3887 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003888 goto err;
3889 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003890
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003891 r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data);
3892 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003893 DMERR("%s: dm_pool_get_free_block_count returned %d",
3894 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003895 goto err;
3896 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003897
3898 r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003899 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003900 DMERR("%s: dm_pool_get_data_dev_size returned %d",
3901 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003902 goto err;
3903 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003904
Joe Thornbercc8394d2012-06-03 00:30:01 +01003905 r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003906 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003907 DMERR("%s: dm_pool_get_metadata_snap returned %d",
3908 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003909 goto err;
3910 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003911
3912 DMEMIT("%llu %llu/%llu %llu/%llu ",
3913 (unsigned long long)transaction_id,
3914 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
3915 (unsigned long long)nr_blocks_metadata,
3916 (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
3917 (unsigned long long)nr_blocks_data);
3918
3919 if (held_root)
Joe Thornbere49e5822012-07-27 15:08:16 +01003920 DMEMIT("%llu ", held_root);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003921 else
Joe Thornbere49e5822012-07-27 15:08:16 +01003922 DMEMIT("- ");
3923
Joe Thornbera9537db2018-09-10 16:50:09 +01003924 mode = get_pool_mode(pool);
3925 if (mode == PM_OUT_OF_DATA_SPACE)
Joe Thornber3e1a0692014-03-03 16:03:26 +00003926 DMEMIT("out_of_data_space ");
Joe Thornbera9537db2018-09-10 16:50:09 +01003927 else if (is_read_only_pool_mode(mode))
Joe Thornbere49e5822012-07-27 15:08:16 +01003928 DMEMIT("ro ");
3929 else
3930 DMEMIT("rw ");
3931
Mike Snitzer018debe2012-12-21 20:23:32 +00003932 if (!pool->pf.discard_enabled)
Mike Snitzer787a996c2013-12-06 16:21:43 -05003933 DMEMIT("ignore_discard ");
Mike Snitzer018debe2012-12-21 20:23:32 +00003934 else if (pool->pf.discard_passdown)
Mike Snitzer787a996c2013-12-06 16:21:43 -05003935 DMEMIT("discard_passdown ");
Joe Thornbere49e5822012-07-27 15:08:16 +01003936 else
Mike Snitzer787a996c2013-12-06 16:21:43 -05003937 DMEMIT("no_discard_passdown ");
3938
3939 if (pool->pf.error_if_no_space)
3940 DMEMIT("error_if_no_space ");
3941 else
3942 DMEMIT("queue_if_no_space ");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003943
Mike Snitzere4c78e22015-07-15 11:40:24 -04003944 if (dm_pool_metadata_needs_check(pool->pmd))
3945 DMEMIT("needs_check ");
3946 else
3947 DMEMIT("- ");
3948
Joe Thornber991d9fa2011-10-31 20:21:18 +00003949 break;
3950
3951 case STATUSTYPE_TABLE:
3952 DMEMIT("%s %s %lu %llu ",
3953 format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
3954 format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
3955 (unsigned long)pool->sectors_per_block,
3956 (unsigned long long)pt->low_water_blocks);
Mike Snitzer0424caa2012-09-26 23:45:47 +01003957 emit_flags(&pt->requested_pf, result, sz, maxlen);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003958 break;
3959 }
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003960 return;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003961
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003962err:
3963 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003964}
3965
3966static int pool_iterate_devices(struct dm_target *ti,
3967 iterate_devices_callout_fn fn, void *data)
3968{
3969 struct pool_c *pt = ti->private;
3970
3971 return fn(ti, pt->data_dev, 0, ti->len, data);
3972}
3973
Joe Thornber991d9fa2011-10-31 20:21:18 +00003974static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
3975{
3976 struct pool_c *pt = ti->private;
3977 struct pool *pool = pt->pool;
Mike Snitzer604ea902014-10-09 18:43:25 -04003978 sector_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
3979
3980 /*
Mike Snitzerd200c302014-11-20 18:07:43 -05003981 * If max_sectors is smaller than pool->sectors_per_block adjust it
3982 * to the highest possible power-of-2 factor of pool->sectors_per_block.
3983 * This is especially beneficial when the pool's data device is a RAID
3984 * device that has a full stripe width that matches pool->sectors_per_block
3985 * -- because even though partial RAID stripe-sized IOs will be issued to a
3986 * single RAID stripe; when aggregated they will end on a full RAID stripe
3987 * boundary.. which avoids additional partial RAID stripe writes cascading
Mike Snitzer604ea902014-10-09 18:43:25 -04003988 */
Mike Snitzer604ea902014-10-09 18:43:25 -04003989 if (limits->max_sectors < pool->sectors_per_block) {
3990 while (!is_factor(pool->sectors_per_block, limits->max_sectors)) {
3991 if ((limits->max_sectors & (limits->max_sectors - 1)) == 0)
3992 limits->max_sectors--;
3993 limits->max_sectors = rounddown_pow_of_two(limits->max_sectors);
3994 }
Mike Snitzer604ea902014-10-09 18:43:25 -04003995 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003996
Mike Snitzer0cc67cd2013-08-20 15:02:41 -04003997 /*
3998 * If the system-determined stacked limits are compatible with the
3999 * pool's blocksize (io_opt is a factor) do not override them.
4000 */
4001 if (io_opt_sectors < pool->sectors_per_block ||
Mike Snitzer604ea902014-10-09 18:43:25 -04004002 !is_factor(io_opt_sectors, pool->sectors_per_block)) {
4003 if (is_factor(pool->sectors_per_block, limits->max_sectors))
4004 blk_limits_io_min(limits, limits->max_sectors << SECTOR_SHIFT);
4005 else
4006 blk_limits_io_min(limits, pool->sectors_per_block << SECTOR_SHIFT);
Mike Snitzer0cc67cd2013-08-20 15:02:41 -04004007 blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
4008 }
Mike Snitzer0424caa2012-09-26 23:45:47 +01004009
4010 /*
4011 * pt->adjusted_pf is a staging area for the actual features to use.
4012 * They get transferred to the live pool in bind_control_target()
4013 * called from pool_preresume().
4014 */
Mike Snitzerb60ab992013-09-19 18:49:11 -04004015 if (!pt->adjusted_pf.discard_enabled) {
4016 /*
4017 * Must explicitly disallow stacking discard limits otherwise the
4018 * block layer will stack them if pool's data device has support.
4019 * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the
4020 * user to see that, so make sure to set all discard limits to 0.
4021 */
4022 limits->discard_granularity = 0;
Mike Snitzer0424caa2012-09-26 23:45:47 +01004023 return;
Mike Snitzerb60ab992013-09-19 18:49:11 -04004024 }
Mike Snitzer0424caa2012-09-26 23:45:47 +01004025
4026 disable_passdown_if_not_supported(pt);
4027
Joe Thornber34fbcf62015-04-16 12:58:35 +01004028 /*
4029 * The pool uses the same discard limits as the underlying data
4030 * device. DM core has already set this up.
4031 */
Joe Thornber991d9fa2011-10-31 20:21:18 +00004032}
4033
4034static struct target_type pool_target = {
4035 .name = "thin-pool",
4036 .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
4037 DM_TARGET_IMMUTABLE,
Joe Thornber202bae52016-05-04 14:12:42 -04004038 .version = {1, 19, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00004039 .module = THIS_MODULE,
4040 .ctr = pool_ctr,
4041 .dtr = pool_dtr,
4042 .map = pool_map,
Mike Snitzer80e96c52014-11-07 15:09:46 -05004043 .presuspend = pool_presuspend,
4044 .presuspend_undo = pool_presuspend_undo,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004045 .postsuspend = pool_postsuspend,
4046 .preresume = pool_preresume,
4047 .resume = pool_resume,
4048 .message = pool_message,
4049 .status = pool_status,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004050 .iterate_devices = pool_iterate_devices,
4051 .io_hints = pool_io_hints,
4052};
4053
4054/*----------------------------------------------------------------
4055 * Thin target methods
4056 *--------------------------------------------------------------*/
Joe Thornberb10ebd32014-04-08 11:29:01 +01004057static void thin_get(struct thin_c *tc)
4058{
4059 atomic_inc(&tc->refcount);
4060}
4061
4062static void thin_put(struct thin_c *tc)
4063{
4064 if (atomic_dec_and_test(&tc->refcount))
4065 complete(&tc->can_destroy);
4066}
4067
Joe Thornber991d9fa2011-10-31 20:21:18 +00004068static void thin_dtr(struct dm_target *ti)
4069{
4070 struct thin_c *tc = ti->private;
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004071 unsigned long flags;
4072
4073 spin_lock_irqsave(&tc->pool->lock, flags);
4074 list_del_rcu(&tc->list);
4075 spin_unlock_irqrestore(&tc->pool->lock, flags);
4076 synchronize_rcu();
Joe Thornber991d9fa2011-10-31 20:21:18 +00004077
Mikulas Patocka17181fb2014-11-05 17:00:13 -05004078 thin_put(tc);
4079 wait_for_completion(&tc->can_destroy);
4080
Joe Thornber991d9fa2011-10-31 20:21:18 +00004081 mutex_lock(&dm_thin_pool_table.mutex);
4082
4083 __pool_dec(tc->pool);
4084 dm_pool_close_thin_device(tc->td);
4085 dm_put_device(ti, tc->pool_dev);
Joe Thornber2dd9c252012-03-28 18:41:28 +01004086 if (tc->origin_dev)
4087 dm_put_device(ti, tc->origin_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004088 kfree(tc);
4089
4090 mutex_unlock(&dm_thin_pool_table.mutex);
4091}
4092
4093/*
4094 * Thin target parameters:
4095 *
Joe Thornber2dd9c252012-03-28 18:41:28 +01004096 * <pool_dev> <dev_id> [origin_dev]
Joe Thornber991d9fa2011-10-31 20:21:18 +00004097 *
4098 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
4099 * dev_id: the internal device identifier
Joe Thornber2dd9c252012-03-28 18:41:28 +01004100 * origin_dev: a device external to the pool that should act as the origin
Joe Thornber67e2e2b2012-03-28 18:41:29 +01004101 *
4102 * If the pool device has discards disabled, they get disabled for the thin
4103 * device as well.
Joe Thornber991d9fa2011-10-31 20:21:18 +00004104 */
4105static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
4106{
4107 int r;
4108 struct thin_c *tc;
Joe Thornber2dd9c252012-03-28 18:41:28 +01004109 struct dm_dev *pool_dev, *origin_dev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004110 struct mapped_device *pool_md;
Joe Thornber5e3283e2014-04-08 11:08:41 +01004111 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004112
4113 mutex_lock(&dm_thin_pool_table.mutex);
4114
Joe Thornber2dd9c252012-03-28 18:41:28 +01004115 if (argc != 2 && argc != 3) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00004116 ti->error = "Invalid argument count";
4117 r = -EINVAL;
4118 goto out_unlock;
4119 }
4120
4121 tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
4122 if (!tc) {
4123 ti->error = "Out of memory";
4124 r = -ENOMEM;
4125 goto out_unlock;
4126 }
Mike Snitzer583024d2014-10-28 20:58:45 -04004127 tc->thin_md = dm_table_get_md(ti->table);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004128 spin_lock_init(&tc->lock);
Joe Thornbera374bb22014-10-10 13:43:14 +01004129 INIT_LIST_HEAD(&tc->deferred_cells);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004130 bio_list_init(&tc->deferred_bio_list);
4131 bio_list_init(&tc->retry_on_resume_list);
Mike Snitzer67324ea2014-03-21 18:33:41 -04004132 tc->sort_bio_list = RB_ROOT;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004133
Joe Thornber2dd9c252012-03-28 18:41:28 +01004134 if (argc == 3) {
4135 r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
4136 if (r) {
4137 ti->error = "Error opening origin device";
4138 goto bad_origin_dev;
4139 }
4140 tc->origin_dev = origin_dev;
4141 }
4142
Joe Thornber991d9fa2011-10-31 20:21:18 +00004143 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
4144 if (r) {
4145 ti->error = "Error opening pool device";
4146 goto bad_pool_dev;
4147 }
4148 tc->pool_dev = pool_dev;
4149
4150 if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
4151 ti->error = "Invalid device id";
4152 r = -EINVAL;
4153 goto bad_common;
4154 }
4155
4156 pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
4157 if (!pool_md) {
4158 ti->error = "Couldn't get pool mapped device";
4159 r = -EINVAL;
4160 goto bad_common;
4161 }
4162
4163 tc->pool = __pool_table_lookup(pool_md);
4164 if (!tc->pool) {
4165 ti->error = "Couldn't find pool object";
4166 r = -EINVAL;
4167 goto bad_pool_lookup;
4168 }
4169 __pool_inc(tc->pool);
4170
Joe Thornbere49e5822012-07-27 15:08:16 +01004171 if (get_pool_mode(tc->pool) == PM_FAIL) {
4172 ti->error = "Couldn't open thin device, Pool is in fail mode";
Mike Snitzer1acacc02014-02-19 20:32:33 -05004173 r = -EINVAL;
Mike Snitzer80e96c52014-11-07 15:09:46 -05004174 goto bad_pool;
Joe Thornbere49e5822012-07-27 15:08:16 +01004175 }
4176
Joe Thornber991d9fa2011-10-31 20:21:18 +00004177 r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
4178 if (r) {
4179 ti->error = "Couldn't open thin internal device";
Mike Snitzer80e96c52014-11-07 15:09:46 -05004180 goto bad_pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004181 }
4182
Mike Snitzer542f9032012-07-27 15:08:00 +01004183 r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
4184 if (r)
Mike Snitzer80e96c52014-11-07 15:09:46 -05004185 goto bad;
Mike Snitzer542f9032012-07-27 15:08:00 +01004186
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00004187 ti->num_flush_bios = 1;
Joe Thornber16ad3d12012-07-27 15:08:07 +01004188 ti->flush_supported = true;
Mike Snitzer30187e12016-01-31 13:28:26 -05004189 ti->per_io_data_size = sizeof(struct dm_thin_endio_hook);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01004190
4191 /* In case the pool supports discards, pass them on. */
Mike Snitzerb60ab992013-09-19 18:49:11 -04004192 ti->discard_zeroes_data_unsupported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01004193 if (tc->pool->pf.discard_enabled) {
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01004194 ti->discards_supported = true;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00004195 ti->num_discard_bios = 1;
Joe Thornber34fbcf62015-04-16 12:58:35 +01004196 ti->split_discard_bios = false;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01004197 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00004198
Joe Thornber991d9fa2011-10-31 20:21:18 +00004199 mutex_unlock(&dm_thin_pool_table.mutex);
4200
Joe Thornber5e3283e2014-04-08 11:08:41 +01004201 spin_lock_irqsave(&tc->pool->lock, flags);
Mike Snitzer80e96c52014-11-07 15:09:46 -05004202 if (tc->pool->suspended) {
4203 spin_unlock_irqrestore(&tc->pool->lock, flags);
4204 mutex_lock(&dm_thin_pool_table.mutex); /* reacquire for __pool_dec */
4205 ti->error = "Unable to activate thin device while pool is suspended";
4206 r = -EINVAL;
4207 goto bad;
4208 }
Marc Dionne2b94e892014-12-17 07:59:59 -05004209 atomic_set(&tc->refcount, 1);
4210 init_completion(&tc->can_destroy);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004211 list_add_tail_rcu(&tc->list, &tc->pool->active_thins);
Joe Thornber5e3283e2014-04-08 11:08:41 +01004212 spin_unlock_irqrestore(&tc->pool->lock, flags);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004213 /*
4214 * This synchronize_rcu() call is needed here otherwise we risk a
4215 * wake_worker() call finding no bios to process (because the newly
4216 * added tc isn't yet visible). So this reduces latency since we
4217 * aren't then dependent on the periodic commit to wake_worker().
4218 */
4219 synchronize_rcu();
4220
Mike Snitzer80e96c52014-11-07 15:09:46 -05004221 dm_put(pool_md);
4222
Joe Thornber991d9fa2011-10-31 20:21:18 +00004223 return 0;
4224
Mike Snitzer80e96c52014-11-07 15:09:46 -05004225bad:
Mike Snitzer1acacc02014-02-19 20:32:33 -05004226 dm_pool_close_thin_device(tc->td);
Mike Snitzer80e96c52014-11-07 15:09:46 -05004227bad_pool:
Joe Thornber991d9fa2011-10-31 20:21:18 +00004228 __pool_dec(tc->pool);
4229bad_pool_lookup:
4230 dm_put(pool_md);
4231bad_common:
4232 dm_put_device(ti, tc->pool_dev);
4233bad_pool_dev:
Joe Thornber2dd9c252012-03-28 18:41:28 +01004234 if (tc->origin_dev)
4235 dm_put_device(ti, tc->origin_dev);
4236bad_origin_dev:
Joe Thornber991d9fa2011-10-31 20:21:18 +00004237 kfree(tc);
4238out_unlock:
4239 mutex_unlock(&dm_thin_pool_table.mutex);
4240
4241 return r;
4242}
4243
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00004244static int thin_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00004245{
Kent Overstreet4f024f32013-10-11 15:44:27 -07004246 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004247
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00004248 return thin_bio_map(ti, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004249}
4250
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00004251static int thin_endio(struct dm_target *ti, struct bio *bio, int err)
Joe Thornbereb2aa482012-03-28 18:41:28 +01004252{
4253 unsigned long flags;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00004254 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01004255 struct list_head work;
Mike Snitzera24c2562012-06-03 00:30:00 +01004256 struct dm_thin_new_mapping *m, *tmp;
Joe Thornbereb2aa482012-03-28 18:41:28 +01004257 struct pool *pool = h->tc->pool;
4258
4259 if (h->shared_read_entry) {
4260 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01004261 dm_deferred_entry_dec(h->shared_read_entry, &work);
Joe Thornbereb2aa482012-03-28 18:41:28 +01004262
4263 spin_lock_irqsave(&pool->lock, flags);
4264 list_for_each_entry_safe(m, tmp, &work, list) {
4265 list_del(&m->list);
Joe Thornber50f3c3e2014-06-13 13:57:09 +01004266 __complete_mapping_preparation(m);
Joe Thornbereb2aa482012-03-28 18:41:28 +01004267 }
4268 spin_unlock_irqrestore(&pool->lock, flags);
4269 }
4270
Joe Thornber104655f2012-03-28 18:41:28 +01004271 if (h->all_io_entry) {
4272 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01004273 dm_deferred_entry_dec(h->all_io_entry, &work);
Joe Thornber563af182012-12-21 20:23:31 +00004274 if (!list_empty(&work)) {
4275 spin_lock_irqsave(&pool->lock, flags);
4276 list_for_each_entry_safe(m, tmp, &work, list)
Mike Snitzerdaec3382013-12-11 14:01:20 -05004277 list_add_tail(&m->list, &pool->prepared_discards);
Joe Thornber563af182012-12-21 20:23:31 +00004278 spin_unlock_irqrestore(&pool->lock, flags);
4279 wake_worker(pool);
4280 }
Joe Thornber104655f2012-03-28 18:41:28 +01004281 }
4282
Joe Thornber34fbcf62015-04-16 12:58:35 +01004283 if (h->cell)
4284 cell_defer_no_holder(h->tc, h->cell);
4285
Joe Thornbereb2aa482012-03-28 18:41:28 +01004286 return 0;
4287}
4288
Joe Thornber738211f2014-03-03 15:52:28 +00004289static void thin_presuspend(struct dm_target *ti)
4290{
4291 struct thin_c *tc = ti->private;
4292
4293 if (dm_noflush_suspending(ti))
4294 noflush_work(tc, do_noflush_start);
4295}
4296
Joe Thornber991d9fa2011-10-31 20:21:18 +00004297static void thin_postsuspend(struct dm_target *ti)
4298{
Joe Thornber738211f2014-03-03 15:52:28 +00004299 struct thin_c *tc = ti->private;
4300
4301 /*
4302 * The dm_noflush_suspending flag has been cleared by now, so
4303 * unfortunately we must always run this.
4304 */
4305 noflush_work(tc, do_noflush_stop);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004306}
4307
Joe Thornbere5aea7b2014-06-13 14:47:24 +01004308static int thin_preresume(struct dm_target *ti)
4309{
4310 struct thin_c *tc = ti->private;
4311
4312 if (tc->origin_dev)
4313 tc->origin_size = get_dev_size(tc->origin_dev->bdev);
4314
4315 return 0;
4316}
4317
Joe Thornber991d9fa2011-10-31 20:21:18 +00004318/*
4319 * <nr mapped sectors> <highest mapped sector>
4320 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004321static void thin_status(struct dm_target *ti, status_type_t type,
4322 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00004323{
4324 int r;
4325 ssize_t sz = 0;
4326 dm_block_t mapped, highest;
4327 char buf[BDEVNAME_SIZE];
4328 struct thin_c *tc = ti->private;
4329
Joe Thornbere49e5822012-07-27 15:08:16 +01004330 if (get_pool_mode(tc->pool) == PM_FAIL) {
4331 DMEMIT("Fail");
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004332 return;
Joe Thornbere49e5822012-07-27 15:08:16 +01004333 }
4334
Joe Thornber991d9fa2011-10-31 20:21:18 +00004335 if (!tc->td)
4336 DMEMIT("-");
4337 else {
4338 switch (type) {
4339 case STATUSTYPE_INFO:
4340 r = dm_thin_get_mapped_count(tc->td, &mapped);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004341 if (r) {
4342 DMERR("dm_thin_get_mapped_count returned %d", r);
4343 goto err;
4344 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00004345
4346 r = dm_thin_get_highest_mapped_block(tc->td, &highest);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004347 if (r < 0) {
4348 DMERR("dm_thin_get_highest_mapped_block returned %d", r);
4349 goto err;
4350 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00004351
4352 DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
4353 if (r)
4354 DMEMIT("%llu", ((highest + 1) *
4355 tc->pool->sectors_per_block) - 1);
4356 else
4357 DMEMIT("-");
4358 break;
4359
4360 case STATUSTYPE_TABLE:
4361 DMEMIT("%s %lu",
4362 format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
4363 (unsigned long) tc->dev_id);
Joe Thornber2dd9c252012-03-28 18:41:28 +01004364 if (tc->origin_dev)
4365 DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
Joe Thornber991d9fa2011-10-31 20:21:18 +00004366 break;
4367 }
4368 }
4369
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004370 return;
4371
4372err:
4373 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00004374}
4375
4376static int thin_iterate_devices(struct dm_target *ti,
4377 iterate_devices_callout_fn fn, void *data)
4378{
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004379 sector_t blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004380 struct thin_c *tc = ti->private;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004381 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004382
4383 /*
4384 * We can't call dm_pool_get_data_dev_size() since that blocks. So
4385 * we follow a more convoluted path through to the pool's target.
4386 */
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004387 if (!pool->ti)
Joe Thornber991d9fa2011-10-31 20:21:18 +00004388 return 0; /* nothing is bound */
4389
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004390 blocks = pool->ti->len;
4391 (void) sector_div(blocks, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004392 if (blocks)
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004393 return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004394
4395 return 0;
4396}
4397
Joe Thornber34fbcf62015-04-16 12:58:35 +01004398static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)
4399{
4400 struct thin_c *tc = ti->private;
4401 struct pool *pool = tc->pool;
Mike Snitzer21607672015-09-08 08:56:13 -04004402
Mike Snitzer0fcb04d2015-11-23 13:44:38 -05004403 if (!pool->pf.discard_enabled)
4404 return;
Joe Thornber34fbcf62015-04-16 12:58:35 +01004405
4406 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
4407 limits->max_discard_sectors = 2048 * 1024 * 16; /* 16G */
4408}
4409
Joe Thornber991d9fa2011-10-31 20:21:18 +00004410static struct target_type thin_target = {
4411 .name = "thin",
Joe Thornber202bae52016-05-04 14:12:42 -04004412 .version = {1, 19, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00004413 .module = THIS_MODULE,
4414 .ctr = thin_ctr,
4415 .dtr = thin_dtr,
4416 .map = thin_map,
Joe Thornbereb2aa482012-03-28 18:41:28 +01004417 .end_io = thin_endio,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01004418 .preresume = thin_preresume,
Joe Thornber738211f2014-03-03 15:52:28 +00004419 .presuspend = thin_presuspend,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004420 .postsuspend = thin_postsuspend,
4421 .status = thin_status,
4422 .iterate_devices = thin_iterate_devices,
Joe Thornber34fbcf62015-04-16 12:58:35 +01004423 .io_hints = thin_io_hints,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004424};
4425
4426/*----------------------------------------------------------------*/
4427
4428static int __init dm_thin_init(void)
4429{
4430 int r;
4431
4432 pool_table_init();
4433
4434 r = dm_register_target(&thin_target);
4435 if (r)
4436 return r;
4437
4438 r = dm_register_target(&pool_target);
4439 if (r)
Mike Snitzera24c2562012-06-03 00:30:00 +01004440 goto bad_pool_target;
4441
4442 r = -ENOMEM;
4443
Mike Snitzera24c2562012-06-03 00:30:00 +01004444 _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
4445 if (!_new_mapping_cache)
4446 goto bad_new_mapping_cache;
4447
Mike Snitzera24c2562012-06-03 00:30:00 +01004448 return 0;
4449
Mike Snitzera24c2562012-06-03 00:30:00 +01004450bad_new_mapping_cache:
Mike Snitzera24c2562012-06-03 00:30:00 +01004451 dm_unregister_target(&pool_target);
4452bad_pool_target:
4453 dm_unregister_target(&thin_target);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004454
4455 return r;
4456}
4457
4458static void dm_thin_exit(void)
4459{
4460 dm_unregister_target(&thin_target);
4461 dm_unregister_target(&pool_target);
Mike Snitzera24c2562012-06-03 00:30:00 +01004462
Mike Snitzera24c2562012-06-03 00:30:00 +01004463 kmem_cache_destroy(_new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004464}
4465
4466module_init(dm_thin_init);
4467module_exit(dm_thin_exit);
4468
Mike Snitzer80c57892014-05-20 13:38:33 -04004469module_param_named(no_space_timeout, no_space_timeout_secs, uint, S_IRUGO | S_IWUSR);
4470MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds");
4471
Alasdair G Kergon7cab8bf2012-05-12 01:43:19 +01004472MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
Joe Thornber991d9fa2011-10-31 20:21:18 +00004473MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
4474MODULE_LICENSE("GPL");