blob: 53728be84dee35ac8dfabbf48087919841049f1a [file] [log] [blame]
Joe Thornber991d9fa2011-10-31 20:21:18 +00001/*
Joe Thornbere49e5822012-07-27 15:08:16 +01002 * Copyright (C) 2011-2012 Red Hat UK.
Joe Thornber991d9fa2011-10-31 20:21:18 +00003 *
4 * This file is released under the GPL.
5 */
6
7#include "dm-thin-metadata.h"
Mike Snitzer4f81a412012-10-12 21:02:13 +01008#include "dm-bio-prison.h"
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01009#include "dm.h"
Joe Thornber991d9fa2011-10-31 20:21:18 +000010
11#include <linux/device-mapper.h>
12#include <linux/dm-io.h>
13#include <linux/dm-kcopyd.h>
14#include <linux/list.h>
Mike Snitzerc140e1c2014-03-20 21:17:14 -040015#include <linux/rculist.h>
Joe Thornber991d9fa2011-10-31 20:21:18 +000016#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/slab.h>
Mike Snitzer67324ea2014-03-21 18:33:41 -040019#include <linux/rbtree.h>
Joe Thornber991d9fa2011-10-31 20:21:18 +000020
21#define DM_MSG_PREFIX "thin"
22
23/*
24 * Tunable constants
25 */
Alasdair G Kergon7768ed32012-07-27 15:07:57 +010026#define ENDIO_HOOK_POOL_SIZE 1024
Joe Thornber991d9fa2011-10-31 20:21:18 +000027#define MAPPING_POOL_SIZE 1024
28#define PRISON_CELLS 1024
Joe Thornber905e51b2012-03-28 18:41:27 +010029#define COMMIT_PERIOD HZ
Joe Thornber991d9fa2011-10-31 20:21:18 +000030
Mikulas Patockadf5d2e92013-03-01 22:45:49 +000031DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
32 "A percentage of time allocated for copy on write");
33
Joe Thornber991d9fa2011-10-31 20:21:18 +000034/*
35 * The block size of the device holding pool data must be
36 * between 64KB and 1GB.
37 */
38#define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
39#define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
40
41/*
Joe Thornber991d9fa2011-10-31 20:21:18 +000042 * Device id is restricted to 24 bits.
43 */
44#define MAX_DEV_ID ((1 << 24) - 1)
45
46/*
47 * How do we handle breaking sharing of data blocks?
48 * =================================================
49 *
50 * We use a standard copy-on-write btree to store the mappings for the
51 * devices (note I'm talking about copy-on-write of the metadata here, not
52 * the data). When you take an internal snapshot you clone the root node
53 * of the origin btree. After this there is no concept of an origin or a
54 * snapshot. They are just two device trees that happen to point to the
55 * same data blocks.
56 *
57 * When we get a write in we decide if it's to a shared data block using
58 * some timestamp magic. If it is, we have to break sharing.
59 *
60 * Let's say we write to a shared block in what was the origin. The
61 * steps are:
62 *
63 * i) plug io further to this physical block. (see bio_prison code).
64 *
65 * ii) quiesce any read io to that shared data block. Obviously
Mike Snitzer44feb382012-10-12 21:02:10 +010066 * including all devices that share this block. (see dm_deferred_set code)
Joe Thornber991d9fa2011-10-31 20:21:18 +000067 *
68 * iii) copy the data block to a newly allocate block. This step can be
69 * missed out if the io covers the block. (schedule_copy).
70 *
71 * iv) insert the new mapping into the origin's btree
Joe Thornberfe878f32012-03-28 18:41:24 +010072 * (process_prepared_mapping). This act of inserting breaks some
Joe Thornber991d9fa2011-10-31 20:21:18 +000073 * sharing of btree nodes between the two devices. Breaking sharing only
74 * effects the btree of that specific device. Btrees for the other
75 * devices that share the block never change. The btree for the origin
76 * device as it was after the last commit is untouched, ie. we're using
77 * persistent data structures in the functional programming sense.
78 *
79 * v) unplug io to this physical block, including the io that triggered
80 * the breaking of sharing.
81 *
82 * Steps (ii) and (iii) occur in parallel.
83 *
84 * The metadata _doesn't_ need to be committed before the io continues. We
85 * get away with this because the io is always written to a _new_ block.
86 * If there's a crash, then:
87 *
88 * - The origin mapping will point to the old origin block (the shared
89 * one). This will contain the data as it was before the io that triggered
90 * the breaking of sharing came in.
91 *
92 * - The snap mapping still points to the old block. As it would after
93 * the commit.
94 *
95 * The downside of this scheme is the timestamp magic isn't perfect, and
96 * will continue to think that data block in the snapshot device is shared
97 * even after the write to the origin has broken sharing. I suspect data
98 * blocks will typically be shared by many different devices, so we're
99 * breaking sharing n + 1 times, rather than n, where n is the number of
100 * devices that reference this data block. At the moment I think the
101 * benefits far, far outweigh the disadvantages.
102 */
103
104/*----------------------------------------------------------------*/
105
106/*
Joe Thornber991d9fa2011-10-31 20:21:18 +0000107 * Key building.
108 */
109static void build_data_key(struct dm_thin_device *td,
Mike Snitzer44feb382012-10-12 21:02:10 +0100110 dm_block_t b, struct dm_cell_key *key)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000111{
112 key->virtual = 0;
113 key->dev = dm_thin_dev_id(td);
114 key->block = b;
115}
116
117static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
Mike Snitzer44feb382012-10-12 21:02:10 +0100118 struct dm_cell_key *key)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000119{
120 key->virtual = 1;
121 key->dev = dm_thin_dev_id(td);
122 key->block = b;
123}
124
125/*----------------------------------------------------------------*/
126
127/*
128 * A pool device ties together a metadata device and a data device. It
129 * also provides the interface for creating and destroying internal
130 * devices.
131 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100132struct dm_thin_new_mapping;
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100133
Joe Thornbere49e5822012-07-27 15:08:16 +0100134/*
Joe Thornber3e1a0692014-03-03 16:03:26 +0000135 * The pool runs in 4 modes. Ordered in degraded order for comparisons.
Joe Thornbere49e5822012-07-27 15:08:16 +0100136 */
137enum pool_mode {
138 PM_WRITE, /* metadata may be changed */
Joe Thornber3e1a0692014-03-03 16:03:26 +0000139 PM_OUT_OF_DATA_SPACE, /* metadata may be changed, though data may not be allocated */
Joe Thornbere49e5822012-07-27 15:08:16 +0100140 PM_READ_ONLY, /* metadata may not be changed */
141 PM_FAIL, /* all I/O fails */
142};
143
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100144struct pool_features {
Joe Thornbere49e5822012-07-27 15:08:16 +0100145 enum pool_mode mode;
146
Mike Snitzer9bc142d2012-09-26 23:45:46 +0100147 bool zero_new_blocks:1;
148 bool discard_enabled:1;
149 bool discard_passdown:1;
Mike Snitzer787a996c2013-12-06 16:21:43 -0500150 bool error_if_no_space:1;
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100151};
152
Joe Thornbere49e5822012-07-27 15:08:16 +0100153struct thin_c;
154typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio);
155typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m);
156
Joe Thornber991d9fa2011-10-31 20:21:18 +0000157struct pool {
158 struct list_head list;
159 struct dm_target *ti; /* Only set if a pool target is bound */
160
161 struct mapped_device *pool_md;
162 struct block_device *md_dev;
163 struct dm_pool_metadata *pmd;
164
Joe Thornber991d9fa2011-10-31 20:21:18 +0000165 dm_block_t low_water_blocks;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100166 uint32_t sectors_per_block;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100167 int sectors_per_block_shift;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000168
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100169 struct pool_features pf;
Joe Thornber88a66212013-12-04 20:16:12 -0500170 bool low_water_triggered:1; /* A dm event has been sent */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000171
Mike Snitzer44feb382012-10-12 21:02:10 +0100172 struct dm_bio_prison *prison;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000173 struct dm_kcopyd_client *copier;
174
175 struct workqueue_struct *wq;
176 struct work_struct worker;
Joe Thornber905e51b2012-03-28 18:41:27 +0100177 struct delayed_work waker;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000178
Joe Thornber905e51b2012-03-28 18:41:27 +0100179 unsigned long last_commit_jiffies;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100180 unsigned ref_count;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000181
182 spinlock_t lock;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000183 struct bio_list deferred_flush_bios;
184 struct list_head prepared_mappings;
Joe Thornber104655f2012-03-28 18:41:28 +0100185 struct list_head prepared_discards;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400186 struct list_head active_thins;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000187
Mike Snitzer44feb382012-10-12 21:02:10 +0100188 struct dm_deferred_set *shared_read_ds;
189 struct dm_deferred_set *all_io_ds;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000190
Mike Snitzera24c2562012-06-03 00:30:00 +0100191 struct dm_thin_new_mapping *next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000192 mempool_t *mapping_pool;
Joe Thornbere49e5822012-07-27 15:08:16 +0100193
194 process_bio_fn process_bio;
195 process_bio_fn process_discard;
196
197 process_mapping_fn process_prepared_mapping;
198 process_mapping_fn process_prepared_discard;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000199};
200
Joe Thornbere49e5822012-07-27 15:08:16 +0100201static enum pool_mode get_pool_mode(struct pool *pool);
Joe Thornberb5330652013-12-04 19:51:33 -0500202static void metadata_operation_failed(struct pool *pool, const char *op, int r);
Joe Thornbere49e5822012-07-27 15:08:16 +0100203
Joe Thornber991d9fa2011-10-31 20:21:18 +0000204/*
205 * Target context for a pool.
206 */
207struct pool_c {
208 struct dm_target *ti;
209 struct pool *pool;
210 struct dm_dev *data_dev;
211 struct dm_dev *metadata_dev;
212 struct dm_target_callbacks callbacks;
213
214 dm_block_t low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +0100215 struct pool_features requested_pf; /* Features requested during table load */
216 struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000217};
218
219/*
220 * Target context for a thin.
221 */
222struct thin_c {
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400223 struct list_head list;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000224 struct dm_dev *pool_dev;
Joe Thornber2dd9c252012-03-28 18:41:28 +0100225 struct dm_dev *origin_dev;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000226 dm_thin_id dev_id;
227
228 struct pool *pool;
229 struct dm_thin_device *td;
Joe Thornber738211f2014-03-03 15:52:28 +0000230 bool requeue_mode:1;
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400231 spinlock_t lock;
232 struct bio_list deferred_bio_list;
233 struct bio_list retry_on_resume_list;
Mike Snitzer67324ea2014-03-21 18:33:41 -0400234 struct rb_root sort_bio_list; /* sorted list of deferred bios */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000235};
236
237/*----------------------------------------------------------------*/
238
Joe Thornber025b9682013-03-01 22:45:50 +0000239/*
240 * wake_worker() is used when new work is queued and when pool_resume is
241 * ready to continue deferred IO processing.
242 */
243static void wake_worker(struct pool *pool)
244{
245 queue_work(pool->wq, &pool->worker);
246}
247
248/*----------------------------------------------------------------*/
249
Joe Thornber6beca5e2013-03-01 22:45:50 +0000250static int bio_detain(struct pool *pool, struct dm_cell_key *key, struct bio *bio,
251 struct dm_bio_prison_cell **cell_result)
252{
253 int r;
254 struct dm_bio_prison_cell *cell_prealloc;
255
256 /*
257 * Allocate a cell from the prison's mempool.
258 * This might block but it can't fail.
259 */
260 cell_prealloc = dm_bio_prison_alloc_cell(pool->prison, GFP_NOIO);
261
262 r = dm_bio_detain(pool->prison, key, bio, cell_prealloc, cell_result);
263 if (r)
264 /*
265 * We reused an old cell; we can get rid of
266 * the new one.
267 */
268 dm_bio_prison_free_cell(pool->prison, cell_prealloc);
269
270 return r;
271}
272
273static void cell_release(struct pool *pool,
274 struct dm_bio_prison_cell *cell,
275 struct bio_list *bios)
276{
277 dm_cell_release(pool->prison, cell, bios);
278 dm_bio_prison_free_cell(pool->prison, cell);
279}
280
281static void cell_release_no_holder(struct pool *pool,
282 struct dm_bio_prison_cell *cell,
283 struct bio_list *bios)
284{
285 dm_cell_release_no_holder(pool->prison, cell, bios);
286 dm_bio_prison_free_cell(pool->prison, cell);
287}
288
Joe Thornber025b9682013-03-01 22:45:50 +0000289static void cell_defer_no_holder_no_free(struct thin_c *tc,
290 struct dm_bio_prison_cell *cell)
291{
292 struct pool *pool = tc->pool;
293 unsigned long flags;
294
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400295 spin_lock_irqsave(&tc->lock, flags);
296 dm_cell_release_no_holder(pool->prison, cell, &tc->deferred_bio_list);
297 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber025b9682013-03-01 22:45:50 +0000298
299 wake_worker(pool);
300}
301
Joe Thornber6beca5e2013-03-01 22:45:50 +0000302static void cell_error(struct pool *pool,
303 struct dm_bio_prison_cell *cell)
304{
305 dm_cell_error(pool->prison, cell);
306 dm_bio_prison_free_cell(pool->prison, cell);
307}
308
309/*----------------------------------------------------------------*/
310
Joe Thornber991d9fa2011-10-31 20:21:18 +0000311/*
312 * A global list of pools that uses a struct mapped_device as a key.
313 */
314static struct dm_thin_pool_table {
315 struct mutex mutex;
316 struct list_head pools;
317} dm_thin_pool_table;
318
319static void pool_table_init(void)
320{
321 mutex_init(&dm_thin_pool_table.mutex);
322 INIT_LIST_HEAD(&dm_thin_pool_table.pools);
323}
324
325static void __pool_table_insert(struct pool *pool)
326{
327 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
328 list_add(&pool->list, &dm_thin_pool_table.pools);
329}
330
331static void __pool_table_remove(struct pool *pool)
332{
333 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
334 list_del(&pool->list);
335}
336
337static struct pool *__pool_table_lookup(struct mapped_device *md)
338{
339 struct pool *pool = NULL, *tmp;
340
341 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
342
343 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
344 if (tmp->pool_md == md) {
345 pool = tmp;
346 break;
347 }
348 }
349
350 return pool;
351}
352
353static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
354{
355 struct pool *pool = NULL, *tmp;
356
357 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
358
359 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
360 if (tmp->md_dev == md_dev) {
361 pool = tmp;
362 break;
363 }
364 }
365
366 return pool;
367}
368
369/*----------------------------------------------------------------*/
370
Mike Snitzera24c2562012-06-03 00:30:00 +0100371struct dm_thin_endio_hook {
Joe Thornbereb2aa482012-03-28 18:41:28 +0100372 struct thin_c *tc;
Mike Snitzer44feb382012-10-12 21:02:10 +0100373 struct dm_deferred_entry *shared_read_entry;
374 struct dm_deferred_entry *all_io_entry;
Mike Snitzera24c2562012-06-03 00:30:00 +0100375 struct dm_thin_new_mapping *overwrite_mapping;
Mike Snitzer67324ea2014-03-21 18:33:41 -0400376 struct rb_node rb_node;
Joe Thornbereb2aa482012-03-28 18:41:28 +0100377};
378
Joe Thornber18adc572014-03-03 15:46:42 +0000379static void requeue_bio_list(struct thin_c *tc, struct bio_list *master)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000380{
381 struct bio *bio;
382 struct bio_list bios;
Joe Thornber18adc572014-03-03 15:46:42 +0000383 unsigned long flags;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000384
385 bio_list_init(&bios);
Joe Thornber18adc572014-03-03 15:46:42 +0000386
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400387 spin_lock_irqsave(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000388 bio_list_merge(&bios, master);
389 bio_list_init(master);
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400390 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000391
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400392 while ((bio = bio_list_pop(&bios)))
393 bio_endio(bio, DM_ENDIO_REQUEUE);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000394}
395
396static void requeue_io(struct thin_c *tc)
397{
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400398 requeue_bio_list(tc, &tc->deferred_bio_list);
399 requeue_bio_list(tc, &tc->retry_on_resume_list);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000400}
401
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400402static void error_thin_retry_list(struct thin_c *tc)
Joe Thornber3e1a0692014-03-03 16:03:26 +0000403{
404 struct bio *bio;
405 unsigned long flags;
406 struct bio_list bios;
407
408 bio_list_init(&bios);
409
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400410 spin_lock_irqsave(&tc->lock, flags);
411 bio_list_merge(&bios, &tc->retry_on_resume_list);
412 bio_list_init(&tc->retry_on_resume_list);
413 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber3e1a0692014-03-03 16:03:26 +0000414
415 while ((bio = bio_list_pop(&bios)))
416 bio_io_error(bio);
417}
418
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400419static void error_retry_list(struct pool *pool)
420{
421 struct thin_c *tc;
422
423 rcu_read_lock();
424 list_for_each_entry_rcu(tc, &pool->active_thins, list)
425 error_thin_retry_list(tc);
426 rcu_read_unlock();
427}
428
Joe Thornber991d9fa2011-10-31 20:21:18 +0000429/*
430 * This section of code contains the logic for processing a thin device's IO.
431 * Much of the code depends on pool object resources (lists, workqueues, etc)
432 * but most is exclusively called from the thin target rather than the thin-pool
433 * target.
434 */
435
Mike Snitzer58f77a22013-03-01 22:45:45 +0000436static bool block_size_is_power_of_two(struct pool *pool)
437{
438 return pool->sectors_per_block_shift >= 0;
439}
440
Joe Thornber991d9fa2011-10-31 20:21:18 +0000441static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
442{
Mike Snitzer58f77a22013-03-01 22:45:45 +0000443 struct pool *pool = tc->pool;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700444 sector_t block_nr = bio->bi_iter.bi_sector;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100445
Mike Snitzer58f77a22013-03-01 22:45:45 +0000446 if (block_size_is_power_of_two(pool))
447 block_nr >>= pool->sectors_per_block_shift;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100448 else
Mike Snitzer58f77a22013-03-01 22:45:45 +0000449 (void) sector_div(block_nr, pool->sectors_per_block);
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100450
451 return block_nr;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000452}
453
454static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
455{
456 struct pool *pool = tc->pool;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700457 sector_t bi_sector = bio->bi_iter.bi_sector;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000458
459 bio->bi_bdev = tc->pool_dev->bdev;
Mike Snitzer58f77a22013-03-01 22:45:45 +0000460 if (block_size_is_power_of_two(pool))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700461 bio->bi_iter.bi_sector =
462 (block << pool->sectors_per_block_shift) |
463 (bi_sector & (pool->sectors_per_block - 1));
Mike Snitzer58f77a22013-03-01 22:45:45 +0000464 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700465 bio->bi_iter.bi_sector = (block * pool->sectors_per_block) +
Mike Snitzer58f77a22013-03-01 22:45:45 +0000466 sector_div(bi_sector, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000467}
468
Joe Thornber2dd9c252012-03-28 18:41:28 +0100469static void remap_to_origin(struct thin_c *tc, struct bio *bio)
470{
471 bio->bi_bdev = tc->origin_dev->bdev;
472}
473
Joe Thornber4afdd682012-07-27 15:08:14 +0100474static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
475{
476 return (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) &&
477 dm_thin_changed_this_transaction(tc->td);
478}
479
Joe Thornbere8088072012-12-21 20:23:31 +0000480static void inc_all_io_entry(struct pool *pool, struct bio *bio)
481{
482 struct dm_thin_endio_hook *h;
483
484 if (bio->bi_rw & REQ_DISCARD)
485 return;
486
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000487 h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbere8088072012-12-21 20:23:31 +0000488 h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
489}
490
Joe Thornber2dd9c252012-03-28 18:41:28 +0100491static void issue(struct thin_c *tc, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000492{
493 struct pool *pool = tc->pool;
494 unsigned long flags;
495
Joe Thornbere49e5822012-07-27 15:08:16 +0100496 if (!bio_triggers_commit(tc, bio)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000497 generic_make_request(bio);
Joe Thornbere49e5822012-07-27 15:08:16 +0100498 return;
499 }
500
501 /*
502 * Complete bio with an error if earlier I/O caused changes to
503 * the metadata that can't be committed e.g, due to I/O errors
504 * on the metadata device.
505 */
506 if (dm_thin_aborted_changes(tc->td)) {
507 bio_io_error(bio);
508 return;
509 }
510
511 /*
512 * Batch together any bios that trigger commits and then issue a
513 * single commit for them in process_deferred_bios().
514 */
515 spin_lock_irqsave(&pool->lock, flags);
516 bio_list_add(&pool->deferred_flush_bios, bio);
517 spin_unlock_irqrestore(&pool->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000518}
519
Joe Thornber2dd9c252012-03-28 18:41:28 +0100520static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
521{
522 remap_to_origin(tc, bio);
523 issue(tc, bio);
524}
525
526static void remap_and_issue(struct thin_c *tc, struct bio *bio,
527 dm_block_t block)
528{
529 remap(tc, bio, block);
530 issue(tc, bio);
531}
532
Joe Thornber991d9fa2011-10-31 20:21:18 +0000533/*----------------------------------------------------------------*/
534
535/*
536 * Bio endio functions.
537 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100538struct dm_thin_new_mapping {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000539 struct list_head list;
540
Mike Snitzer7f214662013-12-17 13:43:31 -0500541 bool quiesced:1;
542 bool prepared:1;
543 bool pass_discard:1;
544 bool definitely_not_shared:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000545
Mike Snitzer7f214662013-12-17 13:43:31 -0500546 int err;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000547 struct thin_c *tc;
548 dm_block_t virt_block;
549 dm_block_t data_block;
Mike Snitzera24c2562012-06-03 00:30:00 +0100550 struct dm_bio_prison_cell *cell, *cell2;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000551
552 /*
553 * If the bio covers the whole area of a block then we can avoid
554 * zeroing or copying. Instead this bio is hooked. The bio will
555 * still be in the cell, so care has to be taken to avoid issuing
556 * the bio twice.
557 */
558 struct bio *bio;
559 bio_end_io_t *saved_bi_end_io;
560};
561
Mike Snitzera24c2562012-06-03 00:30:00 +0100562static void __maybe_add_mapping(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000563{
564 struct pool *pool = m->tc->pool;
565
Joe Thornbereb2aa482012-03-28 18:41:28 +0100566 if (m->quiesced && m->prepared) {
Mike Snitzerdaec3382013-12-11 14:01:20 -0500567 list_add_tail(&m->list, &pool->prepared_mappings);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000568 wake_worker(pool);
569 }
570}
571
572static void copy_complete(int read_err, unsigned long write_err, void *context)
573{
574 unsigned long flags;
Mike Snitzera24c2562012-06-03 00:30:00 +0100575 struct dm_thin_new_mapping *m = context;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000576 struct pool *pool = m->tc->pool;
577
578 m->err = read_err || write_err ? -EIO : 0;
579
580 spin_lock_irqsave(&pool->lock, flags);
Mike Snitzer7f214662013-12-17 13:43:31 -0500581 m->prepared = true;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000582 __maybe_add_mapping(m);
583 spin_unlock_irqrestore(&pool->lock, flags);
584}
585
586static void overwrite_endio(struct bio *bio, int err)
587{
588 unsigned long flags;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000589 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Mike Snitzera24c2562012-06-03 00:30:00 +0100590 struct dm_thin_new_mapping *m = h->overwrite_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000591 struct pool *pool = m->tc->pool;
592
593 m->err = err;
594
595 spin_lock_irqsave(&pool->lock, flags);
Mike Snitzer7f214662013-12-17 13:43:31 -0500596 m->prepared = true;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000597 __maybe_add_mapping(m);
598 spin_unlock_irqrestore(&pool->lock, flags);
599}
600
Joe Thornber991d9fa2011-10-31 20:21:18 +0000601/*----------------------------------------------------------------*/
602
603/*
604 * Workqueue.
605 */
606
607/*
608 * Prepared mapping jobs.
609 */
610
611/*
612 * This sends the bios in the cell back to the deferred_bios list.
613 */
Joe Thornber2aab3852012-12-21 20:23:33 +0000614static void cell_defer(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000615{
616 struct pool *pool = tc->pool;
617 unsigned long flags;
618
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400619 spin_lock_irqsave(&tc->lock, flags);
620 cell_release(pool, cell, &tc->deferred_bio_list);
621 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000622
623 wake_worker(pool);
624}
625
626/*
Joe Thornber6beca5e2013-03-01 22:45:50 +0000627 * Same as cell_defer above, except it omits the original holder of the cell.
Joe Thornber991d9fa2011-10-31 20:21:18 +0000628 */
Joe Thornberf286ba02012-12-21 20:23:33 +0000629static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000630{
Joe Thornber991d9fa2011-10-31 20:21:18 +0000631 struct pool *pool = tc->pool;
632 unsigned long flags;
633
Mike Snitzerc140e1c2014-03-20 21:17:14 -0400634 spin_lock_irqsave(&tc->lock, flags);
635 cell_release_no_holder(pool, cell, &tc->deferred_bio_list);
636 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000637
638 wake_worker(pool);
639}
640
Joe Thornbere49e5822012-07-27 15:08:16 +0100641static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
642{
Kent Overstreet196d38b2013-11-23 18:34:15 -0800643 if (m->bio) {
Joe Thornbere49e5822012-07-27 15:08:16 +0100644 m->bio->bi_end_io = m->saved_bi_end_io;
Kent Overstreet196d38b2013-11-23 18:34:15 -0800645 atomic_inc(&m->bio->bi_remaining);
646 }
Joe Thornber6beca5e2013-03-01 22:45:50 +0000647 cell_error(m->tc->pool, m->cell);
Joe Thornbere49e5822012-07-27 15:08:16 +0100648 list_del(&m->list);
649 mempool_free(m, m->tc->pool->mapping_pool);
650}
Joe Thornber025b9682013-03-01 22:45:50 +0000651
Mike Snitzera24c2562012-06-03 00:30:00 +0100652static void process_prepared_mapping(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000653{
654 struct thin_c *tc = m->tc;
Joe Thornber6beca5e2013-03-01 22:45:50 +0000655 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000656 struct bio *bio;
657 int r;
658
659 bio = m->bio;
Kent Overstreet196d38b2013-11-23 18:34:15 -0800660 if (bio) {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000661 bio->bi_end_io = m->saved_bi_end_io;
Kent Overstreet196d38b2013-11-23 18:34:15 -0800662 atomic_inc(&bio->bi_remaining);
663 }
Joe Thornber991d9fa2011-10-31 20:21:18 +0000664
665 if (m->err) {
Joe Thornber6beca5e2013-03-01 22:45:50 +0000666 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100667 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000668 }
669
670 /*
671 * Commit the prepared block into the mapping btree.
672 * Any I/O for this block arriving after this point will get
673 * remapped to it directly.
674 */
675 r = dm_thin_insert_block(tc->td, m->virt_block, m->data_block);
676 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -0500677 metadata_operation_failed(pool, "dm_thin_insert_block", r);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000678 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100679 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000680 }
681
682 /*
683 * Release any bios held while the block was being provisioned.
684 * If we are processing a write bio that completely covers the block,
685 * we already processed it so can ignore it now when processing
686 * the bios in the cell.
687 */
688 if (bio) {
Joe Thornberf286ba02012-12-21 20:23:33 +0000689 cell_defer_no_holder(tc, m->cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000690 bio_endio(bio, 0);
691 } else
Joe Thornber2aab3852012-12-21 20:23:33 +0000692 cell_defer(tc, m->cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000693
Joe Thornber905386f2012-07-27 15:08:05 +0100694out:
Joe Thornber991d9fa2011-10-31 20:21:18 +0000695 list_del(&m->list);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000696 mempool_free(m, pool->mapping_pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000697}
698
Joe Thornbere49e5822012-07-27 15:08:16 +0100699static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
Joe Thornber104655f2012-03-28 18:41:28 +0100700{
Joe Thornber104655f2012-03-28 18:41:28 +0100701 struct thin_c *tc = m->tc;
702
Joe Thornbere49e5822012-07-27 15:08:16 +0100703 bio_io_error(m->bio);
Joe Thornberf286ba02012-12-21 20:23:33 +0000704 cell_defer_no_holder(tc, m->cell);
705 cell_defer_no_holder(tc, m->cell2);
Joe Thornbere49e5822012-07-27 15:08:16 +0100706 mempool_free(m, tc->pool->mapping_pool);
707}
Joe Thornber104655f2012-03-28 18:41:28 +0100708
Joe Thornbere49e5822012-07-27 15:08:16 +0100709static void process_prepared_discard_passdown(struct dm_thin_new_mapping *m)
710{
711 struct thin_c *tc = m->tc;
712
Joe Thornbere8088072012-12-21 20:23:31 +0000713 inc_all_io_entry(tc->pool, m->bio);
Joe Thornberf286ba02012-12-21 20:23:33 +0000714 cell_defer_no_holder(tc, m->cell);
715 cell_defer_no_holder(tc, m->cell2);
Joe Thornbere8088072012-12-21 20:23:31 +0000716
Joe Thornber104655f2012-03-28 18:41:28 +0100717 if (m->pass_discard)
Joe Thornber19fa1a62013-12-17 12:09:40 -0500718 if (m->definitely_not_shared)
719 remap_and_issue(tc, m->bio, m->data_block);
720 else {
721 bool used = false;
722 if (dm_pool_block_is_used(tc->pool->pmd, m->data_block, &used) || used)
723 bio_endio(m->bio, 0);
724 else
725 remap_and_issue(tc, m->bio, m->data_block);
726 }
Joe Thornber104655f2012-03-28 18:41:28 +0100727 else
728 bio_endio(m->bio, 0);
729
Joe Thornber104655f2012-03-28 18:41:28 +0100730 mempool_free(m, tc->pool->mapping_pool);
731}
732
Joe Thornbere49e5822012-07-27 15:08:16 +0100733static void process_prepared_discard(struct dm_thin_new_mapping *m)
734{
735 int r;
736 struct thin_c *tc = m->tc;
737
738 r = dm_thin_remove_block(tc->td, m->virt_block);
739 if (r)
Mike Snitzerc3977412012-12-21 20:23:34 +0000740 DMERR_LIMIT("dm_thin_remove_block() failed");
Joe Thornbere49e5822012-07-27 15:08:16 +0100741
742 process_prepared_discard_passdown(m);
743}
744
Joe Thornber104655f2012-03-28 18:41:28 +0100745static void process_prepared(struct pool *pool, struct list_head *head,
Joe Thornbere49e5822012-07-27 15:08:16 +0100746 process_mapping_fn *fn)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000747{
748 unsigned long flags;
749 struct list_head maps;
Mike Snitzera24c2562012-06-03 00:30:00 +0100750 struct dm_thin_new_mapping *m, *tmp;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000751
752 INIT_LIST_HEAD(&maps);
753 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +0100754 list_splice_init(head, &maps);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000755 spin_unlock_irqrestore(&pool->lock, flags);
756
757 list_for_each_entry_safe(m, tmp, &maps, list)
Joe Thornbere49e5822012-07-27 15:08:16 +0100758 (*fn)(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000759}
760
761/*
762 * Deferred bio jobs.
763 */
Joe Thornber104655f2012-03-28 18:41:28 +0100764static int io_overlaps_block(struct pool *pool, struct bio *bio)
765{
Kent Overstreet4f024f32013-10-11 15:44:27 -0700766 return bio->bi_iter.bi_size ==
767 (pool->sectors_per_block << SECTOR_SHIFT);
Joe Thornber104655f2012-03-28 18:41:28 +0100768}
769
Joe Thornber991d9fa2011-10-31 20:21:18 +0000770static int io_overwrites_block(struct pool *pool, struct bio *bio)
771{
Joe Thornber104655f2012-03-28 18:41:28 +0100772 return (bio_data_dir(bio) == WRITE) &&
773 io_overlaps_block(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000774}
775
776static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
777 bio_end_io_t *fn)
778{
779 *save = bio->bi_end_io;
780 bio->bi_end_io = fn;
781}
782
783static int ensure_next_mapping(struct pool *pool)
784{
785 if (pool->next_mapping)
786 return 0;
787
788 pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
789
790 return pool->next_mapping ? 0 : -ENOMEM;
791}
792
Mike Snitzera24c2562012-06-03 00:30:00 +0100793static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000794{
Mike Snitzer16961b02013-12-17 13:19:11 -0500795 struct dm_thin_new_mapping *m = pool->next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000796
797 BUG_ON(!pool->next_mapping);
798
Mike Snitzer16961b02013-12-17 13:19:11 -0500799 memset(m, 0, sizeof(struct dm_thin_new_mapping));
800 INIT_LIST_HEAD(&m->list);
801 m->bio = NULL;
802
Joe Thornber991d9fa2011-10-31 20:21:18 +0000803 pool->next_mapping = NULL;
804
Mike Snitzer16961b02013-12-17 13:19:11 -0500805 return m;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000806}
807
808static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
Joe Thornber2dd9c252012-03-28 18:41:28 +0100809 struct dm_dev *origin, dm_block_t data_origin,
810 dm_block_t data_dest,
Mike Snitzera24c2562012-06-03 00:30:00 +0100811 struct dm_bio_prison_cell *cell, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000812{
813 int r;
814 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +0100815 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000816
Joe Thornber991d9fa2011-10-31 20:21:18 +0000817 m->tc = tc;
818 m->virt_block = virt_block;
819 m->data_block = data_dest;
820 m->cell = cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000821
Mike Snitzer44feb382012-10-12 21:02:10 +0100822 if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
Mike Snitzer7f214662013-12-17 13:43:31 -0500823 m->quiesced = true;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000824
825 /*
826 * IO to pool_dev remaps to the pool target's data_dev.
827 *
828 * If the whole block of data is being overwritten, we can issue the
829 * bio immediately. Otherwise we use kcopyd to clone the data first.
830 */
831 if (io_overwrites_block(pool, bio)) {
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000832 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Mike Snitzera24c2562012-06-03 00:30:00 +0100833
Joe Thornbereb2aa482012-03-28 18:41:28 +0100834 h->overwrite_mapping = m;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000835 m->bio = bio;
836 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
Joe Thornbere8088072012-12-21 20:23:31 +0000837 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000838 remap_and_issue(tc, bio, data_dest);
839 } else {
840 struct dm_io_region from, to;
841
Joe Thornber2dd9c252012-03-28 18:41:28 +0100842 from.bdev = origin->bdev;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000843 from.sector = data_origin * pool->sectors_per_block;
844 from.count = pool->sectors_per_block;
845
846 to.bdev = tc->pool_dev->bdev;
847 to.sector = data_dest * pool->sectors_per_block;
848 to.count = pool->sectors_per_block;
849
850 r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
851 0, copy_complete, m);
852 if (r < 0) {
853 mempool_free(m, pool->mapping_pool);
Mike Snitzerc3977412012-12-21 20:23:34 +0000854 DMERR_LIMIT("dm_kcopyd_copy() failed");
Joe Thornber6beca5e2013-03-01 22:45:50 +0000855 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000856 }
857 }
858}
859
Joe Thornber2dd9c252012-03-28 18:41:28 +0100860static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
861 dm_block_t data_origin, dm_block_t data_dest,
Mike Snitzera24c2562012-06-03 00:30:00 +0100862 struct dm_bio_prison_cell *cell, struct bio *bio)
Joe Thornber2dd9c252012-03-28 18:41:28 +0100863{
864 schedule_copy(tc, virt_block, tc->pool_dev,
865 data_origin, data_dest, cell, bio);
866}
867
868static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
869 dm_block_t data_dest,
Mike Snitzera24c2562012-06-03 00:30:00 +0100870 struct dm_bio_prison_cell *cell, struct bio *bio)
Joe Thornber2dd9c252012-03-28 18:41:28 +0100871{
872 schedule_copy(tc, virt_block, tc->origin_dev,
873 virt_block, data_dest, cell, bio);
874}
875
Joe Thornber991d9fa2011-10-31 20:21:18 +0000876static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
Mike Snitzera24c2562012-06-03 00:30:00 +0100877 dm_block_t data_block, struct dm_bio_prison_cell *cell,
Joe Thornber991d9fa2011-10-31 20:21:18 +0000878 struct bio *bio)
879{
880 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +0100881 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000882
Mike Snitzer7f214662013-12-17 13:43:31 -0500883 m->quiesced = true;
884 m->prepared = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000885 m->tc = tc;
886 m->virt_block = virt_block;
887 m->data_block = data_block;
888 m->cell = cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000889
890 /*
891 * If the whole block of data is being overwritten or we are not
892 * zeroing pre-existing data, we can issue the bio immediately.
893 * Otherwise we use kcopyd to zero the data first.
894 */
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100895 if (!pool->pf.zero_new_blocks)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000896 process_prepared_mapping(m);
897
898 else if (io_overwrites_block(pool, bio)) {
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000899 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Mike Snitzera24c2562012-06-03 00:30:00 +0100900
Joe Thornbereb2aa482012-03-28 18:41:28 +0100901 h->overwrite_mapping = m;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000902 m->bio = bio;
903 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
Joe Thornbere8088072012-12-21 20:23:31 +0000904 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000905 remap_and_issue(tc, bio, data_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000906 } else {
907 int r;
908 struct dm_io_region to;
909
910 to.bdev = tc->pool_dev->bdev;
911 to.sector = data_block * pool->sectors_per_block;
912 to.count = pool->sectors_per_block;
913
914 r = dm_kcopyd_zero(pool->copier, 1, &to, 0, copy_complete, m);
915 if (r < 0) {
916 mempool_free(m, pool->mapping_pool);
Mike Snitzerc3977412012-12-21 20:23:34 +0000917 DMERR_LIMIT("dm_kcopyd_zero() failed");
Joe Thornber6beca5e2013-03-01 22:45:50 +0000918 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000919 }
920 }
921}
922
Joe Thornbere49e5822012-07-27 15:08:16 +0100923/*
924 * A non-zero return indicates read_only or fail_io mode.
925 * Many callers don't care about the return value.
926 */
Joe Thornber020cc3b2013-12-04 15:05:36 -0500927static int commit(struct pool *pool)
Joe Thornbere49e5822012-07-27 15:08:16 +0100928{
929 int r;
930
931 if (get_pool_mode(pool) != PM_WRITE)
932 return -EINVAL;
933
Joe Thornber020cc3b2013-12-04 15:05:36 -0500934 r = dm_pool_commit_metadata(pool->pmd);
Joe Thornberb5330652013-12-04 19:51:33 -0500935 if (r)
936 metadata_operation_failed(pool, "dm_pool_commit_metadata", r);
Joe Thornbere49e5822012-07-27 15:08:16 +0100937
938 return r;
939}
940
Joe Thornber88a66212013-12-04 20:16:12 -0500941static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks)
942{
943 unsigned long flags;
944
945 if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
946 DMWARN("%s: reached low water mark for data device: sending event.",
947 dm_device_name(pool->pool_md));
948 spin_lock_irqsave(&pool->lock, flags);
949 pool->low_water_triggered = true;
950 spin_unlock_irqrestore(&pool->lock, flags);
951 dm_table_event(pool->ti->table);
952 }
953}
954
Joe Thornber3e1a0692014-03-03 16:03:26 +0000955static void set_pool_mode(struct pool *pool, enum pool_mode new_mode);
956
Joe Thornber991d9fa2011-10-31 20:21:18 +0000957static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
958{
959 int r;
960 dm_block_t free_blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000961 struct pool *pool = tc->pool;
962
Joe Thornber3e1a0692014-03-03 16:03:26 +0000963 if (WARN_ON(get_pool_mode(pool) != PM_WRITE))
Joe Thornber8d30abf2013-12-04 19:16:11 -0500964 return -EINVAL;
965
Joe Thornber991d9fa2011-10-31 20:21:18 +0000966 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
Joe Thornberb5330652013-12-04 19:51:33 -0500967 if (r) {
968 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000969 return r;
Joe Thornberb5330652013-12-04 19:51:33 -0500970 }
Joe Thornber991d9fa2011-10-31 20:21:18 +0000971
Joe Thornber88a66212013-12-04 20:16:12 -0500972 check_low_water_mark(pool, free_blocks);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000973
974 if (!free_blocks) {
Mike Snitzer94563ba2013-08-22 09:56:18 -0400975 /*
976 * Try to commit to see if that will free up some
977 * more space.
978 */
Joe Thornber020cc3b2013-12-04 15:05:36 -0500979 r = commit(pool);
980 if (r)
981 return r;
Mike Snitzer94563ba2013-08-22 09:56:18 -0400982
983 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
Joe Thornberb5330652013-12-04 19:51:33 -0500984 if (r) {
985 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
Mike Snitzer94563ba2013-08-22 09:56:18 -0400986 return r;
Joe Thornberb5330652013-12-04 19:51:33 -0500987 }
Mike Snitzer94563ba2013-08-22 09:56:18 -0400988
Mike Snitzer94563ba2013-08-22 09:56:18 -0400989 if (!free_blocks) {
Joe Thornber3e1a0692014-03-03 16:03:26 +0000990 set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000991 return -ENOSPC;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000992 }
993 }
994
995 r = dm_pool_alloc_data_block(pool->pmd, result);
Mike Snitzer4a02b342013-12-03 12:20:57 -0500996 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -0500997 metadata_operation_failed(pool, "dm_pool_alloc_data_block", r);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000998 return r;
Mike Snitzer4a02b342013-12-03 12:20:57 -0500999 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001000
1001 return 0;
1002}
1003
1004/*
1005 * If we have run out of space, queue bios until the device is
1006 * resumed, presumably after having been reloaded with more space.
1007 */
1008static void retry_on_resume(struct bio *bio)
1009{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001010 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01001011 struct thin_c *tc = h->tc;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001012 unsigned long flags;
1013
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001014 spin_lock_irqsave(&tc->lock, flags);
1015 bio_list_add(&tc->retry_on_resume_list, bio);
1016 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001017}
1018
Joe Thornber3e1a0692014-03-03 16:03:26 +00001019static bool should_error_unserviceable_bio(struct pool *pool)
1020{
1021 enum pool_mode m = get_pool_mode(pool);
1022
1023 switch (m) {
1024 case PM_WRITE:
1025 /* Shouldn't get here */
1026 DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
1027 return true;
1028
1029 case PM_OUT_OF_DATA_SPACE:
1030 return pool->pf.error_if_no_space;
1031
1032 case PM_READ_ONLY:
1033 case PM_FAIL:
1034 return true;
1035 default:
1036 /* Shouldn't get here */
1037 DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
1038 return true;
1039 }
1040}
1041
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001042static void handle_unserviceable_bio(struct pool *pool, struct bio *bio)
1043{
Joe Thornber3e1a0692014-03-03 16:03:26 +00001044 if (should_error_unserviceable_bio(pool))
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001045 bio_io_error(bio);
Mike Snitzer6d162022013-12-20 18:09:02 -05001046 else
1047 retry_on_resume(bio);
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001048}
1049
Mike Snitzer399cadd2013-12-05 16:03:33 -05001050static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001051{
1052 struct bio *bio;
1053 struct bio_list bios;
1054
Joe Thornber3e1a0692014-03-03 16:03:26 +00001055 if (should_error_unserviceable_bio(pool)) {
1056 cell_error(pool, cell);
1057 return;
1058 }
1059
Joe Thornber991d9fa2011-10-31 20:21:18 +00001060 bio_list_init(&bios);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001061 cell_release(pool, cell, &bios);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001062
Joe Thornber3e1a0692014-03-03 16:03:26 +00001063 if (should_error_unserviceable_bio(pool))
1064 while ((bio = bio_list_pop(&bios)))
1065 bio_io_error(bio);
1066 else
1067 while ((bio = bio_list_pop(&bios)))
1068 retry_on_resume(bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001069}
1070
Joe Thornber104655f2012-03-28 18:41:28 +01001071static void process_discard(struct thin_c *tc, struct bio *bio)
1072{
1073 int r;
Mike Snitzerc3a0ce22012-05-12 01:43:16 +01001074 unsigned long flags;
Joe Thornber104655f2012-03-28 18:41:28 +01001075 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +01001076 struct dm_bio_prison_cell *cell, *cell2;
Mike Snitzer44feb382012-10-12 21:02:10 +01001077 struct dm_cell_key key, key2;
Joe Thornber104655f2012-03-28 18:41:28 +01001078 dm_block_t block = get_bio_block(tc, bio);
1079 struct dm_thin_lookup_result lookup_result;
Mike Snitzera24c2562012-06-03 00:30:00 +01001080 struct dm_thin_new_mapping *m;
Joe Thornber104655f2012-03-28 18:41:28 +01001081
1082 build_virtual_key(tc->td, block, &key);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001083 if (bio_detain(tc->pool, &key, bio, &cell))
Joe Thornber104655f2012-03-28 18:41:28 +01001084 return;
1085
1086 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1087 switch (r) {
1088 case 0:
1089 /*
1090 * Check nobody is fiddling with this pool block. This can
1091 * happen if someone's in the process of breaking sharing
1092 * on this block.
1093 */
1094 build_data_key(tc->td, lookup_result.block, &key2);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001095 if (bio_detain(tc->pool, &key2, bio, &cell2)) {
Joe Thornberf286ba02012-12-21 20:23:33 +00001096 cell_defer_no_holder(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001097 break;
1098 }
1099
1100 if (io_overlaps_block(pool, bio)) {
1101 /*
1102 * IO may still be going to the destination block. We must
1103 * quiesce before we can do the removal.
1104 */
1105 m = get_next_mapping(pool);
1106 m->tc = tc;
Joe Thornber19fa1a62013-12-17 12:09:40 -05001107 m->pass_discard = pool->pf.discard_passdown;
1108 m->definitely_not_shared = !lookup_result.shared;
Joe Thornber104655f2012-03-28 18:41:28 +01001109 m->virt_block = block;
1110 m->data_block = lookup_result.block;
1111 m->cell = cell;
1112 m->cell2 = cell2;
Joe Thornber104655f2012-03-28 18:41:28 +01001113 m->bio = bio;
1114
Mike Snitzer44feb382012-10-12 21:02:10 +01001115 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list)) {
Mike Snitzerc3a0ce22012-05-12 01:43:16 +01001116 spin_lock_irqsave(&pool->lock, flags);
Mike Snitzerdaec3382013-12-11 14:01:20 -05001117 list_add_tail(&m->list, &pool->prepared_discards);
Mike Snitzerc3a0ce22012-05-12 01:43:16 +01001118 spin_unlock_irqrestore(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +01001119 wake_worker(pool);
1120 }
1121 } else {
Joe Thornbere8088072012-12-21 20:23:31 +00001122 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001123 cell_defer_no_holder(tc, cell);
1124 cell_defer_no_holder(tc, cell2);
Joe Thornbere8088072012-12-21 20:23:31 +00001125
Joe Thornber104655f2012-03-28 18:41:28 +01001126 /*
Mikulas Patocka49296302012-07-27 15:08:03 +01001127 * The DM core makes sure that the discard doesn't span
1128 * a block boundary. So we submit the discard of a
1129 * partial block appropriately.
Joe Thornber104655f2012-03-28 18:41:28 +01001130 */
Mikulas Patocka650d2a02012-07-20 14:25:05 +01001131 if ((!lookup_result.shared) && pool->pf.discard_passdown)
1132 remap_and_issue(tc, bio, lookup_result.block);
1133 else
1134 bio_endio(bio, 0);
Joe Thornber104655f2012-03-28 18:41:28 +01001135 }
1136 break;
1137
1138 case -ENODATA:
1139 /*
1140 * It isn't provisioned, just forget it.
1141 */
Joe Thornberf286ba02012-12-21 20:23:33 +00001142 cell_defer_no_holder(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001143 bio_endio(bio, 0);
1144 break;
1145
1146 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001147 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1148 __func__, r);
Joe Thornberf286ba02012-12-21 20:23:33 +00001149 cell_defer_no_holder(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001150 bio_io_error(bio);
1151 break;
1152 }
1153}
1154
Joe Thornber991d9fa2011-10-31 20:21:18 +00001155static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzer44feb382012-10-12 21:02:10 +01001156 struct dm_cell_key *key,
Joe Thornber991d9fa2011-10-31 20:21:18 +00001157 struct dm_thin_lookup_result *lookup_result,
Mike Snitzera24c2562012-06-03 00:30:00 +01001158 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001159{
1160 int r;
1161 dm_block_t data_block;
Mike Snitzerd6fc2042013-08-21 17:40:11 -04001162 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001163
1164 r = alloc_data_block(tc, &data_block);
1165 switch (r) {
1166 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001167 schedule_internal_copy(tc, block, lookup_result->block,
1168 data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001169 break;
1170
1171 case -ENOSPC:
Mike Snitzer399cadd2013-12-05 16:03:33 -05001172 retry_bios_on_resume(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001173 break;
1174
1175 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001176 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1177 __func__, r);
Mike Snitzerd6fc2042013-08-21 17:40:11 -04001178 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001179 break;
1180 }
1181}
1182
1183static void process_shared_bio(struct thin_c *tc, struct bio *bio,
1184 dm_block_t block,
1185 struct dm_thin_lookup_result *lookup_result)
1186{
Mike Snitzera24c2562012-06-03 00:30:00 +01001187 struct dm_bio_prison_cell *cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001188 struct pool *pool = tc->pool;
Mike Snitzer44feb382012-10-12 21:02:10 +01001189 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001190
1191 /*
1192 * If cell is already occupied, then sharing is already in the process
1193 * of being broken so we have nothing further to do here.
1194 */
1195 build_data_key(tc->td, lookup_result->block, &key);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001196 if (bio_detain(pool, &key, bio, &cell))
Joe Thornber991d9fa2011-10-31 20:21:18 +00001197 return;
1198
Kent Overstreet4f024f32013-10-11 15:44:27 -07001199 if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001200 break_sharing(tc, bio, block, &key, lookup_result, cell);
1201 else {
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001202 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornber991d9fa2011-10-31 20:21:18 +00001203
Mike Snitzer44feb382012-10-12 21:02:10 +01001204 h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
Joe Thornbere8088072012-12-21 20:23:31 +00001205 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001206 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001207
Joe Thornber991d9fa2011-10-31 20:21:18 +00001208 remap_and_issue(tc, bio, lookup_result->block);
1209 }
1210}
1211
1212static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzera24c2562012-06-03 00:30:00 +01001213 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001214{
1215 int r;
1216 dm_block_t data_block;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001217 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001218
1219 /*
1220 * Remap empty bios (flushes) immediately, without provisioning.
1221 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07001222 if (!bio->bi_iter.bi_size) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001223 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001224 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001225
Joe Thornber991d9fa2011-10-31 20:21:18 +00001226 remap_and_issue(tc, bio, 0);
1227 return;
1228 }
1229
1230 /*
1231 * Fill read bios with zeroes and complete them immediately.
1232 */
1233 if (bio_data_dir(bio) == READ) {
1234 zero_fill_bio(bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001235 cell_defer_no_holder(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001236 bio_endio(bio, 0);
1237 return;
1238 }
1239
1240 r = alloc_data_block(tc, &data_block);
1241 switch (r) {
1242 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001243 if (tc->origin_dev)
1244 schedule_external_copy(tc, block, data_block, cell, bio);
1245 else
1246 schedule_zero(tc, block, data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001247 break;
1248
1249 case -ENOSPC:
Mike Snitzer399cadd2013-12-05 16:03:33 -05001250 retry_bios_on_resume(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001251 break;
1252
1253 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001254 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1255 __func__, r);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001256 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001257 break;
1258 }
1259}
1260
1261static void process_bio(struct thin_c *tc, struct bio *bio)
1262{
1263 int r;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001264 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001265 dm_block_t block = get_bio_block(tc, bio);
Mike Snitzera24c2562012-06-03 00:30:00 +01001266 struct dm_bio_prison_cell *cell;
Mike Snitzer44feb382012-10-12 21:02:10 +01001267 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001268 struct dm_thin_lookup_result lookup_result;
1269
1270 /*
1271 * If cell is already occupied, then the block is already
1272 * being provisioned so we have nothing further to do here.
1273 */
1274 build_virtual_key(tc->td, block, &key);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001275 if (bio_detain(pool, &key, bio, &cell))
Joe Thornber991d9fa2011-10-31 20:21:18 +00001276 return;
1277
1278 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1279 switch (r) {
1280 case 0:
Joe Thornbere8088072012-12-21 20:23:31 +00001281 if (lookup_result.shared) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001282 process_shared_bio(tc, bio, block, &lookup_result);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001283 cell_defer_no_holder(tc, cell); /* FIXME: pass this cell into process_shared? */
Joe Thornbere8088072012-12-21 20:23:31 +00001284 } else {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001285 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001286 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001287
Joe Thornber991d9fa2011-10-31 20:21:18 +00001288 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001289 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001290 break;
1291
1292 case -ENODATA:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001293 if (bio_data_dir(bio) == READ && tc->origin_dev) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001294 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001295 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001296
Joe Thornber2dd9c252012-03-28 18:41:28 +01001297 remap_to_origin_and_issue(tc, bio);
1298 } else
1299 provision_block(tc, bio, block, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001300 break;
1301
1302 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001303 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1304 __func__, r);
Joe Thornberf286ba02012-12-21 20:23:33 +00001305 cell_defer_no_holder(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001306 bio_io_error(bio);
1307 break;
1308 }
1309}
1310
Joe Thornbere49e5822012-07-27 15:08:16 +01001311static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
1312{
1313 int r;
1314 int rw = bio_data_dir(bio);
1315 dm_block_t block = get_bio_block(tc, bio);
1316 struct dm_thin_lookup_result lookup_result;
1317
1318 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1319 switch (r) {
1320 case 0:
Kent Overstreet4f024f32013-10-11 15:44:27 -07001321 if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size)
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001322 handle_unserviceable_bio(tc->pool, bio);
Joe Thornbere8088072012-12-21 20:23:31 +00001323 else {
1324 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001325 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001326 }
Joe Thornbere49e5822012-07-27 15:08:16 +01001327 break;
1328
1329 case -ENODATA:
1330 if (rw != READ) {
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001331 handle_unserviceable_bio(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001332 break;
1333 }
1334
1335 if (tc->origin_dev) {
Joe Thornbere8088072012-12-21 20:23:31 +00001336 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001337 remap_to_origin_and_issue(tc, bio);
1338 break;
1339 }
1340
1341 zero_fill_bio(bio);
1342 bio_endio(bio, 0);
1343 break;
1344
1345 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001346 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1347 __func__, r);
Joe Thornbere49e5822012-07-27 15:08:16 +01001348 bio_io_error(bio);
1349 break;
1350 }
1351}
1352
Joe Thornber3e1a0692014-03-03 16:03:26 +00001353static void process_bio_success(struct thin_c *tc, struct bio *bio)
1354{
1355 bio_endio(bio, 0);
1356}
1357
Joe Thornbere49e5822012-07-27 15:08:16 +01001358static void process_bio_fail(struct thin_c *tc, struct bio *bio)
1359{
1360 bio_io_error(bio);
1361}
1362
Joe Thornberac8c3f32013-05-10 14:37:21 +01001363/*
1364 * FIXME: should we also commit due to size of transaction, measured in
1365 * metadata blocks?
1366 */
Joe Thornber905e51b2012-03-28 18:41:27 +01001367static int need_commit_due_to_time(struct pool *pool)
1368{
1369 return jiffies < pool->last_commit_jiffies ||
1370 jiffies > pool->last_commit_jiffies + COMMIT_PERIOD;
1371}
1372
Mike Snitzer67324ea2014-03-21 18:33:41 -04001373#define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node)
1374#define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook))
1375
1376static void __thin_bio_rb_add(struct thin_c *tc, struct bio *bio)
1377{
1378 struct rb_node **rbp, *parent;
1379 struct dm_thin_endio_hook *pbd;
1380 sector_t bi_sector = bio->bi_iter.bi_sector;
1381
1382 rbp = &tc->sort_bio_list.rb_node;
1383 parent = NULL;
1384 while (*rbp) {
1385 parent = *rbp;
1386 pbd = thin_pbd(parent);
1387
1388 if (bi_sector < thin_bio(pbd)->bi_iter.bi_sector)
1389 rbp = &(*rbp)->rb_left;
1390 else
1391 rbp = &(*rbp)->rb_right;
1392 }
1393
1394 pbd = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1395 rb_link_node(&pbd->rb_node, parent, rbp);
1396 rb_insert_color(&pbd->rb_node, &tc->sort_bio_list);
1397}
1398
1399static void __extract_sorted_bios(struct thin_c *tc)
1400{
1401 struct rb_node *node;
1402 struct dm_thin_endio_hook *pbd;
1403 struct bio *bio;
1404
1405 for (node = rb_first(&tc->sort_bio_list); node; node = rb_next(node)) {
1406 pbd = thin_pbd(node);
1407 bio = thin_bio(pbd);
1408
1409 bio_list_add(&tc->deferred_bio_list, bio);
1410 rb_erase(&pbd->rb_node, &tc->sort_bio_list);
1411 }
1412
1413 WARN_ON(!RB_EMPTY_ROOT(&tc->sort_bio_list));
1414}
1415
1416static void __sort_thin_deferred_bios(struct thin_c *tc)
1417{
1418 struct bio *bio;
1419 struct bio_list bios;
1420
1421 bio_list_init(&bios);
1422 bio_list_merge(&bios, &tc->deferred_bio_list);
1423 bio_list_init(&tc->deferred_bio_list);
1424
1425 /* Sort deferred_bio_list using rb-tree */
1426 while ((bio = bio_list_pop(&bios)))
1427 __thin_bio_rb_add(tc, bio);
1428
1429 /*
1430 * Transfer the sorted bios in sort_bio_list back to
1431 * deferred_bio_list to allow lockless submission of
1432 * all bios.
1433 */
1434 __extract_sorted_bios(tc);
1435}
1436
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001437static void process_thin_deferred_bios(struct thin_c *tc)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001438{
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001439 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001440 unsigned long flags;
1441 struct bio *bio;
1442 struct bio_list bios;
Mike Snitzer67324ea2014-03-21 18:33:41 -04001443 struct blk_plug plug;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001444
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001445 if (tc->requeue_mode) {
1446 requeue_bio_list(tc, &tc->deferred_bio_list);
1447 return;
1448 }
1449
Joe Thornber991d9fa2011-10-31 20:21:18 +00001450 bio_list_init(&bios);
1451
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001452 spin_lock_irqsave(&tc->lock, flags);
Mike Snitzer67324ea2014-03-21 18:33:41 -04001453
1454 if (bio_list_empty(&tc->deferred_bio_list)) {
1455 spin_unlock_irqrestore(&tc->lock, flags);
1456 return;
1457 }
1458
1459 __sort_thin_deferred_bios(tc);
1460
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001461 bio_list_merge(&bios, &tc->deferred_bio_list);
1462 bio_list_init(&tc->deferred_bio_list);
Mike Snitzer67324ea2014-03-21 18:33:41 -04001463
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001464 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001465
Mike Snitzer67324ea2014-03-21 18:33:41 -04001466 blk_start_plug(&plug);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001467 while ((bio = bio_list_pop(&bios))) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001468 /*
1469 * If we've got no free new_mapping structs, and processing
1470 * this bio might require one, we pause until there are some
1471 * prepared mappings to process.
1472 */
1473 if (ensure_next_mapping(pool)) {
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001474 spin_lock_irqsave(&tc->lock, flags);
1475 bio_list_add(&tc->deferred_bio_list, bio);
1476 bio_list_merge(&tc->deferred_bio_list, &bios);
1477 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001478 break;
1479 }
Joe Thornber104655f2012-03-28 18:41:28 +01001480
1481 if (bio->bi_rw & REQ_DISCARD)
Joe Thornbere49e5822012-07-27 15:08:16 +01001482 pool->process_discard(tc, bio);
Joe Thornber104655f2012-03-28 18:41:28 +01001483 else
Joe Thornbere49e5822012-07-27 15:08:16 +01001484 pool->process_bio(tc, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001485 }
Mike Snitzer67324ea2014-03-21 18:33:41 -04001486 blk_finish_plug(&plug);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001487}
1488
1489static void process_deferred_bios(struct pool *pool)
1490{
1491 unsigned long flags;
1492 struct bio *bio;
1493 struct bio_list bios;
1494 struct thin_c *tc;
1495
1496 rcu_read_lock();
1497 list_for_each_entry_rcu(tc, &pool->active_thins, list)
1498 process_thin_deferred_bios(tc);
1499 rcu_read_unlock();
Joe Thornber991d9fa2011-10-31 20:21:18 +00001500
1501 /*
1502 * If there are any deferred flush bios, we must commit
1503 * the metadata before issuing them.
1504 */
1505 bio_list_init(&bios);
1506 spin_lock_irqsave(&pool->lock, flags);
1507 bio_list_merge(&bios, &pool->deferred_flush_bios);
1508 bio_list_init(&pool->deferred_flush_bios);
1509 spin_unlock_irqrestore(&pool->lock, flags);
1510
Mike Snitzer4d1662a2014-02-06 06:08:56 -05001511 if (bio_list_empty(&bios) &&
1512 !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool)))
Joe Thornber991d9fa2011-10-31 20:21:18 +00001513 return;
1514
Joe Thornber020cc3b2013-12-04 15:05:36 -05001515 if (commit(pool)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001516 while ((bio = bio_list_pop(&bios)))
1517 bio_io_error(bio);
1518 return;
1519 }
Joe Thornber905e51b2012-03-28 18:41:27 +01001520 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001521
1522 while ((bio = bio_list_pop(&bios)))
1523 generic_make_request(bio);
1524}
1525
1526static void do_worker(struct work_struct *ws)
1527{
1528 struct pool *pool = container_of(ws, struct pool, worker);
1529
Joe Thornbere49e5822012-07-27 15:08:16 +01001530 process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
1531 process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001532 process_deferred_bios(pool);
1533}
1534
Joe Thornber905e51b2012-03-28 18:41:27 +01001535/*
1536 * We want to commit periodically so that not too much
1537 * unwritten data builds up.
1538 */
1539static void do_waker(struct work_struct *ws)
1540{
1541 struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
1542 wake_worker(pool);
1543 queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
1544}
1545
Joe Thornber991d9fa2011-10-31 20:21:18 +00001546/*----------------------------------------------------------------*/
1547
Joe Thornber738211f2014-03-03 15:52:28 +00001548struct noflush_work {
1549 struct work_struct worker;
1550 struct thin_c *tc;
1551
1552 atomic_t complete;
1553 wait_queue_head_t wait;
1554};
1555
1556static void complete_noflush_work(struct noflush_work *w)
1557{
1558 atomic_set(&w->complete, 1);
1559 wake_up(&w->wait);
1560}
1561
1562static void do_noflush_start(struct work_struct *ws)
1563{
1564 struct noflush_work *w = container_of(ws, struct noflush_work, worker);
1565 w->tc->requeue_mode = true;
1566 requeue_io(w->tc);
1567 complete_noflush_work(w);
1568}
1569
1570static void do_noflush_stop(struct work_struct *ws)
1571{
1572 struct noflush_work *w = container_of(ws, struct noflush_work, worker);
1573 w->tc->requeue_mode = false;
1574 complete_noflush_work(w);
1575}
1576
1577static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *))
1578{
1579 struct noflush_work w;
1580
1581 INIT_WORK(&w.worker, fn);
1582 w.tc = tc;
1583 atomic_set(&w.complete, 0);
1584 init_waitqueue_head(&w.wait);
1585
1586 queue_work(tc->pool->wq, &w.worker);
1587
1588 wait_event(w.wait, atomic_read(&w.complete));
1589}
1590
1591/*----------------------------------------------------------------*/
1592
Joe Thornbere49e5822012-07-27 15:08:16 +01001593static enum pool_mode get_pool_mode(struct pool *pool)
1594{
1595 return pool->pf.mode;
1596}
1597
Joe Thornber3e1a0692014-03-03 16:03:26 +00001598static void notify_of_pool_mode_change(struct pool *pool, const char *new_mode)
1599{
1600 dm_table_event(pool->ti->table);
1601 DMINFO("%s: switching pool to %s mode",
1602 dm_device_name(pool->pool_md), new_mode);
1603}
1604
Mike Snitzer8b64e882013-12-20 14:27:28 -05001605static void set_pool_mode(struct pool *pool, enum pool_mode new_mode)
Joe Thornbere49e5822012-07-27 15:08:16 +01001606{
Mike Snitzercdc2b412014-02-14 18:10:55 -05001607 struct pool_c *pt = pool->ti->private;
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05001608 bool needs_check = dm_pool_metadata_needs_check(pool->pmd);
1609 enum pool_mode old_mode = get_pool_mode(pool);
1610
1611 /*
1612 * Never allow the pool to transition to PM_WRITE mode if user
1613 * intervention is required to verify metadata and data consistency.
1614 */
1615 if (new_mode == PM_WRITE && needs_check) {
1616 DMERR("%s: unable to switch pool to write mode until repaired.",
1617 dm_device_name(pool->pool_md));
1618 if (old_mode != new_mode)
1619 new_mode = old_mode;
1620 else
1621 new_mode = PM_READ_ONLY;
1622 }
1623 /*
1624 * If we were in PM_FAIL mode, rollback of metadata failed. We're
1625 * not going to recover without a thin_repair. So we never let the
1626 * pool move out of the old mode.
1627 */
1628 if (old_mode == PM_FAIL)
1629 new_mode = old_mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01001630
Mike Snitzer8b64e882013-12-20 14:27:28 -05001631 switch (new_mode) {
Joe Thornbere49e5822012-07-27 15:08:16 +01001632 case PM_FAIL:
Mike Snitzer8b64e882013-12-20 14:27:28 -05001633 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00001634 notify_of_pool_mode_change(pool, "failure");
Joe Thornber5383ef32013-12-04 16:30:01 -05001635 dm_pool_metadata_read_only(pool->pmd);
Joe Thornbere49e5822012-07-27 15:08:16 +01001636 pool->process_bio = process_bio_fail;
1637 pool->process_discard = process_bio_fail;
1638 pool->process_prepared_mapping = process_prepared_mapping_fail;
1639 pool->process_prepared_discard = process_prepared_discard_fail;
Joe Thornber3e1a0692014-03-03 16:03:26 +00001640
1641 error_retry_list(pool);
Joe Thornbere49e5822012-07-27 15:08:16 +01001642 break;
1643
1644 case PM_READ_ONLY:
Mike Snitzer8b64e882013-12-20 14:27:28 -05001645 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00001646 notify_of_pool_mode_change(pool, "read-only");
1647 dm_pool_metadata_read_only(pool->pmd);
1648 pool->process_bio = process_bio_read_only;
1649 pool->process_discard = process_bio_success;
1650 pool->process_prepared_mapping = process_prepared_mapping_fail;
1651 pool->process_prepared_discard = process_prepared_discard_passdown;
1652
1653 error_retry_list(pool);
1654 break;
1655
1656 case PM_OUT_OF_DATA_SPACE:
1657 /*
1658 * Ideally we'd never hit this state; the low water mark
1659 * would trigger userland to extend the pool before we
1660 * completely run out of data space. However, many small
1661 * IOs to unprovisioned space can consume data space at an
1662 * alarming rate. Adjust your low water mark if you're
1663 * frequently seeing this mode.
1664 */
1665 if (old_mode != new_mode)
1666 notify_of_pool_mode_change(pool, "out-of-data-space");
1667 pool->process_bio = process_bio_read_only;
1668 pool->process_discard = process_discard;
1669 pool->process_prepared_mapping = process_prepared_mapping;
1670 pool->process_prepared_discard = process_prepared_discard_passdown;
Joe Thornbere49e5822012-07-27 15:08:16 +01001671 break;
1672
1673 case PM_WRITE:
Mike Snitzer8b64e882013-12-20 14:27:28 -05001674 if (old_mode != new_mode)
Joe Thornber3e1a0692014-03-03 16:03:26 +00001675 notify_of_pool_mode_change(pool, "write");
Joe Thornber9b7aaa62013-12-04 16:58:19 -05001676 dm_pool_metadata_read_write(pool->pmd);
Joe Thornbere49e5822012-07-27 15:08:16 +01001677 pool->process_bio = process_bio;
1678 pool->process_discard = process_discard;
1679 pool->process_prepared_mapping = process_prepared_mapping;
1680 pool->process_prepared_discard = process_prepared_discard;
1681 break;
1682 }
Mike Snitzer8b64e882013-12-20 14:27:28 -05001683
1684 pool->pf.mode = new_mode;
Mike Snitzercdc2b412014-02-14 18:10:55 -05001685 /*
1686 * The pool mode may have changed, sync it so bind_control_target()
1687 * doesn't cause an unexpected mode transition on resume.
1688 */
1689 pt->adjusted_pf.mode = new_mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01001690}
1691
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05001692static void abort_transaction(struct pool *pool)
1693{
1694 const char *dev_name = dm_device_name(pool->pool_md);
1695
1696 DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
1697 if (dm_pool_abort_metadata(pool->pmd)) {
1698 DMERR("%s: failed to abort metadata transaction", dev_name);
1699 set_pool_mode(pool, PM_FAIL);
1700 }
1701
1702 if (dm_pool_metadata_set_needs_check(pool->pmd)) {
1703 DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
1704 set_pool_mode(pool, PM_FAIL);
1705 }
1706}
1707
Joe Thornberb5330652013-12-04 19:51:33 -05001708static void metadata_operation_failed(struct pool *pool, const char *op, int r)
1709{
1710 DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
1711 dm_device_name(pool->pool_md), op, r);
1712
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05001713 abort_transaction(pool);
Joe Thornberb5330652013-12-04 19:51:33 -05001714 set_pool_mode(pool, PM_READ_ONLY);
1715}
1716
Joe Thornbere49e5822012-07-27 15:08:16 +01001717/*----------------------------------------------------------------*/
1718
Joe Thornber991d9fa2011-10-31 20:21:18 +00001719/*
1720 * Mapping functions.
1721 */
1722
1723/*
1724 * Called only while mapping a thin bio to hand it over to the workqueue.
1725 */
1726static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
1727{
1728 unsigned long flags;
1729 struct pool *pool = tc->pool;
1730
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001731 spin_lock_irqsave(&tc->lock, flags);
1732 bio_list_add(&tc->deferred_bio_list, bio);
1733 spin_unlock_irqrestore(&tc->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001734
1735 wake_worker(pool);
1736}
1737
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001738static void thin_hook_bio(struct thin_c *tc, struct bio *bio)
Joe Thornbereb2aa482012-03-28 18:41:28 +01001739{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001740 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01001741
1742 h->tc = tc;
1743 h->shared_read_entry = NULL;
Joe Thornbere8088072012-12-21 20:23:31 +00001744 h->all_io_entry = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01001745 h->overwrite_mapping = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01001746}
1747
Joe Thornber991d9fa2011-10-31 20:21:18 +00001748/*
1749 * Non-blocking function called from the thin target's map function.
1750 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001751static int thin_bio_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001752{
1753 int r;
1754 struct thin_c *tc = ti->private;
1755 dm_block_t block = get_bio_block(tc, bio);
1756 struct dm_thin_device *td = tc->td;
1757 struct dm_thin_lookup_result result;
Joe Thornber025b9682013-03-01 22:45:50 +00001758 struct dm_bio_prison_cell cell1, cell2;
1759 struct dm_bio_prison_cell *cell_result;
Joe Thornbere8088072012-12-21 20:23:31 +00001760 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001761
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001762 thin_hook_bio(tc, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001763
Joe Thornber738211f2014-03-03 15:52:28 +00001764 if (tc->requeue_mode) {
1765 bio_endio(bio, DM_ENDIO_REQUEUE);
1766 return DM_MAPIO_SUBMITTED;
1767 }
1768
Joe Thornbere49e5822012-07-27 15:08:16 +01001769 if (get_pool_mode(tc->pool) == PM_FAIL) {
1770 bio_io_error(bio);
1771 return DM_MAPIO_SUBMITTED;
1772 }
1773
Joe Thornber104655f2012-03-28 18:41:28 +01001774 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001775 thin_defer_bio(tc, bio);
1776 return DM_MAPIO_SUBMITTED;
1777 }
1778
1779 r = dm_thin_find_block(td, block, 0, &result);
1780
1781 /*
1782 * Note that we defer readahead too.
1783 */
1784 switch (r) {
1785 case 0:
1786 if (unlikely(result.shared)) {
1787 /*
1788 * We have a race condition here between the
1789 * result.shared value returned by the lookup and
1790 * snapshot creation, which may cause new
1791 * sharing.
1792 *
1793 * To avoid this always quiesce the origin before
1794 * taking the snap. You want to do this anyway to
1795 * ensure a consistent application view
1796 * (i.e. lockfs).
1797 *
1798 * More distant ancestors are irrelevant. The
1799 * shared flag will be set in their case.
1800 */
1801 thin_defer_bio(tc, bio);
Joe Thornbere8088072012-12-21 20:23:31 +00001802 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001803 }
Joe Thornbere8088072012-12-21 20:23:31 +00001804
1805 build_virtual_key(tc->td, block, &key);
Joe Thornber025b9682013-03-01 22:45:50 +00001806 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell1, &cell_result))
Joe Thornbere8088072012-12-21 20:23:31 +00001807 return DM_MAPIO_SUBMITTED;
1808
1809 build_data_key(tc->td, result.block, &key);
Joe Thornber025b9682013-03-01 22:45:50 +00001810 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell2, &cell_result)) {
1811 cell_defer_no_holder_no_free(tc, &cell1);
Joe Thornbere8088072012-12-21 20:23:31 +00001812 return DM_MAPIO_SUBMITTED;
1813 }
1814
1815 inc_all_io_entry(tc->pool, bio);
Joe Thornber025b9682013-03-01 22:45:50 +00001816 cell_defer_no_holder_no_free(tc, &cell2);
1817 cell_defer_no_holder_no_free(tc, &cell1);
Joe Thornbere8088072012-12-21 20:23:31 +00001818
1819 remap(tc, bio, result.block);
1820 return DM_MAPIO_REMAPPED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001821
1822 case -ENODATA:
Joe Thornbere49e5822012-07-27 15:08:16 +01001823 if (get_pool_mode(tc->pool) == PM_READ_ONLY) {
1824 /*
1825 * This block isn't provisioned, and we have no way
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001826 * of doing so.
Joe Thornbere49e5822012-07-27 15:08:16 +01001827 */
Mike Snitzer8c0f0e82013-12-05 15:47:24 -05001828 handle_unserviceable_bio(tc->pool, bio);
Joe Thornber2aab3852012-12-21 20:23:33 +00001829 return DM_MAPIO_SUBMITTED;
Joe Thornbere49e5822012-07-27 15:08:16 +01001830 }
1831 /* fall through */
1832
1833 case -EWOULDBLOCK:
Joe Thornber991d9fa2011-10-31 20:21:18 +00001834 /*
1835 * In future, the failed dm_thin_find_block above could
1836 * provide the hint to load the metadata into cache.
1837 */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001838 thin_defer_bio(tc, bio);
Joe Thornber2aab3852012-12-21 20:23:33 +00001839 return DM_MAPIO_SUBMITTED;
Joe Thornbere49e5822012-07-27 15:08:16 +01001840
1841 default:
1842 /*
1843 * Must always call bio_io_error on failure.
1844 * dm_thin_find_block can fail with -EINVAL if the
1845 * pool is switched to fail-io mode.
1846 */
1847 bio_io_error(bio);
Joe Thornber2aab3852012-12-21 20:23:33 +00001848 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001849 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001850}
1851
1852static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1853{
Joe Thornber991d9fa2011-10-31 20:21:18 +00001854 struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
Mike Snitzer760fe672014-03-20 08:36:47 -04001855 struct request_queue *q;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001856
Mike Snitzer760fe672014-03-20 08:36:47 -04001857 if (get_pool_mode(pt->pool) == PM_OUT_OF_DATA_SPACE)
1858 return 1;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001859
Mike Snitzer760fe672014-03-20 08:36:47 -04001860 q = bdev_get_queue(pt->data_dev->bdev);
1861 return bdi_congested(&q->backing_dev_info, bdi_bits);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001862}
1863
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001864static void requeue_bios(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001865{
Mike Snitzerc140e1c2014-03-20 21:17:14 -04001866 unsigned long flags;
1867 struct thin_c *tc;
1868
1869 rcu_read_lock();
1870 list_for_each_entry_rcu(tc, &pool->active_thins, list) {
1871 spin_lock_irqsave(&tc->lock, flags);
1872 bio_list_merge(&tc->deferred_bio_list, &tc->retry_on_resume_list);
1873 bio_list_init(&tc->retry_on_resume_list);
1874 spin_unlock_irqrestore(&tc->lock, flags);
1875 }
1876 rcu_read_unlock();
Joe Thornber991d9fa2011-10-31 20:21:18 +00001877}
1878
1879/*----------------------------------------------------------------
1880 * Binding of control targets to a pool object
1881 *--------------------------------------------------------------*/
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001882static bool data_dev_supports_discard(struct pool_c *pt)
1883{
1884 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
1885
1886 return q && blk_queue_discard(q);
1887}
1888
Joe Thornber58051b92013-03-20 17:21:25 +00001889static bool is_factor(sector_t block_size, uint32_t n)
1890{
1891 return !sector_div(block_size, n);
1892}
1893
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001894/*
1895 * If discard_passdown was enabled verify that the data device
Mike Snitzer0424caa2012-09-26 23:45:47 +01001896 * supports discards. Disable discard_passdown if not.
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001897 */
Mike Snitzer0424caa2012-09-26 23:45:47 +01001898static void disable_passdown_if_not_supported(struct pool_c *pt)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001899{
Mike Snitzer0424caa2012-09-26 23:45:47 +01001900 struct pool *pool = pt->pool;
1901 struct block_device *data_bdev = pt->data_dev->bdev;
1902 struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
1903 sector_t block_size = pool->sectors_per_block << SECTOR_SHIFT;
1904 const char *reason = NULL;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001905 char buf[BDEVNAME_SIZE];
1906
Mike Snitzer0424caa2012-09-26 23:45:47 +01001907 if (!pt->adjusted_pf.discard_passdown)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001908 return;
1909
Mike Snitzer0424caa2012-09-26 23:45:47 +01001910 if (!data_dev_supports_discard(pt))
1911 reason = "discard unsupported";
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001912
Mike Snitzer0424caa2012-09-26 23:45:47 +01001913 else if (data_limits->max_discard_sectors < pool->sectors_per_block)
1914 reason = "max discard sectors smaller than a block";
1915
1916 else if (data_limits->discard_granularity > block_size)
1917 reason = "discard granularity larger than a block";
1918
Joe Thornber58051b92013-03-20 17:21:25 +00001919 else if (!is_factor(block_size, data_limits->discard_granularity))
Mike Snitzer0424caa2012-09-26 23:45:47 +01001920 reason = "discard granularity not a factor of block size";
1921
1922 if (reason) {
1923 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
1924 pt->adjusted_pf.discard_passdown = false;
1925 }
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001926}
1927
Joe Thornber991d9fa2011-10-31 20:21:18 +00001928static int bind_control_target(struct pool *pool, struct dm_target *ti)
1929{
1930 struct pool_c *pt = ti->private;
1931
Joe Thornbere49e5822012-07-27 15:08:16 +01001932 /*
Joe Thornber9b7aaa62013-12-04 16:58:19 -05001933 * We want to make sure that a pool in PM_FAIL mode is never upgraded.
Joe Thornbere49e5822012-07-27 15:08:16 +01001934 */
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05001935 enum pool_mode old_mode = get_pool_mode(pool);
Mike Snitzer0424caa2012-09-26 23:45:47 +01001936 enum pool_mode new_mode = pt->adjusted_pf.mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01001937
Joe Thornber9b7aaa62013-12-04 16:58:19 -05001938 /*
Mike Snitzer8b64e882013-12-20 14:27:28 -05001939 * Don't change the pool's mode until set_pool_mode() below.
1940 * Otherwise the pool's process_* function pointers may
1941 * not match the desired pool mode.
1942 */
1943 pt->adjusted_pf.mode = old_mode;
1944
1945 pool->ti = ti;
1946 pool->pf = pt->adjusted_pf;
1947 pool->low_water_blocks = pt->low_water_blocks;
1948
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001949 set_pool_mode(pool, new_mode);
Mike Snitzerf4026932012-05-19 01:01:01 +01001950
Joe Thornber991d9fa2011-10-31 20:21:18 +00001951 return 0;
1952}
1953
1954static void unbind_control_target(struct pool *pool, struct dm_target *ti)
1955{
1956 if (pool->ti == ti)
1957 pool->ti = NULL;
1958}
1959
1960/*----------------------------------------------------------------
1961 * Pool creation
1962 *--------------------------------------------------------------*/
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001963/* Initialize pool features. */
1964static void pool_features_init(struct pool_features *pf)
1965{
Joe Thornbere49e5822012-07-27 15:08:16 +01001966 pf->mode = PM_WRITE;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001967 pf->zero_new_blocks = true;
1968 pf->discard_enabled = true;
1969 pf->discard_passdown = true;
Mike Snitzer787a996c2013-12-06 16:21:43 -05001970 pf->error_if_no_space = false;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001971}
1972
Joe Thornber991d9fa2011-10-31 20:21:18 +00001973static void __pool_destroy(struct pool *pool)
1974{
1975 __pool_table_remove(pool);
1976
1977 if (dm_pool_metadata_close(pool->pmd) < 0)
1978 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
1979
Mike Snitzer44feb382012-10-12 21:02:10 +01001980 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001981 dm_kcopyd_client_destroy(pool->copier);
1982
1983 if (pool->wq)
1984 destroy_workqueue(pool->wq);
1985
1986 if (pool->next_mapping)
1987 mempool_free(pool->next_mapping, pool->mapping_pool);
1988 mempool_destroy(pool->mapping_pool);
Mike Snitzer44feb382012-10-12 21:02:10 +01001989 dm_deferred_set_destroy(pool->shared_read_ds);
1990 dm_deferred_set_destroy(pool->all_io_ds);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001991 kfree(pool);
1992}
1993
Mike Snitzera24c2562012-06-03 00:30:00 +01001994static struct kmem_cache *_new_mapping_cache;
Mike Snitzera24c2562012-06-03 00:30:00 +01001995
Joe Thornber991d9fa2011-10-31 20:21:18 +00001996static struct pool *pool_create(struct mapped_device *pool_md,
1997 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01001998 unsigned long block_size,
1999 int read_only, char **error)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002000{
2001 int r;
2002 void *err_p;
2003 struct pool *pool;
2004 struct dm_pool_metadata *pmd;
Joe Thornbere49e5822012-07-27 15:08:16 +01002005 bool format_device = read_only ? false : true;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002006
Joe Thornbere49e5822012-07-27 15:08:16 +01002007 pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002008 if (IS_ERR(pmd)) {
2009 *error = "Error creating metadata object";
2010 return (struct pool *)pmd;
2011 }
2012
2013 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
2014 if (!pool) {
2015 *error = "Error allocating memory for pool";
2016 err_p = ERR_PTR(-ENOMEM);
2017 goto bad_pool;
2018 }
2019
2020 pool->pmd = pmd;
2021 pool->sectors_per_block = block_size;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +01002022 if (block_size & (block_size - 1))
2023 pool->sectors_per_block_shift = -1;
2024 else
2025 pool->sectors_per_block_shift = __ffs(block_size);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002026 pool->low_water_blocks = 0;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002027 pool_features_init(&pool->pf);
Mike Snitzer44feb382012-10-12 21:02:10 +01002028 pool->prison = dm_bio_prison_create(PRISON_CELLS);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002029 if (!pool->prison) {
2030 *error = "Error creating pool's bio prison";
2031 err_p = ERR_PTR(-ENOMEM);
2032 goto bad_prison;
2033 }
2034
Mikulas Patockadf5d2e92013-03-01 22:45:49 +00002035 pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002036 if (IS_ERR(pool->copier)) {
2037 r = PTR_ERR(pool->copier);
2038 *error = "Error creating pool's kcopyd client";
2039 err_p = ERR_PTR(r);
2040 goto bad_kcopyd_client;
2041 }
2042
2043 /*
2044 * Create singlethreaded workqueue that will service all devices
2045 * that use this metadata.
2046 */
2047 pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2048 if (!pool->wq) {
2049 *error = "Error creating pool's workqueue";
2050 err_p = ERR_PTR(-ENOMEM);
2051 goto bad_wq;
2052 }
2053
2054 INIT_WORK(&pool->worker, do_worker);
Joe Thornber905e51b2012-03-28 18:41:27 +01002055 INIT_DELAYED_WORK(&pool->waker, do_waker);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002056 spin_lock_init(&pool->lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002057 bio_list_init(&pool->deferred_flush_bios);
2058 INIT_LIST_HEAD(&pool->prepared_mappings);
Joe Thornber104655f2012-03-28 18:41:28 +01002059 INIT_LIST_HEAD(&pool->prepared_discards);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002060 INIT_LIST_HEAD(&pool->active_thins);
Joe Thornber88a66212013-12-04 20:16:12 -05002061 pool->low_water_triggered = false;
Mike Snitzer44feb382012-10-12 21:02:10 +01002062
2063 pool->shared_read_ds = dm_deferred_set_create();
2064 if (!pool->shared_read_ds) {
2065 *error = "Error creating pool's shared read deferred set";
2066 err_p = ERR_PTR(-ENOMEM);
2067 goto bad_shared_read_ds;
2068 }
2069
2070 pool->all_io_ds = dm_deferred_set_create();
2071 if (!pool->all_io_ds) {
2072 *error = "Error creating pool's all io deferred set";
2073 err_p = ERR_PTR(-ENOMEM);
2074 goto bad_all_io_ds;
2075 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002076
2077 pool->next_mapping = NULL;
Mike Snitzera24c2562012-06-03 00:30:00 +01002078 pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
2079 _new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002080 if (!pool->mapping_pool) {
2081 *error = "Error creating pool's mapping mempool";
2082 err_p = ERR_PTR(-ENOMEM);
2083 goto bad_mapping_pool;
2084 }
2085
Joe Thornber991d9fa2011-10-31 20:21:18 +00002086 pool->ref_count = 1;
Joe Thornber905e51b2012-03-28 18:41:27 +01002087 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002088 pool->pool_md = pool_md;
2089 pool->md_dev = metadata_dev;
2090 __pool_table_insert(pool);
2091
2092 return pool;
2093
Joe Thornber991d9fa2011-10-31 20:21:18 +00002094bad_mapping_pool:
Mike Snitzer44feb382012-10-12 21:02:10 +01002095 dm_deferred_set_destroy(pool->all_io_ds);
2096bad_all_io_ds:
2097 dm_deferred_set_destroy(pool->shared_read_ds);
2098bad_shared_read_ds:
Joe Thornber991d9fa2011-10-31 20:21:18 +00002099 destroy_workqueue(pool->wq);
2100bad_wq:
2101 dm_kcopyd_client_destroy(pool->copier);
2102bad_kcopyd_client:
Mike Snitzer44feb382012-10-12 21:02:10 +01002103 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002104bad_prison:
2105 kfree(pool);
2106bad_pool:
2107 if (dm_pool_metadata_close(pmd))
2108 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2109
2110 return err_p;
2111}
2112
2113static void __pool_inc(struct pool *pool)
2114{
2115 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2116 pool->ref_count++;
2117}
2118
2119static void __pool_dec(struct pool *pool)
2120{
2121 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2122 BUG_ON(!pool->ref_count);
2123 if (!--pool->ref_count)
2124 __pool_destroy(pool);
2125}
2126
2127static struct pool *__pool_find(struct mapped_device *pool_md,
2128 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002129 unsigned long block_size, int read_only,
2130 char **error, int *created)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002131{
2132 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
2133
2134 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01002135 if (pool->pool_md != pool_md) {
2136 *error = "metadata device already in use by a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00002137 return ERR_PTR(-EBUSY);
Mike Snitzerf09996c2012-07-27 15:07:59 +01002138 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002139 __pool_inc(pool);
2140
2141 } else {
2142 pool = __pool_table_lookup(pool_md);
2143 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01002144 if (pool->md_dev != metadata_dev) {
2145 *error = "different pool cannot replace a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00002146 return ERR_PTR(-EINVAL);
Mike Snitzerf09996c2012-07-27 15:07:59 +01002147 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002148 __pool_inc(pool);
2149
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002150 } else {
Joe Thornbere49e5822012-07-27 15:08:16 +01002151 pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002152 *created = 1;
2153 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002154 }
2155
2156 return pool;
2157}
2158
2159/*----------------------------------------------------------------
2160 * Pool target methods
2161 *--------------------------------------------------------------*/
2162static void pool_dtr(struct dm_target *ti)
2163{
2164 struct pool_c *pt = ti->private;
2165
2166 mutex_lock(&dm_thin_pool_table.mutex);
2167
2168 unbind_control_target(pt->pool, ti);
2169 __pool_dec(pt->pool);
2170 dm_put_device(ti, pt->metadata_dev);
2171 dm_put_device(ti, pt->data_dev);
2172 kfree(pt);
2173
2174 mutex_unlock(&dm_thin_pool_table.mutex);
2175}
2176
Joe Thornber991d9fa2011-10-31 20:21:18 +00002177static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
2178 struct dm_target *ti)
2179{
2180 int r;
2181 unsigned argc;
2182 const char *arg_name;
2183
2184 static struct dm_arg _args[] = {
Mike Snitzer74aa45c2014-01-15 19:07:58 -05002185 {0, 4, "Invalid number of pool feature arguments"},
Joe Thornber991d9fa2011-10-31 20:21:18 +00002186 };
2187
2188 /*
2189 * No feature arguments supplied.
2190 */
2191 if (!as->argc)
2192 return 0;
2193
2194 r = dm_read_arg_group(_args, as, &argc, &ti->error);
2195 if (r)
2196 return -EINVAL;
2197
2198 while (argc && !r) {
2199 arg_name = dm_shift_arg(as);
2200 argc--;
2201
Joe Thornbere49e5822012-07-27 15:08:16 +01002202 if (!strcasecmp(arg_name, "skip_block_zeroing"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002203 pf->zero_new_blocks = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002204
Joe Thornbere49e5822012-07-27 15:08:16 +01002205 else if (!strcasecmp(arg_name, "ignore_discard"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002206 pf->discard_enabled = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01002207
2208 else if (!strcasecmp(arg_name, "no_discard_passdown"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002209 pf->discard_passdown = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01002210
2211 else if (!strcasecmp(arg_name, "read_only"))
2212 pf->mode = PM_READ_ONLY;
2213
Mike Snitzer787a996c2013-12-06 16:21:43 -05002214 else if (!strcasecmp(arg_name, "error_if_no_space"))
2215 pf->error_if_no_space = true;
2216
Joe Thornbere49e5822012-07-27 15:08:16 +01002217 else {
2218 ti->error = "Unrecognised pool feature requested";
2219 r = -EINVAL;
2220 break;
2221 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002222 }
2223
2224 return r;
2225}
2226
Joe Thornberac8c3f32013-05-10 14:37:21 +01002227static void metadata_low_callback(void *context)
2228{
2229 struct pool *pool = context;
2230
2231 DMWARN("%s: reached low water mark for metadata device: sending event.",
2232 dm_device_name(pool->pool_md));
2233
2234 dm_table_event(pool->ti->table);
2235}
2236
Mike Snitzer7d489352014-02-12 23:58:15 -05002237static sector_t get_dev_size(struct block_device *bdev)
Joe Thornberb17446d2013-05-10 14:37:18 +01002238{
Mike Snitzer7d489352014-02-12 23:58:15 -05002239 return i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
2240}
2241
2242static void warn_if_metadata_device_too_big(struct block_device *bdev)
2243{
2244 sector_t metadata_dev_size = get_dev_size(bdev);
Joe Thornberb17446d2013-05-10 14:37:18 +01002245 char buffer[BDEVNAME_SIZE];
2246
Mike Snitzer7d489352014-02-12 23:58:15 -05002247 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
Joe Thornberb17446d2013-05-10 14:37:18 +01002248 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
2249 bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS);
Mike Snitzer7d489352014-02-12 23:58:15 -05002250}
2251
2252static sector_t get_metadata_dev_size(struct block_device *bdev)
2253{
2254 sector_t metadata_dev_size = get_dev_size(bdev);
2255
2256 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS)
2257 metadata_dev_size = THIN_METADATA_MAX_SECTORS;
Joe Thornberb17446d2013-05-10 14:37:18 +01002258
2259 return metadata_dev_size;
2260}
2261
Joe Thornber24347e92013-05-10 14:37:19 +01002262static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev)
2263{
2264 sector_t metadata_dev_size = get_metadata_dev_size(bdev);
2265
Mike Snitzer7d489352014-02-12 23:58:15 -05002266 sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE);
Joe Thornber24347e92013-05-10 14:37:19 +01002267
2268 return metadata_dev_size;
2269}
2270
Joe Thornber991d9fa2011-10-31 20:21:18 +00002271/*
Joe Thornberac8c3f32013-05-10 14:37:21 +01002272 * When a metadata threshold is crossed a dm event is triggered, and
2273 * userland should respond by growing the metadata device. We could let
2274 * userland set the threshold, like we do with the data threshold, but I'm
2275 * not sure they know enough to do this well.
2276 */
2277static dm_block_t calc_metadata_threshold(struct pool_c *pt)
2278{
2279 /*
2280 * 4M is ample for all ops with the possible exception of thin
2281 * device deletion which is harmless if it fails (just retry the
2282 * delete after you've grown the device).
2283 */
2284 dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4;
2285 return min((dm_block_t)1024ULL /* 4M */, quarter);
2286}
2287
2288/*
Joe Thornber991d9fa2011-10-31 20:21:18 +00002289 * thin-pool <metadata dev> <data dev>
2290 * <data block size (sectors)>
2291 * <low water mark (blocks)>
2292 * [<#feature args> [<arg>]*]
2293 *
2294 * Optional feature arguments are:
2295 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002296 * ignore_discard: disable discard
2297 * no_discard_passdown: don't pass discards down to the data device
Mike Snitzer787a996c2013-12-06 16:21:43 -05002298 * read_only: Don't allow any changes to be made to the pool metadata.
2299 * error_if_no_space: error IOs, instead of queueing, if no space.
Joe Thornber991d9fa2011-10-31 20:21:18 +00002300 */
2301static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
2302{
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002303 int r, pool_created = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002304 struct pool_c *pt;
2305 struct pool *pool;
2306 struct pool_features pf;
2307 struct dm_arg_set as;
2308 struct dm_dev *data_dev;
2309 unsigned long block_size;
2310 dm_block_t low_water_blocks;
2311 struct dm_dev *metadata_dev;
Joe Thornber5d0db962013-05-10 14:37:19 +01002312 fmode_t metadata_mode;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002313
2314 /*
2315 * FIXME Remove validation from scope of lock.
2316 */
2317 mutex_lock(&dm_thin_pool_table.mutex);
2318
2319 if (argc < 4) {
2320 ti->error = "Invalid argument count";
2321 r = -EINVAL;
2322 goto out_unlock;
2323 }
Joe Thornber5d0db962013-05-10 14:37:19 +01002324
Joe Thornber991d9fa2011-10-31 20:21:18 +00002325 as.argc = argc;
2326 as.argv = argv;
2327
Joe Thornber5d0db962013-05-10 14:37:19 +01002328 /*
2329 * Set default pool features.
2330 */
2331 pool_features_init(&pf);
2332
2333 dm_consume_args(&as, 4);
2334 r = parse_pool_features(&as, &pf, ti);
2335 if (r)
2336 goto out_unlock;
2337
2338 metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE);
2339 r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002340 if (r) {
2341 ti->error = "Error opening metadata block device";
2342 goto out_unlock;
2343 }
Mike Snitzer7d489352014-02-12 23:58:15 -05002344 warn_if_metadata_device_too_big(metadata_dev->bdev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002345
2346 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
2347 if (r) {
2348 ti->error = "Error getting data device";
2349 goto out_metadata;
2350 }
2351
2352 if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
2353 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
2354 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002355 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00002356 ti->error = "Invalid block size";
2357 r = -EINVAL;
2358 goto out;
2359 }
2360
2361 if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
2362 ti->error = "Invalid low water mark";
2363 r = -EINVAL;
2364 goto out;
2365 }
2366
Joe Thornber991d9fa2011-10-31 20:21:18 +00002367 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
2368 if (!pt) {
2369 r = -ENOMEM;
2370 goto out;
2371 }
2372
2373 pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
Joe Thornbere49e5822012-07-27 15:08:16 +01002374 block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002375 if (IS_ERR(pool)) {
2376 r = PTR_ERR(pool);
2377 goto out_free_pt;
2378 }
2379
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002380 /*
2381 * 'pool_created' reflects whether this is the first table load.
2382 * Top level discard support is not allowed to be changed after
2383 * initial load. This would require a pool reload to trigger thin
2384 * device changes.
2385 */
2386 if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
2387 ti->error = "Discard support cannot be disabled once enabled";
2388 r = -EINVAL;
2389 goto out_flags_changed;
2390 }
2391
Joe Thornber991d9fa2011-10-31 20:21:18 +00002392 pt->pool = pool;
2393 pt->ti = ti;
2394 pt->metadata_dev = metadata_dev;
2395 pt->data_dev = data_dev;
2396 pt->low_water_blocks = low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +01002397 pt->adjusted_pf = pt->requested_pf = pf;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002398 ti->num_flush_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002399
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002400 /*
2401 * Only need to enable discards if the pool should pass
2402 * them down to the data device. The thin device's discard
2403 * processing will cause mappings to be removed from the btree.
2404 */
Mike Snitzerb60ab992013-09-19 18:49:11 -04002405 ti->discard_zeroes_data_unsupported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002406 if (pf.discard_enabled && pf.discard_passdown) {
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002407 ti->num_discard_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002408
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002409 /*
2410 * Setting 'discards_supported' circumvents the normal
2411 * stacking of discard limits (this keeps the pool and
2412 * thin devices' discard limits consistent).
2413 */
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01002414 ti->discards_supported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002415 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002416 ti->private = pt;
2417
Joe Thornberac8c3f32013-05-10 14:37:21 +01002418 r = dm_pool_register_metadata_threshold(pt->pool->pmd,
2419 calc_metadata_threshold(pt),
2420 metadata_low_callback,
2421 pool);
2422 if (r)
2423 goto out_free_pt;
2424
Joe Thornber991d9fa2011-10-31 20:21:18 +00002425 pt->callbacks.congested_fn = pool_is_congested;
2426 dm_table_add_target_callbacks(ti->table, &pt->callbacks);
2427
2428 mutex_unlock(&dm_thin_pool_table.mutex);
2429
2430 return 0;
2431
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002432out_flags_changed:
2433 __pool_dec(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002434out_free_pt:
2435 kfree(pt);
2436out:
2437 dm_put_device(ti, data_dev);
2438out_metadata:
2439 dm_put_device(ti, metadata_dev);
2440out_unlock:
2441 mutex_unlock(&dm_thin_pool_table.mutex);
2442
2443 return r;
2444}
2445
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002446static int pool_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002447{
2448 int r;
2449 struct pool_c *pt = ti->private;
2450 struct pool *pool = pt->pool;
2451 unsigned long flags;
2452
2453 /*
2454 * As this is a singleton target, ti->begin is always zero.
2455 */
2456 spin_lock_irqsave(&pool->lock, flags);
2457 bio->bi_bdev = pt->data_dev->bdev;
2458 r = DM_MAPIO_REMAPPED;
2459 spin_unlock_irqrestore(&pool->lock, flags);
2460
2461 return r;
2462}
2463
Joe Thornberb17446d2013-05-10 14:37:18 +01002464static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit)
2465{
2466 int r;
2467 struct pool_c *pt = ti->private;
2468 struct pool *pool = pt->pool;
2469 sector_t data_size = ti->len;
2470 dm_block_t sb_data_size;
2471
2472 *need_commit = false;
2473
2474 (void) sector_div(data_size, pool->sectors_per_block);
2475
2476 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
2477 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04002478 DMERR("%s: failed to retrieve data device size",
2479 dm_device_name(pool->pool_md));
Joe Thornberb17446d2013-05-10 14:37:18 +01002480 return r;
2481 }
2482
2483 if (data_size < sb_data_size) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04002484 DMERR("%s: pool target (%llu blocks) too small: expected %llu",
2485 dm_device_name(pool->pool_md),
Joe Thornberb17446d2013-05-10 14:37:18 +01002486 (unsigned long long)data_size, sb_data_size);
2487 return -EINVAL;
2488
2489 } else if (data_size > sb_data_size) {
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002490 if (dm_pool_metadata_needs_check(pool->pmd)) {
2491 DMERR("%s: unable to grow the data device until repaired.",
2492 dm_device_name(pool->pool_md));
2493 return 0;
2494 }
2495
Mike Snitzer6f7f51d2013-12-04 10:25:53 -05002496 if (sb_data_size)
2497 DMINFO("%s: growing the data device from %llu to %llu blocks",
2498 dm_device_name(pool->pool_md),
2499 sb_data_size, (unsigned long long)data_size);
Joe Thornberb17446d2013-05-10 14:37:18 +01002500 r = dm_pool_resize_data_dev(pool->pmd, data_size);
2501 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05002502 metadata_operation_failed(pool, "dm_pool_resize_data_dev", r);
Joe Thornberb17446d2013-05-10 14:37:18 +01002503 return r;
2504 }
2505
2506 *need_commit = true;
2507 }
2508
2509 return 0;
2510}
2511
Joe Thornber24347e92013-05-10 14:37:19 +01002512static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit)
2513{
2514 int r;
2515 struct pool_c *pt = ti->private;
2516 struct pool *pool = pt->pool;
2517 dm_block_t metadata_dev_size, sb_metadata_dev_size;
2518
2519 *need_commit = false;
2520
Alasdair G Kergon610bba82013-05-19 18:57:50 +01002521 metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev);
Joe Thornber24347e92013-05-10 14:37:19 +01002522
2523 r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size);
2524 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04002525 DMERR("%s: failed to retrieve metadata device size",
2526 dm_device_name(pool->pool_md));
Joe Thornber24347e92013-05-10 14:37:19 +01002527 return r;
2528 }
2529
2530 if (metadata_dev_size < sb_metadata_dev_size) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04002531 DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
2532 dm_device_name(pool->pool_md),
Joe Thornber24347e92013-05-10 14:37:19 +01002533 metadata_dev_size, sb_metadata_dev_size);
2534 return -EINVAL;
2535
2536 } else if (metadata_dev_size > sb_metadata_dev_size) {
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002537 if (dm_pool_metadata_needs_check(pool->pmd)) {
2538 DMERR("%s: unable to grow the metadata device until repaired.",
2539 dm_device_name(pool->pool_md));
2540 return 0;
2541 }
2542
Mike Snitzer7d489352014-02-12 23:58:15 -05002543 warn_if_metadata_device_too_big(pool->md_dev);
Mike Snitzer6f7f51d2013-12-04 10:25:53 -05002544 DMINFO("%s: growing the metadata device from %llu to %llu blocks",
2545 dm_device_name(pool->pool_md),
2546 sb_metadata_dev_size, metadata_dev_size);
Joe Thornber24347e92013-05-10 14:37:19 +01002547 r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size);
2548 if (r) {
Joe Thornberb5330652013-12-04 19:51:33 -05002549 metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r);
Joe Thornber24347e92013-05-10 14:37:19 +01002550 return r;
2551 }
2552
2553 *need_commit = true;
2554 }
2555
2556 return 0;
2557}
2558
Joe Thornber991d9fa2011-10-31 20:21:18 +00002559/*
2560 * Retrieves the number of blocks of the data device from
2561 * the superblock and compares it to the actual device size,
2562 * thus resizing the data device in case it has grown.
2563 *
2564 * This both copes with opening preallocated data devices in the ctr
2565 * being followed by a resume
2566 * -and-
2567 * calling the resume method individually after userspace has
2568 * grown the data device in reaction to a table event.
2569 */
2570static int pool_preresume(struct dm_target *ti)
2571{
2572 int r;
Joe Thornber24347e92013-05-10 14:37:19 +01002573 bool need_commit1, need_commit2;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002574 struct pool_c *pt = ti->private;
2575 struct pool *pool = pt->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002576
2577 /*
2578 * Take control of the pool object.
2579 */
2580 r = bind_control_target(pool, ti);
2581 if (r)
2582 return r;
2583
Joe Thornberb17446d2013-05-10 14:37:18 +01002584 r = maybe_resize_data_dev(ti, &need_commit1);
2585 if (r)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002586 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002587
Joe Thornber24347e92013-05-10 14:37:19 +01002588 r = maybe_resize_metadata_dev(ti, &need_commit2);
2589 if (r)
2590 return r;
2591
2592 if (need_commit1 || need_commit2)
Joe Thornber020cc3b2013-12-04 15:05:36 -05002593 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002594
2595 return 0;
2596}
2597
2598static void pool_resume(struct dm_target *ti)
2599{
2600 struct pool_c *pt = ti->private;
2601 struct pool *pool = pt->pool;
2602 unsigned long flags;
2603
2604 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber88a66212013-12-04 20:16:12 -05002605 pool->low_water_triggered = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002606 spin_unlock_irqrestore(&pool->lock, flags);
Mike Snitzerc140e1c2014-03-20 21:17:14 -04002607 requeue_bios(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002608
Joe Thornber905e51b2012-03-28 18:41:27 +01002609 do_waker(&pool->waker.work);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002610}
2611
2612static void pool_postsuspend(struct dm_target *ti)
2613{
Joe Thornber991d9fa2011-10-31 20:21:18 +00002614 struct pool_c *pt = ti->private;
2615 struct pool *pool = pt->pool;
2616
Joe Thornber905e51b2012-03-28 18:41:27 +01002617 cancel_delayed_work(&pool->waker);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002618 flush_workqueue(pool->wq);
Joe Thornber020cc3b2013-12-04 15:05:36 -05002619 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002620}
2621
2622static int check_arg_count(unsigned argc, unsigned args_required)
2623{
2624 if (argc != args_required) {
2625 DMWARN("Message received with %u arguments instead of %u.",
2626 argc, args_required);
2627 return -EINVAL;
2628 }
2629
2630 return 0;
2631}
2632
2633static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
2634{
2635 if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
2636 *dev_id <= MAX_DEV_ID)
2637 return 0;
2638
2639 if (warning)
2640 DMWARN("Message received with invalid device id: %s", arg);
2641
2642 return -EINVAL;
2643}
2644
2645static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
2646{
2647 dm_thin_id dev_id;
2648 int r;
2649
2650 r = check_arg_count(argc, 2);
2651 if (r)
2652 return r;
2653
2654 r = read_dev_id(argv[1], &dev_id, 1);
2655 if (r)
2656 return r;
2657
2658 r = dm_pool_create_thin(pool->pmd, dev_id);
2659 if (r) {
2660 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
2661 argv[1]);
2662 return r;
2663 }
2664
2665 return 0;
2666}
2667
2668static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2669{
2670 dm_thin_id dev_id;
2671 dm_thin_id origin_dev_id;
2672 int r;
2673
2674 r = check_arg_count(argc, 3);
2675 if (r)
2676 return r;
2677
2678 r = read_dev_id(argv[1], &dev_id, 1);
2679 if (r)
2680 return r;
2681
2682 r = read_dev_id(argv[2], &origin_dev_id, 1);
2683 if (r)
2684 return r;
2685
2686 r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
2687 if (r) {
2688 DMWARN("Creation of new snapshot %s of device %s failed.",
2689 argv[1], argv[2]);
2690 return r;
2691 }
2692
2693 return 0;
2694}
2695
2696static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
2697{
2698 dm_thin_id dev_id;
2699 int r;
2700
2701 r = check_arg_count(argc, 2);
2702 if (r)
2703 return r;
2704
2705 r = read_dev_id(argv[1], &dev_id, 1);
2706 if (r)
2707 return r;
2708
2709 r = dm_pool_delete_thin_device(pool->pmd, dev_id);
2710 if (r)
2711 DMWARN("Deletion of thin device %s failed.", argv[1]);
2712
2713 return r;
2714}
2715
2716static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
2717{
2718 dm_thin_id old_id, new_id;
2719 int r;
2720
2721 r = check_arg_count(argc, 3);
2722 if (r)
2723 return r;
2724
2725 if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
2726 DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
2727 return -EINVAL;
2728 }
2729
2730 if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
2731 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
2732 return -EINVAL;
2733 }
2734
2735 r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
2736 if (r) {
2737 DMWARN("Failed to change transaction id from %s to %s.",
2738 argv[1], argv[2]);
2739 return r;
2740 }
2741
2742 return 0;
2743}
2744
Joe Thornbercc8394d2012-06-03 00:30:01 +01002745static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2746{
2747 int r;
2748
2749 r = check_arg_count(argc, 1);
2750 if (r)
2751 return r;
2752
Joe Thornber020cc3b2013-12-04 15:05:36 -05002753 (void) commit(pool);
Joe Thornber0d200ae2012-07-03 12:55:31 +01002754
Joe Thornbercc8394d2012-06-03 00:30:01 +01002755 r = dm_pool_reserve_metadata_snap(pool->pmd);
2756 if (r)
2757 DMWARN("reserve_metadata_snap message failed.");
2758
2759 return r;
2760}
2761
2762static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2763{
2764 int r;
2765
2766 r = check_arg_count(argc, 1);
2767 if (r)
2768 return r;
2769
2770 r = dm_pool_release_metadata_snap(pool->pmd);
2771 if (r)
2772 DMWARN("release_metadata_snap message failed.");
2773
2774 return r;
2775}
2776
Joe Thornber991d9fa2011-10-31 20:21:18 +00002777/*
2778 * Messages supported:
2779 * create_thin <dev_id>
2780 * create_snap <dev_id> <origin_id>
2781 * delete <dev_id>
2782 * trim <dev_id> <new_size_in_sectors>
2783 * set_transaction_id <current_trans_id> <new_trans_id>
Joe Thornbercc8394d2012-06-03 00:30:01 +01002784 * reserve_metadata_snap
2785 * release_metadata_snap
Joe Thornber991d9fa2011-10-31 20:21:18 +00002786 */
2787static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
2788{
2789 int r = -EINVAL;
2790 struct pool_c *pt = ti->private;
2791 struct pool *pool = pt->pool;
2792
2793 if (!strcasecmp(argv[0], "create_thin"))
2794 r = process_create_thin_mesg(argc, argv, pool);
2795
2796 else if (!strcasecmp(argv[0], "create_snap"))
2797 r = process_create_snap_mesg(argc, argv, pool);
2798
2799 else if (!strcasecmp(argv[0], "delete"))
2800 r = process_delete_mesg(argc, argv, pool);
2801
2802 else if (!strcasecmp(argv[0], "set_transaction_id"))
2803 r = process_set_transaction_id_mesg(argc, argv, pool);
2804
Joe Thornbercc8394d2012-06-03 00:30:01 +01002805 else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
2806 r = process_reserve_metadata_snap_mesg(argc, argv, pool);
2807
2808 else if (!strcasecmp(argv[0], "release_metadata_snap"))
2809 r = process_release_metadata_snap_mesg(argc, argv, pool);
2810
Joe Thornber991d9fa2011-10-31 20:21:18 +00002811 else
2812 DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
2813
Joe Thornbere49e5822012-07-27 15:08:16 +01002814 if (!r)
Joe Thornber020cc3b2013-12-04 15:05:36 -05002815 (void) commit(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002816
2817 return r;
2818}
2819
Joe Thornbere49e5822012-07-27 15:08:16 +01002820static void emit_flags(struct pool_features *pf, char *result,
2821 unsigned sz, unsigned maxlen)
2822{
2823 unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
Mike Snitzer787a996c2013-12-06 16:21:43 -05002824 !pf->discard_passdown + (pf->mode == PM_READ_ONLY) +
2825 pf->error_if_no_space;
Joe Thornbere49e5822012-07-27 15:08:16 +01002826 DMEMIT("%u ", count);
2827
2828 if (!pf->zero_new_blocks)
2829 DMEMIT("skip_block_zeroing ");
2830
2831 if (!pf->discard_enabled)
2832 DMEMIT("ignore_discard ");
2833
2834 if (!pf->discard_passdown)
2835 DMEMIT("no_discard_passdown ");
2836
2837 if (pf->mode == PM_READ_ONLY)
2838 DMEMIT("read_only ");
Mike Snitzer787a996c2013-12-06 16:21:43 -05002839
2840 if (pf->error_if_no_space)
2841 DMEMIT("error_if_no_space ");
Joe Thornbere49e5822012-07-27 15:08:16 +01002842}
2843
Joe Thornber991d9fa2011-10-31 20:21:18 +00002844/*
2845 * Status line is:
2846 * <transaction id> <used metadata sectors>/<total metadata sectors>
2847 * <used data sectors>/<total data sectors> <held metadata root>
2848 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002849static void pool_status(struct dm_target *ti, status_type_t type,
2850 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002851{
Joe Thornbere49e5822012-07-27 15:08:16 +01002852 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002853 unsigned sz = 0;
2854 uint64_t transaction_id;
2855 dm_block_t nr_free_blocks_data;
2856 dm_block_t nr_free_blocks_metadata;
2857 dm_block_t nr_blocks_data;
2858 dm_block_t nr_blocks_metadata;
2859 dm_block_t held_root;
2860 char buf[BDEVNAME_SIZE];
2861 char buf2[BDEVNAME_SIZE];
2862 struct pool_c *pt = ti->private;
2863 struct pool *pool = pt->pool;
2864
2865 switch (type) {
2866 case STATUSTYPE_INFO:
Joe Thornbere49e5822012-07-27 15:08:16 +01002867 if (get_pool_mode(pool) == PM_FAIL) {
2868 DMEMIT("Fail");
2869 break;
2870 }
2871
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01002872 /* Commit to ensure statistics aren't out-of-date */
2873 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
Joe Thornber020cc3b2013-12-04 15:05:36 -05002874 (void) commit(pool);
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01002875
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002876 r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id);
2877 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04002878 DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
2879 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002880 goto err;
2881 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002882
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002883 r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata);
2884 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04002885 DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
2886 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002887 goto err;
2888 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002889
2890 r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002891 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04002892 DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
2893 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002894 goto err;
2895 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002896
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002897 r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data);
2898 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04002899 DMERR("%s: dm_pool_get_free_block_count returned %d",
2900 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002901 goto err;
2902 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002903
2904 r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002905 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04002906 DMERR("%s: dm_pool_get_data_dev_size returned %d",
2907 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002908 goto err;
2909 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002910
Joe Thornbercc8394d2012-06-03 00:30:01 +01002911 r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002912 if (r) {
Mike Snitzer4fa59712013-08-21 17:30:40 -04002913 DMERR("%s: dm_pool_get_metadata_snap returned %d",
2914 dm_device_name(pool->pool_md), r);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002915 goto err;
2916 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002917
2918 DMEMIT("%llu %llu/%llu %llu/%llu ",
2919 (unsigned long long)transaction_id,
2920 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
2921 (unsigned long long)nr_blocks_metadata,
2922 (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
2923 (unsigned long long)nr_blocks_data);
2924
2925 if (held_root)
Joe Thornbere49e5822012-07-27 15:08:16 +01002926 DMEMIT("%llu ", held_root);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002927 else
Joe Thornbere49e5822012-07-27 15:08:16 +01002928 DMEMIT("- ");
2929
Joe Thornber3e1a0692014-03-03 16:03:26 +00002930 if (pool->pf.mode == PM_OUT_OF_DATA_SPACE)
2931 DMEMIT("out_of_data_space ");
2932 else if (pool->pf.mode == PM_READ_ONLY)
Joe Thornbere49e5822012-07-27 15:08:16 +01002933 DMEMIT("ro ");
2934 else
2935 DMEMIT("rw ");
2936
Mike Snitzer018debe2012-12-21 20:23:32 +00002937 if (!pool->pf.discard_enabled)
Mike Snitzer787a996c2013-12-06 16:21:43 -05002938 DMEMIT("ignore_discard ");
Mike Snitzer018debe2012-12-21 20:23:32 +00002939 else if (pool->pf.discard_passdown)
Mike Snitzer787a996c2013-12-06 16:21:43 -05002940 DMEMIT("discard_passdown ");
Joe Thornbere49e5822012-07-27 15:08:16 +01002941 else
Mike Snitzer787a996c2013-12-06 16:21:43 -05002942 DMEMIT("no_discard_passdown ");
2943
2944 if (pool->pf.error_if_no_space)
2945 DMEMIT("error_if_no_space ");
2946 else
2947 DMEMIT("queue_if_no_space ");
Joe Thornber991d9fa2011-10-31 20:21:18 +00002948
2949 break;
2950
2951 case STATUSTYPE_TABLE:
2952 DMEMIT("%s %s %lu %llu ",
2953 format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
2954 format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
2955 (unsigned long)pool->sectors_per_block,
2956 (unsigned long long)pt->low_water_blocks);
Mike Snitzer0424caa2012-09-26 23:45:47 +01002957 emit_flags(&pt->requested_pf, result, sz, maxlen);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002958 break;
2959 }
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002960 return;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002961
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002962err:
2963 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00002964}
2965
2966static int pool_iterate_devices(struct dm_target *ti,
2967 iterate_devices_callout_fn fn, void *data)
2968{
2969 struct pool_c *pt = ti->private;
2970
2971 return fn(ti, pt->data_dev, 0, ti->len, data);
2972}
2973
2974static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
2975 struct bio_vec *biovec, int max_size)
2976{
2977 struct pool_c *pt = ti->private;
2978 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
2979
2980 if (!q->merge_bvec_fn)
2981 return max_size;
2982
2983 bvm->bi_bdev = pt->data_dev->bdev;
2984
2985 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
2986}
2987
Mike Snitzer0424caa2012-09-26 23:45:47 +01002988static void set_discard_limits(struct pool_c *pt, struct queue_limits *limits)
Joe Thornber104655f2012-03-28 18:41:28 +01002989{
Mike Snitzer0424caa2012-09-26 23:45:47 +01002990 struct pool *pool = pt->pool;
2991 struct queue_limits *data_limits;
2992
Joe Thornber104655f2012-03-28 18:41:28 +01002993 limits->max_discard_sectors = pool->sectors_per_block;
2994
2995 /*
Mike Snitzer0424caa2012-09-26 23:45:47 +01002996 * discard_granularity is just a hint, and not enforced.
Joe Thornber104655f2012-03-28 18:41:28 +01002997 */
Mike Snitzer0424caa2012-09-26 23:45:47 +01002998 if (pt->adjusted_pf.discard_passdown) {
2999 data_limits = &bdev_get_queue(pt->data_dev->bdev)->limits;
3000 limits->discard_granularity = data_limits->discard_granularity;
Mike Snitzerf13945d2013-03-01 22:45:44 +00003001 } else
Mike Snitzer0424caa2012-09-26 23:45:47 +01003002 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
Joe Thornber104655f2012-03-28 18:41:28 +01003003}
3004
Joe Thornber991d9fa2011-10-31 20:21:18 +00003005static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
3006{
3007 struct pool_c *pt = ti->private;
3008 struct pool *pool = pt->pool;
Mike Snitzer0cc67cd2013-08-20 15:02:41 -04003009 uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003010
Mike Snitzer0cc67cd2013-08-20 15:02:41 -04003011 /*
3012 * If the system-determined stacked limits are compatible with the
3013 * pool's blocksize (io_opt is a factor) do not override them.
3014 */
3015 if (io_opt_sectors < pool->sectors_per_block ||
3016 do_div(io_opt_sectors, pool->sectors_per_block)) {
3017 blk_limits_io_min(limits, 0);
3018 blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
3019 }
Mike Snitzer0424caa2012-09-26 23:45:47 +01003020
3021 /*
3022 * pt->adjusted_pf is a staging area for the actual features to use.
3023 * They get transferred to the live pool in bind_control_target()
3024 * called from pool_preresume().
3025 */
Mike Snitzerb60ab992013-09-19 18:49:11 -04003026 if (!pt->adjusted_pf.discard_enabled) {
3027 /*
3028 * Must explicitly disallow stacking discard limits otherwise the
3029 * block layer will stack them if pool's data device has support.
3030 * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the
3031 * user to see that, so make sure to set all discard limits to 0.
3032 */
3033 limits->discard_granularity = 0;
Mike Snitzer0424caa2012-09-26 23:45:47 +01003034 return;
Mike Snitzerb60ab992013-09-19 18:49:11 -04003035 }
Mike Snitzer0424caa2012-09-26 23:45:47 +01003036
3037 disable_passdown_if_not_supported(pt);
3038
3039 set_discard_limits(pt, limits);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003040}
3041
3042static struct target_type pool_target = {
3043 .name = "thin-pool",
3044 .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
3045 DM_TARGET_IMMUTABLE,
Mike Snitzer67324ea2014-03-21 18:33:41 -04003046 .version = {1, 12, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00003047 .module = THIS_MODULE,
3048 .ctr = pool_ctr,
3049 .dtr = pool_dtr,
3050 .map = pool_map,
3051 .postsuspend = pool_postsuspend,
3052 .preresume = pool_preresume,
3053 .resume = pool_resume,
3054 .message = pool_message,
3055 .status = pool_status,
3056 .merge = pool_merge,
3057 .iterate_devices = pool_iterate_devices,
3058 .io_hints = pool_io_hints,
3059};
3060
3061/*----------------------------------------------------------------
3062 * Thin target methods
3063 *--------------------------------------------------------------*/
3064static void thin_dtr(struct dm_target *ti)
3065{
3066 struct thin_c *tc = ti->private;
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003067 unsigned long flags;
3068
3069 spin_lock_irqsave(&tc->pool->lock, flags);
3070 list_del_rcu(&tc->list);
3071 spin_unlock_irqrestore(&tc->pool->lock, flags);
3072 synchronize_rcu();
Joe Thornber991d9fa2011-10-31 20:21:18 +00003073
3074 mutex_lock(&dm_thin_pool_table.mutex);
3075
3076 __pool_dec(tc->pool);
3077 dm_pool_close_thin_device(tc->td);
3078 dm_put_device(ti, tc->pool_dev);
Joe Thornber2dd9c252012-03-28 18:41:28 +01003079 if (tc->origin_dev)
3080 dm_put_device(ti, tc->origin_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003081 kfree(tc);
3082
3083 mutex_unlock(&dm_thin_pool_table.mutex);
3084}
3085
3086/*
3087 * Thin target parameters:
3088 *
Joe Thornber2dd9c252012-03-28 18:41:28 +01003089 * <pool_dev> <dev_id> [origin_dev]
Joe Thornber991d9fa2011-10-31 20:21:18 +00003090 *
3091 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
3092 * dev_id: the internal device identifier
Joe Thornber2dd9c252012-03-28 18:41:28 +01003093 * origin_dev: a device external to the pool that should act as the origin
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003094 *
3095 * If the pool device has discards disabled, they get disabled for the thin
3096 * device as well.
Joe Thornber991d9fa2011-10-31 20:21:18 +00003097 */
3098static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
3099{
3100 int r;
3101 struct thin_c *tc;
Joe Thornber2dd9c252012-03-28 18:41:28 +01003102 struct dm_dev *pool_dev, *origin_dev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003103 struct mapped_device *pool_md;
3104
3105 mutex_lock(&dm_thin_pool_table.mutex);
3106
Joe Thornber2dd9c252012-03-28 18:41:28 +01003107 if (argc != 2 && argc != 3) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00003108 ti->error = "Invalid argument count";
3109 r = -EINVAL;
3110 goto out_unlock;
3111 }
3112
3113 tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
3114 if (!tc) {
3115 ti->error = "Out of memory";
3116 r = -ENOMEM;
3117 goto out_unlock;
3118 }
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003119 spin_lock_init(&tc->lock);
3120 bio_list_init(&tc->deferred_bio_list);
3121 bio_list_init(&tc->retry_on_resume_list);
Mike Snitzer67324ea2014-03-21 18:33:41 -04003122 tc->sort_bio_list = RB_ROOT;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003123
Joe Thornber2dd9c252012-03-28 18:41:28 +01003124 if (argc == 3) {
3125 r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
3126 if (r) {
3127 ti->error = "Error opening origin device";
3128 goto bad_origin_dev;
3129 }
3130 tc->origin_dev = origin_dev;
3131 }
3132
Joe Thornber991d9fa2011-10-31 20:21:18 +00003133 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
3134 if (r) {
3135 ti->error = "Error opening pool device";
3136 goto bad_pool_dev;
3137 }
3138 tc->pool_dev = pool_dev;
3139
3140 if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
3141 ti->error = "Invalid device id";
3142 r = -EINVAL;
3143 goto bad_common;
3144 }
3145
3146 pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
3147 if (!pool_md) {
3148 ti->error = "Couldn't get pool mapped device";
3149 r = -EINVAL;
3150 goto bad_common;
3151 }
3152
3153 tc->pool = __pool_table_lookup(pool_md);
3154 if (!tc->pool) {
3155 ti->error = "Couldn't find pool object";
3156 r = -EINVAL;
3157 goto bad_pool_lookup;
3158 }
3159 __pool_inc(tc->pool);
3160
Joe Thornbere49e5822012-07-27 15:08:16 +01003161 if (get_pool_mode(tc->pool) == PM_FAIL) {
3162 ti->error = "Couldn't open thin device, Pool is in fail mode";
Mike Snitzer1acacc02014-02-19 20:32:33 -05003163 r = -EINVAL;
Joe Thornbere49e5822012-07-27 15:08:16 +01003164 goto bad_thin_open;
3165 }
3166
Joe Thornber991d9fa2011-10-31 20:21:18 +00003167 r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
3168 if (r) {
3169 ti->error = "Couldn't open thin internal device";
3170 goto bad_thin_open;
3171 }
3172
Mike Snitzer542f9032012-07-27 15:08:00 +01003173 r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
3174 if (r)
Mike Snitzer1acacc02014-02-19 20:32:33 -05003175 goto bad_target_max_io_len;
Mike Snitzer542f9032012-07-27 15:08:00 +01003176
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003177 ti->num_flush_bios = 1;
Joe Thornber16ad3d12012-07-27 15:08:07 +01003178 ti->flush_supported = true;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00003179 ti->per_bio_data_size = sizeof(struct dm_thin_endio_hook);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003180
3181 /* In case the pool supports discards, pass them on. */
Mike Snitzerb60ab992013-09-19 18:49:11 -04003182 ti->discard_zeroes_data_unsupported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003183 if (tc->pool->pf.discard_enabled) {
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01003184 ti->discards_supported = true;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003185 ti->num_discard_bios = 1;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003186 /* Discard bios must be split on a block boundary */
3187 ti->split_discard_bios = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01003188 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003189
3190 dm_put(pool_md);
3191
3192 mutex_unlock(&dm_thin_pool_table.mutex);
3193
Mike Snitzerc140e1c2014-03-20 21:17:14 -04003194 spin_lock(&tc->pool->lock);
3195 list_add_tail_rcu(&tc->list, &tc->pool->active_thins);
3196 spin_unlock(&tc->pool->lock);
3197 /*
3198 * This synchronize_rcu() call is needed here otherwise we risk a
3199 * wake_worker() call finding no bios to process (because the newly
3200 * added tc isn't yet visible). So this reduces latency since we
3201 * aren't then dependent on the periodic commit to wake_worker().
3202 */
3203 synchronize_rcu();
3204
Joe Thornber991d9fa2011-10-31 20:21:18 +00003205 return 0;
3206
Mike Snitzer1acacc02014-02-19 20:32:33 -05003207bad_target_max_io_len:
3208 dm_pool_close_thin_device(tc->td);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003209bad_thin_open:
3210 __pool_dec(tc->pool);
3211bad_pool_lookup:
3212 dm_put(pool_md);
3213bad_common:
3214 dm_put_device(ti, tc->pool_dev);
3215bad_pool_dev:
Joe Thornber2dd9c252012-03-28 18:41:28 +01003216 if (tc->origin_dev)
3217 dm_put_device(ti, tc->origin_dev);
3218bad_origin_dev:
Joe Thornber991d9fa2011-10-31 20:21:18 +00003219 kfree(tc);
3220out_unlock:
3221 mutex_unlock(&dm_thin_pool_table.mutex);
3222
3223 return r;
3224}
3225
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003226static int thin_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003227{
Kent Overstreet4f024f32013-10-11 15:44:27 -07003228 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003229
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003230 return thin_bio_map(ti, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003231}
3232
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003233static int thin_endio(struct dm_target *ti, struct bio *bio, int err)
Joe Thornbereb2aa482012-03-28 18:41:28 +01003234{
3235 unsigned long flags;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00003236 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01003237 struct list_head work;
Mike Snitzera24c2562012-06-03 00:30:00 +01003238 struct dm_thin_new_mapping *m, *tmp;
Joe Thornbereb2aa482012-03-28 18:41:28 +01003239 struct pool *pool = h->tc->pool;
3240
3241 if (h->shared_read_entry) {
3242 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01003243 dm_deferred_entry_dec(h->shared_read_entry, &work);
Joe Thornbereb2aa482012-03-28 18:41:28 +01003244
3245 spin_lock_irqsave(&pool->lock, flags);
3246 list_for_each_entry_safe(m, tmp, &work, list) {
3247 list_del(&m->list);
Mike Snitzer7f214662013-12-17 13:43:31 -05003248 m->quiesced = true;
Joe Thornbereb2aa482012-03-28 18:41:28 +01003249 __maybe_add_mapping(m);
3250 }
3251 spin_unlock_irqrestore(&pool->lock, flags);
3252 }
3253
Joe Thornber104655f2012-03-28 18:41:28 +01003254 if (h->all_io_entry) {
3255 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01003256 dm_deferred_entry_dec(h->all_io_entry, &work);
Joe Thornber563af182012-12-21 20:23:31 +00003257 if (!list_empty(&work)) {
3258 spin_lock_irqsave(&pool->lock, flags);
3259 list_for_each_entry_safe(m, tmp, &work, list)
Mike Snitzerdaec3382013-12-11 14:01:20 -05003260 list_add_tail(&m->list, &pool->prepared_discards);
Joe Thornber563af182012-12-21 20:23:31 +00003261 spin_unlock_irqrestore(&pool->lock, flags);
3262 wake_worker(pool);
3263 }
Joe Thornber104655f2012-03-28 18:41:28 +01003264 }
3265
Joe Thornbereb2aa482012-03-28 18:41:28 +01003266 return 0;
3267}
3268
Joe Thornber738211f2014-03-03 15:52:28 +00003269static void thin_presuspend(struct dm_target *ti)
3270{
3271 struct thin_c *tc = ti->private;
3272
3273 if (dm_noflush_suspending(ti))
3274 noflush_work(tc, do_noflush_start);
3275}
3276
Joe Thornber991d9fa2011-10-31 20:21:18 +00003277static void thin_postsuspend(struct dm_target *ti)
3278{
Joe Thornber738211f2014-03-03 15:52:28 +00003279 struct thin_c *tc = ti->private;
3280
3281 /*
3282 * The dm_noflush_suspending flag has been cleared by now, so
3283 * unfortunately we must always run this.
3284 */
3285 noflush_work(tc, do_noflush_stop);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003286}
3287
3288/*
3289 * <nr mapped sectors> <highest mapped sector>
3290 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003291static void thin_status(struct dm_target *ti, status_type_t type,
3292 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003293{
3294 int r;
3295 ssize_t sz = 0;
3296 dm_block_t mapped, highest;
3297 char buf[BDEVNAME_SIZE];
3298 struct thin_c *tc = ti->private;
3299
Joe Thornbere49e5822012-07-27 15:08:16 +01003300 if (get_pool_mode(tc->pool) == PM_FAIL) {
3301 DMEMIT("Fail");
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003302 return;
Joe Thornbere49e5822012-07-27 15:08:16 +01003303 }
3304
Joe Thornber991d9fa2011-10-31 20:21:18 +00003305 if (!tc->td)
3306 DMEMIT("-");
3307 else {
3308 switch (type) {
3309 case STATUSTYPE_INFO:
3310 r = dm_thin_get_mapped_count(tc->td, &mapped);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003311 if (r) {
3312 DMERR("dm_thin_get_mapped_count returned %d", r);
3313 goto err;
3314 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003315
3316 r = dm_thin_get_highest_mapped_block(tc->td, &highest);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003317 if (r < 0) {
3318 DMERR("dm_thin_get_highest_mapped_block returned %d", r);
3319 goto err;
3320 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00003321
3322 DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
3323 if (r)
3324 DMEMIT("%llu", ((highest + 1) *
3325 tc->pool->sectors_per_block) - 1);
3326 else
3327 DMEMIT("-");
3328 break;
3329
3330 case STATUSTYPE_TABLE:
3331 DMEMIT("%s %lu",
3332 format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
3333 (unsigned long) tc->dev_id);
Joe Thornber2dd9c252012-03-28 18:41:28 +01003334 if (tc->origin_dev)
3335 DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
Joe Thornber991d9fa2011-10-31 20:21:18 +00003336 break;
3337 }
3338 }
3339
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003340 return;
3341
3342err:
3343 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003344}
3345
3346static int thin_iterate_devices(struct dm_target *ti,
3347 iterate_devices_callout_fn fn, void *data)
3348{
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01003349 sector_t blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003350 struct thin_c *tc = ti->private;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01003351 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00003352
3353 /*
3354 * We can't call dm_pool_get_data_dev_size() since that blocks. So
3355 * we follow a more convoluted path through to the pool's target.
3356 */
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01003357 if (!pool->ti)
Joe Thornber991d9fa2011-10-31 20:21:18 +00003358 return 0; /* nothing is bound */
3359
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01003360 blocks = pool->ti->len;
3361 (void) sector_div(blocks, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003362 if (blocks)
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01003363 return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003364
3365 return 0;
3366}
3367
Joe Thornber991d9fa2011-10-31 20:21:18 +00003368static struct target_type thin_target = {
3369 .name = "thin",
Mike Snitzer67324ea2014-03-21 18:33:41 -04003370 .version = {1, 12, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00003371 .module = THIS_MODULE,
3372 .ctr = thin_ctr,
3373 .dtr = thin_dtr,
3374 .map = thin_map,
Joe Thornbereb2aa482012-03-28 18:41:28 +01003375 .end_io = thin_endio,
Joe Thornber738211f2014-03-03 15:52:28 +00003376 .presuspend = thin_presuspend,
Joe Thornber991d9fa2011-10-31 20:21:18 +00003377 .postsuspend = thin_postsuspend,
3378 .status = thin_status,
3379 .iterate_devices = thin_iterate_devices,
Joe Thornber991d9fa2011-10-31 20:21:18 +00003380};
3381
3382/*----------------------------------------------------------------*/
3383
3384static int __init dm_thin_init(void)
3385{
3386 int r;
3387
3388 pool_table_init();
3389
3390 r = dm_register_target(&thin_target);
3391 if (r)
3392 return r;
3393
3394 r = dm_register_target(&pool_target);
3395 if (r)
Mike Snitzera24c2562012-06-03 00:30:00 +01003396 goto bad_pool_target;
3397
3398 r = -ENOMEM;
3399
Mike Snitzera24c2562012-06-03 00:30:00 +01003400 _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
3401 if (!_new_mapping_cache)
3402 goto bad_new_mapping_cache;
3403
Mike Snitzera24c2562012-06-03 00:30:00 +01003404 return 0;
3405
Mike Snitzera24c2562012-06-03 00:30:00 +01003406bad_new_mapping_cache:
Mike Snitzera24c2562012-06-03 00:30:00 +01003407 dm_unregister_target(&pool_target);
3408bad_pool_target:
3409 dm_unregister_target(&thin_target);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003410
3411 return r;
3412}
3413
3414static void dm_thin_exit(void)
3415{
3416 dm_unregister_target(&thin_target);
3417 dm_unregister_target(&pool_target);
Mike Snitzera24c2562012-06-03 00:30:00 +01003418
Mike Snitzera24c2562012-06-03 00:30:00 +01003419 kmem_cache_destroy(_new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00003420}
3421
3422module_init(dm_thin_init);
3423module_exit(dm_thin_exit);
3424
Alasdair G Kergon7cab8bf2012-05-12 01:43:19 +01003425MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
Joe Thornber991d9fa2011-10-31 20:21:18 +00003426MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3427MODULE_LICENSE("GPL");