blob: 985baee3a678e8fa57a7d3a21212d67c7514d182 [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"
Joe Thornber742c8fd2016-10-21 10:06:40 -04008#include "dm-bio-prison-v1.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/*
Mike Snitzerf6c36752018-12-11 13:31:40 -0500198 * The pool runs in various 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 Thornber3ab91822018-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 Thornber3ab91822018-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;
Mikulas Patocka873937e2020-01-13 15:04:37 -0500234 struct block_device *data_dev;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000235 struct block_device *md_dev;
236 struct dm_pool_metadata *pmd;
237
Joe Thornber991d9fa2011-10-31 20:21:18 +0000238 dm_block_t low_water_blocks;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100239 uint32_t sectors_per_block;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100240 int sectors_per_block_shift;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000241
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100242 struct pool_features pf;
Joe Thornber88a66212013-12-04 20:16:12 -0500243 bool low_water_triggered:1; /* A dm event has been sent */
Mike Snitzer80e96c52014-11-07 15:09:46 -0500244 bool suspended:1;
Mike Snitzerc3667cc2016-03-10 11:31:35 -0500245 bool out_of_data_space:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000246
Mike Snitzer44feb382012-10-12 21:02:10 +0100247 struct dm_bio_prison *prison;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000248 struct dm_kcopyd_client *copier;
249
Mike Snitzer72d711c2018-05-22 18:26:20 -0400250 struct work_struct worker;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000251 struct workqueue_struct *wq;
Joe Thornber7d327fe2014-10-06 15:45:59 +0100252 struct throttle throttle;
Joe Thornber905e51b2012-03-28 18:41:27 +0100253 struct delayed_work waker;
Joe Thornber85ad6432014-05-09 15:59:38 +0100254 struct delayed_work no_space_timeout;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000255
Joe Thornber905e51b2012-03-28 18:41:27 +0100256 unsigned long last_commit_jiffies;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100257 unsigned ref_count;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000258
259 spinlock_t lock;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000260 struct bio_list deferred_flush_bios;
Nikos Tsironis4ae280b2019-02-14 20:38:47 +0200261 struct bio_list deferred_flush_completions;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000262 struct list_head prepared_mappings;
Joe Thornber104655f2012-03-28 18:41:28 +0100263 struct list_head prepared_discards;
Joe Thornber2a0fbff2016-07-01 14:00:02 +0100264 struct list_head prepared_discards_pt2;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400265 struct list_head active_thins;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000266
Mike Snitzer44feb382012-10-12 21:02:10 +0100267 struct dm_deferred_set *shared_read_ds;
268 struct dm_deferred_set *all_io_ds;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000269
Mike Snitzera24c2562012-06-03 00:30:00 +0100270 struct dm_thin_new_mapping *next_mapping;
Joe Thornbere49e5822012-07-27 15:08:16 +0100271
272 process_bio_fn process_bio;
273 process_bio_fn process_discard;
274
Joe Thornbera374bb22014-10-10 13:43:14 +0100275 process_cell_fn process_cell;
276 process_cell_fn process_discard_cell;
277
Joe Thornbere49e5822012-07-27 15:08:16 +0100278 process_mapping_fn process_prepared_mapping;
279 process_mapping_fn process_prepared_discard;
Joe Thornber2a0fbff2016-07-01 14:00:02 +0100280 process_mapping_fn process_prepared_discard_pt2;
Joe Thornberac4c3f32014-10-10 16:42:10 +0100281
Joe Thornbera822c832015-07-03 10:22:42 +0100282 struct dm_bio_prison_cell **cell_sort_array;
Mike Snitzer72d711c2018-05-22 18:26:20 -0400283
284 mempool_t mapping_pool;
Mikulas Patockaf06c03d2020-01-13 15:41:53 -0500285
286 struct bio flush_bio;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000287};
288
Joe Thornberb5330652013-12-04 19:51:33 -0500289static void metadata_operation_failed(struct pool *pool, const char *op, int r);
Joe Thornbere49e5822012-07-27 15:08:16 +0100290
Mike Snitzerf6c36752018-12-11 13:31:40 -0500291static enum pool_mode get_pool_mode(struct pool *pool)
292{
293 return pool->pf.mode;
294}
295
296static void notify_of_pool_mode_change(struct pool *pool)
297{
298 const char *descs[] = {
299 "write",
300 "out-of-data-space",
301 "read-only",
302 "read-only",
303 "fail"
304 };
305 const char *extra_desc = NULL;
306 enum pool_mode mode = get_pool_mode(pool);
307
308 if (mode == PM_OUT_OF_DATA_SPACE) {
309 if (!pool->pf.error_if_no_space)
310 extra_desc = " (queue IO)";
311 else
312 extra_desc = " (error IO)";
313 }
314
315 dm_table_event(pool->ti->table);
316 DMINFO("%s: switching pool to %s%s mode",
317 dm_device_name(pool->pool_md),
318 descs[(int)mode], extra_desc ? : "");
319}
320
Joe Thornber991d9fa2011-10-31 20:21:18 +0000321/*
322 * Target context for a pool.
323 */
324struct pool_c {
325 struct dm_target *ti;
326 struct pool *pool;
327 struct dm_dev *data_dev;
328 struct dm_dev *metadata_dev;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000329
330 dm_block_t low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +0100331 struct pool_features requested_pf; /* Features requested during table load */
332 struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000333};
334
335/*
336 * Target context for a thin.
337 */
338struct thin_c {
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400339 struct list_head list;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000340 struct dm_dev *pool_dev;
Joe Thornber2dd9c252012-03-28 18:41:28 +0100341 struct dm_dev *origin_dev;
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100342 sector_t origin_size;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000343 dm_thin_id dev_id;
344
345 struct pool *pool;
346 struct dm_thin_device *td;
Mike Snitzer583024d2014-10-28 20:58:45 -0400347 struct mapped_device *thin_md;
348
Joe Thornber738211f2014-03-03 15:52:28 +0000349 bool requeue_mode:1;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400350 spinlock_t lock;
Joe Thornbera374bb22014-10-10 13:43:14 +0100351 struct list_head deferred_cells;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400352 struct bio_list deferred_bio_list;
353 struct bio_list retry_on_resume_list;
Mike Snitzer67324ea2014-03-21 18:33:41 -0400354 struct rb_root sort_bio_list; /* sorted list of deferred bios */
Joe Thornberb10ebd32014-04-08 11:29:01 +0100355
356 /*
357 * Ensures the thin is not destroyed until the worker has finished
358 * iterating the active_thins list.
359 */
John Pittman22d4c292018-08-23 13:35:55 -0400360 refcount_t refcount;
Joe Thornberb10ebd32014-04-08 11:29:01 +0100361 struct completion can_destroy;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000362};
363
364/*----------------------------------------------------------------*/
365
Joe Thornber34fbcf62015-04-16 12:58:35 +0100366static bool block_size_is_power_of_two(struct pool *pool)
367{
368 return pool->sectors_per_block_shift >= 0;
369}
370
371static sector_t block_to_sectors(struct pool *pool, dm_block_t b)
372{
373 return block_size_is_power_of_two(pool) ?
374 (b << pool->sectors_per_block_shift) :
375 (b * pool->sectors_per_block);
376}
377
Joe Thornber202bae52016-05-04 14:12:42 -0400378/*----------------------------------------------------------------*/
379
380struct discard_op {
381 struct thin_c *tc;
382 struct blk_plug plug;
383 struct bio *parent_bio;
384 struct bio *bio;
385};
386
387static void begin_discard(struct discard_op *op, struct thin_c *tc, struct bio *parent)
Joe Thornber34fbcf62015-04-16 12:58:35 +0100388{
Joe Thornber202bae52016-05-04 14:12:42 -0400389 BUG_ON(!parent);
390
391 op->tc = tc;
392 blk_start_plug(&op->plug);
393 op->parent_bio = parent;
394 op->bio = NULL;
395}
396
397static int issue_discard(struct discard_op *op, dm_block_t data_b, dm_block_t data_e)
398{
399 struct thin_c *tc = op->tc;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100400 sector_t s = block_to_sectors(tc->pool, data_b);
401 sector_t len = block_to_sectors(tc->pool, data_e - data_b);
402
Joe Thornber202bae52016-05-04 14:12:42 -0400403 return __blkdev_issue_discard(tc->pool_dev->bdev, s, len,
Mike Christie469e3212016-06-05 14:31:49 -0500404 GFP_NOWAIT, 0, &op->bio);
Joe Thornber202bae52016-05-04 14:12:42 -0400405}
406
407static void end_discard(struct discard_op *op, int r)
408{
409 if (op->bio) {
410 /*
411 * Even if one of the calls to issue_discard failed, we
412 * need to wait for the chain to complete.
413 */
414 bio_chain(op->bio, op->parent_bio);
Mike Christiee6047142016-06-05 14:32:04 -0500415 bio_set_op_attrs(op->bio, REQ_OP_DISCARD, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -0500416 submit_bio(op->bio);
Mike Snitzer3dba53a92016-05-02 20:16:21 -0400417 }
Mike Snitzer3dba53a92016-05-02 20:16:21 -0400418
Joe Thornber202bae52016-05-04 14:12:42 -0400419 blk_finish_plug(&op->plug);
420
421 /*
422 * Even if r is set, there could be sub discards in flight that we
423 * need to wait for.
424 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200425 if (r && !op->parent_bio->bi_status)
426 op->parent_bio->bi_status = errno_to_blk_status(r);
Joe Thornber202bae52016-05-04 14:12:42 -0400427 bio_endio(op->parent_bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +0100428}
429
430/*----------------------------------------------------------------*/
431
Joe Thornber025b9682013-03-01 22:45:50 +0000432/*
433 * wake_worker() is used when new work is queued and when pool_resume is
434 * ready to continue deferred IO processing.
435 */
436static void wake_worker(struct pool *pool)
437{
438 queue_work(pool->wq, &pool->worker);
439}
440
441/*----------------------------------------------------------------*/
442
Joe Thornber6beca5e2013-03-01 22:45:50 +0000443static int bio_detain(struct pool *pool, struct dm_cell_key *key, struct bio *bio,
444 struct dm_bio_prison_cell **cell_result)
445{
446 int r;
447 struct dm_bio_prison_cell *cell_prealloc;
448
449 /*
450 * Allocate a cell from the prison's mempool.
451 * This might block but it can't fail.
452 */
453 cell_prealloc = dm_bio_prison_alloc_cell(pool->prison, GFP_NOIO);
454
455 r = dm_bio_detain(pool->prison, key, bio, cell_prealloc, cell_result);
456 if (r)
457 /*
458 * We reused an old cell; we can get rid of
459 * the new one.
460 */
461 dm_bio_prison_free_cell(pool->prison, cell_prealloc);
462
463 return r;
464}
465
466static void cell_release(struct pool *pool,
467 struct dm_bio_prison_cell *cell,
468 struct bio_list *bios)
469{
470 dm_cell_release(pool->prison, cell, bios);
471 dm_bio_prison_free_cell(pool->prison, cell);
472}
473
Joe Thornber2d759a42014-10-10 15:27:16 +0100474static void cell_visit_release(struct pool *pool,
475 void (*fn)(void *, struct dm_bio_prison_cell *),
476 void *context,
477 struct dm_bio_prison_cell *cell)
478{
479 dm_cell_visit_release(pool->prison, fn, context, cell);
480 dm_bio_prison_free_cell(pool->prison, cell);
481}
482
Joe Thornber6beca5e2013-03-01 22:45:50 +0000483static void cell_release_no_holder(struct pool *pool,
484 struct dm_bio_prison_cell *cell,
485 struct bio_list *bios)
486{
487 dm_cell_release_no_holder(pool->prison, cell, bios);
488 dm_bio_prison_free_cell(pool->prison, cell);
489}
490
Mike Snitzeraf918052014-05-22 14:32:51 -0400491static void cell_error_with_code(struct pool *pool,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200492 struct dm_bio_prison_cell *cell, blk_status_t error_code)
Joe Thornber6beca5e2013-03-01 22:45:50 +0000493{
Mike Snitzeraf918052014-05-22 14:32:51 -0400494 dm_cell_error(pool->prison, cell, error_code);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000495 dm_bio_prison_free_cell(pool->prison, cell);
496}
497
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200498static blk_status_t get_pool_io_error_code(struct pool *pool)
Mike Snitzerc3667cc2016-03-10 11:31:35 -0500499{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200500 return pool->out_of_data_space ? BLK_STS_NOSPC : BLK_STS_IOERR;
Mike Snitzerc3667cc2016-03-10 11:31:35 -0500501}
502
Mike Snitzeraf918052014-05-22 14:32:51 -0400503static void cell_error(struct pool *pool, struct dm_bio_prison_cell *cell)
504{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200505 cell_error_with_code(pool, cell, get_pool_io_error_code(pool));
Mike Snitzeraf918052014-05-22 14:32:51 -0400506}
507
Joe Thornbera374bb22014-10-10 13:43:14 +0100508static void cell_success(struct pool *pool, struct dm_bio_prison_cell *cell)
509{
510 cell_error_with_code(pool, cell, 0);
511}
512
513static void cell_requeue(struct pool *pool, struct dm_bio_prison_cell *cell)
514{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200515 cell_error_with_code(pool, cell, BLK_STS_DM_REQUEUE);
Joe Thornbera374bb22014-10-10 13:43:14 +0100516}
517
Joe Thornber6beca5e2013-03-01 22:45:50 +0000518/*----------------------------------------------------------------*/
519
Joe Thornber991d9fa2011-10-31 20:21:18 +0000520/*
521 * A global list of pools that uses a struct mapped_device as a key.
522 */
523static struct dm_thin_pool_table {
524 struct mutex mutex;
525 struct list_head pools;
526} dm_thin_pool_table;
527
528static void pool_table_init(void)
529{
530 mutex_init(&dm_thin_pool_table.mutex);
531 INIT_LIST_HEAD(&dm_thin_pool_table.pools);
532}
533
Mike Snitzerd5ffebd2018-01-05 21:17:20 -0500534static void pool_table_exit(void)
535{
536 mutex_destroy(&dm_thin_pool_table.mutex);
537}
538
Joe Thornber991d9fa2011-10-31 20:21:18 +0000539static void __pool_table_insert(struct pool *pool)
540{
541 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
542 list_add(&pool->list, &dm_thin_pool_table.pools);
543}
544
545static void __pool_table_remove(struct pool *pool)
546{
547 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
548 list_del(&pool->list);
549}
550
551static struct pool *__pool_table_lookup(struct mapped_device *md)
552{
553 struct pool *pool = NULL, *tmp;
554
555 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
556
557 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
558 if (tmp->pool_md == md) {
559 pool = tmp;
560 break;
561 }
562 }
563
564 return pool;
565}
566
567static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
568{
569 struct pool *pool = NULL, *tmp;
570
571 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
572
573 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
574 if (tmp->md_dev == md_dev) {
575 pool = tmp;
576 break;
577 }
578 }
579
580 return pool;
581}
582
583/*----------------------------------------------------------------*/
584
Mike Snitzera24c2562012-06-03 00:30:00 +0100585struct dm_thin_endio_hook {
Joe Thornbereb2aa482012-03-28 18:41:28 +0100586 struct thin_c *tc;
Mike Snitzer44feb382012-10-12 21:02:10 +0100587 struct dm_deferred_entry *shared_read_entry;
588 struct dm_deferred_entry *all_io_entry;
Mike Snitzera24c2562012-06-03 00:30:00 +0100589 struct dm_thin_new_mapping *overwrite_mapping;
Mike Snitzer67324ea2014-03-21 18:33:41 -0400590 struct rb_node rb_node;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100591 struct dm_bio_prison_cell *cell;
Joe Thornbereb2aa482012-03-28 18:41:28 +0100592};
593
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400594static void __merge_bio_list(struct bio_list *bios, struct bio_list *master)
595{
596 bio_list_merge(bios, master);
597 bio_list_init(master);
598}
599
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200600static void error_bio_list(struct bio_list *bios, blk_status_t error)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000601{
602 struct bio *bio;
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400603
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200604 while ((bio = bio_list_pop(bios))) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200605 bio->bi_status = error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200606 bio_endio(bio);
607 }
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400608}
609
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200610static void error_thin_bio_list(struct thin_c *tc, struct bio_list *master,
611 blk_status_t error)
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400612{
Joe Thornber991d9fa2011-10-31 20:21:18 +0000613 struct bio_list bios;
614
615 bio_list_init(&bios);
Joe Thornber18adc572014-03-03 15:46:42 +0000616
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -0400617 spin_lock_irq(&tc->lock);
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400618 __merge_bio_list(&bios, master);
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -0400619 spin_unlock_irq(&tc->lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000620
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400621 error_bio_list(&bios, error);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000622}
623
Joe Thornbera374bb22014-10-10 13:43:14 +0100624static void requeue_deferred_cells(struct thin_c *tc)
625{
626 struct pool *pool = tc->pool;
Joe Thornbera374bb22014-10-10 13:43:14 +0100627 struct list_head cells;
628 struct dm_bio_prison_cell *cell, *tmp;
629
630 INIT_LIST_HEAD(&cells);
631
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -0400632 spin_lock_irq(&tc->lock);
Joe Thornbera374bb22014-10-10 13:43:14 +0100633 list_splice_init(&tc->deferred_cells, &cells);
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -0400634 spin_unlock_irq(&tc->lock);
Joe Thornbera374bb22014-10-10 13:43:14 +0100635
636 list_for_each_entry_safe(cell, tmp, &cells, user_list)
637 cell_requeue(pool, cell);
638}
639
Joe Thornber991d9fa2011-10-31 20:21:18 +0000640static void requeue_io(struct thin_c *tc)
641{
Joe Thornber3e1a0692014-03-03 16:03:26 +0000642 struct bio_list bios;
643
644 bio_list_init(&bios);
645
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -0400646 spin_lock_irq(&tc->lock);
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400647 __merge_bio_list(&bios, &tc->deferred_bio_list);
648 __merge_bio_list(&bios, &tc->retry_on_resume_list);
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -0400649 spin_unlock_irq(&tc->lock);
Joe Thornber3e1a0692014-03-03 16:03:26 +0000650
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200651 error_bio_list(&bios, BLK_STS_DM_REQUEUE);
Mike Snitzer42d6a8c2014-10-19 07:52:44 -0400652 requeue_deferred_cells(tc);
Joe Thornber3e1a0692014-03-03 16:03:26 +0000653}
654
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200655static void error_retry_list_with_code(struct pool *pool, blk_status_t error)
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400656{
657 struct thin_c *tc;
658
659 rcu_read_lock();
660 list_for_each_entry_rcu(tc, &pool->active_thins, list)
Mike Snitzer0a927c22015-07-21 13:20:46 -0400661 error_thin_bio_list(tc, &tc->retry_on_resume_list, error);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400662 rcu_read_unlock();
663}
664
Mike Snitzer0a927c22015-07-21 13:20:46 -0400665static void error_retry_list(struct pool *pool)
666{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200667 error_retry_list_with_code(pool, get_pool_io_error_code(pool));
Mike Snitzer0a927c22015-07-21 13:20:46 -0400668}
669
Joe Thornber991d9fa2011-10-31 20:21:18 +0000670/*
671 * This section of code contains the logic for processing a thin device's IO.
672 * Much of the code depends on pool object resources (lists, workqueues, etc)
673 * but most is exclusively called from the thin target rather than the thin-pool
674 * target.
675 */
676
677static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
678{
Mike Snitzer58f77a22013-03-01 22:45:45 +0000679 struct pool *pool = tc->pool;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700680 sector_t block_nr = bio->bi_iter.bi_sector;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100681
Mike Snitzer58f77a22013-03-01 22:45:45 +0000682 if (block_size_is_power_of_two(pool))
683 block_nr >>= pool->sectors_per_block_shift;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100684 else
Mike Snitzer58f77a22013-03-01 22:45:45 +0000685 (void) sector_div(block_nr, pool->sectors_per_block);
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100686
687 return block_nr;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000688}
689
Joe Thornber34fbcf62015-04-16 12:58:35 +0100690/*
691 * Returns the _complete_ blocks that this bio covers.
692 */
693static void get_bio_block_range(struct thin_c *tc, struct bio *bio,
694 dm_block_t *begin, dm_block_t *end)
695{
696 struct pool *pool = tc->pool;
697 sector_t b = bio->bi_iter.bi_sector;
698 sector_t e = b + (bio->bi_iter.bi_size >> SECTOR_SHIFT);
699
700 b += pool->sectors_per_block - 1ull; /* so we round up */
701
702 if (block_size_is_power_of_two(pool)) {
703 b >>= pool->sectors_per_block_shift;
704 e >>= pool->sectors_per_block_shift;
705 } else {
706 (void) sector_div(b, pool->sectors_per_block);
707 (void) sector_div(e, pool->sectors_per_block);
708 }
709
710 if (e < b)
711 /* Can happen if the bio is within a single block. */
712 e = b;
713
714 *begin = b;
715 *end = e;
716}
717
Joe Thornber991d9fa2011-10-31 20:21:18 +0000718static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
719{
720 struct pool *pool = tc->pool;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700721 sector_t bi_sector = bio->bi_iter.bi_sector;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000722
Christoph Hellwig74d46992017-08-23 19:10:32 +0200723 bio_set_dev(bio, tc->pool_dev->bdev);
Mike Snitzer58f77a22013-03-01 22:45:45 +0000724 if (block_size_is_power_of_two(pool))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700725 bio->bi_iter.bi_sector =
726 (block << pool->sectors_per_block_shift) |
727 (bi_sector & (pool->sectors_per_block - 1));
Mike Snitzer58f77a22013-03-01 22:45:45 +0000728 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700729 bio->bi_iter.bi_sector = (block * pool->sectors_per_block) +
Mike Snitzer58f77a22013-03-01 22:45:45 +0000730 sector_div(bi_sector, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000731}
732
Joe Thornber2dd9c252012-03-28 18:41:28 +0100733static void remap_to_origin(struct thin_c *tc, struct bio *bio)
734{
Christoph Hellwig74d46992017-08-23 19:10:32 +0200735 bio_set_dev(bio, tc->origin_dev->bdev);
Joe Thornber2dd9c252012-03-28 18:41:28 +0100736}
737
Joe Thornber4afdd682012-07-27 15:08:14 +0100738static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
739{
Christoph Hellwigf73f44e2017-01-27 08:30:47 -0700740 return op_is_flush(bio->bi_opf) &&
Joe Thornber4afdd682012-07-27 15:08:14 +0100741 dm_thin_changed_this_transaction(tc->td);
742}
743
Joe Thornbere8088072012-12-21 20:23:31 +0000744static void inc_all_io_entry(struct pool *pool, struct bio *bio)
745{
746 struct dm_thin_endio_hook *h;
747
Mike Christiee6047142016-06-05 14:32:04 -0500748 if (bio_op(bio) == REQ_OP_DISCARD)
Joe Thornbere8088072012-12-21 20:23:31 +0000749 return;
750
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000751 h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbere8088072012-12-21 20:23:31 +0000752 h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
753}
754
Joe Thornber2dd9c252012-03-28 18:41:28 +0100755static void issue(struct thin_c *tc, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000756{
757 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000758
Joe Thornbere49e5822012-07-27 15:08:16 +0100759 if (!bio_triggers_commit(tc, bio)) {
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200760 submit_bio_noacct(bio);
Joe Thornbere49e5822012-07-27 15:08:16 +0100761 return;
762 }
763
764 /*
765 * Complete bio with an error if earlier I/O caused changes to
766 * the metadata that can't be committed e.g, due to I/O errors
767 * on the metadata device.
768 */
769 if (dm_thin_aborted_changes(tc->td)) {
770 bio_io_error(bio);
771 return;
772 }
773
774 /*
775 * Batch together any bios that trigger commits and then issue a
776 * single commit for them in process_deferred_bios().
777 */
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -0400778 spin_lock_irq(&pool->lock);
Joe Thornbere49e5822012-07-27 15:08:16 +0100779 bio_list_add(&pool->deferred_flush_bios, bio);
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -0400780 spin_unlock_irq(&pool->lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000781}
782
Joe Thornber2dd9c252012-03-28 18:41:28 +0100783static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
784{
785 remap_to_origin(tc, bio);
786 issue(tc, bio);
787}
788
789static void remap_and_issue(struct thin_c *tc, struct bio *bio,
790 dm_block_t block)
791{
792 remap(tc, bio, block);
793 issue(tc, bio);
794}
795
Joe Thornber991d9fa2011-10-31 20:21:18 +0000796/*----------------------------------------------------------------*/
797
798/*
799 * Bio endio functions.
800 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100801struct dm_thin_new_mapping {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000802 struct list_head list;
803
Mike Snitzer7f214662013-12-17 13:43:31 -0500804 bool pass_discard:1;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100805 bool maybe_shared:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000806
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100807 /*
808 * Track quiescing, copying and zeroing preparation actions. When this
809 * counter hits zero the block is prepared and can be inserted into the
810 * btree.
811 */
812 atomic_t prepare_actions;
813
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200814 blk_status_t status;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000815 struct thin_c *tc;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100816 dm_block_t virt_begin, virt_end;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000817 dm_block_t data_block;
Joe Thornber34fbcf62015-04-16 12:58:35 +0100818 struct dm_bio_prison_cell *cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000819
820 /*
821 * If the bio covers the whole area of a block then we can avoid
822 * zeroing or copying. Instead this bio is hooked. The bio will
823 * still be in the cell, so care has to be taken to avoid issuing
824 * the bio twice.
825 */
826 struct bio *bio;
827 bio_end_io_t *saved_bi_end_io;
828};
829
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100830static void __complete_mapping_preparation(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000831{
832 struct pool *pool = m->tc->pool;
833
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100834 if (atomic_dec_and_test(&m->prepare_actions)) {
Mike Snitzerdaec3382013-12-11 14:01:20 -0500835 list_add_tail(&m->list, &pool->prepared_mappings);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000836 wake_worker(pool);
837 }
838}
839
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100840static void complete_mapping_preparation(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000841{
842 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000843 struct pool *pool = m->tc->pool;
844
Joe Thornber991d9fa2011-10-31 20:21:18 +0000845 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber50f3c3e2014-06-13 13:57:09 +0100846 __complete_mapping_preparation(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000847 spin_unlock_irqrestore(&pool->lock, flags);
848}
849
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100850static void copy_complete(int read_err, unsigned long write_err, void *context)
851{
852 struct dm_thin_new_mapping *m = context;
853
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200854 m->status = read_err || write_err ? BLK_STS_IOERR : 0;
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100855 complete_mapping_preparation(m);
856}
857
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200858static void overwrite_endio(struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000859{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000860 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Mike Snitzera24c2562012-06-03 00:30:00 +0100861 struct dm_thin_new_mapping *m = h->overwrite_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000862
Mike Snitzer8b908f82015-05-13 17:53:13 -0400863 bio->bi_end_io = m->saved_bi_end_io;
864
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200865 m->status = bio->bi_status;
Joe Thornbere5aea7b2014-06-13 14:47:24 +0100866 complete_mapping_preparation(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000867}
868
Joe Thornber991d9fa2011-10-31 20:21:18 +0000869/*----------------------------------------------------------------*/
870
871/*
872 * Workqueue.
873 */
874
875/*
876 * Prepared mapping jobs.
877 */
878
879/*
Joe Thornber2d759a42014-10-10 15:27:16 +0100880 * This sends the bios in the cell, except the original holder, back
881 * to the deferred_bios list.
Joe Thornber991d9fa2011-10-31 20:21:18 +0000882 */
Joe Thornberf286ba02012-12-21 20:23:33 +0000883static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000884{
Joe Thornber991d9fa2011-10-31 20:21:18 +0000885 struct pool *pool = tc->pool;
886 unsigned long flags;
Jeffle Xud256d792019-11-18 09:50:38 +0800887 int has_work;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000888
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400889 spin_lock_irqsave(&tc->lock, flags);
890 cell_release_no_holder(pool, cell, &tc->deferred_bio_list);
Jeffle Xud256d792019-11-18 09:50:38 +0800891 has_work = !bio_list_empty(&tc->deferred_bio_list);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400892 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000893
Jeffle Xud256d792019-11-18 09:50:38 +0800894 if (has_work)
895 wake_worker(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000896}
897
Joe Thornbera374bb22014-10-10 13:43:14 +0100898static void thin_defer_bio(struct thin_c *tc, struct bio *bio);
899
Joe Thornber2d759a42014-10-10 15:27:16 +0100900struct remap_info {
901 struct thin_c *tc;
902 struct bio_list defer_bios;
903 struct bio_list issue_bios;
904};
905
906static void __inc_remap_and_issue_cell(void *context,
907 struct dm_bio_prison_cell *cell)
908{
909 struct remap_info *info = context;
910 struct bio *bio;
911
912 while ((bio = bio_list_pop(&cell->bios))) {
Christoph Hellwigf73f44e2017-01-27 08:30:47 -0700913 if (op_is_flush(bio->bi_opf) || bio_op(bio) == REQ_OP_DISCARD)
Joe Thornber2d759a42014-10-10 15:27:16 +0100914 bio_list_add(&info->defer_bios, bio);
915 else {
916 inc_all_io_entry(info->tc->pool, bio);
917
918 /*
919 * We can't issue the bios with the bio prison lock
920 * held, so we add them to a list to issue on
921 * return from this function.
922 */
923 bio_list_add(&info->issue_bios, bio);
924 }
925 }
926}
927
Joe Thornbera374bb22014-10-10 13:43:14 +0100928static void inc_remap_and_issue_cell(struct thin_c *tc,
929 struct dm_bio_prison_cell *cell,
930 dm_block_t block)
931{
932 struct bio *bio;
Joe Thornber2d759a42014-10-10 15:27:16 +0100933 struct remap_info info;
Joe Thornbera374bb22014-10-10 13:43:14 +0100934
Joe Thornber2d759a42014-10-10 15:27:16 +0100935 info.tc = tc;
936 bio_list_init(&info.defer_bios);
937 bio_list_init(&info.issue_bios);
Joe Thornbera374bb22014-10-10 13:43:14 +0100938
Joe Thornber2d759a42014-10-10 15:27:16 +0100939 /*
940 * We have to be careful to inc any bios we're about to issue
941 * before the cell is released, and avoid a race with new bios
942 * being added to the cell.
943 */
944 cell_visit_release(tc->pool, __inc_remap_and_issue_cell,
945 &info, cell);
946
947 while ((bio = bio_list_pop(&info.defer_bios)))
948 thin_defer_bio(tc, bio);
949
950 while ((bio = bio_list_pop(&info.issue_bios)))
951 remap_and_issue(info.tc, bio, block);
Joe Thornbera374bb22014-10-10 13:43:14 +0100952}
953
Joe Thornbere49e5822012-07-27 15:08:16 +0100954static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
955{
Joe Thornber6beca5e2013-03-01 22:45:50 +0000956 cell_error(m->tc->pool, m->cell);
Joe Thornbere49e5822012-07-27 15:08:16 +0100957 list_del(&m->list);
Kent Overstreet6f1c8192018-05-20 18:25:53 -0400958 mempool_free(m, &m->tc->pool->mapping_pool);
Joe Thornbere49e5822012-07-27 15:08:16 +0100959}
Joe Thornber025b9682013-03-01 22:45:50 +0000960
Nikos Tsironis4ae280b2019-02-14 20:38:47 +0200961static void complete_overwrite_bio(struct thin_c *tc, struct bio *bio)
962{
963 struct pool *pool = tc->pool;
Nikos Tsironis4ae280b2019-02-14 20:38:47 +0200964
965 /*
966 * If the bio has the REQ_FUA flag set we must commit the metadata
967 * before signaling its completion.
968 */
969 if (!bio_triggers_commit(tc, bio)) {
970 bio_endio(bio);
971 return;
972 }
973
974 /*
975 * Complete bio with an error if earlier I/O caused changes to the
976 * metadata that can't be committed, e.g, due to I/O errors on the
977 * metadata device.
978 */
979 if (dm_thin_aborted_changes(tc->td)) {
980 bio_io_error(bio);
981 return;
982 }
983
984 /*
985 * Batch together any bios that trigger commits and then issue a
986 * single commit for them in process_deferred_bios().
987 */
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -0400988 spin_lock_irq(&pool->lock);
Nikos Tsironis4ae280b2019-02-14 20:38:47 +0200989 bio_list_add(&pool->deferred_flush_completions, bio);
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -0400990 spin_unlock_irq(&pool->lock);
Nikos Tsironis4ae280b2019-02-14 20:38:47 +0200991}
992
Mike Snitzera24c2562012-06-03 00:30:00 +0100993static void process_prepared_mapping(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000994{
995 struct thin_c *tc = m->tc;
Joe Thornber6beca5e2013-03-01 22:45:50 +0000996 struct pool *pool = tc->pool;
Mike Snitzer8b908f82015-05-13 17:53:13 -0400997 struct bio *bio = m->bio;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000998 int r;
999
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001000 if (m->status) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001001 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +01001002 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001003 }
1004
1005 /*
1006 * Commit the prepared block into the mapping btree.
1007 * Any I/O for this block arriving after this point will get
1008 * remapped to it directly.
1009 */
Joe Thornber34fbcf62015-04-16 12:58:35 +01001010 r = dm_thin_insert_block(tc->td, m->virt_begin, m->data_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001011 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05001012 metadata_operation_failed(pool, "dm_thin_insert_block", r);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001013 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +01001014 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001015 }
1016
1017 /*
1018 * Release any bios held while the block was being provisioned.
1019 * If we are processing a write bio that completely covers the block,
1020 * we already processed it so can ignore it now when processing
1021 * the bios in the cell.
1022 */
1023 if (bio) {
Joe Thornber2d759a42014-10-10 15:27:16 +01001024 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
Nikos Tsironis4ae280b2019-02-14 20:38:47 +02001025 complete_overwrite_bio(tc, bio);
Joe Thornber2d759a42014-10-10 15:27:16 +01001026 } else {
1027 inc_all_io_entry(tc->pool, m->cell->holder);
1028 remap_and_issue(tc, m->cell->holder, m->data_block);
1029 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
1030 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001031
Joe Thornber905386f2012-07-27 15:08:05 +01001032out:
Joe Thornber991d9fa2011-10-31 20:21:18 +00001033 list_del(&m->list);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001034 mempool_free(m, &pool->mapping_pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001035}
1036
Joe Thornber34fbcf62015-04-16 12:58:35 +01001037/*----------------------------------------------------------------*/
1038
1039static void free_discard_mapping(struct dm_thin_new_mapping *m)
1040{
1041 struct thin_c *tc = m->tc;
1042 if (m->cell)
1043 cell_defer_no_holder(tc, m->cell);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001044 mempool_free(m, &tc->pool->mapping_pool);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001045}
1046
Joe Thornbere49e5822012-07-27 15:08:16 +01001047static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
Joe Thornber104655f2012-03-28 18:41:28 +01001048{
Joe Thornbere49e5822012-07-27 15:08:16 +01001049 bio_io_error(m->bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001050 free_discard_mapping(m);
Joe Thornbere49e5822012-07-27 15:08:16 +01001051}
Joe Thornber104655f2012-03-28 18:41:28 +01001052
Joe Thornber34fbcf62015-04-16 12:58:35 +01001053static void process_prepared_discard_success(struct dm_thin_new_mapping *m)
Joe Thornbere49e5822012-07-27 15:08:16 +01001054{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001055 bio_endio(m->bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001056 free_discard_mapping(m);
Joe Thornber104655f2012-03-28 18:41:28 +01001057}
1058
Joe Thornber34fbcf62015-04-16 12:58:35 +01001059static void process_prepared_discard_no_passdown(struct dm_thin_new_mapping *m)
Joe Thornbere49e5822012-07-27 15:08:16 +01001060{
1061 int r;
1062 struct thin_c *tc = m->tc;
1063
Joe Thornber34fbcf62015-04-16 12:58:35 +01001064 r = dm_thin_remove_range(tc->td, m->cell->key.block_begin, m->cell->key.block_end);
1065 if (r) {
1066 metadata_operation_failed(tc->pool, "dm_thin_remove_range", r);
1067 bio_io_error(m->bio);
1068 } else
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001069 bio_endio(m->bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001070
Joe Thornber34fbcf62015-04-16 12:58:35 +01001071 cell_defer_no_holder(tc, m->cell);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001072 mempool_free(m, &tc->pool->mapping_pool);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001073}
1074
Joe Thornber202bae52016-05-04 14:12:42 -04001075/*----------------------------------------------------------------*/
1076
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001077static void passdown_double_checking_shared_status(struct dm_thin_new_mapping *m,
1078 struct bio *discard_parent)
Joe Thornber34fbcf62015-04-16 12:58:35 +01001079{
1080 /*
1081 * We've already unmapped this range of blocks, but before we
1082 * passdown we have to check that these blocks are now unused.
1083 */
Joe Thornber202bae52016-05-04 14:12:42 -04001084 int r = 0;
Joe Thornberd445bd92019-01-15 13:27:01 -05001085 bool shared = true;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001086 struct thin_c *tc = m->tc;
1087 struct pool *pool = tc->pool;
1088 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 -04001089 struct discard_op op;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001090
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001091 begin_discard(&op, tc, discard_parent);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001092 while (b != end) {
1093 /* find start of unmapped run */
1094 for (; b < end; b++) {
Joe Thornberd445bd92019-01-15 13:27:01 -05001095 r = dm_pool_block_is_shared(pool->pmd, b, &shared);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001096 if (r)
Joe Thornber202bae52016-05-04 14:12:42 -04001097 goto out;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001098
Joe Thornberd445bd92019-01-15 13:27:01 -05001099 if (!shared)
Joe Thornber34fbcf62015-04-16 12:58:35 +01001100 break;
1101 }
1102
1103 if (b == end)
1104 break;
1105
1106 /* find end of run */
1107 for (e = b + 1; e != end; e++) {
Joe Thornberd445bd92019-01-15 13:27:01 -05001108 r = dm_pool_block_is_shared(pool->pmd, e, &shared);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001109 if (r)
Joe Thornber202bae52016-05-04 14:12:42 -04001110 goto out;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001111
Joe Thornberd445bd92019-01-15 13:27:01 -05001112 if (shared)
Joe Thornber34fbcf62015-04-16 12:58:35 +01001113 break;
1114 }
1115
Joe Thornber202bae52016-05-04 14:12:42 -04001116 r = issue_discard(&op, b, e);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001117 if (r)
Joe Thornber202bae52016-05-04 14:12:42 -04001118 goto out;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001119
1120 b = e;
1121 }
Joe Thornber202bae52016-05-04 14:12:42 -04001122out:
1123 end_discard(&op, r);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001124}
1125
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001126static void queue_passdown_pt2(struct dm_thin_new_mapping *m)
1127{
1128 unsigned long flags;
1129 struct pool *pool = m->tc->pool;
1130
1131 spin_lock_irqsave(&pool->lock, flags);
1132 list_add_tail(&m->list, &pool->prepared_discards_pt2);
1133 spin_unlock_irqrestore(&pool->lock, flags);
1134 wake_worker(pool);
1135}
1136
1137static void passdown_endio(struct bio *bio)
1138{
1139 /*
1140 * It doesn't matter if the passdown discard failed, we still want
1141 * to unmap (we ignore err).
1142 */
1143 queue_passdown_pt2(bio->bi_private);
Dennis Yang948f5812017-04-18 15:27:06 +08001144 bio_put(bio);
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001145}
1146
1147static void process_prepared_discard_passdown_pt1(struct dm_thin_new_mapping *m)
1148{
1149 int r;
1150 struct thin_c *tc = m->tc;
1151 struct pool *pool = tc->pool;
1152 struct bio *discard_parent;
1153 dm_block_t data_end = m->data_block + (m->virt_end - m->virt_begin);
1154
1155 /*
1156 * Only this thread allocates blocks, so we can be sure that the
1157 * newly unmapped blocks will not be allocated before the end of
1158 * the function.
1159 */
1160 r = dm_thin_remove_range(tc->td, m->virt_begin, m->virt_end);
1161 if (r) {
1162 metadata_operation_failed(pool, "dm_thin_remove_range", r);
1163 bio_io_error(m->bio);
1164 cell_defer_no_holder(tc, m->cell);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001165 mempool_free(m, &pool->mapping_pool);
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001166 return;
1167 }
1168
Vallish Vaidyeshwara00a0ea32017-06-23 18:53:06 +00001169 /*
1170 * Increment the unmapped blocks. This prevents a race between the
1171 * passdown io and reallocation of freed blocks.
1172 */
1173 r = dm_pool_inc_data_range(pool->pmd, m->data_block, data_end);
1174 if (r) {
1175 metadata_operation_failed(pool, "dm_pool_inc_data_range", r);
1176 bio_io_error(m->bio);
1177 cell_defer_no_holder(tc, m->cell);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001178 mempool_free(m, &pool->mapping_pool);
Vallish Vaidyeshwara00a0ea32017-06-23 18:53:06 +00001179 return;
1180 }
1181
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001182 discard_parent = bio_alloc(GFP_NOIO, 1);
1183 if (!discard_parent) {
1184 DMWARN("%s: unable to allocate top level discard bio for passdown. Skipping passdown.",
1185 dm_device_name(tc->pool->pool_md));
1186 queue_passdown_pt2(m);
1187
1188 } else {
1189 discard_parent->bi_end_io = passdown_endio;
1190 discard_parent->bi_private = m;
1191
1192 if (m->maybe_shared)
1193 passdown_double_checking_shared_status(m, discard_parent);
1194 else {
1195 struct discard_op op;
1196
1197 begin_discard(&op, tc, discard_parent);
1198 r = issue_discard(&op, m->data_block, data_end);
1199 end_discard(&op, r);
1200 }
1201 }
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001202}
1203
1204static void process_prepared_discard_passdown_pt2(struct dm_thin_new_mapping *m)
Joe Thornber34fbcf62015-04-16 12:58:35 +01001205{
1206 int r;
1207 struct thin_c *tc = m->tc;
1208 struct pool *pool = tc->pool;
1209
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001210 /*
1211 * The passdown has completed, so now we can decrement all those
1212 * unmapped blocks.
1213 */
1214 r = dm_pool_dec_data_range(pool->pmd, m->data_block,
1215 m->data_block + (m->virt_end - m->virt_begin));
Joe Thornber202bae52016-05-04 14:12:42 -04001216 if (r) {
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001217 metadata_operation_failed(pool, "dm_pool_dec_data_range", r);
Joe Thornber202bae52016-05-04 14:12:42 -04001218 bio_io_error(m->bio);
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001219 } else
1220 bio_endio(m->bio);
Joe Thornber202bae52016-05-04 14:12:42 -04001221
Joe Thornber34fbcf62015-04-16 12:58:35 +01001222 cell_defer_no_holder(tc, m->cell);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001223 mempool_free(m, &pool->mapping_pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01001224}
1225
Joe Thornber104655f2012-03-28 18:41:28 +01001226static void process_prepared(struct pool *pool, struct list_head *head,
Joe Thornbere49e5822012-07-27 15:08:16 +01001227 process_mapping_fn *fn)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001228{
Joe Thornber991d9fa2011-10-31 20:21:18 +00001229 struct list_head maps;
Mike Snitzera24c2562012-06-03 00:30:00 +01001230 struct dm_thin_new_mapping *m, *tmp;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001231
1232 INIT_LIST_HEAD(&maps);
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04001233 spin_lock_irq(&pool->lock);
Joe Thornber104655f2012-03-28 18:41:28 +01001234 list_splice_init(head, &maps);
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04001235 spin_unlock_irq(&pool->lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001236
1237 list_for_each_entry_safe(m, tmp, &maps, list)
Joe Thornbere49e5822012-07-27 15:08:16 +01001238 (*fn)(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001239}
1240
1241/*
1242 * Deferred bio jobs.
1243 */
Joe Thornber104655f2012-03-28 18:41:28 +01001244static int io_overlaps_block(struct pool *pool, struct bio *bio)
1245{
Kent Overstreet4f024f32013-10-11 15:44:27 -07001246 return bio->bi_iter.bi_size ==
1247 (pool->sectors_per_block << SECTOR_SHIFT);
Joe Thornber104655f2012-03-28 18:41:28 +01001248}
1249
Joe Thornber991d9fa2011-10-31 20:21:18 +00001250static int io_overwrites_block(struct pool *pool, struct bio *bio)
1251{
Joe Thornber104655f2012-03-28 18:41:28 +01001252 return (bio_data_dir(bio) == WRITE) &&
1253 io_overlaps_block(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001254}
1255
1256static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
1257 bio_end_io_t *fn)
1258{
1259 *save = bio->bi_end_io;
1260 bio->bi_end_io = fn;
1261}
1262
1263static int ensure_next_mapping(struct pool *pool)
1264{
1265 if (pool->next_mapping)
1266 return 0;
1267
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001268 pool->next_mapping = mempool_alloc(&pool->mapping_pool, GFP_ATOMIC);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001269
1270 return pool->next_mapping ? 0 : -ENOMEM;
1271}
1272
Mike Snitzera24c2562012-06-03 00:30:00 +01001273static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001274{
Mike Snitzer16961b02013-12-17 13:19:11 -05001275 struct dm_thin_new_mapping *m = pool->next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001276
1277 BUG_ON(!pool->next_mapping);
1278
Mike Snitzer16961b02013-12-17 13:19:11 -05001279 memset(m, 0, sizeof(struct dm_thin_new_mapping));
1280 INIT_LIST_HEAD(&m->list);
1281 m->bio = NULL;
1282
Joe Thornber991d9fa2011-10-31 20:21:18 +00001283 pool->next_mapping = NULL;
1284
Mike Snitzer16961b02013-12-17 13:19:11 -05001285 return m;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001286}
1287
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001288static void ll_zero(struct thin_c *tc, struct dm_thin_new_mapping *m,
1289 sector_t begin, sector_t end)
1290{
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001291 struct dm_io_region to;
1292
1293 to.bdev = tc->pool_dev->bdev;
1294 to.sector = begin;
1295 to.count = end - begin;
1296
Mike Snitzer7209049d2018-07-31 17:27:02 -04001297 dm_kcopyd_zero(tc->pool->copier, 1, &to, 0, copy_complete, m);
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001298}
1299
Mike Snitzer452d7a62014-10-09 19:20:21 -04001300static void remap_and_issue_overwrite(struct thin_c *tc, struct bio *bio,
Joe Thornber34fbcf62015-04-16 12:58:35 +01001301 dm_block_t data_begin,
Mike Snitzer452d7a62014-10-09 19:20:21 -04001302 struct dm_thin_new_mapping *m)
1303{
1304 struct pool *pool = tc->pool;
1305 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1306
1307 h->overwrite_mapping = m;
1308 m->bio = bio;
1309 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
1310 inc_all_io_entry(pool, bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001311 remap_and_issue(tc, bio, data_begin);
Mike Snitzer452d7a62014-10-09 19:20:21 -04001312}
1313
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001314/*
1315 * A partial copy also needs to zero the uncopied region.
1316 */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001317static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
Joe Thornber2dd9c252012-03-28 18:41:28 +01001318 struct dm_dev *origin, dm_block_t data_origin,
1319 dm_block_t data_dest,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001320 struct dm_bio_prison_cell *cell, struct bio *bio,
1321 sector_t len)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001322{
Joe Thornber991d9fa2011-10-31 20:21:18 +00001323 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +01001324 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001325
Joe Thornber991d9fa2011-10-31 20:21:18 +00001326 m->tc = tc;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001327 m->virt_begin = virt_block;
1328 m->virt_end = virt_block + 1u;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001329 m->data_block = data_dest;
1330 m->cell = cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001331
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001332 /*
1333 * quiesce action + copy action + an extra reference held for the
1334 * duration of this function (we may need to inc later for a
1335 * partial zero).
1336 */
1337 atomic_set(&m->prepare_actions, 3);
1338
Mike Snitzer44feb382012-10-12 21:02:10 +01001339 if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001340 complete_mapping_preparation(m); /* already quiesced */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001341
1342 /*
1343 * IO to pool_dev remaps to the pool target's data_dev.
1344 *
1345 * If the whole block of data is being overwritten, we can issue the
1346 * bio immediately. Otherwise we use kcopyd to clone the data first.
1347 */
Mike Snitzer452d7a62014-10-09 19:20:21 -04001348 if (io_overwrites_block(pool, bio))
1349 remap_and_issue_overwrite(tc, bio, data_dest, m);
1350 else {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001351 struct dm_io_region from, to;
1352
Joe Thornber2dd9c252012-03-28 18:41:28 +01001353 from.bdev = origin->bdev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001354 from.sector = data_origin * pool->sectors_per_block;
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001355 from.count = len;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001356
1357 to.bdev = tc->pool_dev->bdev;
1358 to.sector = data_dest * pool->sectors_per_block;
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001359 to.count = len;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001360
Mike Snitzer7209049d2018-07-31 17:27:02 -04001361 dm_kcopyd_copy(pool->copier, &from, 1, &to,
1362 0, copy_complete, m);
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001363
1364 /*
1365 * Do we need to zero a tail region?
1366 */
1367 if (len < pool->sectors_per_block && pool->pf.zero_new_blocks) {
1368 atomic_inc(&m->prepare_actions);
1369 ll_zero(tc, m,
1370 data_dest * pool->sectors_per_block + len,
1371 (data_dest + 1) * pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001372 }
1373 }
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001374
1375 complete_mapping_preparation(m); /* drop our ref */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001376}
1377
Joe Thornber2dd9c252012-03-28 18:41:28 +01001378static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
1379 dm_block_t data_origin, dm_block_t data_dest,
Mike Snitzera24c2562012-06-03 00:30:00 +01001380 struct dm_bio_prison_cell *cell, struct bio *bio)
Joe Thornber2dd9c252012-03-28 18:41:28 +01001381{
1382 schedule_copy(tc, virt_block, tc->pool_dev,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001383 data_origin, data_dest, cell, bio,
1384 tc->pool->sectors_per_block);
Joe Thornber2dd9c252012-03-28 18:41:28 +01001385}
1386
Joe Thornber991d9fa2011-10-31 20:21:18 +00001387static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
Mike Snitzera24c2562012-06-03 00:30:00 +01001388 dm_block_t data_block, struct dm_bio_prison_cell *cell,
Joe Thornber991d9fa2011-10-31 20:21:18 +00001389 struct bio *bio)
1390{
1391 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +01001392 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001393
Joe Thornber50f3c3e2014-06-13 13:57:09 +01001394 atomic_set(&m->prepare_actions, 1); /* no need to quiesce */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001395 m->tc = tc;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001396 m->virt_begin = virt_block;
1397 m->virt_end = virt_block + 1u;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001398 m->data_block = data_block;
1399 m->cell = cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001400
1401 /*
1402 * If the whole block of data is being overwritten or we are not
1403 * zeroing pre-existing data, we can issue the bio immediately.
1404 * Otherwise we use kcopyd to zero the data first.
1405 */
Mike Snitzerf8ae7522015-05-14 11:28:37 -04001406 if (pool->pf.zero_new_blocks) {
1407 if (io_overwrites_block(pool, bio))
1408 remap_and_issue_overwrite(tc, bio, data_block, m);
1409 else
1410 ll_zero(tc, m, data_block * pool->sectors_per_block,
1411 (data_block + 1) * pool->sectors_per_block);
1412 } else
Joe Thornber991d9fa2011-10-31 20:21:18 +00001413 process_prepared_mapping(m);
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001414}
Joe Thornber991d9fa2011-10-31 20:21:18 +00001415
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001416static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
1417 dm_block_t data_dest,
1418 struct dm_bio_prison_cell *cell, struct bio *bio)
1419{
1420 struct pool *pool = tc->pool;
1421 sector_t virt_block_begin = virt_block * pool->sectors_per_block;
1422 sector_t virt_block_end = (virt_block + 1) * pool->sectors_per_block;
1423
1424 if (virt_block_end <= tc->origin_size)
1425 schedule_copy(tc, virt_block, tc->origin_dev,
1426 virt_block, data_dest, cell, bio,
1427 pool->sectors_per_block);
1428
1429 else if (virt_block_begin < tc->origin_size)
1430 schedule_copy(tc, virt_block, tc->origin_dev,
1431 virt_block, data_dest, cell, bio,
1432 tc->origin_size - virt_block_begin);
1433
1434 else
1435 schedule_zero(tc, virt_block, data_dest, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001436}
1437
Joe Thornber2c43fd22014-12-11 11:12:19 +00001438static void set_pool_mode(struct pool *pool, enum pool_mode new_mode);
1439
Mike Snitzera6855572018-06-26 12:04:23 -04001440static void requeue_bios(struct pool *pool);
1441
Joe Thornber3ab91822018-09-10 16:50:09 +01001442static bool is_read_only_pool_mode(enum pool_mode mode)
1443{
1444 return (mode == PM_OUT_OF_METADATA_SPACE || mode == PM_READ_ONLY);
1445}
1446
1447static bool is_read_only(struct pool *pool)
1448{
1449 return is_read_only_pool_mode(get_pool_mode(pool));
1450}
1451
1452static void check_for_metadata_space(struct pool *pool)
1453{
1454 int r;
1455 const char *ooms_reason = NULL;
1456 dm_block_t nr_free;
1457
1458 r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free);
1459 if (r)
1460 ooms_reason = "Could not get free metadata blocks";
1461 else if (!nr_free)
1462 ooms_reason = "No free metadata blocks";
1463
1464 if (ooms_reason && !is_read_only(pool)) {
1465 DMERR("%s", ooms_reason);
1466 set_pool_mode(pool, PM_OUT_OF_METADATA_SPACE);
1467 }
1468}
1469
1470static void check_for_data_space(struct pool *pool)
Joe Thornber2c43fd22014-12-11 11:12:19 +00001471{
1472 int r;
1473 dm_block_t nr_free;
1474
1475 if (get_pool_mode(pool) != PM_OUT_OF_DATA_SPACE)
1476 return;
1477
1478 r = dm_pool_get_free_block_count(pool->pmd, &nr_free);
1479 if (r)
1480 return;
1481
Mike Snitzera6855572018-06-26 12:04:23 -04001482 if (nr_free) {
Joe Thornber2c43fd22014-12-11 11:12:19 +00001483 set_pool_mode(pool, PM_WRITE);
Mike Snitzera6855572018-06-26 12:04:23 -04001484 requeue_bios(pool);
1485 }
Joe Thornber2c43fd22014-12-11 11:12:19 +00001486}
1487
Joe Thornbere49e5822012-07-27 15:08:16 +01001488/*
1489 * A non-zero return indicates read_only or fail_io mode.
1490 * Many callers don't care about the return value.
1491 */
Joe Thornber020cc3b2013-12-04 15:05:36 -05001492static int commit(struct pool *pool)
Joe Thornbere49e5822012-07-27 15:08:16 +01001493{
1494 int r;
1495
Joe Thornber3ab91822018-09-10 16:50:09 +01001496 if (get_pool_mode(pool) >= PM_OUT_OF_METADATA_SPACE)
Joe Thornbere49e5822012-07-27 15:08:16 +01001497 return -EINVAL;
1498
Joe Thornber020cc3b2013-12-04 15:05:36 -05001499 r = dm_pool_commit_metadata(pool->pmd);
Joe Thornberb5330652013-12-04 19:51:33 -05001500 if (r)
1501 metadata_operation_failed(pool, "dm_pool_commit_metadata", r);
Joe Thornber3ab91822018-09-10 16:50:09 +01001502 else {
1503 check_for_metadata_space(pool);
1504 check_for_data_space(pool);
1505 }
Joe Thornbere49e5822012-07-27 15:08:16 +01001506
1507 return r;
1508}
1509
Joe Thornber88a66212013-12-04 20:16:12 -05001510static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks)
1511{
Joe Thornber88a66212013-12-04 20:16:12 -05001512 if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
1513 DMWARN("%s: reached low water mark for data device: sending event.",
1514 dm_device_name(pool->pool_md));
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04001515 spin_lock_irq(&pool->lock);
Joe Thornber88a66212013-12-04 20:16:12 -05001516 pool->low_water_triggered = true;
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04001517 spin_unlock_irq(&pool->lock);
Joe Thornber88a66212013-12-04 20:16:12 -05001518 dm_table_event(pool->ti->table);
1519 }
1520}
1521
Joe Thornber991d9fa2011-10-31 20:21:18 +00001522static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
1523{
1524 int r;
1525 dm_block_t free_blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001526 struct pool *pool = tc->pool;
1527
Joe Thornber3e1a0692014-03-03 16:03:26 +00001528 if (WARN_ON(get_pool_mode(pool) != PM_WRITE))
Joe Thornber8d30abf2013-12-04 19:16:11 -05001529 return -EINVAL;
1530
Joe Thornber991d9fa2011-10-31 20:21:18 +00001531 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
Joe Thornberb5330652013-12-04 19:51:33 -05001532 if (r) {
1533 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001534 return r;
Joe Thornberb5330652013-12-04 19:51:33 -05001535 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001536
Joe Thornber88a66212013-12-04 20:16:12 -05001537 check_low_water_mark(pool, free_blocks);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001538
1539 if (!free_blocks) {
Mike Snitzer94563ba2013-08-22 09:56:18 -04001540 /*
1541 * Try to commit to see if that will free up some
1542 * more space.
1543 */
Joe Thornber020cc3b2013-12-04 15:05:36 -05001544 r = commit(pool);
1545 if (r)
1546 return r;
Mike Snitzer94563ba2013-08-22 09:56:18 -04001547
1548 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
Joe Thornberb5330652013-12-04 19:51:33 -05001549 if (r) {
1550 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
Mike Snitzer94563ba2013-08-22 09:56:18 -04001551 return r;
Joe Thornberb5330652013-12-04 19:51:33 -05001552 }
Mike Snitzer94563ba2013-08-22 09:56:18 -04001553
Mike Snitzer94563ba2013-08-22 09:56:18 -04001554 if (!free_blocks) {
Joe Thornber3e1a0692014-03-03 16:03:26 +00001555 set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001556 return -ENOSPC;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001557 }
1558 }
1559
1560 r = dm_pool_alloc_data_block(pool->pmd, result);
Mike Snitzer4a02b342013-12-03 12:20:57 -05001561 if (r) {
Mike Snitzera6855572018-06-26 12:04:23 -04001562 if (r == -ENOSPC)
1563 set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
1564 else
1565 metadata_operation_failed(pool, "dm_pool_alloc_data_block", r);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001566 return r;
Mike Snitzer4a02b342013-12-03 12:20:57 -05001567 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001568
Joe Thornber3ab91822018-09-10 16:50:09 +01001569 r = dm_pool_get_free_metadata_block_count(pool->pmd, &free_blocks);
1570 if (r) {
1571 metadata_operation_failed(pool, "dm_pool_get_free_metadata_block_count", r);
1572 return r;
1573 }
1574
1575 if (!free_blocks) {
1576 /* Let's commit before we use up the metadata reserve. */
1577 r = commit(pool);
1578 if (r)
1579 return r;
1580 }
1581
Joe Thornber991d9fa2011-10-31 20:21:18 +00001582 return 0;
1583}
1584
1585/*
1586 * If we have run out of space, queue bios until the device is
1587 * resumed, presumably after having been reloaded with more space.
1588 */
1589static void retry_on_resume(struct bio *bio)
1590{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001591 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01001592 struct thin_c *tc = h->tc;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001593
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04001594 spin_lock_irq(&tc->lock);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001595 bio_list_add(&tc->retry_on_resume_list, bio);
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04001596 spin_unlock_irq(&tc->lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001597}
1598
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001599static blk_status_t should_error_unserviceable_bio(struct pool *pool)
Joe Thornber3e1a0692014-03-03 16:03:26 +00001600{
1601 enum pool_mode m = get_pool_mode(pool);
1602
1603 switch (m) {
1604 case PM_WRITE:
1605 /* Shouldn't get here */
1606 DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001607 return BLK_STS_IOERR;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001608
1609 case PM_OUT_OF_DATA_SPACE:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001610 return pool->pf.error_if_no_space ? BLK_STS_NOSPC : 0;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001611
Joe Thornber3ab91822018-09-10 16:50:09 +01001612 case PM_OUT_OF_METADATA_SPACE:
Joe Thornber3e1a0692014-03-03 16:03:26 +00001613 case PM_READ_ONLY:
1614 case PM_FAIL:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001615 return BLK_STS_IOERR;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001616 default:
1617 /* Shouldn't get here */
1618 DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001619 return BLK_STS_IOERR;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001620 }
1621}
1622
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001623static void handle_unserviceable_bio(struct pool *pool, struct bio *bio)
1624{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001625 blk_status_t error = should_error_unserviceable_bio(pool);
Mike Snitzeraf918052014-05-22 14:32:51 -04001626
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001627 if (error) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001628 bio->bi_status = error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001629 bio_endio(bio);
1630 } else
Mike Snitzer6d162022013-12-20 18:09:02 -05001631 retry_on_resume(bio);
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001632}
1633
Mike Snitzer399cadd2013-12-05 16:03:33 -05001634static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001635{
1636 struct bio *bio;
1637 struct bio_list bios;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001638 blk_status_t error;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001639
Mike Snitzeraf918052014-05-22 14:32:51 -04001640 error = should_error_unserviceable_bio(pool);
1641 if (error) {
1642 cell_error_with_code(pool, cell, error);
Joe Thornber3e1a0692014-03-03 16:03:26 +00001643 return;
1644 }
1645
Joe Thornber991d9fa2011-10-31 20:21:18 +00001646 bio_list_init(&bios);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001647 cell_release(pool, cell, &bios);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001648
Mike Snitzer9d094ee2014-10-19 08:23:09 -04001649 while ((bio = bio_list_pop(&bios)))
1650 retry_on_resume(bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001651}
1652
Joe Thornber34fbcf62015-04-16 12:58:35 +01001653static void process_discard_cell_no_passdown(struct thin_c *tc,
1654 struct dm_bio_prison_cell *virt_cell)
Joe Thornber104655f2012-03-28 18:41:28 +01001655{
Joe Thornber104655f2012-03-28 18:41:28 +01001656 struct pool *pool = tc->pool;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001657 struct dm_thin_new_mapping *m = get_next_mapping(pool);
1658
1659 /*
1660 * We don't need to lock the data blocks, since there's no
1661 * passdown. We only lock data blocks for allocation and breaking sharing.
1662 */
1663 m->tc = tc;
1664 m->virt_begin = virt_cell->key.block_begin;
1665 m->virt_end = virt_cell->key.block_end;
1666 m->cell = virt_cell;
1667 m->bio = virt_cell->holder;
1668
1669 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
1670 pool->process_prepared_discard(m);
1671}
1672
Joe Thornber34fbcf62015-04-16 12:58:35 +01001673static void break_up_discard_bio(struct thin_c *tc, dm_block_t begin, dm_block_t end,
1674 struct bio *bio)
1675{
1676 struct pool *pool = tc->pool;
1677
1678 int r;
1679 bool maybe_shared;
1680 struct dm_cell_key data_key;
1681 struct dm_bio_prison_cell *data_cell;
Mike Snitzera24c2562012-06-03 00:30:00 +01001682 struct dm_thin_new_mapping *m;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001683 dm_block_t virt_begin, virt_end, data_begin;
Joe Thornber104655f2012-03-28 18:41:28 +01001684
Joe Thornber34fbcf62015-04-16 12:58:35 +01001685 while (begin != end) {
1686 r = ensure_next_mapping(pool);
1687 if (r)
1688 /* we did our best */
1689 return;
Joe Thornber104655f2012-03-28 18:41:28 +01001690
Joe Thornber34fbcf62015-04-16 12:58:35 +01001691 r = dm_thin_find_mapped_range(tc->td, begin, end, &virt_begin, &virt_end,
1692 &data_begin, &maybe_shared);
1693 if (r)
1694 /*
1695 * Silently fail, letting any mappings we've
1696 * created complete.
1697 */
Joe Thornber104655f2012-03-28 18:41:28 +01001698 break;
Joe Thornber34fbcf62015-04-16 12:58:35 +01001699
1700 build_key(tc->td, PHYSICAL, data_begin, data_begin + (virt_end - virt_begin), &data_key);
1701 if (bio_detain(tc->pool, &data_key, NULL, &data_cell)) {
1702 /* contention, we'll give up with this range */
1703 begin = virt_end;
1704 continue;
Joe Thornber104655f2012-03-28 18:41:28 +01001705 }
1706
Joe Thornber104655f2012-03-28 18:41:28 +01001707 /*
Joe Thornber34fbcf62015-04-16 12:58:35 +01001708 * IO may still be going to the destination block. We must
1709 * quiesce before we can do the removal.
Joe Thornber104655f2012-03-28 18:41:28 +01001710 */
Joe Thornber34fbcf62015-04-16 12:58:35 +01001711 m = get_next_mapping(pool);
1712 m->tc = tc;
1713 m->maybe_shared = maybe_shared;
1714 m->virt_begin = virt_begin;
1715 m->virt_end = virt_end;
1716 m->data_block = data_begin;
1717 m->cell = data_cell;
1718 m->bio = bio;
Joe Thornber104655f2012-03-28 18:41:28 +01001719
Joe Thornber34fbcf62015-04-16 12:58:35 +01001720 /*
1721 * The parent bio must not complete before sub discard bios are
Joe Thornber202bae52016-05-04 14:12:42 -04001722 * chained to it (see end_discard's bio_chain)!
Joe Thornber34fbcf62015-04-16 12:58:35 +01001723 *
1724 * This per-mapping bi_remaining increment is paired with
1725 * the implicit decrement that occurs via bio_endio() in
Joe Thornber202bae52016-05-04 14:12:42 -04001726 * end_discard().
Joe Thornber34fbcf62015-04-16 12:58:35 +01001727 */
Mike Snitzer13e4f8a2016-05-04 15:05:44 -04001728 bio_inc_remaining(bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001729 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
1730 pool->process_prepared_discard(m);
1731
1732 begin = virt_end;
Joe Thornber104655f2012-03-28 18:41:28 +01001733 }
1734}
1735
Joe Thornber34fbcf62015-04-16 12:58:35 +01001736static void process_discard_cell_passdown(struct thin_c *tc, struct dm_bio_prison_cell *virt_cell)
1737{
1738 struct bio *bio = virt_cell->holder;
1739 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1740
1741 /*
1742 * The virt_cell will only get freed once the origin bio completes.
1743 * This means it will remain locked while all the individual
1744 * passdown bios are in flight.
1745 */
1746 h->cell = virt_cell;
1747 break_up_discard_bio(tc, virt_cell->key.block_begin, virt_cell->key.block_end, bio);
1748
1749 /*
1750 * We complete the bio now, knowing that the bi_remaining field
1751 * will prevent completion until the sub range discards have
1752 * completed.
1753 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001754 bio_endio(bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001755}
1756
Joe Thornbera374bb22014-10-10 13:43:14 +01001757static void process_discard_bio(struct thin_c *tc, struct bio *bio)
1758{
Joe Thornber34fbcf62015-04-16 12:58:35 +01001759 dm_block_t begin, end;
1760 struct dm_cell_key virt_key;
1761 struct dm_bio_prison_cell *virt_cell;
Joe Thornbera374bb22014-10-10 13:43:14 +01001762
Joe Thornber34fbcf62015-04-16 12:58:35 +01001763 get_bio_block_range(tc, bio, &begin, &end);
1764 if (begin == end) {
1765 /*
1766 * The discard covers less than a block.
1767 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001768 bio_endio(bio);
Joe Thornber34fbcf62015-04-16 12:58:35 +01001769 return;
1770 }
1771
1772 build_key(tc->td, VIRTUAL, begin, end, &virt_key);
1773 if (bio_detain(tc->pool, &virt_key, bio, &virt_cell))
1774 /*
1775 * Potential starvation issue: We're relying on the
1776 * fs/application being well behaved, and not trying to
1777 * send IO to a region at the same time as discarding it.
1778 * If they do this persistently then it's possible this
1779 * cell will never be granted.
1780 */
Joe Thornbera374bb22014-10-10 13:43:14 +01001781 return;
1782
Joe Thornber34fbcf62015-04-16 12:58:35 +01001783 tc->pool->process_discard_cell(tc, virt_cell);
Joe Thornbera374bb22014-10-10 13:43:14 +01001784}
1785
Joe Thornber991d9fa2011-10-31 20:21:18 +00001786static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzer44feb382012-10-12 21:02:10 +01001787 struct dm_cell_key *key,
Joe Thornber991d9fa2011-10-31 20:21:18 +00001788 struct dm_thin_lookup_result *lookup_result,
Mike Snitzera24c2562012-06-03 00:30:00 +01001789 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001790{
1791 int r;
1792 dm_block_t data_block;
Mike Snitzerd6fc2042013-08-21 17:40:11 -04001793 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001794
1795 r = alloc_data_block(tc, &data_block);
1796 switch (r) {
1797 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001798 schedule_internal_copy(tc, block, lookup_result->block,
1799 data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001800 break;
1801
1802 case -ENOSPC:
Mike Snitzer399cadd2013-12-05 16:03:33 -05001803 retry_bios_on_resume(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001804 break;
1805
1806 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001807 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1808 __func__, r);
Mike Snitzerd6fc2042013-08-21 17:40:11 -04001809 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001810 break;
1811 }
1812}
1813
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001814static void __remap_and_issue_shared_cell(void *context,
1815 struct dm_bio_prison_cell *cell)
1816{
1817 struct remap_info *info = context;
1818 struct bio *bio;
1819
1820 while ((bio = bio_list_pop(&cell->bios))) {
Christoph Hellwigf73f44e2017-01-27 08:30:47 -07001821 if (bio_data_dir(bio) == WRITE || op_is_flush(bio->bi_opf) ||
1822 bio_op(bio) == REQ_OP_DISCARD)
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001823 bio_list_add(&info->defer_bios, bio);
1824 else {
Luis de Bethencourtbd6d1e02018-01-17 15:09:25 +00001825 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001826
1827 h->shared_read_entry = dm_deferred_entry_inc(info->tc->pool->shared_read_ds);
1828 inc_all_io_entry(info->tc->pool, bio);
1829 bio_list_add(&info->issue_bios, bio);
1830 }
1831 }
1832}
1833
1834static void remap_and_issue_shared_cell(struct thin_c *tc,
1835 struct dm_bio_prison_cell *cell,
1836 dm_block_t block)
1837{
1838 struct bio *bio;
1839 struct remap_info info;
1840
1841 info.tc = tc;
1842 bio_list_init(&info.defer_bios);
1843 bio_list_init(&info.issue_bios);
1844
1845 cell_visit_release(tc->pool, __remap_and_issue_shared_cell,
1846 &info, cell);
1847
1848 while ((bio = bio_list_pop(&info.defer_bios)))
1849 thin_defer_bio(tc, bio);
1850
1851 while ((bio = bio_list_pop(&info.issue_bios)))
1852 remap_and_issue(tc, bio, block);
1853}
1854
Joe Thornber991d9fa2011-10-31 20:21:18 +00001855static void process_shared_bio(struct thin_c *tc, struct bio *bio,
1856 dm_block_t block,
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001857 struct dm_thin_lookup_result *lookup_result,
1858 struct dm_bio_prison_cell *virt_cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001859{
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001860 struct dm_bio_prison_cell *data_cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001861 struct pool *pool = tc->pool;
Mike Snitzer44feb382012-10-12 21:02:10 +01001862 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001863
1864 /*
1865 * If cell is already occupied, then sharing is already in the process
1866 * of being broken so we have nothing further to do here.
1867 */
1868 build_data_key(tc->td, lookup_result->block, &key);
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001869 if (bio_detain(pool, &key, bio, &data_cell)) {
1870 cell_defer_no_holder(tc, virt_cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001871 return;
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001872 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001873
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001874 if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size) {
1875 break_sharing(tc, bio, block, &key, lookup_result, data_cell);
1876 cell_defer_no_holder(tc, virt_cell);
1877 } else {
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001878 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornber991d9fa2011-10-31 20:21:18 +00001879
Mike Snitzer44feb382012-10-12 21:02:10 +01001880 h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
Joe Thornbere8088072012-12-21 20:23:31 +00001881 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001882 remap_and_issue(tc, bio, lookup_result->block);
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001883
1884 remap_and_issue_shared_cell(tc, data_cell, lookup_result->block);
1885 remap_and_issue_shared_cell(tc, virt_cell, lookup_result->block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001886 }
1887}
1888
1889static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzera24c2562012-06-03 00:30:00 +01001890 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001891{
1892 int r;
1893 dm_block_t data_block;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001894 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001895
1896 /*
1897 * Remap empty bios (flushes) immediately, without provisioning.
1898 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07001899 if (!bio->bi_iter.bi_size) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001900 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001901 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001902
Joe Thornber991d9fa2011-10-31 20:21:18 +00001903 remap_and_issue(tc, bio, 0);
1904 return;
1905 }
1906
1907 /*
1908 * Fill read bios with zeroes and complete them immediately.
1909 */
1910 if (bio_data_dir(bio) == READ) {
1911 zero_fill_bio(bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001912 cell_defer_no_holder(tc, cell);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001913 bio_endio(bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001914 return;
1915 }
1916
1917 r = alloc_data_block(tc, &data_block);
1918 switch (r) {
1919 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001920 if (tc->origin_dev)
1921 schedule_external_copy(tc, block, data_block, cell, bio);
1922 else
1923 schedule_zero(tc, block, data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001924 break;
1925
1926 case -ENOSPC:
Mike Snitzer399cadd2013-12-05 16:03:33 -05001927 retry_bios_on_resume(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001928 break;
1929
1930 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001931 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1932 __func__, r);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001933 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001934 break;
1935 }
1936}
1937
Joe Thornbera374bb22014-10-10 13:43:14 +01001938static void process_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001939{
1940 int r;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001941 struct pool *pool = tc->pool;
Joe Thornbera374bb22014-10-10 13:43:14 +01001942 struct bio *bio = cell->holder;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001943 dm_block_t block = get_bio_block(tc, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001944 struct dm_thin_lookup_result lookup_result;
1945
Joe Thornbera374bb22014-10-10 13:43:14 +01001946 if (tc->requeue_mode) {
1947 cell_requeue(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001948 return;
Joe Thornbera374bb22014-10-10 13:43:14 +01001949 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001950
1951 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1952 switch (r) {
1953 case 0:
Joe Thornber23ca2bb2014-10-15 14:46:58 +01001954 if (lookup_result.shared)
1955 process_shared_bio(tc, bio, block, &lookup_result, cell);
1956 else {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001957 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001958 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbera374bb22014-10-10 13:43:14 +01001959 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001960 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001961 break;
1962
1963 case -ENODATA:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001964 if (bio_data_dir(bio) == READ && tc->origin_dev) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001965 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001966 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001967
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001968 if (bio_end_sector(bio) <= tc->origin_size)
1969 remap_to_origin_and_issue(tc, bio);
1970
1971 else if (bio->bi_iter.bi_sector < tc->origin_size) {
1972 zero_fill_bio(bio);
1973 bio->bi_iter.bi_size = (tc->origin_size - bio->bi_iter.bi_sector) << SECTOR_SHIFT;
1974 remap_to_origin_and_issue(tc, bio);
1975
1976 } else {
1977 zero_fill_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001978 bio_endio(bio);
Joe Thornbere5aea7b2014-06-13 14:47:24 +01001979 }
Joe Thornber2dd9c252012-03-28 18:41:28 +01001980 } else
1981 provision_block(tc, bio, block, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001982 break;
1983
1984 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001985 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1986 __func__, r);
Joe Thornberf286ba02012-12-21 20:23:33 +00001987 cell_defer_no_holder(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001988 bio_io_error(bio);
1989 break;
1990 }
1991}
1992
Joe Thornbera374bb22014-10-10 13:43:14 +01001993static void process_bio(struct thin_c *tc, struct bio *bio)
1994{
1995 struct pool *pool = tc->pool;
1996 dm_block_t block = get_bio_block(tc, bio);
1997 struct dm_bio_prison_cell *cell;
1998 struct dm_cell_key key;
1999
2000 /*
2001 * If cell is already occupied, then the block is already
2002 * being provisioned so we have nothing further to do here.
2003 */
2004 build_virtual_key(tc->td, block, &key);
2005 if (bio_detain(pool, &key, bio, &cell))
2006 return;
2007
2008 process_cell(tc, cell);
2009}
2010
2011static void __process_bio_read_only(struct thin_c *tc, struct bio *bio,
2012 struct dm_bio_prison_cell *cell)
Joe Thornbere49e5822012-07-27 15:08:16 +01002013{
2014 int r;
2015 int rw = bio_data_dir(bio);
2016 dm_block_t block = get_bio_block(tc, bio);
2017 struct dm_thin_lookup_result lookup_result;
2018
2019 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
2020 switch (r) {
2021 case 0:
Joe Thornbera374bb22014-10-10 13:43:14 +01002022 if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size) {
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05002023 handle_unserviceable_bio(tc->pool, bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01002024 if (cell)
2025 cell_defer_no_holder(tc, cell);
2026 } else {
Joe Thornbere8088072012-12-21 20:23:31 +00002027 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01002028 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbera374bb22014-10-10 13:43:14 +01002029 if (cell)
2030 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00002031 }
Joe Thornbere49e5822012-07-27 15:08:16 +01002032 break;
2033
2034 case -ENODATA:
Joe Thornbera374bb22014-10-10 13:43:14 +01002035 if (cell)
2036 cell_defer_no_holder(tc, cell);
Joe Thornbere49e5822012-07-27 15:08:16 +01002037 if (rw != READ) {
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05002038 handle_unserviceable_bio(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01002039 break;
2040 }
2041
2042 if (tc->origin_dev) {
Joe Thornbere8088072012-12-21 20:23:31 +00002043 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01002044 remap_to_origin_and_issue(tc, bio);
2045 break;
2046 }
2047
2048 zero_fill_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002049 bio_endio(bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01002050 break;
2051
2052 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00002053 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
2054 __func__, r);
Joe Thornbera374bb22014-10-10 13:43:14 +01002055 if (cell)
2056 cell_defer_no_holder(tc, cell);
Joe Thornbere49e5822012-07-27 15:08:16 +01002057 bio_io_error(bio);
2058 break;
2059 }
2060}
2061
Joe Thornbera374bb22014-10-10 13:43:14 +01002062static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
2063{
2064 __process_bio_read_only(tc, bio, NULL);
2065}
2066
2067static void process_cell_read_only(struct thin_c *tc, struct dm_bio_prison_cell *cell)
2068{
2069 __process_bio_read_only(tc, cell->holder, cell);
2070}
2071
Joe Thornber3e1a0692014-03-03 16:03:26 +00002072static void process_bio_success(struct thin_c *tc, struct bio *bio)
2073{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002074 bio_endio(bio);
Joe Thornber3e1a0692014-03-03 16:03:26 +00002075}
2076
Joe Thornbere49e5822012-07-27 15:08:16 +01002077static void process_bio_fail(struct thin_c *tc, struct bio *bio)
2078{
2079 bio_io_error(bio);
2080}
2081
Joe Thornbera374bb22014-10-10 13:43:14 +01002082static void process_cell_success(struct thin_c *tc, struct dm_bio_prison_cell *cell)
2083{
2084 cell_success(tc->pool, cell);
2085}
2086
2087static void process_cell_fail(struct thin_c *tc, struct dm_bio_prison_cell *cell)
2088{
2089 cell_error(tc->pool, cell);
2090}
2091
Joe Thornberac8c3f32013-05-10 14:37:21 +01002092/*
2093 * FIXME: should we also commit due to size of transaction, measured in
2094 * metadata blocks?
2095 */
Joe Thornber905e51b2012-03-28 18:41:27 +01002096static int need_commit_due_to_time(struct pool *pool)
2097{
Manuel Schölling0f30af92014-05-22 22:42:37 +02002098 return !time_in_range(jiffies, pool->last_commit_jiffies,
2099 pool->last_commit_jiffies + COMMIT_PERIOD);
Joe Thornber905e51b2012-03-28 18:41:27 +01002100}
2101
Mike Snitzer67324ea2014-03-21 18:33:41 -04002102#define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node)
2103#define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook))
2104
2105static void __thin_bio_rb_add(struct thin_c *tc, struct bio *bio)
2106{
2107 struct rb_node **rbp, *parent;
2108 struct dm_thin_endio_hook *pbd;
2109 sector_t bi_sector = bio->bi_iter.bi_sector;
2110
2111 rbp = &tc->sort_bio_list.rb_node;
2112 parent = NULL;
2113 while (*rbp) {
2114 parent = *rbp;
2115 pbd = thin_pbd(parent);
2116
2117 if (bi_sector < thin_bio(pbd)->bi_iter.bi_sector)
2118 rbp = &(*rbp)->rb_left;
2119 else
2120 rbp = &(*rbp)->rb_right;
2121 }
2122
2123 pbd = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
2124 rb_link_node(&pbd->rb_node, parent, rbp);
2125 rb_insert_color(&pbd->rb_node, &tc->sort_bio_list);
2126}
2127
2128static void __extract_sorted_bios(struct thin_c *tc)
2129{
2130 struct rb_node *node;
2131 struct dm_thin_endio_hook *pbd;
2132 struct bio *bio;
2133
2134 for (node = rb_first(&tc->sort_bio_list); node; node = rb_next(node)) {
2135 pbd = thin_pbd(node);
2136 bio = thin_bio(pbd);
2137
2138 bio_list_add(&tc->deferred_bio_list, bio);
2139 rb_erase(&pbd->rb_node, &tc->sort_bio_list);
2140 }
2141
2142 WARN_ON(!RB_EMPTY_ROOT(&tc->sort_bio_list));
2143}
2144
2145static void __sort_thin_deferred_bios(struct thin_c *tc)
2146{
2147 struct bio *bio;
2148 struct bio_list bios;
2149
2150 bio_list_init(&bios);
2151 bio_list_merge(&bios, &tc->deferred_bio_list);
2152 bio_list_init(&tc->deferred_bio_list);
2153
2154 /* Sort deferred_bio_list using rb-tree */
2155 while ((bio = bio_list_pop(&bios)))
2156 __thin_bio_rb_add(tc, bio);
2157
2158 /*
2159 * Transfer the sorted bios in sort_bio_list back to
2160 * deferred_bio_list to allow lockless submission of
2161 * all bios.
2162 */
2163 __extract_sorted_bios(tc);
2164}
2165
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002166static void process_thin_deferred_bios(struct thin_c *tc)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002167{
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002168 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002169 struct bio *bio;
2170 struct bio_list bios;
Mike Snitzer67324ea2014-03-21 18:33:41 -04002171 struct blk_plug plug;
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002172 unsigned count = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002173
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002174 if (tc->requeue_mode) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002175 error_thin_bio_list(tc, &tc->deferred_bio_list,
2176 BLK_STS_DM_REQUEUE);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002177 return;
2178 }
2179
Joe Thornber991d9fa2011-10-31 20:21:18 +00002180 bio_list_init(&bios);
2181
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04002182 spin_lock_irq(&tc->lock);
Mike Snitzer67324ea2014-03-21 18:33:41 -04002183
2184 if (bio_list_empty(&tc->deferred_bio_list)) {
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04002185 spin_unlock_irq(&tc->lock);
Mike Snitzer67324ea2014-03-21 18:33:41 -04002186 return;
2187 }
2188
2189 __sort_thin_deferred_bios(tc);
2190
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002191 bio_list_merge(&bios, &tc->deferred_bio_list);
2192 bio_list_init(&tc->deferred_bio_list);
Mike Snitzer67324ea2014-03-21 18:33:41 -04002193
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04002194 spin_unlock_irq(&tc->lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002195
Mike Snitzer67324ea2014-03-21 18:33:41 -04002196 blk_start_plug(&plug);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002197 while ((bio = bio_list_pop(&bios))) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00002198 /*
2199 * If we've got no free new_mapping structs, and processing
2200 * this bio might require one, we pause until there are some
2201 * prepared mappings to process.
2202 */
2203 if (ensure_next_mapping(pool)) {
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04002204 spin_lock_irq(&tc->lock);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002205 bio_list_add(&tc->deferred_bio_list, bio);
2206 bio_list_merge(&tc->deferred_bio_list, &bios);
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04002207 spin_unlock_irq(&tc->lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002208 break;
2209 }
Joe Thornber104655f2012-03-28 18:41:28 +01002210
Mike Christiee6047142016-06-05 14:32:04 -05002211 if (bio_op(bio) == REQ_OP_DISCARD)
Joe Thornbere49e5822012-07-27 15:08:16 +01002212 pool->process_discard(tc, bio);
Joe Thornber104655f2012-03-28 18:41:28 +01002213 else
Joe Thornbere49e5822012-07-27 15:08:16 +01002214 pool->process_bio(tc, bio);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002215
2216 if ((count++ & 127) == 0) {
Joe Thornber7d327fe2014-10-06 15:45:59 +01002217 throttle_work_update(&pool->throttle);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002218 dm_pool_issue_prefetches(pool->pmd);
2219 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002220 }
Mike Snitzer67324ea2014-03-21 18:33:41 -04002221 blk_finish_plug(&plug);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002222}
2223
Joe Thornberac4c3f32014-10-10 16:42:10 +01002224static int cmp_cells(const void *lhs, const void *rhs)
2225{
2226 struct dm_bio_prison_cell *lhs_cell = *((struct dm_bio_prison_cell **) lhs);
2227 struct dm_bio_prison_cell *rhs_cell = *((struct dm_bio_prison_cell **) rhs);
2228
2229 BUG_ON(!lhs_cell->holder);
2230 BUG_ON(!rhs_cell->holder);
2231
2232 if (lhs_cell->holder->bi_iter.bi_sector < rhs_cell->holder->bi_iter.bi_sector)
2233 return -1;
2234
2235 if (lhs_cell->holder->bi_iter.bi_sector > rhs_cell->holder->bi_iter.bi_sector)
2236 return 1;
2237
2238 return 0;
2239}
2240
2241static unsigned sort_cells(struct pool *pool, struct list_head *cells)
2242{
2243 unsigned count = 0;
2244 struct dm_bio_prison_cell *cell, *tmp;
2245
2246 list_for_each_entry_safe(cell, tmp, cells, user_list) {
2247 if (count >= CELL_SORT_ARRAY_SIZE)
2248 break;
2249
2250 pool->cell_sort_array[count++] = cell;
2251 list_del(&cell->user_list);
2252 }
2253
2254 sort(pool->cell_sort_array, count, sizeof(cell), cmp_cells, NULL);
2255
2256 return count;
2257}
2258
Joe Thornbera374bb22014-10-10 13:43:14 +01002259static void process_thin_deferred_cells(struct thin_c *tc)
2260{
2261 struct pool *pool = tc->pool;
Joe Thornbera374bb22014-10-10 13:43:14 +01002262 struct list_head cells;
Joe Thornberac4c3f32014-10-10 16:42:10 +01002263 struct dm_bio_prison_cell *cell;
2264 unsigned i, j, count;
Joe Thornbera374bb22014-10-10 13:43:14 +01002265
2266 INIT_LIST_HEAD(&cells);
2267
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04002268 spin_lock_irq(&tc->lock);
Joe Thornbera374bb22014-10-10 13:43:14 +01002269 list_splice_init(&tc->deferred_cells, &cells);
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04002270 spin_unlock_irq(&tc->lock);
Joe Thornbera374bb22014-10-10 13:43:14 +01002271
2272 if (list_empty(&cells))
2273 return;
2274
Joe Thornberac4c3f32014-10-10 16:42:10 +01002275 do {
2276 count = sort_cells(tc->pool, &cells);
Joe Thornbera374bb22014-10-10 13:43:14 +01002277
Joe Thornberac4c3f32014-10-10 16:42:10 +01002278 for (i = 0; i < count; i++) {
2279 cell = pool->cell_sort_array[i];
2280 BUG_ON(!cell->holder);
2281
2282 /*
2283 * If we've got no free new_mapping structs, and processing
2284 * this bio might require one, we pause until there are some
2285 * prepared mappings to process.
2286 */
2287 if (ensure_next_mapping(pool)) {
2288 for (j = i; j < count; j++)
2289 list_add(&pool->cell_sort_array[j]->user_list, &cells);
2290
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04002291 spin_lock_irq(&tc->lock);
Joe Thornberac4c3f32014-10-10 16:42:10 +01002292 list_splice(&cells, &tc->deferred_cells);
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04002293 spin_unlock_irq(&tc->lock);
Joe Thornberac4c3f32014-10-10 16:42:10 +01002294 return;
2295 }
2296
Mike Christiee6047142016-06-05 14:32:04 -05002297 if (bio_op(cell->holder) == REQ_OP_DISCARD)
Joe Thornberac4c3f32014-10-10 16:42:10 +01002298 pool->process_discard_cell(tc, cell);
2299 else
2300 pool->process_cell(tc, cell);
Joe Thornbera374bb22014-10-10 13:43:14 +01002301 }
Joe Thornberac4c3f32014-10-10 16:42:10 +01002302 } while (!list_empty(&cells));
Joe Thornbera374bb22014-10-10 13:43:14 +01002303}
2304
Joe Thornberb10ebd32014-04-08 11:29:01 +01002305static void thin_get(struct thin_c *tc);
2306static void thin_put(struct thin_c *tc);
2307
2308/*
2309 * We can't hold rcu_read_lock() around code that can block. So we
2310 * find a thin with the rcu lock held; bump a refcount; then drop
2311 * the lock.
2312 */
2313static struct thin_c *get_first_thin(struct pool *pool)
2314{
2315 struct thin_c *tc = NULL;
2316
2317 rcu_read_lock();
2318 if (!list_empty(&pool->active_thins)) {
2319 tc = list_entry_rcu(pool->active_thins.next, struct thin_c, list);
2320 thin_get(tc);
2321 }
2322 rcu_read_unlock();
2323
2324 return tc;
2325}
2326
2327static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc)
2328{
2329 struct thin_c *old_tc = tc;
2330
2331 rcu_read_lock();
2332 list_for_each_entry_continue_rcu(tc, &pool->active_thins, list) {
2333 thin_get(tc);
2334 thin_put(old_tc);
2335 rcu_read_unlock();
2336 return tc;
2337 }
2338 thin_put(old_tc);
2339 rcu_read_unlock();
2340
2341 return NULL;
2342}
2343
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002344static void process_deferred_bios(struct pool *pool)
2345{
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002346 struct bio *bio;
Nikos Tsironis4ae280b2019-02-14 20:38:47 +02002347 struct bio_list bios, bio_completions;
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002348 struct thin_c *tc;
2349
Joe Thornberb10ebd32014-04-08 11:29:01 +01002350 tc = get_first_thin(pool);
2351 while (tc) {
Joe Thornbera374bb22014-10-10 13:43:14 +01002352 process_thin_deferred_cells(tc);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002353 process_thin_deferred_bios(tc);
Joe Thornberb10ebd32014-04-08 11:29:01 +01002354 tc = get_next_thin(pool, tc);
2355 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002356
2357 /*
Nikos Tsironis4ae280b2019-02-14 20:38:47 +02002358 * If there are any deferred flush bios, we must commit the metadata
2359 * before issuing them or signaling their completion.
Joe Thornber991d9fa2011-10-31 20:21:18 +00002360 */
2361 bio_list_init(&bios);
Nikos Tsironis4ae280b2019-02-14 20:38:47 +02002362 bio_list_init(&bio_completions);
2363
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04002364 spin_lock_irq(&pool->lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002365 bio_list_merge(&bios, &pool->deferred_flush_bios);
2366 bio_list_init(&pool->deferred_flush_bios);
Nikos Tsironis4ae280b2019-02-14 20:38:47 +02002367
2368 bio_list_merge(&bio_completions, &pool->deferred_flush_completions);
2369 bio_list_init(&pool->deferred_flush_completions);
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04002370 spin_unlock_irq(&pool->lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002371
Nikos Tsironis4ae280b2019-02-14 20:38:47 +02002372 if (bio_list_empty(&bios) && bio_list_empty(&bio_completions) &&
Mike Snitzer4d1662a2014-02-06 06:08:56 -05002373 !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool)))
Joe Thornber991d9fa2011-10-31 20:21:18 +00002374 return;
2375
Joe Thornber020cc3b2013-12-04 15:05:36 -05002376 if (commit(pool)) {
Nikos Tsironis4ae280b2019-02-14 20:38:47 +02002377 bio_list_merge(&bios, &bio_completions);
2378
Joe Thornber991d9fa2011-10-31 20:21:18 +00002379 while ((bio = bio_list_pop(&bios)))
2380 bio_io_error(bio);
2381 return;
2382 }
Joe Thornber905e51b2012-03-28 18:41:27 +01002383 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002384
Nikos Tsironis4ae280b2019-02-14 20:38:47 +02002385 while ((bio = bio_list_pop(&bio_completions)))
2386 bio_endio(bio);
2387
Nikos Tsironis694cfe72019-12-04 16:07:42 +02002388 while ((bio = bio_list_pop(&bios))) {
2389 /*
2390 * The data device was flushed as part of metadata commit,
2391 * so complete redundant flushes immediately.
2392 */
2393 if (bio->bi_opf & REQ_PREFLUSH)
2394 bio_endio(bio);
2395 else
Christoph Hellwiged00aab2020-07-01 10:59:44 +02002396 submit_bio_noacct(bio);
Nikos Tsironis694cfe72019-12-04 16:07:42 +02002397 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002398}
2399
2400static void do_worker(struct work_struct *ws)
2401{
2402 struct pool *pool = container_of(ws, struct pool, worker);
2403
Joe Thornber7d327fe2014-10-06 15:45:59 +01002404 throttle_work_start(&pool->throttle);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002405 dm_pool_issue_prefetches(pool->pmd);
Joe Thornber7d327fe2014-10-06 15:45:59 +01002406 throttle_work_update(&pool->throttle);
Joe Thornbere49e5822012-07-27 15:08:16 +01002407 process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
Joe Thornber7d327fe2014-10-06 15:45:59 +01002408 throttle_work_update(&pool->throttle);
Joe Thornbere49e5822012-07-27 15:08:16 +01002409 process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
Joe Thornber7d327fe2014-10-06 15:45:59 +01002410 throttle_work_update(&pool->throttle);
Joe Thornber2a0fbff2016-07-01 14:00:02 +01002411 process_prepared(pool, &pool->prepared_discards_pt2, &pool->process_prepared_discard_pt2);
2412 throttle_work_update(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002413 process_deferred_bios(pool);
Joe Thornber7d327fe2014-10-06 15:45:59 +01002414 throttle_work_complete(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002415}
2416
Joe Thornber905e51b2012-03-28 18:41:27 +01002417/*
2418 * We want to commit periodically so that not too much
2419 * unwritten data builds up.
2420 */
2421static void do_waker(struct work_struct *ws)
2422{
2423 struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
2424 wake_worker(pool);
2425 queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
2426}
2427
Joe Thornber85ad6432014-05-09 15:59:38 +01002428/*
2429 * We're holding onto IO to allow userland time to react. After the
2430 * timeout either the pool will have been resized (and thus back in
Mike Snitzerbcc696f2015-07-15 16:52:04 -04002431 * PM_WRITE mode), or we degrade to PM_OUT_OF_DATA_SPACE w/ error_if_no_space.
Joe Thornber85ad6432014-05-09 15:59:38 +01002432 */
2433static void do_no_space_timeout(struct work_struct *ws)
2434{
2435 struct pool *pool = container_of(to_delayed_work(ws), struct pool,
2436 no_space_timeout);
2437
Mike Snitzerbcc696f2015-07-15 16:52:04 -04002438 if (get_pool_mode(pool) == PM_OUT_OF_DATA_SPACE && !pool->pf.error_if_no_space) {
2439 pool->pf.error_if_no_space = true;
Mike Snitzerf6c36752018-12-11 13:31:40 -05002440 notify_of_pool_mode_change(pool);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002441 error_retry_list_with_code(pool, BLK_STS_NOSPC);
Mike Snitzerbcc696f2015-07-15 16:52:04 -04002442 }
Joe Thornber85ad6432014-05-09 15:59:38 +01002443}
2444
Joe Thornber991d9fa2011-10-31 20:21:18 +00002445/*----------------------------------------------------------------*/
2446
Joe Thornbere7a3e872014-05-13 16:14:14 -04002447struct pool_work {
Joe Thornber738211f2014-03-03 15:52:28 +00002448 struct work_struct worker;
Joe Thornbere7a3e872014-05-13 16:14:14 -04002449 struct completion complete;
Joe Thornber738211f2014-03-03 15:52:28 +00002450};
2451
Joe Thornbere7a3e872014-05-13 16:14:14 -04002452static struct pool_work *to_pool_work(struct work_struct *ws)
Joe Thornber738211f2014-03-03 15:52:28 +00002453{
Joe Thornbere7a3e872014-05-13 16:14:14 -04002454 return container_of(ws, struct pool_work, worker);
2455}
2456
2457static void pool_work_complete(struct pool_work *pw)
2458{
2459 complete(&pw->complete);
2460}
2461
2462static void pool_work_wait(struct pool_work *pw, struct pool *pool,
2463 void (*fn)(struct work_struct *))
2464{
2465 INIT_WORK_ONSTACK(&pw->worker, fn);
2466 init_completion(&pw->complete);
2467 queue_work(pool->wq, &pw->worker);
2468 wait_for_completion(&pw->complete);
2469}
2470
2471/*----------------------------------------------------------------*/
2472
2473struct noflush_work {
2474 struct pool_work pw;
2475 struct thin_c *tc;
2476};
2477
2478static struct noflush_work *to_noflush(struct work_struct *ws)
2479{
2480 return container_of(to_pool_work(ws), struct noflush_work, pw);
Joe Thornber738211f2014-03-03 15:52:28 +00002481}
2482
2483static void do_noflush_start(struct work_struct *ws)
2484{
Joe Thornbere7a3e872014-05-13 16:14:14 -04002485 struct noflush_work *w = to_noflush(ws);
Joe Thornber738211f2014-03-03 15:52:28 +00002486 w->tc->requeue_mode = true;
2487 requeue_io(w->tc);
Joe Thornbere7a3e872014-05-13 16:14:14 -04002488 pool_work_complete(&w->pw);
Joe Thornber738211f2014-03-03 15:52:28 +00002489}
2490
2491static void do_noflush_stop(struct work_struct *ws)
2492{
Joe Thornbere7a3e872014-05-13 16:14:14 -04002493 struct noflush_work *w = to_noflush(ws);
Joe Thornber738211f2014-03-03 15:52:28 +00002494 w->tc->requeue_mode = false;
Joe Thornbere7a3e872014-05-13 16:14:14 -04002495 pool_work_complete(&w->pw);
Joe Thornber738211f2014-03-03 15:52:28 +00002496}
2497
2498static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *))
2499{
2500 struct noflush_work w;
2501
Joe Thornber738211f2014-03-03 15:52:28 +00002502 w.tc = tc;
Joe Thornbere7a3e872014-05-13 16:14:14 -04002503 pool_work_wait(&w.pw, tc->pool, fn);
Joe Thornber738211f2014-03-03 15:52:28 +00002504}
2505
2506/*----------------------------------------------------------------*/
2507
Joe Thornber34fbcf62015-04-16 12:58:35 +01002508static bool passdown_enabled(struct pool_c *pt)
2509{
2510 return pt->adjusted_pf.discard_passdown;
2511}
2512
2513static void set_discard_callbacks(struct pool *pool)
2514{
2515 struct pool_c *pt = pool->ti->private;
2516
2517 if (passdown_enabled(pt)) {
2518 pool->process_discard_cell = process_discard_cell_passdown;
Joe Thornber2a0fbff2016-07-01 14:00:02 +01002519 pool->process_prepared_discard = process_prepared_discard_passdown_pt1;
2520 pool->process_prepared_discard_pt2 = process_prepared_discard_passdown_pt2;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002521 } else {
2522 pool->process_discard_cell = process_discard_cell_no_passdown;
2523 pool->process_prepared_discard = process_prepared_discard_no_passdown;
2524 }
2525}
2526
Mike Snitzer8b64e882013-12-20 14:27:28 -05002527static void set_pool_mode(struct pool *pool, enum pool_mode new_mode)
Joe Thornbere49e5822012-07-27 15:08:16 +01002528{
Mike Snitzercdc2b412014-02-14 18:10:55 -05002529 struct pool_c *pt = pool->ti->private;
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002530 bool needs_check = dm_pool_metadata_needs_check(pool->pmd);
2531 enum pool_mode old_mode = get_pool_mode(pool);
Mark Rutland6aa7de02017-10-23 14:07:29 -07002532 unsigned long no_space_timeout = READ_ONCE(no_space_timeout_secs) * HZ;
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002533
2534 /*
2535 * Never allow the pool to transition to PM_WRITE mode if user
2536 * intervention is required to verify metadata and data consistency.
2537 */
2538 if (new_mode == PM_WRITE && needs_check) {
2539 DMERR("%s: unable to switch pool to write mode until repaired.",
2540 dm_device_name(pool->pool_md));
2541 if (old_mode != new_mode)
2542 new_mode = old_mode;
2543 else
2544 new_mode = PM_READ_ONLY;
2545 }
2546 /*
2547 * If we were in PM_FAIL mode, rollback of metadata failed. We're
2548 * not going to recover without a thin_repair. So we never let the
2549 * pool move out of the old mode.
2550 */
2551 if (old_mode == PM_FAIL)
2552 new_mode = old_mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002553
Mike Snitzer8b64e882013-12-20 14:27:28 -05002554 switch (new_mode) {
Joe Thornbere49e5822012-07-27 15:08:16 +01002555 case PM_FAIL:
Joe Thornber5383ef32013-12-04 16:30:01 -05002556 dm_pool_metadata_read_only(pool->pmd);
Joe Thornbere49e5822012-07-27 15:08:16 +01002557 pool->process_bio = process_bio_fail;
2558 pool->process_discard = process_bio_fail;
Joe Thornbera374bb22014-10-10 13:43:14 +01002559 pool->process_cell = process_cell_fail;
2560 pool->process_discard_cell = process_cell_fail;
Joe Thornbere49e5822012-07-27 15:08:16 +01002561 pool->process_prepared_mapping = process_prepared_mapping_fail;
2562 pool->process_prepared_discard = process_prepared_discard_fail;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002563
2564 error_retry_list(pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01002565 break;
2566
Joe Thornber3ab91822018-09-10 16:50:09 +01002567 case PM_OUT_OF_METADATA_SPACE:
Joe Thornbere49e5822012-07-27 15:08:16 +01002568 case PM_READ_ONLY:
Joe Thornber3e1a0692014-03-03 16:03:26 +00002569 dm_pool_metadata_read_only(pool->pmd);
2570 pool->process_bio = process_bio_read_only;
2571 pool->process_discard = process_bio_success;
Joe Thornbera374bb22014-10-10 13:43:14 +01002572 pool->process_cell = process_cell_read_only;
2573 pool->process_discard_cell = process_cell_success;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002574 pool->process_prepared_mapping = process_prepared_mapping_fail;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002575 pool->process_prepared_discard = process_prepared_discard_success;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002576
2577 error_retry_list(pool);
2578 break;
2579
2580 case PM_OUT_OF_DATA_SPACE:
2581 /*
2582 * Ideally we'd never hit this state; the low water mark
2583 * would trigger userland to extend the pool before we
2584 * completely run out of data space. However, many small
2585 * IOs to unprovisioned space can consume data space at an
2586 * alarming rate. Adjust your low water mark if you're
2587 * frequently seeing this mode.
2588 */
Mike Snitzerc3667cc2016-03-10 11:31:35 -05002589 pool->out_of_data_space = true;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002590 pool->process_bio = process_bio_read_only;
Joe Thornbera374bb22014-10-10 13:43:14 +01002591 pool->process_discard = process_discard_bio;
2592 pool->process_cell = process_cell_read_only;
Joe Thornber3e1a0692014-03-03 16:03:26 +00002593 pool->process_prepared_mapping = process_prepared_mapping;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002594 set_discard_callbacks(pool);
Joe Thornber85ad6432014-05-09 15:59:38 +01002595
Mike Snitzer80c57892014-05-20 13:38:33 -04002596 if (!pool->pf.error_if_no_space && no_space_timeout)
2597 queue_delayed_work(pool->wq, &pool->no_space_timeout, no_space_timeout);
Joe Thornbere49e5822012-07-27 15:08:16 +01002598 break;
2599
2600 case PM_WRITE:
Hou Tao75294442018-08-02 16:18:24 +08002601 if (old_mode == PM_OUT_OF_DATA_SPACE)
2602 cancel_delayed_work_sync(&pool->no_space_timeout);
Mike Snitzerc3667cc2016-03-10 11:31:35 -05002603 pool->out_of_data_space = false;
Mike Snitzer172c2382015-11-06 10:53:01 -05002604 pool->pf.error_if_no_space = pt->requested_pf.error_if_no_space;
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002605 dm_pool_metadata_read_write(pool->pmd);
Joe Thornbere49e5822012-07-27 15:08:16 +01002606 pool->process_bio = process_bio;
Joe Thornbera374bb22014-10-10 13:43:14 +01002607 pool->process_discard = process_discard_bio;
2608 pool->process_cell = process_cell;
Joe Thornbere49e5822012-07-27 15:08:16 +01002609 pool->process_prepared_mapping = process_prepared_mapping;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002610 set_discard_callbacks(pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01002611 break;
2612 }
Mike Snitzer8b64e882013-12-20 14:27:28 -05002613
2614 pool->pf.mode = new_mode;
Mike Snitzercdc2b412014-02-14 18:10:55 -05002615 /*
2616 * The pool mode may have changed, sync it so bind_control_target()
2617 * doesn't cause an unexpected mode transition on resume.
2618 */
2619 pt->adjusted_pf.mode = new_mode;
Mike Snitzerf6c36752018-12-11 13:31:40 -05002620
2621 if (old_mode != new_mode)
2622 notify_of_pool_mode_change(pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01002623}
2624
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002625static void abort_transaction(struct pool *pool)
2626{
2627 const char *dev_name = dm_device_name(pool->pool_md);
2628
2629 DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
2630 if (dm_pool_abort_metadata(pool->pmd)) {
2631 DMERR("%s: failed to abort metadata transaction", dev_name);
2632 set_pool_mode(pool, PM_FAIL);
2633 }
2634
2635 if (dm_pool_metadata_set_needs_check(pool->pmd)) {
2636 DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
2637 set_pool_mode(pool, PM_FAIL);
2638 }
2639}
2640
Joe Thornberb5330652013-12-04 19:51:33 -05002641static void metadata_operation_failed(struct pool *pool, const char *op, int r)
2642{
2643 DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
2644 dm_device_name(pool->pool_md), op, r);
2645
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002646 abort_transaction(pool);
Joe Thornberb5330652013-12-04 19:51:33 -05002647 set_pool_mode(pool, PM_READ_ONLY);
2648}
2649
Joe Thornbere49e5822012-07-27 15:08:16 +01002650/*----------------------------------------------------------------*/
2651
Joe Thornber991d9fa2011-10-31 20:21:18 +00002652/*
2653 * Mapping functions.
2654 */
2655
2656/*
2657 * Called only while mapping a thin bio to hand it over to the workqueue.
2658 */
2659static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
2660{
Joe Thornber991d9fa2011-10-31 20:21:18 +00002661 struct pool *pool = tc->pool;
2662
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04002663 spin_lock_irq(&tc->lock);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002664 bio_list_add(&tc->deferred_bio_list, bio);
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04002665 spin_unlock_irq(&tc->lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002666
2667 wake_worker(pool);
2668}
2669
Joe Thornber7d327fe2014-10-06 15:45:59 +01002670static void thin_defer_bio_with_throttle(struct thin_c *tc, struct bio *bio)
2671{
2672 struct pool *pool = tc->pool;
2673
2674 throttle_lock(&pool->throttle);
2675 thin_defer_bio(tc, bio);
2676 throttle_unlock(&pool->throttle);
2677}
2678
Joe Thornbera374bb22014-10-10 13:43:14 +01002679static void thin_defer_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
2680{
Joe Thornbera374bb22014-10-10 13:43:14 +01002681 struct pool *pool = tc->pool;
2682
2683 throttle_lock(&pool->throttle);
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04002684 spin_lock_irq(&tc->lock);
Joe Thornbera374bb22014-10-10 13:43:14 +01002685 list_add_tail(&cell->user_list, &tc->deferred_cells);
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04002686 spin_unlock_irq(&tc->lock);
Joe Thornbera374bb22014-10-10 13:43:14 +01002687 throttle_unlock(&pool->throttle);
2688
2689 wake_worker(pool);
2690}
2691
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002692static void thin_hook_bio(struct thin_c *tc, struct bio *bio)
Joe Thornbereb2aa482012-03-28 18:41:28 +01002693{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002694 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01002695
2696 h->tc = tc;
2697 h->shared_read_entry = NULL;
Joe Thornbere8088072012-12-21 20:23:31 +00002698 h->all_io_entry = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01002699 h->overwrite_mapping = NULL;
Joe Thornber34fbcf62015-04-16 12:58:35 +01002700 h->cell = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01002701}
2702
Joe Thornber991d9fa2011-10-31 20:21:18 +00002703/*
2704 * Non-blocking function called from the thin target's map function.
2705 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002706static int thin_bio_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002707{
2708 int r;
2709 struct thin_c *tc = ti->private;
2710 dm_block_t block = get_bio_block(tc, bio);
2711 struct dm_thin_device *td = tc->td;
2712 struct dm_thin_lookup_result result;
Joe Thornbera374bb22014-10-10 13:43:14 +01002713 struct dm_bio_prison_cell *virt_cell, *data_cell;
Joe Thornbere8088072012-12-21 20:23:31 +00002714 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002715
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002716 thin_hook_bio(tc, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01002717
Joe Thornber738211f2014-03-03 15:52:28 +00002718 if (tc->requeue_mode) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002719 bio->bi_status = BLK_STS_DM_REQUEUE;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002720 bio_endio(bio);
Joe Thornber738211f2014-03-03 15:52:28 +00002721 return DM_MAPIO_SUBMITTED;
2722 }
2723
Joe Thornbere49e5822012-07-27 15:08:16 +01002724 if (get_pool_mode(tc->pool) == PM_FAIL) {
2725 bio_io_error(bio);
2726 return DM_MAPIO_SUBMITTED;
2727 }
2728
Christoph Hellwigf73f44e2017-01-27 08:30:47 -07002729 if (op_is_flush(bio->bi_opf) || bio_op(bio) == REQ_OP_DISCARD) {
Joe Thornber7d327fe2014-10-06 15:45:59 +01002730 thin_defer_bio_with_throttle(tc, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002731 return DM_MAPIO_SUBMITTED;
2732 }
2733
Joe Thornberc822ed962014-10-10 09:41:09 +01002734 /*
2735 * We must hold the virtual cell before doing the lookup, otherwise
2736 * there's a race with discard.
2737 */
2738 build_virtual_key(tc->td, block, &key);
Joe Thornbera374bb22014-10-10 13:43:14 +01002739 if (bio_detain(tc->pool, &key, bio, &virt_cell))
Joe Thornberc822ed962014-10-10 09:41:09 +01002740 return DM_MAPIO_SUBMITTED;
2741
Joe Thornber991d9fa2011-10-31 20:21:18 +00002742 r = dm_thin_find_block(td, block, 0, &result);
2743
2744 /*
2745 * Note that we defer readahead too.
2746 */
2747 switch (r) {
2748 case 0:
2749 if (unlikely(result.shared)) {
2750 /*
2751 * We have a race condition here between the
2752 * result.shared value returned by the lookup and
2753 * snapshot creation, which may cause new
2754 * sharing.
2755 *
2756 * To avoid this always quiesce the origin before
2757 * taking the snap. You want to do this anyway to
2758 * ensure a consistent application view
2759 * (i.e. lockfs).
2760 *
2761 * More distant ancestors are irrelevant. The
2762 * shared flag will be set in their case.
2763 */
Joe Thornbera374bb22014-10-10 13:43:14 +01002764 thin_defer_cell(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002765 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002766 }
Joe Thornbere8088072012-12-21 20:23:31 +00002767
Joe Thornbere8088072012-12-21 20:23:31 +00002768 build_data_key(tc->td, result.block, &key);
Joe Thornbera374bb22014-10-10 13:43:14 +01002769 if (bio_detain(tc->pool, &key, bio, &data_cell)) {
2770 cell_defer_no_holder(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002771 return DM_MAPIO_SUBMITTED;
2772 }
2773
2774 inc_all_io_entry(tc->pool, bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01002775 cell_defer_no_holder(tc, data_cell);
2776 cell_defer_no_holder(tc, virt_cell);
Joe Thornbere8088072012-12-21 20:23:31 +00002777
2778 remap(tc, bio, result.block);
2779 return DM_MAPIO_REMAPPED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002780
2781 case -ENODATA:
Joe Thornbere49e5822012-07-27 15:08:16 +01002782 case -EWOULDBLOCK:
Joe Thornbera374bb22014-10-10 13:43:14 +01002783 thin_defer_cell(tc, virt_cell);
Joe Thornber2aab3852012-12-21 20:23:33 +00002784 return DM_MAPIO_SUBMITTED;
Joe Thornbere49e5822012-07-27 15:08:16 +01002785
2786 default:
2787 /*
2788 * Must always call bio_io_error on failure.
2789 * dm_thin_find_block can fail with -EINVAL if the
2790 * pool is switched to fail-io mode.
2791 */
2792 bio_io_error(bio);
Joe Thornbera374bb22014-10-10 13:43:14 +01002793 cell_defer_no_holder(tc, virt_cell);
Joe Thornber2aab3852012-12-21 20:23:33 +00002794 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002795 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002796}
2797
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002798static void requeue_bios(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002799{
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002800 struct thin_c *tc;
2801
2802 rcu_read_lock();
2803 list_for_each_entry_rcu(tc, &pool->active_thins, list) {
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04002804 spin_lock_irq(&tc->lock);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002805 bio_list_merge(&tc->deferred_bio_list, &tc->retry_on_resume_list);
2806 bio_list_init(&tc->retry_on_resume_list);
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04002807 spin_unlock_irq(&tc->lock);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002808 }
2809 rcu_read_unlock();
Joe Thornber991d9fa2011-10-31 20:21:18 +00002810}
2811
2812/*----------------------------------------------------------------
2813 * Binding of control targets to a pool object
2814 *--------------------------------------------------------------*/
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002815static bool data_dev_supports_discard(struct pool_c *pt)
2816{
2817 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
2818
Xu Wang695902b2021-03-19 08:11:13 +00002819 return blk_queue_discard(q);
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002820}
2821
Joe Thornber58051b92013-03-20 17:21:25 +00002822static bool is_factor(sector_t block_size, uint32_t n)
2823{
2824 return !sector_div(block_size, n);
2825}
2826
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002827/*
2828 * If discard_passdown was enabled verify that the data device
Mike Snitzer0424caa2012-09-26 23:45:47 +01002829 * supports discards. Disable discard_passdown if not.
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002830 */
Mike Snitzer0424caa2012-09-26 23:45:47 +01002831static void disable_passdown_if_not_supported(struct pool_c *pt)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002832{
Mike Snitzer0424caa2012-09-26 23:45:47 +01002833 struct pool *pool = pt->pool;
2834 struct block_device *data_bdev = pt->data_dev->bdev;
2835 struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
Mike Snitzer0424caa2012-09-26 23:45:47 +01002836 const char *reason = NULL;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002837 char buf[BDEVNAME_SIZE];
2838
Mike Snitzer0424caa2012-09-26 23:45:47 +01002839 if (!pt->adjusted_pf.discard_passdown)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002840 return;
2841
Mike Snitzer0424caa2012-09-26 23:45:47 +01002842 if (!data_dev_supports_discard(pt))
2843 reason = "discard unsupported";
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002844
Mike Snitzer0424caa2012-09-26 23:45:47 +01002845 else if (data_limits->max_discard_sectors < pool->sectors_per_block)
2846 reason = "max discard sectors smaller than a block";
2847
Mike Snitzer0424caa2012-09-26 23:45:47 +01002848 if (reason) {
2849 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
2850 pt->adjusted_pf.discard_passdown = false;
2851 }
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002852}
2853
Joe Thornber991d9fa2011-10-31 20:21:18 +00002854static int bind_control_target(struct pool *pool, struct dm_target *ti)
2855{
2856 struct pool_c *pt = ti->private;
2857
Joe Thornbere49e5822012-07-27 15:08:16 +01002858 /*
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002859 * We want to make sure that a pool in PM_FAIL mode is never upgraded.
Joe Thornbere49e5822012-07-27 15:08:16 +01002860 */
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002861 enum pool_mode old_mode = get_pool_mode(pool);
Mike Snitzer0424caa2012-09-26 23:45:47 +01002862 enum pool_mode new_mode = pt->adjusted_pf.mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01002863
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002864 /*
Mike Snitzer8b64e882013-12-20 14:27:28 -05002865 * Don't change the pool's mode until set_pool_mode() below.
2866 * Otherwise the pool's process_* function pointers may
2867 * not match the desired pool mode.
2868 */
2869 pt->adjusted_pf.mode = old_mode;
2870
2871 pool->ti = ti;
2872 pool->pf = pt->adjusted_pf;
2873 pool->low_water_blocks = pt->low_water_blocks;
2874
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002875 set_pool_mode(pool, new_mode);
Mike Snitzerf4026932012-05-19 01:01:01 +01002876
Joe Thornber991d9fa2011-10-31 20:21:18 +00002877 return 0;
2878}
2879
2880static void unbind_control_target(struct pool *pool, struct dm_target *ti)
2881{
2882 if (pool->ti == ti)
2883 pool->ti = NULL;
2884}
2885
2886/*----------------------------------------------------------------
2887 * Pool creation
2888 *--------------------------------------------------------------*/
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002889/* Initialize pool features. */
2890static void pool_features_init(struct pool_features *pf)
2891{
Joe Thornbere49e5822012-07-27 15:08:16 +01002892 pf->mode = PM_WRITE;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002893 pf->zero_new_blocks = true;
2894 pf->discard_enabled = true;
2895 pf->discard_passdown = true;
Mike Snitzer787a996c2013-12-06 16:21:43 -05002896 pf->error_if_no_space = false;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002897}
2898
Joe Thornber991d9fa2011-10-31 20:21:18 +00002899static void __pool_destroy(struct pool *pool)
2900{
2901 __pool_table_remove(pool);
2902
Joe Thornbera822c832015-07-03 10:22:42 +01002903 vfree(pool->cell_sort_array);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002904 if (dm_pool_metadata_close(pool->pmd) < 0)
2905 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2906
Mike Snitzer44feb382012-10-12 21:02:10 +01002907 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002908 dm_kcopyd_client_destroy(pool->copier);
2909
2910 if (pool->wq)
2911 destroy_workqueue(pool->wq);
2912
2913 if (pool->next_mapping)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002914 mempool_free(pool->next_mapping, &pool->mapping_pool);
2915 mempool_exit(&pool->mapping_pool);
Mikulas Patockaf06c03d2020-01-13 15:41:53 -05002916 bio_uninit(&pool->flush_bio);
Mike Snitzer44feb382012-10-12 21:02:10 +01002917 dm_deferred_set_destroy(pool->shared_read_ds);
2918 dm_deferred_set_destroy(pool->all_io_ds);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002919 kfree(pool);
2920}
2921
Mike Snitzera24c2562012-06-03 00:30:00 +01002922static struct kmem_cache *_new_mapping_cache;
Mike Snitzera24c2562012-06-03 00:30:00 +01002923
Joe Thornber991d9fa2011-10-31 20:21:18 +00002924static struct pool *pool_create(struct mapped_device *pool_md,
2925 struct block_device *metadata_dev,
Mikulas Patocka873937e2020-01-13 15:04:37 -05002926 struct block_device *data_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002927 unsigned long block_size,
2928 int read_only, char **error)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002929{
2930 int r;
2931 void *err_p;
2932 struct pool *pool;
2933 struct dm_pool_metadata *pmd;
Joe Thornbere49e5822012-07-27 15:08:16 +01002934 bool format_device = read_only ? false : true;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002935
Joe Thornbere49e5822012-07-27 15:08:16 +01002936 pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002937 if (IS_ERR(pmd)) {
2938 *error = "Error creating metadata object";
2939 return (struct pool *)pmd;
2940 }
2941
Kent Overstreetd3775352018-06-05 05:26:33 -04002942 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002943 if (!pool) {
2944 *error = "Error allocating memory for pool";
2945 err_p = ERR_PTR(-ENOMEM);
2946 goto bad_pool;
2947 }
2948
2949 pool->pmd = pmd;
2950 pool->sectors_per_block = block_size;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +01002951 if (block_size & (block_size - 1))
2952 pool->sectors_per_block_shift = -1;
2953 else
2954 pool->sectors_per_block_shift = __ffs(block_size);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002955 pool->low_water_blocks = 0;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002956 pool_features_init(&pool->pf);
Joe Thornbera195db22014-10-06 16:30:06 -04002957 pool->prison = dm_bio_prison_create();
Joe Thornber991d9fa2011-10-31 20:21:18 +00002958 if (!pool->prison) {
2959 *error = "Error creating pool's bio prison";
2960 err_p = ERR_PTR(-ENOMEM);
2961 goto bad_prison;
2962 }
2963
Mikulas Patockadf5d2e92013-03-01 22:45:49 +00002964 pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002965 if (IS_ERR(pool->copier)) {
2966 r = PTR_ERR(pool->copier);
2967 *error = "Error creating pool's kcopyd client";
2968 err_p = ERR_PTR(r);
2969 goto bad_kcopyd_client;
2970 }
2971
2972 /*
2973 * Create singlethreaded workqueue that will service all devices
2974 * that use this metadata.
2975 */
2976 pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2977 if (!pool->wq) {
2978 *error = "Error creating pool's workqueue";
2979 err_p = ERR_PTR(-ENOMEM);
2980 goto bad_wq;
2981 }
2982
Joe Thornber7d327fe2014-10-06 15:45:59 +01002983 throttle_init(&pool->throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002984 INIT_WORK(&pool->worker, do_worker);
Joe Thornber905e51b2012-03-28 18:41:27 +01002985 INIT_DELAYED_WORK(&pool->waker, do_waker);
Joe Thornber85ad6432014-05-09 15:59:38 +01002986 INIT_DELAYED_WORK(&pool->no_space_timeout, do_no_space_timeout);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002987 spin_lock_init(&pool->lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002988 bio_list_init(&pool->deferred_flush_bios);
Nikos Tsironis4ae280b2019-02-14 20:38:47 +02002989 bio_list_init(&pool->deferred_flush_completions);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002990 INIT_LIST_HEAD(&pool->prepared_mappings);
Joe Thornber104655f2012-03-28 18:41:28 +01002991 INIT_LIST_HEAD(&pool->prepared_discards);
Joe Thornber2a0fbff2016-07-01 14:00:02 +01002992 INIT_LIST_HEAD(&pool->prepared_discards_pt2);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002993 INIT_LIST_HEAD(&pool->active_thins);
Joe Thornber88a66212013-12-04 20:16:12 -05002994 pool->low_water_triggered = false;
Mike Snitzer80e96c52014-11-07 15:09:46 -05002995 pool->suspended = true;
Mike Snitzerc3667cc2016-03-10 11:31:35 -05002996 pool->out_of_data_space = false;
Mikulas Patockaf06c03d2020-01-13 15:41:53 -05002997 bio_init(&pool->flush_bio, NULL, 0);
Mike Snitzer44feb382012-10-12 21:02:10 +01002998
2999 pool->shared_read_ds = dm_deferred_set_create();
3000 if (!pool->shared_read_ds) {
3001 *error = "Error creating pool's shared read deferred set";
3002 err_p = ERR_PTR(-ENOMEM);
3003 goto bad_shared_read_ds;
3004 }
3005
3006 pool->all_io_ds = dm_deferred_set_create();
3007 if (!pool->all_io_ds) {
3008 *error = "Error creating pool's all io deferred set";
3009 err_p = ERR_PTR(-ENOMEM);
3010 goto bad_all_io_ds;
3011 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003012
3013 pool->next_mapping = NULL;
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003014 r = mempool_init_slab_pool(&pool->mapping_pool, MAPPING_POOL_SIZE,
3015 _new_mapping_cache);
3016 if (r) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00003017 *error = "Error creating pool's mapping mempool";
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003018 err_p = ERR_PTR(r);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003019 goto bad_mapping_pool;
3020 }
3021
Kees Cook42bc47b2018-06-12 14:27:11 -07003022 pool->cell_sort_array =
3023 vmalloc(array_size(CELL_SORT_ARRAY_SIZE,
3024 sizeof(*pool->cell_sort_array)));
Joe Thornbera822c832015-07-03 10:22:42 +01003025 if (!pool->cell_sort_array) {
3026 *error = "Error allocating cell sort array";
3027 err_p = ERR_PTR(-ENOMEM);
3028 goto bad_sort_array;
3029 }
3030
Joe Thornber991d9fa2011-10-31 20:21:18 +00003031 pool->ref_count = 1;
Joe Thornber905e51b2012-03-28 18:41:27 +01003032 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003033 pool->pool_md = pool_md;
3034 pool->md_dev = metadata_dev;
Mikulas Patocka873937e2020-01-13 15:04:37 -05003035 pool->data_dev = data_dev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003036 __pool_table_insert(pool);
3037
3038 return pool;
3039
Joe Thornbera822c832015-07-03 10:22:42 +01003040bad_sort_array:
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003041 mempool_exit(&pool->mapping_pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003042bad_mapping_pool:
Mike Snitzer44feb382012-10-12 21:02:10 +01003043 dm_deferred_set_destroy(pool->all_io_ds);
3044bad_all_io_ds:
3045 dm_deferred_set_destroy(pool->shared_read_ds);
3046bad_shared_read_ds:
Joe Thornber991d9fa2011-10-31 20:21:18 +00003047 destroy_workqueue(pool->wq);
3048bad_wq:
3049 dm_kcopyd_client_destroy(pool->copier);
3050bad_kcopyd_client:
Mike Snitzer44feb382012-10-12 21:02:10 +01003051 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003052bad_prison:
3053 kfree(pool);
3054bad_pool:
3055 if (dm_pool_metadata_close(pmd))
3056 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
3057
3058 return err_p;
3059}
3060
3061static void __pool_inc(struct pool *pool)
3062{
3063 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
3064 pool->ref_count++;
3065}
3066
3067static void __pool_dec(struct pool *pool)
3068{
3069 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
3070 BUG_ON(!pool->ref_count);
3071 if (!--pool->ref_count)
3072 __pool_destroy(pool);
3073}
3074
3075static struct pool *__pool_find(struct mapped_device *pool_md,
3076 struct block_device *metadata_dev,
Mikulas Patocka873937e2020-01-13 15:04:37 -05003077 struct block_device *data_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01003078 unsigned long block_size, int read_only,
3079 char **error, int *created)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003080{
3081 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
3082
3083 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01003084 if (pool->pool_md != pool_md) {
3085 *error = "metadata device already in use by a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00003086 return ERR_PTR(-EBUSY);
Mike Snitzerf09996c2012-07-27 15:07:59 +01003087 }
Mikulas Patocka873937e2020-01-13 15:04:37 -05003088 if (pool->data_dev != data_dev) {
3089 *error = "data device already in use by a pool";
3090 return ERR_PTR(-EBUSY);
3091 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003092 __pool_inc(pool);
3093
3094 } else {
3095 pool = __pool_table_lookup(pool_md);
3096 if (pool) {
Mikulas Patocka873937e2020-01-13 15:04:37 -05003097 if (pool->md_dev != metadata_dev || pool->data_dev != data_dev) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01003098 *error = "different pool cannot replace a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00003099 return ERR_PTR(-EINVAL);
Mike Snitzerf09996c2012-07-27 15:07:59 +01003100 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003101 __pool_inc(pool);
3102
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003103 } else {
Mikulas Patocka873937e2020-01-13 15:04:37 -05003104 pool = pool_create(pool_md, metadata_dev, data_dev, block_size, read_only, error);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003105 *created = 1;
3106 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003107 }
3108
3109 return pool;
3110}
3111
3112/*----------------------------------------------------------------
3113 * Pool target methods
3114 *--------------------------------------------------------------*/
3115static void pool_dtr(struct dm_target *ti)
3116{
3117 struct pool_c *pt = ti->private;
3118
3119 mutex_lock(&dm_thin_pool_table.mutex);
3120
3121 unbind_control_target(pt->pool, ti);
3122 __pool_dec(pt->pool);
3123 dm_put_device(ti, pt->metadata_dev);
3124 dm_put_device(ti, pt->data_dev);
3125 kfree(pt);
3126
3127 mutex_unlock(&dm_thin_pool_table.mutex);
3128}
3129
Joe Thornber991d9fa2011-10-31 20:21:18 +00003130static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
3131 struct dm_target *ti)
3132{
3133 int r;
3134 unsigned argc;
3135 const char *arg_name;
3136
Eric Biggers5916a222017-06-22 11:32:45 -07003137 static const struct dm_arg _args[] = {
Mike Snitzer74aa45c2014-01-15 19:07:58 -05003138 {0, 4, "Invalid number of pool feature arguments"},
Joe Thornber991d9fa2011-10-31 20:21:18 +00003139 };
3140
3141 /*
3142 * No feature arguments supplied.
3143 */
3144 if (!as->argc)
3145 return 0;
3146
3147 r = dm_read_arg_group(_args, as, &argc, &ti->error);
3148 if (r)
3149 return -EINVAL;
3150
3151 while (argc && !r) {
3152 arg_name = dm_shift_arg(as);
3153 argc--;
3154
Joe Thornbere49e5822012-07-27 15:08:16 +01003155 if (!strcasecmp(arg_name, "skip_block_zeroing"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003156 pf->zero_new_blocks = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003157
Joe Thornbere49e5822012-07-27 15:08:16 +01003158 else if (!strcasecmp(arg_name, "ignore_discard"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003159 pf->discard_enabled = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01003160
3161 else if (!strcasecmp(arg_name, "no_discard_passdown"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003162 pf->discard_passdown = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01003163
3164 else if (!strcasecmp(arg_name, "read_only"))
3165 pf->mode = PM_READ_ONLY;
3166
Mike Snitzer787a996c2013-12-06 16:21:43 -05003167 else if (!strcasecmp(arg_name, "error_if_no_space"))
3168 pf->error_if_no_space = true;
3169
Joe Thornbere49e5822012-07-27 15:08:16 +01003170 else {
3171 ti->error = "Unrecognised pool feature requested";
3172 r = -EINVAL;
3173 break;
3174 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003175 }
3176
3177 return r;
3178}
3179
Joe Thornberac8c3f32013-05-10 14:37:21 +01003180static void metadata_low_callback(void *context)
3181{
3182 struct pool *pool = context;
3183
3184 DMWARN("%s: reached low water mark for metadata device: sending event.",
3185 dm_device_name(pool->pool_md));
3186
3187 dm_table_event(pool->ti->table);
3188}
3189
Nikos Tsironis694cfe72019-12-04 16:07:42 +02003190/*
3191 * We need to flush the data device **before** committing the metadata.
3192 *
3193 * This ensures that the data blocks of any newly inserted mappings are
3194 * properly written to non-volatile storage and won't be lost in case of a
3195 * crash.
3196 *
3197 * Failure to do so can result in data corruption in the case of internal or
3198 * external snapshots and in the case of newly provisioned blocks, when block
3199 * zeroing is enabled.
3200 */
3201static int metadata_pre_commit_callback(void *context)
3202{
Mikulas Patockaf06c03d2020-01-13 15:41:53 -05003203 struct pool *pool = context;
3204 struct bio *flush_bio = &pool->flush_bio;
Nikos Tsironis694cfe72019-12-04 16:07:42 +02003205
3206 bio_reset(flush_bio);
Mikulas Patockaf06c03d2020-01-13 15:41:53 -05003207 bio_set_dev(flush_bio, pool->data_dev);
Nikos Tsironis694cfe72019-12-04 16:07:42 +02003208 flush_bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
3209
3210 return submit_bio_wait(flush_bio);
3211}
3212
Mike Snitzer7d489352014-02-12 23:58:15 -05003213static sector_t get_dev_size(struct block_device *bdev)
Joe Thornberb17446d2013-05-10 14:37:18 +01003214{
Mike Snitzer7d489352014-02-12 23:58:15 -05003215 return i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
3216}
3217
3218static void warn_if_metadata_device_too_big(struct block_device *bdev)
3219{
3220 sector_t metadata_dev_size = get_dev_size(bdev);
Joe Thornberb17446d2013-05-10 14:37:18 +01003221 char buffer[BDEVNAME_SIZE];
3222
Mike Snitzer7d489352014-02-12 23:58:15 -05003223 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
Joe Thornberb17446d2013-05-10 14:37:18 +01003224 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
3225 bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS);
Mike Snitzer7d489352014-02-12 23:58:15 -05003226}
3227
3228static sector_t get_metadata_dev_size(struct block_device *bdev)
3229{
3230 sector_t metadata_dev_size = get_dev_size(bdev);
3231
3232 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS)
3233 metadata_dev_size = THIN_METADATA_MAX_SECTORS;
Joe Thornberb17446d2013-05-10 14:37:18 +01003234
3235 return metadata_dev_size;
3236}
3237
Joe Thornber24347e92013-05-10 14:37:19 +01003238static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev)
3239{
3240 sector_t metadata_dev_size = get_metadata_dev_size(bdev);
3241
Mike Snitzer7d489352014-02-12 23:58:15 -05003242 sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE);
Joe Thornber24347e92013-05-10 14:37:19 +01003243
3244 return metadata_dev_size;
3245}
3246
Joe Thornber991d9fa2011-10-31 20:21:18 +00003247/*
Joe Thornberac8c3f32013-05-10 14:37:21 +01003248 * When a metadata threshold is crossed a dm event is triggered, and
3249 * userland should respond by growing the metadata device. We could let
3250 * userland set the threshold, like we do with the data threshold, but I'm
3251 * not sure they know enough to do this well.
3252 */
3253static dm_block_t calc_metadata_threshold(struct pool_c *pt)
3254{
3255 /*
3256 * 4M is ample for all ops with the possible exception of thin
3257 * device deletion which is harmless if it fails (just retry the
3258 * delete after you've grown the device).
3259 */
3260 dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4;
3261 return min((dm_block_t)1024ULL /* 4M */, quarter);
3262}
3263
3264/*
Joe Thornber991d9fa2011-10-31 20:21:18 +00003265 * thin-pool <metadata dev> <data dev>
3266 * <data block size (sectors)>
3267 * <low water mark (blocks)>
3268 * [<#feature args> [<arg>]*]
3269 *
3270 * Optional feature arguments are:
3271 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003272 * ignore_discard: disable discard
3273 * no_discard_passdown: don't pass discards down to the data device
Mike Snitzer787a996c2013-12-06 16:21:43 -05003274 * read_only: Don't allow any changes to be made to the pool metadata.
3275 * error_if_no_space: error IOs, instead of queueing, if no space.
Joe Thornber991d9fa2011-10-31 20:21:18 +00003276 */
3277static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
3278{
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003279 int r, pool_created = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003280 struct pool_c *pt;
3281 struct pool *pool;
3282 struct pool_features pf;
3283 struct dm_arg_set as;
3284 struct dm_dev *data_dev;
3285 unsigned long block_size;
3286 dm_block_t low_water_blocks;
3287 struct dm_dev *metadata_dev;
Joe Thornber5d0db962013-05-10 14:37:19 +01003288 fmode_t metadata_mode;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003289
3290 /*
3291 * FIXME Remove validation from scope of lock.
3292 */
3293 mutex_lock(&dm_thin_pool_table.mutex);
3294
3295 if (argc < 4) {
3296 ti->error = "Invalid argument count";
3297 r = -EINVAL;
3298 goto out_unlock;
3299 }
Joe Thornber5d0db962013-05-10 14:37:19 +01003300
Joe Thornber991d9fa2011-10-31 20:21:18 +00003301 as.argc = argc;
3302 as.argv = argv;
3303
Jason Cai (Xiang Feng)70de2cb2019-01-20 22:39:13 +08003304 /* make sure metadata and data are different devices */
3305 if (!strcmp(argv[0], argv[1])) {
3306 ti->error = "Error setting metadata or data device";
3307 r = -EINVAL;
3308 goto out_unlock;
3309 }
3310
Joe Thornber5d0db962013-05-10 14:37:19 +01003311 /*
3312 * Set default pool features.
3313 */
3314 pool_features_init(&pf);
3315
3316 dm_consume_args(&as, 4);
3317 r = parse_pool_features(&as, &pf, ti);
3318 if (r)
3319 goto out_unlock;
3320
3321 metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE);
3322 r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003323 if (r) {
3324 ti->error = "Error opening metadata block device";
3325 goto out_unlock;
3326 }
Mike Snitzer7d489352014-02-12 23:58:15 -05003327 warn_if_metadata_device_too_big(metadata_dev->bdev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003328
3329 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
3330 if (r) {
3331 ti->error = "Error getting data device";
3332 goto out_metadata;
3333 }
3334
3335 if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
3336 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
3337 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01003338 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00003339 ti->error = "Invalid block size";
3340 r = -EINVAL;
3341 goto out;
3342 }
3343
3344 if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
3345 ti->error = "Invalid low water mark";
3346 r = -EINVAL;
3347 goto out;
3348 }
3349
Joe Thornber991d9fa2011-10-31 20:21:18 +00003350 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
3351 if (!pt) {
3352 r = -ENOMEM;
3353 goto out;
3354 }
3355
Mikulas Patocka873937e2020-01-13 15:04:37 -05003356 pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev, data_dev->bdev,
Joe Thornbere49e5822012-07-27 15:08:16 +01003357 block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003358 if (IS_ERR(pool)) {
3359 r = PTR_ERR(pool);
3360 goto out_free_pt;
3361 }
3362
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003363 /*
3364 * 'pool_created' reflects whether this is the first table load.
3365 * Top level discard support is not allowed to be changed after
3366 * initial load. This would require a pool reload to trigger thin
3367 * device changes.
3368 */
3369 if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
3370 ti->error = "Discard support cannot be disabled once enabled";
3371 r = -EINVAL;
3372 goto out_flags_changed;
3373 }
3374
Joe Thornber991d9fa2011-10-31 20:21:18 +00003375 pt->pool = pool;
3376 pt->ti = ti;
3377 pt->metadata_dev = metadata_dev;
3378 pt->data_dev = data_dev;
3379 pt->low_water_blocks = low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +01003380 pt->adjusted_pf = pt->requested_pf = pf;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003381 ti->num_flush_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003382
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003383 /*
3384 * Only need to enable discards if the pool should pass
3385 * them down to the data device. The thin device's discard
3386 * processing will cause mappings to be removed from the btree.
3387 */
3388 if (pf.discard_enabled && pf.discard_passdown) {
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003389 ti->num_discard_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01003390
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003391 /*
3392 * Setting 'discards_supported' circumvents the normal
3393 * stacking of discard limits (this keeps the pool and
3394 * thin devices' discard limits consistent).
3395 */
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01003396 ti->discards_supported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003397 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003398 ti->private = pt;
3399
Joe Thornberac8c3f32013-05-10 14:37:21 +01003400 r = dm_pool_register_metadata_threshold(pt->pool->pmd,
3401 calc_metadata_threshold(pt),
3402 metadata_low_callback,
3403 pool);
3404 if (r)
Mike Snitzerba30670f2015-10-13 12:04:28 -04003405 goto out_flags_changed;
Joe Thornberac8c3f32013-05-10 14:37:21 +01003406
Mikulas Patockaf06c03d2020-01-13 15:41:53 -05003407 dm_pool_register_pre_commit_callback(pool->pmd,
3408 metadata_pre_commit_callback, pool);
3409
Joe Thornber991d9fa2011-10-31 20:21:18 +00003410 mutex_unlock(&dm_thin_pool_table.mutex);
3411
3412 return 0;
3413
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003414out_flags_changed:
3415 __pool_dec(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003416out_free_pt:
3417 kfree(pt);
3418out:
3419 dm_put_device(ti, data_dev);
3420out_metadata:
3421 dm_put_device(ti, metadata_dev);
3422out_unlock:
3423 mutex_unlock(&dm_thin_pool_table.mutex);
3424
3425 return r;
3426}
3427
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003428static int pool_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003429{
3430 int r;
3431 struct pool_c *pt = ti->private;
3432 struct pool *pool = pt->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003433
3434 /*
3435 * As this is a singleton target, ti->begin is always zero.
3436 */
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04003437 spin_lock_irq(&pool->lock);
Christoph Hellwig74d46992017-08-23 19:10:32 +02003438 bio_set_dev(bio, pt->data_dev->bdev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003439 r = DM_MAPIO_REMAPPED;
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04003440 spin_unlock_irq(&pool->lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003441
3442 return r;
3443}
3444
Joe Thornberb17446d2013-05-10 14:37:18 +01003445static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit)
3446{
3447 int r;
3448 struct pool_c *pt = ti->private;
3449 struct pool *pool = pt->pool;
3450 sector_t data_size = ti->len;
3451 dm_block_t sb_data_size;
3452
3453 *need_commit = false;
3454
3455 (void) sector_div(data_size, pool->sectors_per_block);
3456
3457 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
3458 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003459 DMERR("%s: failed to retrieve data device size",
3460 dm_device_name(pool->pool_md));
Joe Thornberb17446d2013-05-10 14:37:18 +01003461 return r;
3462 }
3463
3464 if (data_size < sb_data_size) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003465 DMERR("%s: pool target (%llu blocks) too small: expected %llu",
3466 dm_device_name(pool->pool_md),
Joe Thornberb17446d2013-05-10 14:37:18 +01003467 (unsigned long long)data_size, sb_data_size);
3468 return -EINVAL;
3469
3470 } else if (data_size > sb_data_size) {
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05003471 if (dm_pool_metadata_needs_check(pool->pmd)) {
3472 DMERR("%s: unable to grow the data device until repaired.",
3473 dm_device_name(pool->pool_md));
3474 return 0;
3475 }
3476
Mike Snitzer6f7f51d2013-12-04 10:25:53 -05003477 if (sb_data_size)
3478 DMINFO("%s: growing the data device from %llu to %llu blocks",
3479 dm_device_name(pool->pool_md),
3480 sb_data_size, (unsigned long long)data_size);
Joe Thornberb17446d2013-05-10 14:37:18 +01003481 r = dm_pool_resize_data_dev(pool->pmd, data_size);
3482 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05003483 metadata_operation_failed(pool, "dm_pool_resize_data_dev", r);
Joe Thornberb17446d2013-05-10 14:37:18 +01003484 return r;
3485 }
3486
3487 *need_commit = true;
3488 }
3489
3490 return 0;
3491}
3492
Joe Thornber24347e92013-05-10 14:37:19 +01003493static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit)
3494{
3495 int r;
3496 struct pool_c *pt = ti->private;
3497 struct pool *pool = pt->pool;
3498 dm_block_t metadata_dev_size, sb_metadata_dev_size;
3499
3500 *need_commit = false;
3501
Alasdair G Kergon610bba82013-05-19 18:57:50 +01003502 metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev);
Joe Thornber24347e92013-05-10 14:37:19 +01003503
3504 r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size);
3505 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003506 DMERR("%s: failed to retrieve metadata device size",
3507 dm_device_name(pool->pool_md));
Joe Thornber24347e92013-05-10 14:37:19 +01003508 return r;
3509 }
3510
3511 if (metadata_dev_size < sb_metadata_dev_size) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003512 DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
3513 dm_device_name(pool->pool_md),
Joe Thornber24347e92013-05-10 14:37:19 +01003514 metadata_dev_size, sb_metadata_dev_size);
3515 return -EINVAL;
3516
3517 } else if (metadata_dev_size > sb_metadata_dev_size) {
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05003518 if (dm_pool_metadata_needs_check(pool->pmd)) {
3519 DMERR("%s: unable to grow the metadata device until repaired.",
3520 dm_device_name(pool->pool_md));
3521 return 0;
3522 }
3523
Mike Snitzer7d489352014-02-12 23:58:15 -05003524 warn_if_metadata_device_too_big(pool->md_dev);
Mike Snitzer6f7f51d2013-12-04 10:25:53 -05003525 DMINFO("%s: growing the metadata device from %llu to %llu blocks",
3526 dm_device_name(pool->pool_md),
3527 sb_metadata_dev_size, metadata_dev_size);
Joe Thornber3ab91822018-09-10 16:50:09 +01003528
3529 if (get_pool_mode(pool) == PM_OUT_OF_METADATA_SPACE)
3530 set_pool_mode(pool, PM_WRITE);
3531
Joe Thornber24347e92013-05-10 14:37:19 +01003532 r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size);
3533 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05003534 metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r);
Joe Thornber24347e92013-05-10 14:37:19 +01003535 return r;
3536 }
3537
3538 *need_commit = true;
3539 }
3540
3541 return 0;
3542}
3543
Joe Thornber991d9fa2011-10-31 20:21:18 +00003544/*
3545 * Retrieves the number of blocks of the data device from
3546 * the superblock and compares it to the actual device size,
3547 * thus resizing the data device in case it has grown.
3548 *
3549 * This both copes with opening preallocated data devices in the ctr
3550 * being followed by a resume
3551 * -and-
3552 * calling the resume method individually after userspace has
3553 * grown the data device in reaction to a table event.
3554 */
3555static int pool_preresume(struct dm_target *ti)
3556{
3557 int r;
Joe Thornber24347e92013-05-10 14:37:19 +01003558 bool need_commit1, need_commit2;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003559 struct pool_c *pt = ti->private;
3560 struct pool *pool = pt->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003561
3562 /*
3563 * Take control of the pool object.
3564 */
3565 r = bind_control_target(pool, ti);
3566 if (r)
3567 return r;
3568
Joe Thornberb17446d2013-05-10 14:37:18 +01003569 r = maybe_resize_data_dev(ti, &need_commit1);
3570 if (r)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003571 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003572
Joe Thornber24347e92013-05-10 14:37:19 +01003573 r = maybe_resize_metadata_dev(ti, &need_commit2);
3574 if (r)
3575 return r;
3576
3577 if (need_commit1 || need_commit2)
Joe Thornber020cc3b2013-12-04 15:05:36 -05003578 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003579
3580 return 0;
3581}
3582
Mike Snitzer583024d2014-10-28 20:58:45 -04003583static void pool_suspend_active_thins(struct pool *pool)
3584{
3585 struct thin_c *tc;
3586
3587 /* Suspend all active thin devices */
3588 tc = get_first_thin(pool);
3589 while (tc) {
3590 dm_internal_suspend_noflush(tc->thin_md);
3591 tc = get_next_thin(pool, tc);
3592 }
3593}
3594
3595static void pool_resume_active_thins(struct pool *pool)
3596{
3597 struct thin_c *tc;
3598
3599 /* Resume all active thin devices */
3600 tc = get_first_thin(pool);
3601 while (tc) {
3602 dm_internal_resume(tc->thin_md);
3603 tc = get_next_thin(pool, tc);
3604 }
3605}
3606
Joe Thornber991d9fa2011-10-31 20:21:18 +00003607static void pool_resume(struct dm_target *ti)
3608{
3609 struct pool_c *pt = ti->private;
3610 struct pool *pool = pt->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003611
Mike Snitzer583024d2014-10-28 20:58:45 -04003612 /*
3613 * Must requeue active_thins' bios and then resume
3614 * active_thins _before_ clearing 'suspend' flag.
3615 */
3616 requeue_bios(pool);
3617 pool_resume_active_thins(pool);
3618
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04003619 spin_lock_irq(&pool->lock);
Joe Thornber88a66212013-12-04 20:16:12 -05003620 pool->low_water_triggered = false;
Mike Snitzer80e96c52014-11-07 15:09:46 -05003621 pool->suspended = false;
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04003622 spin_unlock_irq(&pool->lock);
Mike Snitzer80e96c52014-11-07 15:09:46 -05003623
Joe Thornber905e51b2012-03-28 18:41:27 +01003624 do_waker(&pool->waker.work);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003625}
3626
Mike Snitzer80e96c52014-11-07 15:09:46 -05003627static void pool_presuspend(struct dm_target *ti)
3628{
3629 struct pool_c *pt = ti->private;
3630 struct pool *pool = pt->pool;
Mike Snitzer80e96c52014-11-07 15:09:46 -05003631
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04003632 spin_lock_irq(&pool->lock);
Mike Snitzer80e96c52014-11-07 15:09:46 -05003633 pool->suspended = true;
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04003634 spin_unlock_irq(&pool->lock);
Mike Snitzer583024d2014-10-28 20:58:45 -04003635
3636 pool_suspend_active_thins(pool);
Mike Snitzer80e96c52014-11-07 15:09:46 -05003637}
3638
3639static void pool_presuspend_undo(struct dm_target *ti)
3640{
3641 struct pool_c *pt = ti->private;
3642 struct pool *pool = pt->pool;
Mike Snitzer80e96c52014-11-07 15:09:46 -05003643
Mike Snitzer583024d2014-10-28 20:58:45 -04003644 pool_resume_active_thins(pool);
3645
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04003646 spin_lock_irq(&pool->lock);
Mike Snitzer80e96c52014-11-07 15:09:46 -05003647 pool->suspended = false;
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04003648 spin_unlock_irq(&pool->lock);
Mike Snitzer80e96c52014-11-07 15:09:46 -05003649}
3650
Joe Thornber991d9fa2011-10-31 20:21:18 +00003651static void pool_postsuspend(struct dm_target *ti)
3652{
Joe Thornber991d9fa2011-10-31 20:21:18 +00003653 struct pool_c *pt = ti->private;
3654 struct pool *pool = pt->pool;
3655
Nikolay Borisov18d03e82015-12-17 18:03:35 +02003656 cancel_delayed_work_sync(&pool->waker);
3657 cancel_delayed_work_sync(&pool->no_space_timeout);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003658 flush_workqueue(pool->wq);
Joe Thornber020cc3b2013-12-04 15:05:36 -05003659 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003660}
3661
3662static int check_arg_count(unsigned argc, unsigned args_required)
3663{
3664 if (argc != args_required) {
3665 DMWARN("Message received with %u arguments instead of %u.",
3666 argc, args_required);
3667 return -EINVAL;
3668 }
3669
3670 return 0;
3671}
3672
3673static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
3674{
3675 if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
3676 *dev_id <= MAX_DEV_ID)
3677 return 0;
3678
3679 if (warning)
3680 DMWARN("Message received with invalid device id: %s", arg);
3681
3682 return -EINVAL;
3683}
3684
3685static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
3686{
3687 dm_thin_id dev_id;
3688 int r;
3689
3690 r = check_arg_count(argc, 2);
3691 if (r)
3692 return r;
3693
3694 r = read_dev_id(argv[1], &dev_id, 1);
3695 if (r)
3696 return r;
3697
3698 r = dm_pool_create_thin(pool->pmd, dev_id);
3699 if (r) {
3700 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
3701 argv[1]);
3702 return r;
3703 }
3704
3705 return 0;
3706}
3707
3708static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3709{
3710 dm_thin_id dev_id;
3711 dm_thin_id origin_dev_id;
3712 int r;
3713
3714 r = check_arg_count(argc, 3);
3715 if (r)
3716 return r;
3717
3718 r = read_dev_id(argv[1], &dev_id, 1);
3719 if (r)
3720 return r;
3721
3722 r = read_dev_id(argv[2], &origin_dev_id, 1);
3723 if (r)
3724 return r;
3725
3726 r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
3727 if (r) {
3728 DMWARN("Creation of new snapshot %s of device %s failed.",
3729 argv[1], argv[2]);
3730 return r;
3731 }
3732
3733 return 0;
3734}
3735
3736static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
3737{
3738 dm_thin_id dev_id;
3739 int r;
3740
3741 r = check_arg_count(argc, 2);
3742 if (r)
3743 return r;
3744
3745 r = read_dev_id(argv[1], &dev_id, 1);
3746 if (r)
3747 return r;
3748
3749 r = dm_pool_delete_thin_device(pool->pmd, dev_id);
3750 if (r)
3751 DMWARN("Deletion of thin device %s failed.", argv[1]);
3752
3753 return r;
3754}
3755
3756static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
3757{
3758 dm_thin_id old_id, new_id;
3759 int r;
3760
3761 r = check_arg_count(argc, 3);
3762 if (r)
3763 return r;
3764
3765 if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
3766 DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
3767 return -EINVAL;
3768 }
3769
3770 if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
3771 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
3772 return -EINVAL;
3773 }
3774
3775 r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
3776 if (r) {
3777 DMWARN("Failed to change transaction id from %s to %s.",
3778 argv[1], argv[2]);
3779 return r;
3780 }
3781
3782 return 0;
3783}
3784
Joe Thornbercc8394d2012-06-03 00:30:01 +01003785static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3786{
3787 int r;
3788
3789 r = check_arg_count(argc, 1);
3790 if (r)
3791 return r;
3792
Joe Thornber020cc3b2013-12-04 15:05:36 -05003793 (void) commit(pool);
Joe Thornber0d200ae2012-07-03 12:55:31 +01003794
Joe Thornbercc8394d2012-06-03 00:30:01 +01003795 r = dm_pool_reserve_metadata_snap(pool->pmd);
3796 if (r)
3797 DMWARN("reserve_metadata_snap message failed.");
3798
3799 return r;
3800}
3801
3802static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3803{
3804 int r;
3805
3806 r = check_arg_count(argc, 1);
3807 if (r)
3808 return r;
3809
3810 r = dm_pool_release_metadata_snap(pool->pmd);
3811 if (r)
3812 DMWARN("release_metadata_snap message failed.");
3813
3814 return r;
3815}
3816
Joe Thornber991d9fa2011-10-31 20:21:18 +00003817/*
3818 * Messages supported:
3819 * create_thin <dev_id>
3820 * create_snap <dev_id> <origin_id>
3821 * delete <dev_id>
Joe Thornber991d9fa2011-10-31 20:21:18 +00003822 * set_transaction_id <current_trans_id> <new_trans_id>
Joe Thornbercc8394d2012-06-03 00:30:01 +01003823 * reserve_metadata_snap
3824 * release_metadata_snap
Joe Thornber991d9fa2011-10-31 20:21:18 +00003825 */
Mike Snitzer1eb5fa82018-02-28 15:59:59 -05003826static int pool_message(struct dm_target *ti, unsigned argc, char **argv,
3827 char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003828{
3829 int r = -EINVAL;
3830 struct pool_c *pt = ti->private;
3831 struct pool *pool = pt->pool;
3832
Joe Thornber3ab91822018-09-10 16:50:09 +01003833 if (get_pool_mode(pool) >= PM_OUT_OF_METADATA_SPACE) {
Joe Thornber2a7eaea2015-01-26 11:38:21 +00003834 DMERR("%s: unable to service pool target messages in READ_ONLY or FAIL mode",
3835 dm_device_name(pool->pool_md));
Mike Snitzerfd467692015-06-09 12:31:26 -04003836 return -EOPNOTSUPP;
Joe Thornber2a7eaea2015-01-26 11:38:21 +00003837 }
3838
Joe Thornber991d9fa2011-10-31 20:21:18 +00003839 if (!strcasecmp(argv[0], "create_thin"))
3840 r = process_create_thin_mesg(argc, argv, pool);
3841
3842 else if (!strcasecmp(argv[0], "create_snap"))
3843 r = process_create_snap_mesg(argc, argv, pool);
3844
3845 else if (!strcasecmp(argv[0], "delete"))
3846 r = process_delete_mesg(argc, argv, pool);
3847
3848 else if (!strcasecmp(argv[0], "set_transaction_id"))
3849 r = process_set_transaction_id_mesg(argc, argv, pool);
3850
Joe Thornbercc8394d2012-06-03 00:30:01 +01003851 else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
3852 r = process_reserve_metadata_snap_mesg(argc, argv, pool);
3853
3854 else if (!strcasecmp(argv[0], "release_metadata_snap"))
3855 r = process_release_metadata_snap_mesg(argc, argv, pool);
3856
Joe Thornber991d9fa2011-10-31 20:21:18 +00003857 else
3858 DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
3859
Joe Thornbere49e5822012-07-27 15:08:16 +01003860 if (!r)
Joe Thornber020cc3b2013-12-04 15:05:36 -05003861 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003862
3863 return r;
3864}
3865
Joe Thornbere49e5822012-07-27 15:08:16 +01003866static void emit_flags(struct pool_features *pf, char *result,
3867 unsigned sz, unsigned maxlen)
3868{
3869 unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
Mike Snitzer787a996c2013-12-06 16:21:43 -05003870 !pf->discard_passdown + (pf->mode == PM_READ_ONLY) +
3871 pf->error_if_no_space;
Joe Thornbere49e5822012-07-27 15:08:16 +01003872 DMEMIT("%u ", count);
3873
3874 if (!pf->zero_new_blocks)
3875 DMEMIT("skip_block_zeroing ");
3876
3877 if (!pf->discard_enabled)
3878 DMEMIT("ignore_discard ");
3879
3880 if (!pf->discard_passdown)
3881 DMEMIT("no_discard_passdown ");
3882
3883 if (pf->mode == PM_READ_ONLY)
3884 DMEMIT("read_only ");
Mike Snitzer787a996c2013-12-06 16:21:43 -05003885
3886 if (pf->error_if_no_space)
3887 DMEMIT("error_if_no_space ");
Joe Thornbere49e5822012-07-27 15:08:16 +01003888}
3889
Joe Thornber991d9fa2011-10-31 20:21:18 +00003890/*
3891 * Status line is:
3892 * <transaction id> <used metadata sectors>/<total metadata sectors>
3893 * <used data sectors>/<total data sectors> <held metadata root>
Mike Snitzere4c78e22015-07-15 11:40:24 -04003894 * <pool mode> <discard config> <no space config> <needs_check>
Joe Thornber991d9fa2011-10-31 20:21:18 +00003895 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003896static void pool_status(struct dm_target *ti, status_type_t type,
3897 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003898{
Joe Thornbere49e5822012-07-27 15:08:16 +01003899 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003900 unsigned sz = 0;
3901 uint64_t transaction_id;
3902 dm_block_t nr_free_blocks_data;
3903 dm_block_t nr_free_blocks_metadata;
3904 dm_block_t nr_blocks_data;
3905 dm_block_t nr_blocks_metadata;
3906 dm_block_t held_root;
Joe Thornber3ab91822018-09-10 16:50:09 +01003907 enum pool_mode mode;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003908 char buf[BDEVNAME_SIZE];
3909 char buf2[BDEVNAME_SIZE];
3910 struct pool_c *pt = ti->private;
3911 struct pool *pool = pt->pool;
3912
3913 switch (type) {
3914 case STATUSTYPE_INFO:
Joe Thornbere49e5822012-07-27 15:08:16 +01003915 if (get_pool_mode(pool) == PM_FAIL) {
3916 DMEMIT("Fail");
3917 break;
3918 }
3919
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01003920 /* Commit to ensure statistics aren't out-of-date */
3921 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
Joe Thornber020cc3b2013-12-04 15:05:36 -05003922 (void) commit(pool);
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01003923
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003924 r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id);
3925 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003926 DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
3927 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003928 goto err;
3929 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003930
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003931 r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata);
3932 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003933 DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
3934 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003935 goto err;
3936 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003937
3938 r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003939 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003940 DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
3941 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003942 goto err;
3943 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003944
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003945 r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data);
3946 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003947 DMERR("%s: dm_pool_get_free_block_count returned %d",
3948 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003949 goto err;
3950 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003951
3952 r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003953 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003954 DMERR("%s: dm_pool_get_data_dev_size returned %d",
3955 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003956 goto err;
3957 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003958
Joe Thornbercc8394d2012-06-03 00:30:01 +01003959 r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003960 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04003961 DMERR("%s: dm_pool_get_metadata_snap returned %d",
3962 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003963 goto err;
3964 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003965
3966 DMEMIT("%llu %llu/%llu %llu/%llu ",
3967 (unsigned long long)transaction_id,
3968 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
3969 (unsigned long long)nr_blocks_metadata,
3970 (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
3971 (unsigned long long)nr_blocks_data);
3972
3973 if (held_root)
Joe Thornbere49e5822012-07-27 15:08:16 +01003974 DMEMIT("%llu ", held_root);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003975 else
Joe Thornbere49e5822012-07-27 15:08:16 +01003976 DMEMIT("- ");
3977
Joe Thornber3ab91822018-09-10 16:50:09 +01003978 mode = get_pool_mode(pool);
3979 if (mode == PM_OUT_OF_DATA_SPACE)
Joe Thornber3e1a0692014-03-03 16:03:26 +00003980 DMEMIT("out_of_data_space ");
Joe Thornber3ab91822018-09-10 16:50:09 +01003981 else if (is_read_only_pool_mode(mode))
Joe Thornbere49e5822012-07-27 15:08:16 +01003982 DMEMIT("ro ");
3983 else
3984 DMEMIT("rw ");
3985
Mike Snitzer018debe2012-12-21 20:23:32 +00003986 if (!pool->pf.discard_enabled)
Mike Snitzer787a996c2013-12-06 16:21:43 -05003987 DMEMIT("ignore_discard ");
Mike Snitzer018debe2012-12-21 20:23:32 +00003988 else if (pool->pf.discard_passdown)
Mike Snitzer787a996c2013-12-06 16:21:43 -05003989 DMEMIT("discard_passdown ");
Joe Thornbere49e5822012-07-27 15:08:16 +01003990 else
Mike Snitzer787a996c2013-12-06 16:21:43 -05003991 DMEMIT("no_discard_passdown ");
3992
3993 if (pool->pf.error_if_no_space)
3994 DMEMIT("error_if_no_space ");
3995 else
3996 DMEMIT("queue_if_no_space ");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003997
Mike Snitzere4c78e22015-07-15 11:40:24 -04003998 if (dm_pool_metadata_needs_check(pool->pmd))
3999 DMEMIT("needs_check ");
4000 else
4001 DMEMIT("- ");
4002
Andy Grover63c8ecb2018-07-27 15:51:57 -07004003 DMEMIT("%llu ", (unsigned long long)calc_metadata_threshold(pt));
4004
Joe Thornber991d9fa2011-10-31 20:21:18 +00004005 break;
4006
4007 case STATUSTYPE_TABLE:
4008 DMEMIT("%s %s %lu %llu ",
4009 format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
4010 format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
4011 (unsigned long)pool->sectors_per_block,
4012 (unsigned long long)pt->low_water_blocks);
Mike Snitzer0424caa2012-09-26 23:45:47 +01004013 emit_flags(&pt->requested_pf, result, sz, maxlen);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004014 break;
4015 }
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004016 return;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004017
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004018err:
4019 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00004020}
4021
4022static int pool_iterate_devices(struct dm_target *ti,
4023 iterate_devices_callout_fn fn, void *data)
4024{
4025 struct pool_c *pt = ti->private;
4026
4027 return fn(ti, pt->data_dev, 0, ti->len, data);
4028}
4029
Joe Thornber991d9fa2011-10-31 20:21:18 +00004030static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
4031{
4032 struct pool_c *pt = ti->private;
4033 struct pool *pool = pt->pool;
Mike Snitzer604ea902014-10-09 18:43:25 -04004034 sector_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
4035
4036 /*
Mike Snitzerd200c302014-11-20 18:07:43 -05004037 * If max_sectors is smaller than pool->sectors_per_block adjust it
4038 * to the highest possible power-of-2 factor of pool->sectors_per_block.
4039 * This is especially beneficial when the pool's data device is a RAID
4040 * device that has a full stripe width that matches pool->sectors_per_block
4041 * -- because even though partial RAID stripe-sized IOs will be issued to a
4042 * single RAID stripe; when aggregated they will end on a full RAID stripe
4043 * boundary.. which avoids additional partial RAID stripe writes cascading
Mike Snitzer604ea902014-10-09 18:43:25 -04004044 */
Mike Snitzer604ea902014-10-09 18:43:25 -04004045 if (limits->max_sectors < pool->sectors_per_block) {
4046 while (!is_factor(pool->sectors_per_block, limits->max_sectors)) {
4047 if ((limits->max_sectors & (limits->max_sectors - 1)) == 0)
4048 limits->max_sectors--;
4049 limits->max_sectors = rounddown_pow_of_two(limits->max_sectors);
4050 }
Mike Snitzer604ea902014-10-09 18:43:25 -04004051 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00004052
Mike Snitzer0cc67cd2013-08-20 15:02:41 -04004053 /*
4054 * If the system-determined stacked limits are compatible with the
4055 * pool's blocksize (io_opt is a factor) do not override them.
4056 */
4057 if (io_opt_sectors < pool->sectors_per_block ||
Mike Snitzer604ea902014-10-09 18:43:25 -04004058 !is_factor(io_opt_sectors, pool->sectors_per_block)) {
4059 if (is_factor(pool->sectors_per_block, limits->max_sectors))
4060 blk_limits_io_min(limits, limits->max_sectors << SECTOR_SHIFT);
4061 else
4062 blk_limits_io_min(limits, pool->sectors_per_block << SECTOR_SHIFT);
Mike Snitzer0cc67cd2013-08-20 15:02:41 -04004063 blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
4064 }
Mike Snitzer0424caa2012-09-26 23:45:47 +01004065
4066 /*
4067 * pt->adjusted_pf is a staging area for the actual features to use.
4068 * They get transferred to the live pool in bind_control_target()
4069 * called from pool_preresume().
4070 */
Mike Snitzerb60ab992013-09-19 18:49:11 -04004071 if (!pt->adjusted_pf.discard_enabled) {
4072 /*
4073 * Must explicitly disallow stacking discard limits otherwise the
4074 * block layer will stack them if pool's data device has support.
4075 * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the
4076 * user to see that, so make sure to set all discard limits to 0.
4077 */
4078 limits->discard_granularity = 0;
Mike Snitzer0424caa2012-09-26 23:45:47 +01004079 return;
Mike Snitzerb60ab992013-09-19 18:49:11 -04004080 }
Mike Snitzer0424caa2012-09-26 23:45:47 +01004081
4082 disable_passdown_if_not_supported(pt);
4083
Joe Thornber34fbcf62015-04-16 12:58:35 +01004084 /*
4085 * The pool uses the same discard limits as the underlying data
4086 * device. DM core has already set this up.
4087 */
Joe Thornber991d9fa2011-10-31 20:21:18 +00004088}
4089
4090static struct target_type pool_target = {
4091 .name = "thin-pool",
4092 .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
4093 DM_TARGET_IMMUTABLE,
Mikulas Patocka873937e2020-01-13 15:04:37 -05004094 .version = {1, 22, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00004095 .module = THIS_MODULE,
4096 .ctr = pool_ctr,
4097 .dtr = pool_dtr,
4098 .map = pool_map,
Mike Snitzer80e96c52014-11-07 15:09:46 -05004099 .presuspend = pool_presuspend,
4100 .presuspend_undo = pool_presuspend_undo,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004101 .postsuspend = pool_postsuspend,
4102 .preresume = pool_preresume,
4103 .resume = pool_resume,
4104 .message = pool_message,
4105 .status = pool_status,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004106 .iterate_devices = pool_iterate_devices,
4107 .io_hints = pool_io_hints,
4108};
4109
4110/*----------------------------------------------------------------
4111 * Thin target methods
4112 *--------------------------------------------------------------*/
Joe Thornberb10ebd32014-04-08 11:29:01 +01004113static void thin_get(struct thin_c *tc)
4114{
John Pittman22d4c292018-08-23 13:35:55 -04004115 refcount_inc(&tc->refcount);
Joe Thornberb10ebd32014-04-08 11:29:01 +01004116}
4117
4118static void thin_put(struct thin_c *tc)
4119{
John Pittman22d4c292018-08-23 13:35:55 -04004120 if (refcount_dec_and_test(&tc->refcount))
Joe Thornberb10ebd32014-04-08 11:29:01 +01004121 complete(&tc->can_destroy);
4122}
4123
Joe Thornber991d9fa2011-10-31 20:21:18 +00004124static void thin_dtr(struct dm_target *ti)
4125{
4126 struct thin_c *tc = ti->private;
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004127
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04004128 spin_lock_irq(&tc->pool->lock);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004129 list_del_rcu(&tc->list);
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04004130 spin_unlock_irq(&tc->pool->lock);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004131 synchronize_rcu();
Joe Thornber991d9fa2011-10-31 20:21:18 +00004132
Mikulas Patocka17181fb2014-11-05 17:00:13 -05004133 thin_put(tc);
4134 wait_for_completion(&tc->can_destroy);
4135
Joe Thornber991d9fa2011-10-31 20:21:18 +00004136 mutex_lock(&dm_thin_pool_table.mutex);
4137
4138 __pool_dec(tc->pool);
4139 dm_pool_close_thin_device(tc->td);
4140 dm_put_device(ti, tc->pool_dev);
Joe Thornber2dd9c252012-03-28 18:41:28 +01004141 if (tc->origin_dev)
4142 dm_put_device(ti, tc->origin_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004143 kfree(tc);
4144
4145 mutex_unlock(&dm_thin_pool_table.mutex);
4146}
4147
4148/*
4149 * Thin target parameters:
4150 *
Joe Thornber2dd9c252012-03-28 18:41:28 +01004151 * <pool_dev> <dev_id> [origin_dev]
Joe Thornber991d9fa2011-10-31 20:21:18 +00004152 *
4153 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
4154 * dev_id: the internal device identifier
Joe Thornber2dd9c252012-03-28 18:41:28 +01004155 * origin_dev: a device external to the pool that should act as the origin
Joe Thornber67e2e2b2012-03-28 18:41:29 +01004156 *
4157 * If the pool device has discards disabled, they get disabled for the thin
4158 * device as well.
Joe Thornber991d9fa2011-10-31 20:21:18 +00004159 */
4160static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
4161{
4162 int r;
4163 struct thin_c *tc;
Joe Thornber2dd9c252012-03-28 18:41:28 +01004164 struct dm_dev *pool_dev, *origin_dev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004165 struct mapped_device *pool_md;
4166
4167 mutex_lock(&dm_thin_pool_table.mutex);
4168
Joe Thornber2dd9c252012-03-28 18:41:28 +01004169 if (argc != 2 && argc != 3) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00004170 ti->error = "Invalid argument count";
4171 r = -EINVAL;
4172 goto out_unlock;
4173 }
4174
4175 tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
4176 if (!tc) {
4177 ti->error = "Out of memory";
4178 r = -ENOMEM;
4179 goto out_unlock;
4180 }
Mike Snitzer583024d2014-10-28 20:58:45 -04004181 tc->thin_md = dm_table_get_md(ti->table);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004182 spin_lock_init(&tc->lock);
Joe Thornbera374bb22014-10-10 13:43:14 +01004183 INIT_LIST_HEAD(&tc->deferred_cells);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004184 bio_list_init(&tc->deferred_bio_list);
4185 bio_list_init(&tc->retry_on_resume_list);
Mike Snitzer67324ea2014-03-21 18:33:41 -04004186 tc->sort_bio_list = RB_ROOT;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004187
Joe Thornber2dd9c252012-03-28 18:41:28 +01004188 if (argc == 3) {
Jason Cai (Xiang Feng)70de2cb2019-01-20 22:39:13 +08004189 if (!strcmp(argv[0], argv[2])) {
4190 ti->error = "Error setting origin device";
4191 r = -EINVAL;
4192 goto bad_origin_dev;
4193 }
4194
Joe Thornber2dd9c252012-03-28 18:41:28 +01004195 r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
4196 if (r) {
4197 ti->error = "Error opening origin device";
4198 goto bad_origin_dev;
4199 }
4200 tc->origin_dev = origin_dev;
4201 }
4202
Joe Thornber991d9fa2011-10-31 20:21:18 +00004203 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
4204 if (r) {
4205 ti->error = "Error opening pool device";
4206 goto bad_pool_dev;
4207 }
4208 tc->pool_dev = pool_dev;
4209
4210 if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
4211 ti->error = "Invalid device id";
4212 r = -EINVAL;
4213 goto bad_common;
4214 }
4215
4216 pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
4217 if (!pool_md) {
4218 ti->error = "Couldn't get pool mapped device";
4219 r = -EINVAL;
4220 goto bad_common;
4221 }
4222
4223 tc->pool = __pool_table_lookup(pool_md);
4224 if (!tc->pool) {
4225 ti->error = "Couldn't find pool object";
4226 r = -EINVAL;
4227 goto bad_pool_lookup;
4228 }
4229 __pool_inc(tc->pool);
4230
Joe Thornbere49e5822012-07-27 15:08:16 +01004231 if (get_pool_mode(tc->pool) == PM_FAIL) {
4232 ti->error = "Couldn't open thin device, Pool is in fail mode";
Mike Snitzer1acacc02014-02-19 20:32:33 -05004233 r = -EINVAL;
Mike Snitzer80e96c52014-11-07 15:09:46 -05004234 goto bad_pool;
Joe Thornbere49e5822012-07-27 15:08:16 +01004235 }
4236
Joe Thornber991d9fa2011-10-31 20:21:18 +00004237 r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
4238 if (r) {
4239 ti->error = "Couldn't open thin internal device";
Mike Snitzer80e96c52014-11-07 15:09:46 -05004240 goto bad_pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004241 }
4242
Mike Snitzer542f9032012-07-27 15:08:00 +01004243 r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
4244 if (r)
Mike Snitzer80e96c52014-11-07 15:09:46 -05004245 goto bad;
Mike Snitzer542f9032012-07-27 15:08:00 +01004246
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00004247 ti->num_flush_bios = 1;
Joe Thornber16ad3d12012-07-27 15:08:07 +01004248 ti->flush_supported = true;
Mike Snitzer30187e12016-01-31 13:28:26 -05004249 ti->per_io_data_size = sizeof(struct dm_thin_endio_hook);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01004250
4251 /* In case the pool supports discards, pass them on. */
4252 if (tc->pool->pf.discard_enabled) {
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01004253 ti->discards_supported = true;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00004254 ti->num_discard_bios = 1;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01004255 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00004256
Joe Thornber991d9fa2011-10-31 20:21:18 +00004257 mutex_unlock(&dm_thin_pool_table.mutex);
4258
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04004259 spin_lock_irq(&tc->pool->lock);
Mike Snitzer80e96c52014-11-07 15:09:46 -05004260 if (tc->pool->suspended) {
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04004261 spin_unlock_irq(&tc->pool->lock);
Mike Snitzer80e96c52014-11-07 15:09:46 -05004262 mutex_lock(&dm_thin_pool_table.mutex); /* reacquire for __pool_dec */
4263 ti->error = "Unable to activate thin device while pool is suspended";
4264 r = -EINVAL;
4265 goto bad;
4266 }
John Pittman22d4c292018-08-23 13:35:55 -04004267 refcount_set(&tc->refcount, 1);
Marc Dionne2b94e892014-12-17 07:59:59 -05004268 init_completion(&tc->can_destroy);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004269 list_add_tail_rcu(&tc->list, &tc->pool->active_thins);
Mikulas Patocka8e0c9da2019-10-15 08:16:29 -04004270 spin_unlock_irq(&tc->pool->lock);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04004271 /*
4272 * This synchronize_rcu() call is needed here otherwise we risk a
4273 * wake_worker() call finding no bios to process (because the newly
4274 * added tc isn't yet visible). So this reduces latency since we
4275 * aren't then dependent on the periodic commit to wake_worker().
4276 */
4277 synchronize_rcu();
4278
Mike Snitzer80e96c52014-11-07 15:09:46 -05004279 dm_put(pool_md);
4280
Joe Thornber991d9fa2011-10-31 20:21:18 +00004281 return 0;
4282
Mike Snitzer80e96c52014-11-07 15:09:46 -05004283bad:
Mike Snitzer1acacc02014-02-19 20:32:33 -05004284 dm_pool_close_thin_device(tc->td);
Mike Snitzer80e96c52014-11-07 15:09:46 -05004285bad_pool:
Joe Thornber991d9fa2011-10-31 20:21:18 +00004286 __pool_dec(tc->pool);
4287bad_pool_lookup:
4288 dm_put(pool_md);
4289bad_common:
4290 dm_put_device(ti, tc->pool_dev);
4291bad_pool_dev:
Joe Thornber2dd9c252012-03-28 18:41:28 +01004292 if (tc->origin_dev)
4293 dm_put_device(ti, tc->origin_dev);
4294bad_origin_dev:
Joe Thornber991d9fa2011-10-31 20:21:18 +00004295 kfree(tc);
4296out_unlock:
4297 mutex_unlock(&dm_thin_pool_table.mutex);
4298
4299 return r;
4300}
4301
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00004302static int thin_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00004303{
Kent Overstreet4f024f32013-10-11 15:44:27 -07004304 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004305
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00004306 return thin_bio_map(ti, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004307}
4308
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02004309static int thin_endio(struct dm_target *ti, struct bio *bio,
4310 blk_status_t *err)
Joe Thornbereb2aa482012-03-28 18:41:28 +01004311{
4312 unsigned long flags;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00004313 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01004314 struct list_head work;
Mike Snitzera24c2562012-06-03 00:30:00 +01004315 struct dm_thin_new_mapping *m, *tmp;
Joe Thornbereb2aa482012-03-28 18:41:28 +01004316 struct pool *pool = h->tc->pool;
4317
4318 if (h->shared_read_entry) {
4319 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01004320 dm_deferred_entry_dec(h->shared_read_entry, &work);
Joe Thornbereb2aa482012-03-28 18:41:28 +01004321
4322 spin_lock_irqsave(&pool->lock, flags);
4323 list_for_each_entry_safe(m, tmp, &work, list) {
4324 list_del(&m->list);
Joe Thornber50f3c3e2014-06-13 13:57:09 +01004325 __complete_mapping_preparation(m);
Joe Thornbereb2aa482012-03-28 18:41:28 +01004326 }
4327 spin_unlock_irqrestore(&pool->lock, flags);
4328 }
4329
Joe Thornber104655f2012-03-28 18:41:28 +01004330 if (h->all_io_entry) {
4331 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01004332 dm_deferred_entry_dec(h->all_io_entry, &work);
Joe Thornber563af182012-12-21 20:23:31 +00004333 if (!list_empty(&work)) {
4334 spin_lock_irqsave(&pool->lock, flags);
4335 list_for_each_entry_safe(m, tmp, &work, list)
Mike Snitzerdaec3382013-12-11 14:01:20 -05004336 list_add_tail(&m->list, &pool->prepared_discards);
Joe Thornber563af182012-12-21 20:23:31 +00004337 spin_unlock_irqrestore(&pool->lock, flags);
4338 wake_worker(pool);
4339 }
Joe Thornber104655f2012-03-28 18:41:28 +01004340 }
4341
Joe Thornber34fbcf62015-04-16 12:58:35 +01004342 if (h->cell)
4343 cell_defer_no_holder(h->tc, h->cell);
4344
Christoph Hellwig1be56902017-06-03 09:38:03 +02004345 return DM_ENDIO_DONE;
Joe Thornbereb2aa482012-03-28 18:41:28 +01004346}
4347
Joe Thornber738211f2014-03-03 15:52:28 +00004348static void thin_presuspend(struct dm_target *ti)
4349{
4350 struct thin_c *tc = ti->private;
4351
4352 if (dm_noflush_suspending(ti))
4353 noflush_work(tc, do_noflush_start);
4354}
4355
Joe Thornber991d9fa2011-10-31 20:21:18 +00004356static void thin_postsuspend(struct dm_target *ti)
4357{
Joe Thornber738211f2014-03-03 15:52:28 +00004358 struct thin_c *tc = ti->private;
4359
4360 /*
4361 * The dm_noflush_suspending flag has been cleared by now, so
4362 * unfortunately we must always run this.
4363 */
4364 noflush_work(tc, do_noflush_stop);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004365}
4366
Joe Thornbere5aea7b2014-06-13 14:47:24 +01004367static int thin_preresume(struct dm_target *ti)
4368{
4369 struct thin_c *tc = ti->private;
4370
4371 if (tc->origin_dev)
4372 tc->origin_size = get_dev_size(tc->origin_dev->bdev);
4373
4374 return 0;
4375}
4376
Joe Thornber991d9fa2011-10-31 20:21:18 +00004377/*
4378 * <nr mapped sectors> <highest mapped sector>
4379 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004380static void thin_status(struct dm_target *ti, status_type_t type,
4381 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00004382{
4383 int r;
4384 ssize_t sz = 0;
4385 dm_block_t mapped, highest;
4386 char buf[BDEVNAME_SIZE];
4387 struct thin_c *tc = ti->private;
4388
Joe Thornbere49e5822012-07-27 15:08:16 +01004389 if (get_pool_mode(tc->pool) == PM_FAIL) {
4390 DMEMIT("Fail");
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004391 return;
Joe Thornbere49e5822012-07-27 15:08:16 +01004392 }
4393
Joe Thornber991d9fa2011-10-31 20:21:18 +00004394 if (!tc->td)
4395 DMEMIT("-");
4396 else {
4397 switch (type) {
4398 case STATUSTYPE_INFO:
4399 r = dm_thin_get_mapped_count(tc->td, &mapped);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004400 if (r) {
4401 DMERR("dm_thin_get_mapped_count returned %d", r);
4402 goto err;
4403 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00004404
4405 r = dm_thin_get_highest_mapped_block(tc->td, &highest);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004406 if (r < 0) {
4407 DMERR("dm_thin_get_highest_mapped_block returned %d", r);
4408 goto err;
4409 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00004410
4411 DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
4412 if (r)
4413 DMEMIT("%llu", ((highest + 1) *
4414 tc->pool->sectors_per_block) - 1);
4415 else
4416 DMEMIT("-");
4417 break;
4418
4419 case STATUSTYPE_TABLE:
4420 DMEMIT("%s %lu",
4421 format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
4422 (unsigned long) tc->dev_id);
Joe Thornber2dd9c252012-03-28 18:41:28 +01004423 if (tc->origin_dev)
4424 DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
Joe Thornber991d9fa2011-10-31 20:21:18 +00004425 break;
4426 }
4427 }
4428
Mikulas Patockafd7c0922013-03-01 22:45:44 +00004429 return;
4430
4431err:
4432 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00004433}
4434
4435static int thin_iterate_devices(struct dm_target *ti,
4436 iterate_devices_callout_fn fn, void *data)
4437{
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004438 sector_t blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004439 struct thin_c *tc = ti->private;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004440 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004441
4442 /*
4443 * We can't call dm_pool_get_data_dev_size() since that blocks. So
4444 * we follow a more convoluted path through to the pool's target.
4445 */
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004446 if (!pool->ti)
Joe Thornber991d9fa2011-10-31 20:21:18 +00004447 return 0; /* nothing is bound */
4448
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004449 blocks = pool->ti->len;
4450 (void) sector_div(blocks, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004451 if (blocks)
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01004452 return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004453
4454 return 0;
4455}
4456
Joe Thornber34fbcf62015-04-16 12:58:35 +01004457static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)
4458{
4459 struct thin_c *tc = ti->private;
4460 struct pool *pool = tc->pool;
Mike Snitzer21607672015-09-08 08:56:13 -04004461
Mike Snitzer0fcb04d2015-11-23 13:44:38 -05004462 if (!pool->pf.discard_enabled)
4463 return;
Joe Thornber34fbcf62015-04-16 12:58:35 +01004464
4465 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
4466 limits->max_discard_sectors = 2048 * 1024 * 16; /* 16G */
4467}
4468
Joe Thornber991d9fa2011-10-31 20:21:18 +00004469static struct target_type thin_target = {
4470 .name = "thin",
Mikulas Patocka873937e2020-01-13 15:04:37 -05004471 .version = {1, 22, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00004472 .module = THIS_MODULE,
4473 .ctr = thin_ctr,
4474 .dtr = thin_dtr,
4475 .map = thin_map,
Joe Thornbereb2aa482012-03-28 18:41:28 +01004476 .end_io = thin_endio,
Joe Thornbere5aea7b2014-06-13 14:47:24 +01004477 .preresume = thin_preresume,
Joe Thornber738211f2014-03-03 15:52:28 +00004478 .presuspend = thin_presuspend,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004479 .postsuspend = thin_postsuspend,
4480 .status = thin_status,
4481 .iterate_devices = thin_iterate_devices,
Joe Thornber34fbcf62015-04-16 12:58:35 +01004482 .io_hints = thin_io_hints,
Joe Thornber991d9fa2011-10-31 20:21:18 +00004483};
4484
4485/*----------------------------------------------------------------*/
4486
4487static int __init dm_thin_init(void)
4488{
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08004489 int r = -ENOMEM;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004490
4491 pool_table_init();
4492
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08004493 _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
4494 if (!_new_mapping_cache)
4495 return r;
4496
Joe Thornber991d9fa2011-10-31 20:21:18 +00004497 r = dm_register_target(&thin_target);
4498 if (r)
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08004499 goto bad_new_mapping_cache;
Joe Thornber991d9fa2011-10-31 20:21:18 +00004500
4501 r = dm_register_target(&pool_target);
4502 if (r)
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08004503 goto bad_thin_target;
Mike Snitzera24c2562012-06-03 00:30:00 +01004504
Mike Snitzera24c2562012-06-03 00:30:00 +01004505 return 0;
4506
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08004507bad_thin_target:
Mike Snitzera24c2562012-06-03 00:30:00 +01004508 dm_unregister_target(&thin_target);
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08004509bad_new_mapping_cache:
4510 kmem_cache_destroy(_new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00004511
4512 return r;
4513}
4514
4515static void dm_thin_exit(void)
4516{
4517 dm_unregister_target(&thin_target);
4518 dm_unregister_target(&pool_target);
Mike Snitzera24c2562012-06-03 00:30:00 +01004519
Mike Snitzera24c2562012-06-03 00:30:00 +01004520 kmem_cache_destroy(_new_mapping_cache);
Mike Snitzerd5ffebd2018-01-05 21:17:20 -05004521
4522 pool_table_exit();
Joe Thornber991d9fa2011-10-31 20:21:18 +00004523}
4524
4525module_init(dm_thin_init);
4526module_exit(dm_thin_exit);
4527
Mike Snitzer80c57892014-05-20 13:38:33 -04004528module_param_named(no_space_timeout, no_space_timeout_secs, uint, S_IRUGO | S_IWUSR);
4529MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds");
4530
Alasdair G Kergon7cab8bf2012-05-12 01:43:19 +01004531MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
Joe Thornber991d9fa2011-10-31 20:21:18 +00004532MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
4533MODULE_LICENSE("GPL");