blob: 5304e3a29a14714247238ec4e65e34590a812400 [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>
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18
19#define DM_MSG_PREFIX "thin"
20
21/*
22 * Tunable constants
23 */
Alasdair G Kergon7768ed32012-07-27 15:07:57 +010024#define ENDIO_HOOK_POOL_SIZE 1024
Joe Thornber991d9fa2011-10-31 20:21:18 +000025#define MAPPING_POOL_SIZE 1024
26#define PRISON_CELLS 1024
Joe Thornber905e51b2012-03-28 18:41:27 +010027#define COMMIT_PERIOD HZ
Joe Thornber991d9fa2011-10-31 20:21:18 +000028
Mikulas Patockadf5d2e92013-03-01 22:45:49 +000029DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
30 "A percentage of time allocated for copy on write");
31
Joe Thornber991d9fa2011-10-31 20:21:18 +000032/*
33 * The block size of the device holding pool data must be
34 * between 64KB and 1GB.
35 */
36#define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
37#define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
38
39/*
Joe Thornber991d9fa2011-10-31 20:21:18 +000040 * Device id is restricted to 24 bits.
41 */
42#define MAX_DEV_ID ((1 << 24) - 1)
43
44/*
45 * How do we handle breaking sharing of data blocks?
46 * =================================================
47 *
48 * We use a standard copy-on-write btree to store the mappings for the
49 * devices (note I'm talking about copy-on-write of the metadata here, not
50 * the data). When you take an internal snapshot you clone the root node
51 * of the origin btree. After this there is no concept of an origin or a
52 * snapshot. They are just two device trees that happen to point to the
53 * same data blocks.
54 *
55 * When we get a write in we decide if it's to a shared data block using
56 * some timestamp magic. If it is, we have to break sharing.
57 *
58 * Let's say we write to a shared block in what was the origin. The
59 * steps are:
60 *
61 * i) plug io further to this physical block. (see bio_prison code).
62 *
63 * ii) quiesce any read io to that shared data block. Obviously
Mike Snitzer44feb382012-10-12 21:02:10 +010064 * including all devices that share this block. (see dm_deferred_set code)
Joe Thornber991d9fa2011-10-31 20:21:18 +000065 *
66 * iii) copy the data block to a newly allocate block. This step can be
67 * missed out if the io covers the block. (schedule_copy).
68 *
69 * iv) insert the new mapping into the origin's btree
Joe Thornberfe878f32012-03-28 18:41:24 +010070 * (process_prepared_mapping). This act of inserting breaks some
Joe Thornber991d9fa2011-10-31 20:21:18 +000071 * sharing of btree nodes between the two devices. Breaking sharing only
72 * effects the btree of that specific device. Btrees for the other
73 * devices that share the block never change. The btree for the origin
74 * device as it was after the last commit is untouched, ie. we're using
75 * persistent data structures in the functional programming sense.
76 *
77 * v) unplug io to this physical block, including the io that triggered
78 * the breaking of sharing.
79 *
80 * Steps (ii) and (iii) occur in parallel.
81 *
82 * The metadata _doesn't_ need to be committed before the io continues. We
83 * get away with this because the io is always written to a _new_ block.
84 * If there's a crash, then:
85 *
86 * - The origin mapping will point to the old origin block (the shared
87 * one). This will contain the data as it was before the io that triggered
88 * the breaking of sharing came in.
89 *
90 * - The snap mapping still points to the old block. As it would after
91 * the commit.
92 *
93 * The downside of this scheme is the timestamp magic isn't perfect, and
94 * will continue to think that data block in the snapshot device is shared
95 * even after the write to the origin has broken sharing. I suspect data
96 * blocks will typically be shared by many different devices, so we're
97 * breaking sharing n + 1 times, rather than n, where n is the number of
98 * devices that reference this data block. At the moment I think the
99 * benefits far, far outweigh the disadvantages.
100 */
101
102/*----------------------------------------------------------------*/
103
104/*
Joe Thornber991d9fa2011-10-31 20:21:18 +0000105 * Key building.
106 */
107static void build_data_key(struct dm_thin_device *td,
Mike Snitzer44feb382012-10-12 21:02:10 +0100108 dm_block_t b, struct dm_cell_key *key)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000109{
110 key->virtual = 0;
111 key->dev = dm_thin_dev_id(td);
112 key->block = b;
113}
114
115static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
Mike Snitzer44feb382012-10-12 21:02:10 +0100116 struct dm_cell_key *key)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000117{
118 key->virtual = 1;
119 key->dev = dm_thin_dev_id(td);
120 key->block = b;
121}
122
123/*----------------------------------------------------------------*/
124
125/*
126 * A pool device ties together a metadata device and a data device. It
127 * also provides the interface for creating and destroying internal
128 * devices.
129 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100130struct dm_thin_new_mapping;
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100131
Joe Thornbere49e5822012-07-27 15:08:16 +0100132/*
133 * The pool runs in 3 modes. Ordered in degraded order for comparisons.
134 */
135enum pool_mode {
136 PM_WRITE, /* metadata may be changed */
137 PM_READ_ONLY, /* metadata may not be changed */
138 PM_FAIL, /* all I/O fails */
139};
140
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100141struct pool_features {
Joe Thornbere49e5822012-07-27 15:08:16 +0100142 enum pool_mode mode;
143
Mike Snitzer9bc142d2012-09-26 23:45:46 +0100144 bool zero_new_blocks:1;
145 bool discard_enabled:1;
146 bool discard_passdown:1;
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100147};
148
Joe Thornbere49e5822012-07-27 15:08:16 +0100149struct thin_c;
150typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio);
151typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m);
152
Joe Thornber991d9fa2011-10-31 20:21:18 +0000153struct pool {
154 struct list_head list;
155 struct dm_target *ti; /* Only set if a pool target is bound */
156
157 struct mapped_device *pool_md;
158 struct block_device *md_dev;
159 struct dm_pool_metadata *pmd;
160
Joe Thornber991d9fa2011-10-31 20:21:18 +0000161 dm_block_t low_water_blocks;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100162 uint32_t sectors_per_block;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100163 int sectors_per_block_shift;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000164
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100165 struct pool_features pf;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000166 unsigned low_water_triggered:1; /* A dm event has been sent */
167 unsigned no_free_space:1; /* A -ENOSPC warning has been issued */
168
Mike Snitzer44feb382012-10-12 21:02:10 +0100169 struct dm_bio_prison *prison;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000170 struct dm_kcopyd_client *copier;
171
172 struct workqueue_struct *wq;
173 struct work_struct worker;
Joe Thornber905e51b2012-03-28 18:41:27 +0100174 struct delayed_work waker;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000175
Joe Thornber905e51b2012-03-28 18:41:27 +0100176 unsigned long last_commit_jiffies;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100177 unsigned ref_count;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000178
179 spinlock_t lock;
180 struct bio_list deferred_bios;
181 struct bio_list deferred_flush_bios;
182 struct list_head prepared_mappings;
Joe Thornber104655f2012-03-28 18:41:28 +0100183 struct list_head prepared_discards;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000184
185 struct bio_list retry_on_resume_list;
186
Mike Snitzer44feb382012-10-12 21:02:10 +0100187 struct dm_deferred_set *shared_read_ds;
188 struct dm_deferred_set *all_io_ds;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000189
Mike Snitzera24c2562012-06-03 00:30:00 +0100190 struct dm_thin_new_mapping *next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000191 mempool_t *mapping_pool;
Joe Thornbere49e5822012-07-27 15:08:16 +0100192
193 process_bio_fn process_bio;
194 process_bio_fn process_discard;
195
196 process_mapping_fn process_prepared_mapping;
197 process_mapping_fn process_prepared_discard;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000198};
199
Joe Thornbere49e5822012-07-27 15:08:16 +0100200static enum pool_mode get_pool_mode(struct pool *pool);
201static void set_pool_mode(struct pool *pool, enum pool_mode mode);
202
Joe Thornber991d9fa2011-10-31 20:21:18 +0000203/*
204 * Target context for a pool.
205 */
206struct pool_c {
207 struct dm_target *ti;
208 struct pool *pool;
209 struct dm_dev *data_dev;
210 struct dm_dev *metadata_dev;
211 struct dm_target_callbacks callbacks;
212
213 dm_block_t low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +0100214 struct pool_features requested_pf; /* Features requested during table load */
215 struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000216};
217
218/*
219 * Target context for a thin.
220 */
221struct thin_c {
222 struct dm_dev *pool_dev;
Joe Thornber2dd9c252012-03-28 18:41:28 +0100223 struct dm_dev *origin_dev;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000224 dm_thin_id dev_id;
225
226 struct pool *pool;
227 struct dm_thin_device *td;
228};
229
230/*----------------------------------------------------------------*/
231
Joe Thornber6beca5e2013-03-01 22:45:50 +0000232static int bio_detain(struct pool *pool, struct dm_cell_key *key, struct bio *bio,
233 struct dm_bio_prison_cell **cell_result)
234{
235 int r;
236 struct dm_bio_prison_cell *cell_prealloc;
237
238 /*
239 * Allocate a cell from the prison's mempool.
240 * This might block but it can't fail.
241 */
242 cell_prealloc = dm_bio_prison_alloc_cell(pool->prison, GFP_NOIO);
243
244 r = dm_bio_detain(pool->prison, key, bio, cell_prealloc, cell_result);
245 if (r)
246 /*
247 * We reused an old cell; we can get rid of
248 * the new one.
249 */
250 dm_bio_prison_free_cell(pool->prison, cell_prealloc);
251
252 return r;
253}
254
255static void cell_release(struct pool *pool,
256 struct dm_bio_prison_cell *cell,
257 struct bio_list *bios)
258{
259 dm_cell_release(pool->prison, cell, bios);
260 dm_bio_prison_free_cell(pool->prison, cell);
261}
262
263static void cell_release_no_holder(struct pool *pool,
264 struct dm_bio_prison_cell *cell,
265 struct bio_list *bios)
266{
267 dm_cell_release_no_holder(pool->prison, cell, bios);
268 dm_bio_prison_free_cell(pool->prison, cell);
269}
270
271static void cell_error(struct pool *pool,
272 struct dm_bio_prison_cell *cell)
273{
274 dm_cell_error(pool->prison, cell);
275 dm_bio_prison_free_cell(pool->prison, cell);
276}
277
278/*----------------------------------------------------------------*/
279
Joe Thornber991d9fa2011-10-31 20:21:18 +0000280/*
281 * A global list of pools that uses a struct mapped_device as a key.
282 */
283static struct dm_thin_pool_table {
284 struct mutex mutex;
285 struct list_head pools;
286} dm_thin_pool_table;
287
288static void pool_table_init(void)
289{
290 mutex_init(&dm_thin_pool_table.mutex);
291 INIT_LIST_HEAD(&dm_thin_pool_table.pools);
292}
293
294static void __pool_table_insert(struct pool *pool)
295{
296 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
297 list_add(&pool->list, &dm_thin_pool_table.pools);
298}
299
300static void __pool_table_remove(struct pool *pool)
301{
302 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
303 list_del(&pool->list);
304}
305
306static struct pool *__pool_table_lookup(struct mapped_device *md)
307{
308 struct pool *pool = NULL, *tmp;
309
310 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
311
312 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
313 if (tmp->pool_md == md) {
314 pool = tmp;
315 break;
316 }
317 }
318
319 return pool;
320}
321
322static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
323{
324 struct pool *pool = NULL, *tmp;
325
326 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
327
328 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
329 if (tmp->md_dev == md_dev) {
330 pool = tmp;
331 break;
332 }
333 }
334
335 return pool;
336}
337
338/*----------------------------------------------------------------*/
339
Mike Snitzera24c2562012-06-03 00:30:00 +0100340struct dm_thin_endio_hook {
Joe Thornbereb2aa482012-03-28 18:41:28 +0100341 struct thin_c *tc;
Mike Snitzer44feb382012-10-12 21:02:10 +0100342 struct dm_deferred_entry *shared_read_entry;
343 struct dm_deferred_entry *all_io_entry;
Mike Snitzera24c2562012-06-03 00:30:00 +0100344 struct dm_thin_new_mapping *overwrite_mapping;
Joe Thornbereb2aa482012-03-28 18:41:28 +0100345};
346
Joe Thornber991d9fa2011-10-31 20:21:18 +0000347static void __requeue_bio_list(struct thin_c *tc, struct bio_list *master)
348{
349 struct bio *bio;
350 struct bio_list bios;
351
352 bio_list_init(&bios);
353 bio_list_merge(&bios, master);
354 bio_list_init(master);
355
356 while ((bio = bio_list_pop(&bios))) {
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000357 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Mike Snitzera24c2562012-06-03 00:30:00 +0100358
Joe Thornbereb2aa482012-03-28 18:41:28 +0100359 if (h->tc == tc)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000360 bio_endio(bio, DM_ENDIO_REQUEUE);
361 else
362 bio_list_add(master, bio);
363 }
364}
365
366static void requeue_io(struct thin_c *tc)
367{
368 struct pool *pool = tc->pool;
369 unsigned long flags;
370
371 spin_lock_irqsave(&pool->lock, flags);
372 __requeue_bio_list(tc, &pool->deferred_bios);
373 __requeue_bio_list(tc, &pool->retry_on_resume_list);
374 spin_unlock_irqrestore(&pool->lock, flags);
375}
376
377/*
378 * This section of code contains the logic for processing a thin device's IO.
379 * Much of the code depends on pool object resources (lists, workqueues, etc)
380 * but most is exclusively called from the thin target rather than the thin-pool
381 * target.
382 */
383
Mike Snitzer58f77a22013-03-01 22:45:45 +0000384static bool block_size_is_power_of_two(struct pool *pool)
385{
386 return pool->sectors_per_block_shift >= 0;
387}
388
Joe Thornber991d9fa2011-10-31 20:21:18 +0000389static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
390{
Mike Snitzer58f77a22013-03-01 22:45:45 +0000391 struct pool *pool = tc->pool;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100392 sector_t block_nr = bio->bi_sector;
393
Mike Snitzer58f77a22013-03-01 22:45:45 +0000394 if (block_size_is_power_of_two(pool))
395 block_nr >>= pool->sectors_per_block_shift;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100396 else
Mike Snitzer58f77a22013-03-01 22:45:45 +0000397 (void) sector_div(block_nr, pool->sectors_per_block);
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100398
399 return block_nr;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000400}
401
402static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
403{
404 struct pool *pool = tc->pool;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100405 sector_t bi_sector = bio->bi_sector;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000406
407 bio->bi_bdev = tc->pool_dev->bdev;
Mike Snitzer58f77a22013-03-01 22:45:45 +0000408 if (block_size_is_power_of_two(pool))
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100409 bio->bi_sector = (block << pool->sectors_per_block_shift) |
410 (bi_sector & (pool->sectors_per_block - 1));
Mike Snitzer58f77a22013-03-01 22:45:45 +0000411 else
412 bio->bi_sector = (block * pool->sectors_per_block) +
413 sector_div(bi_sector, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000414}
415
Joe Thornber2dd9c252012-03-28 18:41:28 +0100416static void remap_to_origin(struct thin_c *tc, struct bio *bio)
417{
418 bio->bi_bdev = tc->origin_dev->bdev;
419}
420
Joe Thornber4afdd682012-07-27 15:08:14 +0100421static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
422{
423 return (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) &&
424 dm_thin_changed_this_transaction(tc->td);
425}
426
Joe Thornbere8088072012-12-21 20:23:31 +0000427static void inc_all_io_entry(struct pool *pool, struct bio *bio)
428{
429 struct dm_thin_endio_hook *h;
430
431 if (bio->bi_rw & REQ_DISCARD)
432 return;
433
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000434 h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbere8088072012-12-21 20:23:31 +0000435 h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
436}
437
Joe Thornber2dd9c252012-03-28 18:41:28 +0100438static void issue(struct thin_c *tc, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000439{
440 struct pool *pool = tc->pool;
441 unsigned long flags;
442
Joe Thornbere49e5822012-07-27 15:08:16 +0100443 if (!bio_triggers_commit(tc, bio)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000444 generic_make_request(bio);
Joe Thornbere49e5822012-07-27 15:08:16 +0100445 return;
446 }
447
448 /*
449 * Complete bio with an error if earlier I/O caused changes to
450 * the metadata that can't be committed e.g, due to I/O errors
451 * on the metadata device.
452 */
453 if (dm_thin_aborted_changes(tc->td)) {
454 bio_io_error(bio);
455 return;
456 }
457
458 /*
459 * Batch together any bios that trigger commits and then issue a
460 * single commit for them in process_deferred_bios().
461 */
462 spin_lock_irqsave(&pool->lock, flags);
463 bio_list_add(&pool->deferred_flush_bios, bio);
464 spin_unlock_irqrestore(&pool->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000465}
466
Joe Thornber2dd9c252012-03-28 18:41:28 +0100467static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
468{
469 remap_to_origin(tc, bio);
470 issue(tc, bio);
471}
472
473static void remap_and_issue(struct thin_c *tc, struct bio *bio,
474 dm_block_t block)
475{
476 remap(tc, bio, block);
477 issue(tc, bio);
478}
479
Joe Thornber991d9fa2011-10-31 20:21:18 +0000480/*
481 * wake_worker() is used when new work is queued and when pool_resume is
482 * ready to continue deferred IO processing.
483 */
484static void wake_worker(struct pool *pool)
485{
486 queue_work(pool->wq, &pool->worker);
487}
488
489/*----------------------------------------------------------------*/
490
491/*
492 * Bio endio functions.
493 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100494struct dm_thin_new_mapping {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000495 struct list_head list;
496
Joe Thornbereb2aa482012-03-28 18:41:28 +0100497 unsigned quiesced:1;
498 unsigned prepared:1;
Joe Thornber104655f2012-03-28 18:41:28 +0100499 unsigned pass_discard:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000500
501 struct thin_c *tc;
502 dm_block_t virt_block;
503 dm_block_t data_block;
Mike Snitzera24c2562012-06-03 00:30:00 +0100504 struct dm_bio_prison_cell *cell, *cell2;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000505 int err;
506
507 /*
508 * If the bio covers the whole area of a block then we can avoid
509 * zeroing or copying. Instead this bio is hooked. The bio will
510 * still be in the cell, so care has to be taken to avoid issuing
511 * the bio twice.
512 */
513 struct bio *bio;
514 bio_end_io_t *saved_bi_end_io;
515};
516
Mike Snitzera24c2562012-06-03 00:30:00 +0100517static void __maybe_add_mapping(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000518{
519 struct pool *pool = m->tc->pool;
520
Joe Thornbereb2aa482012-03-28 18:41:28 +0100521 if (m->quiesced && m->prepared) {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000522 list_add(&m->list, &pool->prepared_mappings);
523 wake_worker(pool);
524 }
525}
526
527static void copy_complete(int read_err, unsigned long write_err, void *context)
528{
529 unsigned long flags;
Mike Snitzera24c2562012-06-03 00:30:00 +0100530 struct dm_thin_new_mapping *m = context;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000531 struct pool *pool = m->tc->pool;
532
533 m->err = read_err || write_err ? -EIO : 0;
534
535 spin_lock_irqsave(&pool->lock, flags);
536 m->prepared = 1;
537 __maybe_add_mapping(m);
538 spin_unlock_irqrestore(&pool->lock, flags);
539}
540
541static void overwrite_endio(struct bio *bio, int err)
542{
543 unsigned long flags;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000544 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Mike Snitzera24c2562012-06-03 00:30:00 +0100545 struct dm_thin_new_mapping *m = h->overwrite_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000546 struct pool *pool = m->tc->pool;
547
548 m->err = err;
549
550 spin_lock_irqsave(&pool->lock, flags);
551 m->prepared = 1;
552 __maybe_add_mapping(m);
553 spin_unlock_irqrestore(&pool->lock, flags);
554}
555
Joe Thornber991d9fa2011-10-31 20:21:18 +0000556/*----------------------------------------------------------------*/
557
558/*
559 * Workqueue.
560 */
561
562/*
563 * Prepared mapping jobs.
564 */
565
566/*
567 * This sends the bios in the cell back to the deferred_bios list.
568 */
Joe Thornber2aab3852012-12-21 20:23:33 +0000569static void cell_defer(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000570{
571 struct pool *pool = tc->pool;
572 unsigned long flags;
573
574 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000575 cell_release(pool, cell, &pool->deferred_bios);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000576 spin_unlock_irqrestore(&tc->pool->lock, flags);
577
578 wake_worker(pool);
579}
580
581/*
Joe Thornber6beca5e2013-03-01 22:45:50 +0000582 * Same as cell_defer above, except it omits the original holder of the cell.
Joe Thornber991d9fa2011-10-31 20:21:18 +0000583 */
Joe Thornberf286ba02012-12-21 20:23:33 +0000584static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000585{
Joe Thornber991d9fa2011-10-31 20:21:18 +0000586 struct pool *pool = tc->pool;
587 unsigned long flags;
588
Joe Thornber991d9fa2011-10-31 20:21:18 +0000589 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000590 cell_release_no_holder(pool, cell, &pool->deferred_bios);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000591 spin_unlock_irqrestore(&pool->lock, flags);
592
593 wake_worker(pool);
594}
595
Joe Thornbere49e5822012-07-27 15:08:16 +0100596static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
597{
598 if (m->bio)
599 m->bio->bi_end_io = m->saved_bi_end_io;
Joe Thornber6beca5e2013-03-01 22:45:50 +0000600 cell_error(m->tc->pool, m->cell);
Joe Thornbere49e5822012-07-27 15:08:16 +0100601 list_del(&m->list);
602 mempool_free(m, m->tc->pool->mapping_pool);
603}
Mike Snitzera24c2562012-06-03 00:30:00 +0100604static void process_prepared_mapping(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000605{
606 struct thin_c *tc = m->tc;
Joe Thornber6beca5e2013-03-01 22:45:50 +0000607 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000608 struct bio *bio;
609 int r;
610
611 bio = m->bio;
612 if (bio)
613 bio->bi_end_io = m->saved_bi_end_io;
614
615 if (m->err) {
Joe Thornber6beca5e2013-03-01 22:45:50 +0000616 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100617 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000618 }
619
620 /*
621 * Commit the prepared block into the mapping btree.
622 * Any I/O for this block arriving after this point will get
623 * remapped to it directly.
624 */
625 r = dm_thin_insert_block(tc->td, m->virt_block, m->data_block);
626 if (r) {
Mike Snitzerc3977412012-12-21 20:23:34 +0000627 DMERR_LIMIT("dm_thin_insert_block() failed");
Joe Thornber6beca5e2013-03-01 22:45:50 +0000628 cell_error(pool, m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100629 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000630 }
631
632 /*
633 * Release any bios held while the block was being provisioned.
634 * If we are processing a write bio that completely covers the block,
635 * we already processed it so can ignore it now when processing
636 * the bios in the cell.
637 */
638 if (bio) {
Joe Thornberf286ba02012-12-21 20:23:33 +0000639 cell_defer_no_holder(tc, m->cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000640 bio_endio(bio, 0);
641 } else
Joe Thornber2aab3852012-12-21 20:23:33 +0000642 cell_defer(tc, m->cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000643
Joe Thornber905386f2012-07-27 15:08:05 +0100644out:
Joe Thornber991d9fa2011-10-31 20:21:18 +0000645 list_del(&m->list);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000646 mempool_free(m, pool->mapping_pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000647}
648
Joe Thornbere49e5822012-07-27 15:08:16 +0100649static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
Joe Thornber104655f2012-03-28 18:41:28 +0100650{
Joe Thornber104655f2012-03-28 18:41:28 +0100651 struct thin_c *tc = m->tc;
652
Joe Thornbere49e5822012-07-27 15:08:16 +0100653 bio_io_error(m->bio);
Joe Thornberf286ba02012-12-21 20:23:33 +0000654 cell_defer_no_holder(tc, m->cell);
655 cell_defer_no_holder(tc, m->cell2);
Joe Thornbere49e5822012-07-27 15:08:16 +0100656 mempool_free(m, tc->pool->mapping_pool);
657}
Joe Thornber104655f2012-03-28 18:41:28 +0100658
Joe Thornbere49e5822012-07-27 15:08:16 +0100659static void process_prepared_discard_passdown(struct dm_thin_new_mapping *m)
660{
661 struct thin_c *tc = m->tc;
662
Joe Thornbere8088072012-12-21 20:23:31 +0000663 inc_all_io_entry(tc->pool, m->bio);
Joe Thornberf286ba02012-12-21 20:23:33 +0000664 cell_defer_no_holder(tc, m->cell);
665 cell_defer_no_holder(tc, m->cell2);
Joe Thornbere8088072012-12-21 20:23:31 +0000666
Joe Thornber104655f2012-03-28 18:41:28 +0100667 if (m->pass_discard)
668 remap_and_issue(tc, m->bio, m->data_block);
669 else
670 bio_endio(m->bio, 0);
671
Joe Thornber104655f2012-03-28 18:41:28 +0100672 mempool_free(m, tc->pool->mapping_pool);
673}
674
Joe Thornbere49e5822012-07-27 15:08:16 +0100675static void process_prepared_discard(struct dm_thin_new_mapping *m)
676{
677 int r;
678 struct thin_c *tc = m->tc;
679
680 r = dm_thin_remove_block(tc->td, m->virt_block);
681 if (r)
Mike Snitzerc3977412012-12-21 20:23:34 +0000682 DMERR_LIMIT("dm_thin_remove_block() failed");
Joe Thornbere49e5822012-07-27 15:08:16 +0100683
684 process_prepared_discard_passdown(m);
685}
686
Joe Thornber104655f2012-03-28 18:41:28 +0100687static void process_prepared(struct pool *pool, struct list_head *head,
Joe Thornbere49e5822012-07-27 15:08:16 +0100688 process_mapping_fn *fn)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000689{
690 unsigned long flags;
691 struct list_head maps;
Mike Snitzera24c2562012-06-03 00:30:00 +0100692 struct dm_thin_new_mapping *m, *tmp;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000693
694 INIT_LIST_HEAD(&maps);
695 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +0100696 list_splice_init(head, &maps);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000697 spin_unlock_irqrestore(&pool->lock, flags);
698
699 list_for_each_entry_safe(m, tmp, &maps, list)
Joe Thornbere49e5822012-07-27 15:08:16 +0100700 (*fn)(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000701}
702
703/*
704 * Deferred bio jobs.
705 */
Joe Thornber104655f2012-03-28 18:41:28 +0100706static int io_overlaps_block(struct pool *pool, struct bio *bio)
707{
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100708 return bio->bi_size == (pool->sectors_per_block << SECTOR_SHIFT);
Joe Thornber104655f2012-03-28 18:41:28 +0100709}
710
Joe Thornber991d9fa2011-10-31 20:21:18 +0000711static int io_overwrites_block(struct pool *pool, struct bio *bio)
712{
Joe Thornber104655f2012-03-28 18:41:28 +0100713 return (bio_data_dir(bio) == WRITE) &&
714 io_overlaps_block(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000715}
716
717static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
718 bio_end_io_t *fn)
719{
720 *save = bio->bi_end_io;
721 bio->bi_end_io = fn;
722}
723
724static int ensure_next_mapping(struct pool *pool)
725{
726 if (pool->next_mapping)
727 return 0;
728
729 pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
730
731 return pool->next_mapping ? 0 : -ENOMEM;
732}
733
Mike Snitzera24c2562012-06-03 00:30:00 +0100734static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000735{
Mike Snitzera24c2562012-06-03 00:30:00 +0100736 struct dm_thin_new_mapping *r = pool->next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000737
738 BUG_ON(!pool->next_mapping);
739
740 pool->next_mapping = NULL;
741
742 return r;
743}
744
745static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
Joe Thornber2dd9c252012-03-28 18:41:28 +0100746 struct dm_dev *origin, dm_block_t data_origin,
747 dm_block_t data_dest,
Mike Snitzera24c2562012-06-03 00:30:00 +0100748 struct dm_bio_prison_cell *cell, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000749{
750 int r;
751 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +0100752 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000753
754 INIT_LIST_HEAD(&m->list);
Joe Thornbereb2aa482012-03-28 18:41:28 +0100755 m->quiesced = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000756 m->prepared = 0;
757 m->tc = tc;
758 m->virt_block = virt_block;
759 m->data_block = data_dest;
760 m->cell = cell;
761 m->err = 0;
762 m->bio = NULL;
763
Mike Snitzer44feb382012-10-12 21:02:10 +0100764 if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
Joe Thornbereb2aa482012-03-28 18:41:28 +0100765 m->quiesced = 1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000766
767 /*
768 * IO to pool_dev remaps to the pool target's data_dev.
769 *
770 * If the whole block of data is being overwritten, we can issue the
771 * bio immediately. Otherwise we use kcopyd to clone the data first.
772 */
773 if (io_overwrites_block(pool, bio)) {
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000774 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Mike Snitzera24c2562012-06-03 00:30:00 +0100775
Joe Thornbereb2aa482012-03-28 18:41:28 +0100776 h->overwrite_mapping = m;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000777 m->bio = bio;
778 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
Joe Thornbere8088072012-12-21 20:23:31 +0000779 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000780 remap_and_issue(tc, bio, data_dest);
781 } else {
782 struct dm_io_region from, to;
783
Joe Thornber2dd9c252012-03-28 18:41:28 +0100784 from.bdev = origin->bdev;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000785 from.sector = data_origin * pool->sectors_per_block;
786 from.count = pool->sectors_per_block;
787
788 to.bdev = tc->pool_dev->bdev;
789 to.sector = data_dest * pool->sectors_per_block;
790 to.count = pool->sectors_per_block;
791
792 r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
793 0, copy_complete, m);
794 if (r < 0) {
795 mempool_free(m, pool->mapping_pool);
Mike Snitzerc3977412012-12-21 20:23:34 +0000796 DMERR_LIMIT("dm_kcopyd_copy() failed");
Joe Thornber6beca5e2013-03-01 22:45:50 +0000797 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000798 }
799 }
800}
801
Joe Thornber2dd9c252012-03-28 18:41:28 +0100802static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
803 dm_block_t data_origin, dm_block_t data_dest,
Mike Snitzera24c2562012-06-03 00:30:00 +0100804 struct dm_bio_prison_cell *cell, struct bio *bio)
Joe Thornber2dd9c252012-03-28 18:41:28 +0100805{
806 schedule_copy(tc, virt_block, tc->pool_dev,
807 data_origin, data_dest, cell, bio);
808}
809
810static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
811 dm_block_t data_dest,
Mike Snitzera24c2562012-06-03 00:30:00 +0100812 struct dm_bio_prison_cell *cell, struct bio *bio)
Joe Thornber2dd9c252012-03-28 18:41:28 +0100813{
814 schedule_copy(tc, virt_block, tc->origin_dev,
815 virt_block, data_dest, cell, bio);
816}
817
Joe Thornber991d9fa2011-10-31 20:21:18 +0000818static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
Mike Snitzera24c2562012-06-03 00:30:00 +0100819 dm_block_t data_block, struct dm_bio_prison_cell *cell,
Joe Thornber991d9fa2011-10-31 20:21:18 +0000820 struct bio *bio)
821{
822 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +0100823 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000824
825 INIT_LIST_HEAD(&m->list);
Joe Thornbereb2aa482012-03-28 18:41:28 +0100826 m->quiesced = 1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000827 m->prepared = 0;
828 m->tc = tc;
829 m->virt_block = virt_block;
830 m->data_block = data_block;
831 m->cell = cell;
832 m->err = 0;
833 m->bio = NULL;
834
835 /*
836 * If the whole block of data is being overwritten or we are not
837 * zeroing pre-existing data, we can issue the bio immediately.
838 * Otherwise we use kcopyd to zero the data first.
839 */
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100840 if (!pool->pf.zero_new_blocks)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000841 process_prepared_mapping(m);
842
843 else if (io_overwrites_block(pool, bio)) {
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000844 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Mike Snitzera24c2562012-06-03 00:30:00 +0100845
Joe Thornbereb2aa482012-03-28 18:41:28 +0100846 h->overwrite_mapping = m;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000847 m->bio = bio;
848 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
Joe Thornbere8088072012-12-21 20:23:31 +0000849 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000850 remap_and_issue(tc, bio, data_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000851 } else {
852 int r;
853 struct dm_io_region to;
854
855 to.bdev = tc->pool_dev->bdev;
856 to.sector = data_block * pool->sectors_per_block;
857 to.count = pool->sectors_per_block;
858
859 r = dm_kcopyd_zero(pool->copier, 1, &to, 0, copy_complete, m);
860 if (r < 0) {
861 mempool_free(m, pool->mapping_pool);
Mike Snitzerc3977412012-12-21 20:23:34 +0000862 DMERR_LIMIT("dm_kcopyd_zero() failed");
Joe Thornber6beca5e2013-03-01 22:45:50 +0000863 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000864 }
865 }
866}
867
Joe Thornbere49e5822012-07-27 15:08:16 +0100868static int commit(struct pool *pool)
869{
870 int r;
871
872 r = dm_pool_commit_metadata(pool->pmd);
873 if (r)
Mike Snitzerc3977412012-12-21 20:23:34 +0000874 DMERR_LIMIT("commit failed: error = %d", r);
Joe Thornbere49e5822012-07-27 15:08:16 +0100875
876 return r;
877}
878
879/*
880 * A non-zero return indicates read_only or fail_io mode.
881 * Many callers don't care about the return value.
882 */
883static int commit_or_fallback(struct pool *pool)
884{
885 int r;
886
887 if (get_pool_mode(pool) != PM_WRITE)
888 return -EINVAL;
889
890 r = commit(pool);
891 if (r)
892 set_pool_mode(pool, PM_READ_ONLY);
893
894 return r;
895}
896
Joe Thornber991d9fa2011-10-31 20:21:18 +0000897static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
898{
899 int r;
900 dm_block_t free_blocks;
901 unsigned long flags;
902 struct pool *pool = tc->pool;
903
904 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
905 if (r)
906 return r;
907
908 if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
909 DMWARN("%s: reached low water mark, sending event.",
910 dm_device_name(pool->pool_md));
911 spin_lock_irqsave(&pool->lock, flags);
912 pool->low_water_triggered = 1;
913 spin_unlock_irqrestore(&pool->lock, flags);
914 dm_table_event(pool->ti->table);
915 }
916
917 if (!free_blocks) {
918 if (pool->no_free_space)
919 return -ENOSPC;
920 else {
921 /*
922 * Try to commit to see if that will free up some
923 * more space.
924 */
Joe Thornbere49e5822012-07-27 15:08:16 +0100925 (void) commit_or_fallback(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000926
927 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
928 if (r)
929 return r;
930
931 /*
932 * If we still have no space we set a flag to avoid
933 * doing all this checking and return -ENOSPC.
934 */
935 if (!free_blocks) {
936 DMWARN("%s: no free space available.",
937 dm_device_name(pool->pool_md));
938 spin_lock_irqsave(&pool->lock, flags);
939 pool->no_free_space = 1;
940 spin_unlock_irqrestore(&pool->lock, flags);
941 return -ENOSPC;
942 }
943 }
944 }
945
946 r = dm_pool_alloc_data_block(pool->pmd, result);
947 if (r)
948 return r;
949
950 return 0;
951}
952
953/*
954 * If we have run out of space, queue bios until the device is
955 * resumed, presumably after having been reloaded with more space.
956 */
957static void retry_on_resume(struct bio *bio)
958{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +0000959 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +0100960 struct thin_c *tc = h->tc;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000961 struct pool *pool = tc->pool;
962 unsigned long flags;
963
964 spin_lock_irqsave(&pool->lock, flags);
965 bio_list_add(&pool->retry_on_resume_list, bio);
966 spin_unlock_irqrestore(&pool->lock, flags);
967}
968
Joe Thornber6beca5e2013-03-01 22:45:50 +0000969static void no_space(struct pool *pool, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000970{
971 struct bio *bio;
972 struct bio_list bios;
973
974 bio_list_init(&bios);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000975 cell_release(pool, cell, &bios);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000976
977 while ((bio = bio_list_pop(&bios)))
978 retry_on_resume(bio);
979}
980
Joe Thornber104655f2012-03-28 18:41:28 +0100981static void process_discard(struct thin_c *tc, struct bio *bio)
982{
983 int r;
Mike Snitzerc3a0ce22012-05-12 01:43:16 +0100984 unsigned long flags;
Joe Thornber104655f2012-03-28 18:41:28 +0100985 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +0100986 struct dm_bio_prison_cell *cell, *cell2;
Mike Snitzer44feb382012-10-12 21:02:10 +0100987 struct dm_cell_key key, key2;
Joe Thornber104655f2012-03-28 18:41:28 +0100988 dm_block_t block = get_bio_block(tc, bio);
989 struct dm_thin_lookup_result lookup_result;
Mike Snitzera24c2562012-06-03 00:30:00 +0100990 struct dm_thin_new_mapping *m;
Joe Thornber104655f2012-03-28 18:41:28 +0100991
992 build_virtual_key(tc->td, block, &key);
Joe Thornber6beca5e2013-03-01 22:45:50 +0000993 if (bio_detain(tc->pool, &key, bio, &cell))
Joe Thornber104655f2012-03-28 18:41:28 +0100994 return;
995
996 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
997 switch (r) {
998 case 0:
999 /*
1000 * Check nobody is fiddling with this pool block. This can
1001 * happen if someone's in the process of breaking sharing
1002 * on this block.
1003 */
1004 build_data_key(tc->td, lookup_result.block, &key2);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001005 if (bio_detain(tc->pool, &key2, bio, &cell2)) {
Joe Thornberf286ba02012-12-21 20:23:33 +00001006 cell_defer_no_holder(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001007 break;
1008 }
1009
1010 if (io_overlaps_block(pool, bio)) {
1011 /*
1012 * IO may still be going to the destination block. We must
1013 * quiesce before we can do the removal.
1014 */
1015 m = get_next_mapping(pool);
1016 m->tc = tc;
Mike Snitzer17b7d632012-07-27 15:07:57 +01001017 m->pass_discard = (!lookup_result.shared) && pool->pf.discard_passdown;
Joe Thornber104655f2012-03-28 18:41:28 +01001018 m->virt_block = block;
1019 m->data_block = lookup_result.block;
1020 m->cell = cell;
1021 m->cell2 = cell2;
1022 m->err = 0;
1023 m->bio = bio;
1024
Mike Snitzer44feb382012-10-12 21:02:10 +01001025 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list)) {
Mike Snitzerc3a0ce22012-05-12 01:43:16 +01001026 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +01001027 list_add(&m->list, &pool->prepared_discards);
Mike Snitzerc3a0ce22012-05-12 01:43:16 +01001028 spin_unlock_irqrestore(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +01001029 wake_worker(pool);
1030 }
1031 } else {
Joe Thornbere8088072012-12-21 20:23:31 +00001032 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001033 cell_defer_no_holder(tc, cell);
1034 cell_defer_no_holder(tc, cell2);
Joe Thornbere8088072012-12-21 20:23:31 +00001035
Joe Thornber104655f2012-03-28 18:41:28 +01001036 /*
Mikulas Patocka49296302012-07-27 15:08:03 +01001037 * The DM core makes sure that the discard doesn't span
1038 * a block boundary. So we submit the discard of a
1039 * partial block appropriately.
Joe Thornber104655f2012-03-28 18:41:28 +01001040 */
Mikulas Patocka650d2a02012-07-20 14:25:05 +01001041 if ((!lookup_result.shared) && pool->pf.discard_passdown)
1042 remap_and_issue(tc, bio, lookup_result.block);
1043 else
1044 bio_endio(bio, 0);
Joe Thornber104655f2012-03-28 18:41:28 +01001045 }
1046 break;
1047
1048 case -ENODATA:
1049 /*
1050 * It isn't provisioned, just forget it.
1051 */
Joe Thornberf286ba02012-12-21 20:23:33 +00001052 cell_defer_no_holder(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001053 bio_endio(bio, 0);
1054 break;
1055
1056 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001057 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1058 __func__, r);
Joe Thornberf286ba02012-12-21 20:23:33 +00001059 cell_defer_no_holder(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001060 bio_io_error(bio);
1061 break;
1062 }
1063}
1064
Joe Thornber991d9fa2011-10-31 20:21:18 +00001065static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzer44feb382012-10-12 21:02:10 +01001066 struct dm_cell_key *key,
Joe Thornber991d9fa2011-10-31 20:21:18 +00001067 struct dm_thin_lookup_result *lookup_result,
Mike Snitzera24c2562012-06-03 00:30:00 +01001068 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001069{
1070 int r;
1071 dm_block_t data_block;
1072
1073 r = alloc_data_block(tc, &data_block);
1074 switch (r) {
1075 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001076 schedule_internal_copy(tc, block, lookup_result->block,
1077 data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001078 break;
1079
1080 case -ENOSPC:
Joe Thornber6beca5e2013-03-01 22:45:50 +00001081 no_space(tc->pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001082 break;
1083
1084 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001085 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1086 __func__, r);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001087 cell_error(tc->pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001088 break;
1089 }
1090}
1091
1092static void process_shared_bio(struct thin_c *tc, struct bio *bio,
1093 dm_block_t block,
1094 struct dm_thin_lookup_result *lookup_result)
1095{
Mike Snitzera24c2562012-06-03 00:30:00 +01001096 struct dm_bio_prison_cell *cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001097 struct pool *pool = tc->pool;
Mike Snitzer44feb382012-10-12 21:02:10 +01001098 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001099
1100 /*
1101 * If cell is already occupied, then sharing is already in the process
1102 * of being broken so we have nothing further to do here.
1103 */
1104 build_data_key(tc->td, lookup_result->block, &key);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001105 if (bio_detain(pool, &key, bio, &cell))
Joe Thornber991d9fa2011-10-31 20:21:18 +00001106 return;
1107
Joe Thornber60049702012-07-27 15:08:06 +01001108 if (bio_data_dir(bio) == WRITE && bio->bi_size)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001109 break_sharing(tc, bio, block, &key, lookup_result, cell);
1110 else {
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001111 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornber991d9fa2011-10-31 20:21:18 +00001112
Mike Snitzer44feb382012-10-12 21:02:10 +01001113 h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
Joe Thornbere8088072012-12-21 20:23:31 +00001114 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001115 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001116
Joe Thornber991d9fa2011-10-31 20:21:18 +00001117 remap_and_issue(tc, bio, lookup_result->block);
1118 }
1119}
1120
1121static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzera24c2562012-06-03 00:30:00 +01001122 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001123{
1124 int r;
1125 dm_block_t data_block;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001126 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001127
1128 /*
1129 * Remap empty bios (flushes) immediately, without provisioning.
1130 */
1131 if (!bio->bi_size) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001132 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001133 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001134
Joe Thornber991d9fa2011-10-31 20:21:18 +00001135 remap_and_issue(tc, bio, 0);
1136 return;
1137 }
1138
1139 /*
1140 * Fill read bios with zeroes and complete them immediately.
1141 */
1142 if (bio_data_dir(bio) == READ) {
1143 zero_fill_bio(bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001144 cell_defer_no_holder(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001145 bio_endio(bio, 0);
1146 return;
1147 }
1148
1149 r = alloc_data_block(tc, &data_block);
1150 switch (r) {
1151 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001152 if (tc->origin_dev)
1153 schedule_external_copy(tc, block, data_block, cell, bio);
1154 else
1155 schedule_zero(tc, block, data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001156 break;
1157
1158 case -ENOSPC:
Joe Thornber6beca5e2013-03-01 22:45:50 +00001159 no_space(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001160 break;
1161
1162 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001163 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1164 __func__, r);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001165 set_pool_mode(pool, PM_READ_ONLY);
1166 cell_error(pool, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001167 break;
1168 }
1169}
1170
1171static void process_bio(struct thin_c *tc, struct bio *bio)
1172{
1173 int r;
Joe Thornber6beca5e2013-03-01 22:45:50 +00001174 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001175 dm_block_t block = get_bio_block(tc, bio);
Mike Snitzera24c2562012-06-03 00:30:00 +01001176 struct dm_bio_prison_cell *cell;
Mike Snitzer44feb382012-10-12 21:02:10 +01001177 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001178 struct dm_thin_lookup_result lookup_result;
1179
1180 /*
1181 * If cell is already occupied, then the block is already
1182 * being provisioned so we have nothing further to do here.
1183 */
1184 build_virtual_key(tc->td, block, &key);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001185 if (bio_detain(pool, &key, bio, &cell))
Joe Thornber991d9fa2011-10-31 20:21:18 +00001186 return;
1187
1188 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1189 switch (r) {
1190 case 0:
Joe Thornbere8088072012-12-21 20:23:31 +00001191 if (lookup_result.shared) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001192 process_shared_bio(tc, bio, block, &lookup_result);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001193 cell_defer_no_holder(tc, cell); /* FIXME: pass this cell into process_shared? */
Joe Thornbere8088072012-12-21 20:23:31 +00001194 } else {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001195 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001196 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001197
Joe Thornber991d9fa2011-10-31 20:21:18 +00001198 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001199 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001200 break;
1201
1202 case -ENODATA:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001203 if (bio_data_dir(bio) == READ && tc->origin_dev) {
Joe Thornber6beca5e2013-03-01 22:45:50 +00001204 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001205 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001206
Joe Thornber2dd9c252012-03-28 18:41:28 +01001207 remap_to_origin_and_issue(tc, bio);
1208 } else
1209 provision_block(tc, bio, block, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001210 break;
1211
1212 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001213 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1214 __func__, r);
Joe Thornberf286ba02012-12-21 20:23:33 +00001215 cell_defer_no_holder(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001216 bio_io_error(bio);
1217 break;
1218 }
1219}
1220
Joe Thornbere49e5822012-07-27 15:08:16 +01001221static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
1222{
1223 int r;
1224 int rw = bio_data_dir(bio);
1225 dm_block_t block = get_bio_block(tc, bio);
1226 struct dm_thin_lookup_result lookup_result;
1227
1228 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1229 switch (r) {
1230 case 0:
1231 if (lookup_result.shared && (rw == WRITE) && bio->bi_size)
1232 bio_io_error(bio);
Joe Thornbere8088072012-12-21 20:23:31 +00001233 else {
1234 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001235 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001236 }
Joe Thornbere49e5822012-07-27 15:08:16 +01001237 break;
1238
1239 case -ENODATA:
1240 if (rw != READ) {
1241 bio_io_error(bio);
1242 break;
1243 }
1244
1245 if (tc->origin_dev) {
Joe Thornbere8088072012-12-21 20:23:31 +00001246 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001247 remap_to_origin_and_issue(tc, bio);
1248 break;
1249 }
1250
1251 zero_fill_bio(bio);
1252 bio_endio(bio, 0);
1253 break;
1254
1255 default:
Mike Snitzerc3977412012-12-21 20:23:34 +00001256 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1257 __func__, r);
Joe Thornbere49e5822012-07-27 15:08:16 +01001258 bio_io_error(bio);
1259 break;
1260 }
1261}
1262
1263static void process_bio_fail(struct thin_c *tc, struct bio *bio)
1264{
1265 bio_io_error(bio);
1266}
1267
Joe Thornber905e51b2012-03-28 18:41:27 +01001268static int need_commit_due_to_time(struct pool *pool)
1269{
1270 return jiffies < pool->last_commit_jiffies ||
1271 jiffies > pool->last_commit_jiffies + COMMIT_PERIOD;
1272}
1273
Joe Thornber991d9fa2011-10-31 20:21:18 +00001274static void process_deferred_bios(struct pool *pool)
1275{
1276 unsigned long flags;
1277 struct bio *bio;
1278 struct bio_list bios;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001279
1280 bio_list_init(&bios);
1281
1282 spin_lock_irqsave(&pool->lock, flags);
1283 bio_list_merge(&bios, &pool->deferred_bios);
1284 bio_list_init(&pool->deferred_bios);
1285 spin_unlock_irqrestore(&pool->lock, flags);
1286
1287 while ((bio = bio_list_pop(&bios))) {
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001288 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01001289 struct thin_c *tc = h->tc;
1290
Joe Thornber991d9fa2011-10-31 20:21:18 +00001291 /*
1292 * If we've got no free new_mapping structs, and processing
1293 * this bio might require one, we pause until there are some
1294 * prepared mappings to process.
1295 */
1296 if (ensure_next_mapping(pool)) {
1297 spin_lock_irqsave(&pool->lock, flags);
1298 bio_list_merge(&pool->deferred_bios, &bios);
1299 spin_unlock_irqrestore(&pool->lock, flags);
1300
1301 break;
1302 }
Joe Thornber104655f2012-03-28 18:41:28 +01001303
1304 if (bio->bi_rw & REQ_DISCARD)
Joe Thornbere49e5822012-07-27 15:08:16 +01001305 pool->process_discard(tc, bio);
Joe Thornber104655f2012-03-28 18:41:28 +01001306 else
Joe Thornbere49e5822012-07-27 15:08:16 +01001307 pool->process_bio(tc, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001308 }
1309
1310 /*
1311 * If there are any deferred flush bios, we must commit
1312 * the metadata before issuing them.
1313 */
1314 bio_list_init(&bios);
1315 spin_lock_irqsave(&pool->lock, flags);
1316 bio_list_merge(&bios, &pool->deferred_flush_bios);
1317 bio_list_init(&pool->deferred_flush_bios);
1318 spin_unlock_irqrestore(&pool->lock, flags);
1319
Joe Thornber905e51b2012-03-28 18:41:27 +01001320 if (bio_list_empty(&bios) && !need_commit_due_to_time(pool))
Joe Thornber991d9fa2011-10-31 20:21:18 +00001321 return;
1322
Joe Thornbere49e5822012-07-27 15:08:16 +01001323 if (commit_or_fallback(pool)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001324 while ((bio = bio_list_pop(&bios)))
1325 bio_io_error(bio);
1326 return;
1327 }
Joe Thornber905e51b2012-03-28 18:41:27 +01001328 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001329
1330 while ((bio = bio_list_pop(&bios)))
1331 generic_make_request(bio);
1332}
1333
1334static void do_worker(struct work_struct *ws)
1335{
1336 struct pool *pool = container_of(ws, struct pool, worker);
1337
Joe Thornbere49e5822012-07-27 15:08:16 +01001338 process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
1339 process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001340 process_deferred_bios(pool);
1341}
1342
Joe Thornber905e51b2012-03-28 18:41:27 +01001343/*
1344 * We want to commit periodically so that not too much
1345 * unwritten data builds up.
1346 */
1347static void do_waker(struct work_struct *ws)
1348{
1349 struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
1350 wake_worker(pool);
1351 queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
1352}
1353
Joe Thornber991d9fa2011-10-31 20:21:18 +00001354/*----------------------------------------------------------------*/
1355
Joe Thornbere49e5822012-07-27 15:08:16 +01001356static enum pool_mode get_pool_mode(struct pool *pool)
1357{
1358 return pool->pf.mode;
1359}
1360
1361static void set_pool_mode(struct pool *pool, enum pool_mode mode)
1362{
1363 int r;
1364
1365 pool->pf.mode = mode;
1366
1367 switch (mode) {
1368 case PM_FAIL:
1369 DMERR("switching pool to failure mode");
1370 pool->process_bio = process_bio_fail;
1371 pool->process_discard = process_bio_fail;
1372 pool->process_prepared_mapping = process_prepared_mapping_fail;
1373 pool->process_prepared_discard = process_prepared_discard_fail;
1374 break;
1375
1376 case PM_READ_ONLY:
1377 DMERR("switching pool to read-only mode");
1378 r = dm_pool_abort_metadata(pool->pmd);
1379 if (r) {
1380 DMERR("aborting transaction failed");
1381 set_pool_mode(pool, PM_FAIL);
1382 } else {
1383 dm_pool_metadata_read_only(pool->pmd);
1384 pool->process_bio = process_bio_read_only;
1385 pool->process_discard = process_discard;
1386 pool->process_prepared_mapping = process_prepared_mapping_fail;
1387 pool->process_prepared_discard = process_prepared_discard_passdown;
1388 }
1389 break;
1390
1391 case PM_WRITE:
1392 pool->process_bio = process_bio;
1393 pool->process_discard = process_discard;
1394 pool->process_prepared_mapping = process_prepared_mapping;
1395 pool->process_prepared_discard = process_prepared_discard;
1396 break;
1397 }
1398}
1399
1400/*----------------------------------------------------------------*/
1401
Joe Thornber991d9fa2011-10-31 20:21:18 +00001402/*
1403 * Mapping functions.
1404 */
1405
1406/*
1407 * Called only while mapping a thin bio to hand it over to the workqueue.
1408 */
1409static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
1410{
1411 unsigned long flags;
1412 struct pool *pool = tc->pool;
1413
1414 spin_lock_irqsave(&pool->lock, flags);
1415 bio_list_add(&pool->deferred_bios, bio);
1416 spin_unlock_irqrestore(&pool->lock, flags);
1417
1418 wake_worker(pool);
1419}
1420
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001421static void thin_hook_bio(struct thin_c *tc, struct bio *bio)
Joe Thornbereb2aa482012-03-28 18:41:28 +01001422{
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001423 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01001424
1425 h->tc = tc;
1426 h->shared_read_entry = NULL;
Joe Thornbere8088072012-12-21 20:23:31 +00001427 h->all_io_entry = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01001428 h->overwrite_mapping = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01001429}
1430
Joe Thornber991d9fa2011-10-31 20:21:18 +00001431/*
1432 * Non-blocking function called from the thin target's map function.
1433 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001434static int thin_bio_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001435{
1436 int r;
1437 struct thin_c *tc = ti->private;
1438 dm_block_t block = get_bio_block(tc, bio);
1439 struct dm_thin_device *td = tc->td;
1440 struct dm_thin_lookup_result result;
Joe Thornbere8088072012-12-21 20:23:31 +00001441 struct dm_bio_prison_cell *cell1, *cell2;
1442 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001443
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00001444 thin_hook_bio(tc, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001445
1446 if (get_pool_mode(tc->pool) == PM_FAIL) {
1447 bio_io_error(bio);
1448 return DM_MAPIO_SUBMITTED;
1449 }
1450
Joe Thornber104655f2012-03-28 18:41:28 +01001451 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001452 thin_defer_bio(tc, bio);
1453 return DM_MAPIO_SUBMITTED;
1454 }
1455
1456 r = dm_thin_find_block(td, block, 0, &result);
1457
1458 /*
1459 * Note that we defer readahead too.
1460 */
1461 switch (r) {
1462 case 0:
1463 if (unlikely(result.shared)) {
1464 /*
1465 * We have a race condition here between the
1466 * result.shared value returned by the lookup and
1467 * snapshot creation, which may cause new
1468 * sharing.
1469 *
1470 * To avoid this always quiesce the origin before
1471 * taking the snap. You want to do this anyway to
1472 * ensure a consistent application view
1473 * (i.e. lockfs).
1474 *
1475 * More distant ancestors are irrelevant. The
1476 * shared flag will be set in their case.
1477 */
1478 thin_defer_bio(tc, bio);
Joe Thornbere8088072012-12-21 20:23:31 +00001479 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001480 }
Joe Thornbere8088072012-12-21 20:23:31 +00001481
1482 build_virtual_key(tc->td, block, &key);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001483 if (bio_detain(tc->pool, &key, bio, &cell1))
Joe Thornbere8088072012-12-21 20:23:31 +00001484 return DM_MAPIO_SUBMITTED;
1485
1486 build_data_key(tc->td, result.block, &key);
Joe Thornber6beca5e2013-03-01 22:45:50 +00001487 if (bio_detain(tc->pool, &key, bio, &cell2)) {
Joe Thornberf286ba02012-12-21 20:23:33 +00001488 cell_defer_no_holder(tc, cell1);
Joe Thornbere8088072012-12-21 20:23:31 +00001489 return DM_MAPIO_SUBMITTED;
1490 }
1491
1492 inc_all_io_entry(tc->pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001493 cell_defer_no_holder(tc, cell2);
1494 cell_defer_no_holder(tc, cell1);
Joe Thornbere8088072012-12-21 20:23:31 +00001495
1496 remap(tc, bio, result.block);
1497 return DM_MAPIO_REMAPPED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001498
1499 case -ENODATA:
Joe Thornbere49e5822012-07-27 15:08:16 +01001500 if (get_pool_mode(tc->pool) == PM_READ_ONLY) {
1501 /*
1502 * This block isn't provisioned, and we have no way
1503 * of doing so. Just error it.
1504 */
1505 bio_io_error(bio);
Joe Thornber2aab3852012-12-21 20:23:33 +00001506 return DM_MAPIO_SUBMITTED;
Joe Thornbere49e5822012-07-27 15:08:16 +01001507 }
1508 /* fall through */
1509
1510 case -EWOULDBLOCK:
Joe Thornber991d9fa2011-10-31 20:21:18 +00001511 /*
1512 * In future, the failed dm_thin_find_block above could
1513 * provide the hint to load the metadata into cache.
1514 */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001515 thin_defer_bio(tc, bio);
Joe Thornber2aab3852012-12-21 20:23:33 +00001516 return DM_MAPIO_SUBMITTED;
Joe Thornbere49e5822012-07-27 15:08:16 +01001517
1518 default:
1519 /*
1520 * Must always call bio_io_error on failure.
1521 * dm_thin_find_block can fail with -EINVAL if the
1522 * pool is switched to fail-io mode.
1523 */
1524 bio_io_error(bio);
Joe Thornber2aab3852012-12-21 20:23:33 +00001525 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001526 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001527}
1528
1529static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1530{
1531 int r;
1532 unsigned long flags;
1533 struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
1534
1535 spin_lock_irqsave(&pt->pool->lock, flags);
1536 r = !bio_list_empty(&pt->pool->retry_on_resume_list);
1537 spin_unlock_irqrestore(&pt->pool->lock, flags);
1538
1539 if (!r) {
1540 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
1541 r = bdi_congested(&q->backing_dev_info, bdi_bits);
1542 }
1543
1544 return r;
1545}
1546
1547static void __requeue_bios(struct pool *pool)
1548{
1549 bio_list_merge(&pool->deferred_bios, &pool->retry_on_resume_list);
1550 bio_list_init(&pool->retry_on_resume_list);
1551}
1552
1553/*----------------------------------------------------------------
1554 * Binding of control targets to a pool object
1555 *--------------------------------------------------------------*/
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001556static bool data_dev_supports_discard(struct pool_c *pt)
1557{
1558 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
1559
1560 return q && blk_queue_discard(q);
1561}
1562
1563/*
1564 * If discard_passdown was enabled verify that the data device
Mike Snitzer0424caa2012-09-26 23:45:47 +01001565 * supports discards. Disable discard_passdown if not.
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001566 */
Mike Snitzer0424caa2012-09-26 23:45:47 +01001567static void disable_passdown_if_not_supported(struct pool_c *pt)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001568{
Mike Snitzer0424caa2012-09-26 23:45:47 +01001569 struct pool *pool = pt->pool;
1570 struct block_device *data_bdev = pt->data_dev->bdev;
1571 struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
1572 sector_t block_size = pool->sectors_per_block << SECTOR_SHIFT;
1573 const char *reason = NULL;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001574 char buf[BDEVNAME_SIZE];
1575
Mike Snitzer0424caa2012-09-26 23:45:47 +01001576 if (!pt->adjusted_pf.discard_passdown)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001577 return;
1578
Mike Snitzer0424caa2012-09-26 23:45:47 +01001579 if (!data_dev_supports_discard(pt))
1580 reason = "discard unsupported";
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001581
Mike Snitzer0424caa2012-09-26 23:45:47 +01001582 else if (data_limits->max_discard_sectors < pool->sectors_per_block)
1583 reason = "max discard sectors smaller than a block";
1584
1585 else if (data_limits->discard_granularity > block_size)
1586 reason = "discard granularity larger than a block";
1587
1588 else if (block_size & (data_limits->discard_granularity - 1))
1589 reason = "discard granularity not a factor of block size";
1590
1591 if (reason) {
1592 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
1593 pt->adjusted_pf.discard_passdown = false;
1594 }
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001595}
1596
Joe Thornber991d9fa2011-10-31 20:21:18 +00001597static int bind_control_target(struct pool *pool, struct dm_target *ti)
1598{
1599 struct pool_c *pt = ti->private;
1600
Joe Thornbere49e5822012-07-27 15:08:16 +01001601 /*
1602 * We want to make sure that degraded pools are never upgraded.
1603 */
1604 enum pool_mode old_mode = pool->pf.mode;
Mike Snitzer0424caa2012-09-26 23:45:47 +01001605 enum pool_mode new_mode = pt->adjusted_pf.mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01001606
1607 if (old_mode > new_mode)
1608 new_mode = old_mode;
1609
Joe Thornber991d9fa2011-10-31 20:21:18 +00001610 pool->ti = ti;
1611 pool->low_water_blocks = pt->low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +01001612 pool->pf = pt->adjusted_pf;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001613
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001614 set_pool_mode(pool, new_mode);
Mike Snitzerf4026932012-05-19 01:01:01 +01001615
Joe Thornber991d9fa2011-10-31 20:21:18 +00001616 return 0;
1617}
1618
1619static void unbind_control_target(struct pool *pool, struct dm_target *ti)
1620{
1621 if (pool->ti == ti)
1622 pool->ti = NULL;
1623}
1624
1625/*----------------------------------------------------------------
1626 * Pool creation
1627 *--------------------------------------------------------------*/
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001628/* Initialize pool features. */
1629static void pool_features_init(struct pool_features *pf)
1630{
Joe Thornbere49e5822012-07-27 15:08:16 +01001631 pf->mode = PM_WRITE;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001632 pf->zero_new_blocks = true;
1633 pf->discard_enabled = true;
1634 pf->discard_passdown = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001635}
1636
Joe Thornber991d9fa2011-10-31 20:21:18 +00001637static void __pool_destroy(struct pool *pool)
1638{
1639 __pool_table_remove(pool);
1640
1641 if (dm_pool_metadata_close(pool->pmd) < 0)
1642 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
1643
Mike Snitzer44feb382012-10-12 21:02:10 +01001644 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001645 dm_kcopyd_client_destroy(pool->copier);
1646
1647 if (pool->wq)
1648 destroy_workqueue(pool->wq);
1649
1650 if (pool->next_mapping)
1651 mempool_free(pool->next_mapping, pool->mapping_pool);
1652 mempool_destroy(pool->mapping_pool);
Mike Snitzer44feb382012-10-12 21:02:10 +01001653 dm_deferred_set_destroy(pool->shared_read_ds);
1654 dm_deferred_set_destroy(pool->all_io_ds);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001655 kfree(pool);
1656}
1657
Mike Snitzera24c2562012-06-03 00:30:00 +01001658static struct kmem_cache *_new_mapping_cache;
Mike Snitzera24c2562012-06-03 00:30:00 +01001659
Joe Thornber991d9fa2011-10-31 20:21:18 +00001660static struct pool *pool_create(struct mapped_device *pool_md,
1661 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01001662 unsigned long block_size,
1663 int read_only, char **error)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001664{
1665 int r;
1666 void *err_p;
1667 struct pool *pool;
1668 struct dm_pool_metadata *pmd;
Joe Thornbere49e5822012-07-27 15:08:16 +01001669 bool format_device = read_only ? false : true;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001670
Joe Thornbere49e5822012-07-27 15:08:16 +01001671 pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001672 if (IS_ERR(pmd)) {
1673 *error = "Error creating metadata object";
1674 return (struct pool *)pmd;
1675 }
1676
1677 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
1678 if (!pool) {
1679 *error = "Error allocating memory for pool";
1680 err_p = ERR_PTR(-ENOMEM);
1681 goto bad_pool;
1682 }
1683
1684 pool->pmd = pmd;
1685 pool->sectors_per_block = block_size;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +01001686 if (block_size & (block_size - 1))
1687 pool->sectors_per_block_shift = -1;
1688 else
1689 pool->sectors_per_block_shift = __ffs(block_size);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001690 pool->low_water_blocks = 0;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001691 pool_features_init(&pool->pf);
Mike Snitzer44feb382012-10-12 21:02:10 +01001692 pool->prison = dm_bio_prison_create(PRISON_CELLS);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001693 if (!pool->prison) {
1694 *error = "Error creating pool's bio prison";
1695 err_p = ERR_PTR(-ENOMEM);
1696 goto bad_prison;
1697 }
1698
Mikulas Patockadf5d2e92013-03-01 22:45:49 +00001699 pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001700 if (IS_ERR(pool->copier)) {
1701 r = PTR_ERR(pool->copier);
1702 *error = "Error creating pool's kcopyd client";
1703 err_p = ERR_PTR(r);
1704 goto bad_kcopyd_client;
1705 }
1706
1707 /*
1708 * Create singlethreaded workqueue that will service all devices
1709 * that use this metadata.
1710 */
1711 pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
1712 if (!pool->wq) {
1713 *error = "Error creating pool's workqueue";
1714 err_p = ERR_PTR(-ENOMEM);
1715 goto bad_wq;
1716 }
1717
1718 INIT_WORK(&pool->worker, do_worker);
Joe Thornber905e51b2012-03-28 18:41:27 +01001719 INIT_DELAYED_WORK(&pool->waker, do_waker);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001720 spin_lock_init(&pool->lock);
1721 bio_list_init(&pool->deferred_bios);
1722 bio_list_init(&pool->deferred_flush_bios);
1723 INIT_LIST_HEAD(&pool->prepared_mappings);
Joe Thornber104655f2012-03-28 18:41:28 +01001724 INIT_LIST_HEAD(&pool->prepared_discards);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001725 pool->low_water_triggered = 0;
1726 pool->no_free_space = 0;
1727 bio_list_init(&pool->retry_on_resume_list);
Mike Snitzer44feb382012-10-12 21:02:10 +01001728
1729 pool->shared_read_ds = dm_deferred_set_create();
1730 if (!pool->shared_read_ds) {
1731 *error = "Error creating pool's shared read deferred set";
1732 err_p = ERR_PTR(-ENOMEM);
1733 goto bad_shared_read_ds;
1734 }
1735
1736 pool->all_io_ds = dm_deferred_set_create();
1737 if (!pool->all_io_ds) {
1738 *error = "Error creating pool's all io deferred set";
1739 err_p = ERR_PTR(-ENOMEM);
1740 goto bad_all_io_ds;
1741 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001742
1743 pool->next_mapping = NULL;
Mike Snitzera24c2562012-06-03 00:30:00 +01001744 pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
1745 _new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001746 if (!pool->mapping_pool) {
1747 *error = "Error creating pool's mapping mempool";
1748 err_p = ERR_PTR(-ENOMEM);
1749 goto bad_mapping_pool;
1750 }
1751
Joe Thornber991d9fa2011-10-31 20:21:18 +00001752 pool->ref_count = 1;
Joe Thornber905e51b2012-03-28 18:41:27 +01001753 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001754 pool->pool_md = pool_md;
1755 pool->md_dev = metadata_dev;
1756 __pool_table_insert(pool);
1757
1758 return pool;
1759
Joe Thornber991d9fa2011-10-31 20:21:18 +00001760bad_mapping_pool:
Mike Snitzer44feb382012-10-12 21:02:10 +01001761 dm_deferred_set_destroy(pool->all_io_ds);
1762bad_all_io_ds:
1763 dm_deferred_set_destroy(pool->shared_read_ds);
1764bad_shared_read_ds:
Joe Thornber991d9fa2011-10-31 20:21:18 +00001765 destroy_workqueue(pool->wq);
1766bad_wq:
1767 dm_kcopyd_client_destroy(pool->copier);
1768bad_kcopyd_client:
Mike Snitzer44feb382012-10-12 21:02:10 +01001769 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001770bad_prison:
1771 kfree(pool);
1772bad_pool:
1773 if (dm_pool_metadata_close(pmd))
1774 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
1775
1776 return err_p;
1777}
1778
1779static void __pool_inc(struct pool *pool)
1780{
1781 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
1782 pool->ref_count++;
1783}
1784
1785static void __pool_dec(struct pool *pool)
1786{
1787 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
1788 BUG_ON(!pool->ref_count);
1789 if (!--pool->ref_count)
1790 __pool_destroy(pool);
1791}
1792
1793static struct pool *__pool_find(struct mapped_device *pool_md,
1794 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01001795 unsigned long block_size, int read_only,
1796 char **error, int *created)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001797{
1798 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
1799
1800 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01001801 if (pool->pool_md != pool_md) {
1802 *error = "metadata device already in use by a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00001803 return ERR_PTR(-EBUSY);
Mike Snitzerf09996c2012-07-27 15:07:59 +01001804 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001805 __pool_inc(pool);
1806
1807 } else {
1808 pool = __pool_table_lookup(pool_md);
1809 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01001810 if (pool->md_dev != metadata_dev) {
1811 *error = "different pool cannot replace a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00001812 return ERR_PTR(-EINVAL);
Mike Snitzerf09996c2012-07-27 15:07:59 +01001813 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001814 __pool_inc(pool);
1815
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001816 } else {
Joe Thornbere49e5822012-07-27 15:08:16 +01001817 pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001818 *created = 1;
1819 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001820 }
1821
1822 return pool;
1823}
1824
1825/*----------------------------------------------------------------
1826 * Pool target methods
1827 *--------------------------------------------------------------*/
1828static void pool_dtr(struct dm_target *ti)
1829{
1830 struct pool_c *pt = ti->private;
1831
1832 mutex_lock(&dm_thin_pool_table.mutex);
1833
1834 unbind_control_target(pt->pool, ti);
1835 __pool_dec(pt->pool);
1836 dm_put_device(ti, pt->metadata_dev);
1837 dm_put_device(ti, pt->data_dev);
1838 kfree(pt);
1839
1840 mutex_unlock(&dm_thin_pool_table.mutex);
1841}
1842
Joe Thornber991d9fa2011-10-31 20:21:18 +00001843static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
1844 struct dm_target *ti)
1845{
1846 int r;
1847 unsigned argc;
1848 const char *arg_name;
1849
1850 static struct dm_arg _args[] = {
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001851 {0, 3, "Invalid number of pool feature arguments"},
Joe Thornber991d9fa2011-10-31 20:21:18 +00001852 };
1853
1854 /*
1855 * No feature arguments supplied.
1856 */
1857 if (!as->argc)
1858 return 0;
1859
1860 r = dm_read_arg_group(_args, as, &argc, &ti->error);
1861 if (r)
1862 return -EINVAL;
1863
1864 while (argc && !r) {
1865 arg_name = dm_shift_arg(as);
1866 argc--;
1867
Joe Thornbere49e5822012-07-27 15:08:16 +01001868 if (!strcasecmp(arg_name, "skip_block_zeroing"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001869 pf->zero_new_blocks = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001870
Joe Thornbere49e5822012-07-27 15:08:16 +01001871 else if (!strcasecmp(arg_name, "ignore_discard"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001872 pf->discard_enabled = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01001873
1874 else if (!strcasecmp(arg_name, "no_discard_passdown"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001875 pf->discard_passdown = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01001876
1877 else if (!strcasecmp(arg_name, "read_only"))
1878 pf->mode = PM_READ_ONLY;
1879
1880 else {
1881 ti->error = "Unrecognised pool feature requested";
1882 r = -EINVAL;
1883 break;
1884 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001885 }
1886
1887 return r;
1888}
1889
1890/*
1891 * thin-pool <metadata dev> <data dev>
1892 * <data block size (sectors)>
1893 * <low water mark (blocks)>
1894 * [<#feature args> [<arg>]*]
1895 *
1896 * Optional feature arguments are:
1897 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001898 * ignore_discard: disable discard
1899 * no_discard_passdown: don't pass discards down to the data device
Joe Thornber991d9fa2011-10-31 20:21:18 +00001900 */
1901static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
1902{
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001903 int r, pool_created = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001904 struct pool_c *pt;
1905 struct pool *pool;
1906 struct pool_features pf;
1907 struct dm_arg_set as;
1908 struct dm_dev *data_dev;
1909 unsigned long block_size;
1910 dm_block_t low_water_blocks;
1911 struct dm_dev *metadata_dev;
1912 sector_t metadata_dev_size;
Mike Snitzerc4a69ec2012-03-28 18:41:28 +01001913 char b[BDEVNAME_SIZE];
Joe Thornber991d9fa2011-10-31 20:21:18 +00001914
1915 /*
1916 * FIXME Remove validation from scope of lock.
1917 */
1918 mutex_lock(&dm_thin_pool_table.mutex);
1919
1920 if (argc < 4) {
1921 ti->error = "Invalid argument count";
1922 r = -EINVAL;
1923 goto out_unlock;
1924 }
1925 as.argc = argc;
1926 as.argv = argv;
1927
1928 r = dm_get_device(ti, argv[0], FMODE_READ | FMODE_WRITE, &metadata_dev);
1929 if (r) {
1930 ti->error = "Error opening metadata block device";
1931 goto out_unlock;
1932 }
1933
1934 metadata_dev_size = i_size_read(metadata_dev->bdev->bd_inode) >> SECTOR_SHIFT;
Mike Snitzerc4a69ec2012-03-28 18:41:28 +01001935 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
1936 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
1937 bdevname(metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001938
1939 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
1940 if (r) {
1941 ti->error = "Error getting data device";
1942 goto out_metadata;
1943 }
1944
1945 if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
1946 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
1947 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01001948 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001949 ti->error = "Invalid block size";
1950 r = -EINVAL;
1951 goto out;
1952 }
1953
1954 if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
1955 ti->error = "Invalid low water mark";
1956 r = -EINVAL;
1957 goto out;
1958 }
1959
1960 /*
1961 * Set default pool features.
1962 */
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001963 pool_features_init(&pf);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001964
1965 dm_consume_args(&as, 4);
1966 r = parse_pool_features(&as, &pf, ti);
1967 if (r)
1968 goto out;
1969
1970 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
1971 if (!pt) {
1972 r = -ENOMEM;
1973 goto out;
1974 }
1975
1976 pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
Joe Thornbere49e5822012-07-27 15:08:16 +01001977 block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001978 if (IS_ERR(pool)) {
1979 r = PTR_ERR(pool);
1980 goto out_free_pt;
1981 }
1982
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001983 /*
1984 * 'pool_created' reflects whether this is the first table load.
1985 * Top level discard support is not allowed to be changed after
1986 * initial load. This would require a pool reload to trigger thin
1987 * device changes.
1988 */
1989 if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
1990 ti->error = "Discard support cannot be disabled once enabled";
1991 r = -EINVAL;
1992 goto out_flags_changed;
1993 }
1994
Joe Thornber991d9fa2011-10-31 20:21:18 +00001995 pt->pool = pool;
1996 pt->ti = ti;
1997 pt->metadata_dev = metadata_dev;
1998 pt->data_dev = data_dev;
1999 pt->low_water_blocks = low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +01002000 pt->adjusted_pf = pt->requested_pf = pf;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002001 ti->num_flush_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002002
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002003 /*
2004 * Only need to enable discards if the pool should pass
2005 * them down to the data device. The thin device's discard
2006 * processing will cause mappings to be removed from the btree.
2007 */
2008 if (pf.discard_enabled && pf.discard_passdown) {
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002009 ti->num_discard_bios = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01002010
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002011 /*
2012 * Setting 'discards_supported' circumvents the normal
2013 * stacking of discard limits (this keeps the pool and
2014 * thin devices' discard limits consistent).
2015 */
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01002016 ti->discards_supported = true;
Mike Snitzer307615a2012-09-26 23:45:39 +01002017 ti->discard_zeroes_data_unsupported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002018 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002019 ti->private = pt;
2020
2021 pt->callbacks.congested_fn = pool_is_congested;
2022 dm_table_add_target_callbacks(ti->table, &pt->callbacks);
2023
2024 mutex_unlock(&dm_thin_pool_table.mutex);
2025
2026 return 0;
2027
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002028out_flags_changed:
2029 __pool_dec(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002030out_free_pt:
2031 kfree(pt);
2032out:
2033 dm_put_device(ti, data_dev);
2034out_metadata:
2035 dm_put_device(ti, metadata_dev);
2036out_unlock:
2037 mutex_unlock(&dm_thin_pool_table.mutex);
2038
2039 return r;
2040}
2041
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002042static int pool_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002043{
2044 int r;
2045 struct pool_c *pt = ti->private;
2046 struct pool *pool = pt->pool;
2047 unsigned long flags;
2048
2049 /*
2050 * As this is a singleton target, ti->begin is always zero.
2051 */
2052 spin_lock_irqsave(&pool->lock, flags);
2053 bio->bi_bdev = pt->data_dev->bdev;
2054 r = DM_MAPIO_REMAPPED;
2055 spin_unlock_irqrestore(&pool->lock, flags);
2056
2057 return r;
2058}
2059
2060/*
2061 * Retrieves the number of blocks of the data device from
2062 * the superblock and compares it to the actual device size,
2063 * thus resizing the data device in case it has grown.
2064 *
2065 * This both copes with opening preallocated data devices in the ctr
2066 * being followed by a resume
2067 * -and-
2068 * calling the resume method individually after userspace has
2069 * grown the data device in reaction to a table event.
2070 */
2071static int pool_preresume(struct dm_target *ti)
2072{
2073 int r;
2074 struct pool_c *pt = ti->private;
2075 struct pool *pool = pt->pool;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002076 sector_t data_size = ti->len;
2077 dm_block_t sb_data_size;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002078
2079 /*
2080 * Take control of the pool object.
2081 */
2082 r = bind_control_target(pool, ti);
2083 if (r)
2084 return r;
2085
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002086 (void) sector_div(data_size, pool->sectors_per_block);
2087
Joe Thornber991d9fa2011-10-31 20:21:18 +00002088 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
2089 if (r) {
2090 DMERR("failed to retrieve data device size");
2091 return r;
2092 }
2093
2094 if (data_size < sb_data_size) {
2095 DMERR("pool target too small, is %llu blocks (expected %llu)",
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002096 (unsigned long long)data_size, sb_data_size);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002097 return -EINVAL;
2098
2099 } else if (data_size > sb_data_size) {
2100 r = dm_pool_resize_data_dev(pool->pmd, data_size);
2101 if (r) {
2102 DMERR("failed to resize data device");
Joe Thornbere49e5822012-07-27 15:08:16 +01002103 /* FIXME Stricter than necessary: Rollback transaction instead here */
2104 set_pool_mode(pool, PM_READ_ONLY);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002105 return r;
2106 }
2107
Joe Thornbere49e5822012-07-27 15:08:16 +01002108 (void) commit_or_fallback(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002109 }
2110
2111 return 0;
2112}
2113
2114static void pool_resume(struct dm_target *ti)
2115{
2116 struct pool_c *pt = ti->private;
2117 struct pool *pool = pt->pool;
2118 unsigned long flags;
2119
2120 spin_lock_irqsave(&pool->lock, flags);
2121 pool->low_water_triggered = 0;
2122 pool->no_free_space = 0;
2123 __requeue_bios(pool);
2124 spin_unlock_irqrestore(&pool->lock, flags);
2125
Joe Thornber905e51b2012-03-28 18:41:27 +01002126 do_waker(&pool->waker.work);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002127}
2128
2129static void pool_postsuspend(struct dm_target *ti)
2130{
Joe Thornber991d9fa2011-10-31 20:21:18 +00002131 struct pool_c *pt = ti->private;
2132 struct pool *pool = pt->pool;
2133
Joe Thornber905e51b2012-03-28 18:41:27 +01002134 cancel_delayed_work(&pool->waker);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002135 flush_workqueue(pool->wq);
Joe Thornbere49e5822012-07-27 15:08:16 +01002136 (void) commit_or_fallback(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002137}
2138
2139static int check_arg_count(unsigned argc, unsigned args_required)
2140{
2141 if (argc != args_required) {
2142 DMWARN("Message received with %u arguments instead of %u.",
2143 argc, args_required);
2144 return -EINVAL;
2145 }
2146
2147 return 0;
2148}
2149
2150static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
2151{
2152 if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
2153 *dev_id <= MAX_DEV_ID)
2154 return 0;
2155
2156 if (warning)
2157 DMWARN("Message received with invalid device id: %s", arg);
2158
2159 return -EINVAL;
2160}
2161
2162static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
2163{
2164 dm_thin_id dev_id;
2165 int r;
2166
2167 r = check_arg_count(argc, 2);
2168 if (r)
2169 return r;
2170
2171 r = read_dev_id(argv[1], &dev_id, 1);
2172 if (r)
2173 return r;
2174
2175 r = dm_pool_create_thin(pool->pmd, dev_id);
2176 if (r) {
2177 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
2178 argv[1]);
2179 return r;
2180 }
2181
2182 return 0;
2183}
2184
2185static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2186{
2187 dm_thin_id dev_id;
2188 dm_thin_id origin_dev_id;
2189 int r;
2190
2191 r = check_arg_count(argc, 3);
2192 if (r)
2193 return r;
2194
2195 r = read_dev_id(argv[1], &dev_id, 1);
2196 if (r)
2197 return r;
2198
2199 r = read_dev_id(argv[2], &origin_dev_id, 1);
2200 if (r)
2201 return r;
2202
2203 r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
2204 if (r) {
2205 DMWARN("Creation of new snapshot %s of device %s failed.",
2206 argv[1], argv[2]);
2207 return r;
2208 }
2209
2210 return 0;
2211}
2212
2213static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
2214{
2215 dm_thin_id dev_id;
2216 int r;
2217
2218 r = check_arg_count(argc, 2);
2219 if (r)
2220 return r;
2221
2222 r = read_dev_id(argv[1], &dev_id, 1);
2223 if (r)
2224 return r;
2225
2226 r = dm_pool_delete_thin_device(pool->pmd, dev_id);
2227 if (r)
2228 DMWARN("Deletion of thin device %s failed.", argv[1]);
2229
2230 return r;
2231}
2232
2233static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
2234{
2235 dm_thin_id old_id, new_id;
2236 int r;
2237
2238 r = check_arg_count(argc, 3);
2239 if (r)
2240 return r;
2241
2242 if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
2243 DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
2244 return -EINVAL;
2245 }
2246
2247 if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
2248 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
2249 return -EINVAL;
2250 }
2251
2252 r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
2253 if (r) {
2254 DMWARN("Failed to change transaction id from %s to %s.",
2255 argv[1], argv[2]);
2256 return r;
2257 }
2258
2259 return 0;
2260}
2261
Joe Thornbercc8394d2012-06-03 00:30:01 +01002262static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2263{
2264 int r;
2265
2266 r = check_arg_count(argc, 1);
2267 if (r)
2268 return r;
2269
Joe Thornbere49e5822012-07-27 15:08:16 +01002270 (void) commit_or_fallback(pool);
Joe Thornber0d200ae2012-07-03 12:55:31 +01002271
Joe Thornbercc8394d2012-06-03 00:30:01 +01002272 r = dm_pool_reserve_metadata_snap(pool->pmd);
2273 if (r)
2274 DMWARN("reserve_metadata_snap message failed.");
2275
2276 return r;
2277}
2278
2279static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2280{
2281 int r;
2282
2283 r = check_arg_count(argc, 1);
2284 if (r)
2285 return r;
2286
2287 r = dm_pool_release_metadata_snap(pool->pmd);
2288 if (r)
2289 DMWARN("release_metadata_snap message failed.");
2290
2291 return r;
2292}
2293
Joe Thornber991d9fa2011-10-31 20:21:18 +00002294/*
2295 * Messages supported:
2296 * create_thin <dev_id>
2297 * create_snap <dev_id> <origin_id>
2298 * delete <dev_id>
2299 * trim <dev_id> <new_size_in_sectors>
2300 * set_transaction_id <current_trans_id> <new_trans_id>
Joe Thornbercc8394d2012-06-03 00:30:01 +01002301 * reserve_metadata_snap
2302 * release_metadata_snap
Joe Thornber991d9fa2011-10-31 20:21:18 +00002303 */
2304static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
2305{
2306 int r = -EINVAL;
2307 struct pool_c *pt = ti->private;
2308 struct pool *pool = pt->pool;
2309
2310 if (!strcasecmp(argv[0], "create_thin"))
2311 r = process_create_thin_mesg(argc, argv, pool);
2312
2313 else if (!strcasecmp(argv[0], "create_snap"))
2314 r = process_create_snap_mesg(argc, argv, pool);
2315
2316 else if (!strcasecmp(argv[0], "delete"))
2317 r = process_delete_mesg(argc, argv, pool);
2318
2319 else if (!strcasecmp(argv[0], "set_transaction_id"))
2320 r = process_set_transaction_id_mesg(argc, argv, pool);
2321
Joe Thornbercc8394d2012-06-03 00:30:01 +01002322 else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
2323 r = process_reserve_metadata_snap_mesg(argc, argv, pool);
2324
2325 else if (!strcasecmp(argv[0], "release_metadata_snap"))
2326 r = process_release_metadata_snap_mesg(argc, argv, pool);
2327
Joe Thornber991d9fa2011-10-31 20:21:18 +00002328 else
2329 DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
2330
Joe Thornbere49e5822012-07-27 15:08:16 +01002331 if (!r)
2332 (void) commit_or_fallback(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002333
2334 return r;
2335}
2336
Joe Thornbere49e5822012-07-27 15:08:16 +01002337static void emit_flags(struct pool_features *pf, char *result,
2338 unsigned sz, unsigned maxlen)
2339{
2340 unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
2341 !pf->discard_passdown + (pf->mode == PM_READ_ONLY);
2342 DMEMIT("%u ", count);
2343
2344 if (!pf->zero_new_blocks)
2345 DMEMIT("skip_block_zeroing ");
2346
2347 if (!pf->discard_enabled)
2348 DMEMIT("ignore_discard ");
2349
2350 if (!pf->discard_passdown)
2351 DMEMIT("no_discard_passdown ");
2352
2353 if (pf->mode == PM_READ_ONLY)
2354 DMEMIT("read_only ");
2355}
2356
Joe Thornber991d9fa2011-10-31 20:21:18 +00002357/*
2358 * Status line is:
2359 * <transaction id> <used metadata sectors>/<total metadata sectors>
2360 * <used data sectors>/<total data sectors> <held metadata root>
2361 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002362static void pool_status(struct dm_target *ti, status_type_t type,
2363 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002364{
Joe Thornbere49e5822012-07-27 15:08:16 +01002365 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002366 unsigned sz = 0;
2367 uint64_t transaction_id;
2368 dm_block_t nr_free_blocks_data;
2369 dm_block_t nr_free_blocks_metadata;
2370 dm_block_t nr_blocks_data;
2371 dm_block_t nr_blocks_metadata;
2372 dm_block_t held_root;
2373 char buf[BDEVNAME_SIZE];
2374 char buf2[BDEVNAME_SIZE];
2375 struct pool_c *pt = ti->private;
2376 struct pool *pool = pt->pool;
2377
2378 switch (type) {
2379 case STATUSTYPE_INFO:
Joe Thornbere49e5822012-07-27 15:08:16 +01002380 if (get_pool_mode(pool) == PM_FAIL) {
2381 DMEMIT("Fail");
2382 break;
2383 }
2384
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01002385 /* Commit to ensure statistics aren't out-of-date */
2386 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
2387 (void) commit_or_fallback(pool);
2388
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002389 r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id);
2390 if (r) {
2391 DMERR("dm_pool_get_metadata_transaction_id returned %d", r);
2392 goto err;
2393 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002394
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002395 r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata);
2396 if (r) {
2397 DMERR("dm_pool_get_free_metadata_block_count returned %d", r);
2398 goto err;
2399 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002400
2401 r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002402 if (r) {
2403 DMERR("dm_pool_get_metadata_dev_size returned %d", r);
2404 goto err;
2405 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002406
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002407 r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data);
2408 if (r) {
2409 DMERR("dm_pool_get_free_block_count returned %d", r);
2410 goto err;
2411 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002412
2413 r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002414 if (r) {
2415 DMERR("dm_pool_get_data_dev_size returned %d", r);
2416 goto err;
2417 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002418
Joe Thornbercc8394d2012-06-03 00:30:01 +01002419 r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002420 if (r) {
2421 DMERR("dm_pool_get_metadata_snap returned %d", r);
2422 goto err;
2423 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002424
2425 DMEMIT("%llu %llu/%llu %llu/%llu ",
2426 (unsigned long long)transaction_id,
2427 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
2428 (unsigned long long)nr_blocks_metadata,
2429 (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
2430 (unsigned long long)nr_blocks_data);
2431
2432 if (held_root)
Joe Thornbere49e5822012-07-27 15:08:16 +01002433 DMEMIT("%llu ", held_root);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002434 else
Joe Thornbere49e5822012-07-27 15:08:16 +01002435 DMEMIT("- ");
2436
2437 if (pool->pf.mode == PM_READ_ONLY)
2438 DMEMIT("ro ");
2439 else
2440 DMEMIT("rw ");
2441
Mike Snitzer018debe2012-12-21 20:23:32 +00002442 if (!pool->pf.discard_enabled)
2443 DMEMIT("ignore_discard");
2444 else if (pool->pf.discard_passdown)
Joe Thornbere49e5822012-07-27 15:08:16 +01002445 DMEMIT("discard_passdown");
2446 else
2447 DMEMIT("no_discard_passdown");
Joe Thornber991d9fa2011-10-31 20:21:18 +00002448
2449 break;
2450
2451 case STATUSTYPE_TABLE:
2452 DMEMIT("%s %s %lu %llu ",
2453 format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
2454 format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
2455 (unsigned long)pool->sectors_per_block,
2456 (unsigned long long)pt->low_water_blocks);
Mike Snitzer0424caa2012-09-26 23:45:47 +01002457 emit_flags(&pt->requested_pf, result, sz, maxlen);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002458 break;
2459 }
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002460 return;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002461
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002462err:
2463 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00002464}
2465
2466static int pool_iterate_devices(struct dm_target *ti,
2467 iterate_devices_callout_fn fn, void *data)
2468{
2469 struct pool_c *pt = ti->private;
2470
2471 return fn(ti, pt->data_dev, 0, ti->len, data);
2472}
2473
2474static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
2475 struct bio_vec *biovec, int max_size)
2476{
2477 struct pool_c *pt = ti->private;
2478 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
2479
2480 if (!q->merge_bvec_fn)
2481 return max_size;
2482
2483 bvm->bi_bdev = pt->data_dev->bdev;
2484
2485 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
2486}
2487
Mike Snitzer0424caa2012-09-26 23:45:47 +01002488static void set_discard_limits(struct pool_c *pt, struct queue_limits *limits)
Joe Thornber104655f2012-03-28 18:41:28 +01002489{
Mike Snitzer0424caa2012-09-26 23:45:47 +01002490 struct pool *pool = pt->pool;
2491 struct queue_limits *data_limits;
2492
Joe Thornber104655f2012-03-28 18:41:28 +01002493 limits->max_discard_sectors = pool->sectors_per_block;
2494
2495 /*
Mike Snitzer0424caa2012-09-26 23:45:47 +01002496 * discard_granularity is just a hint, and not enforced.
Joe Thornber104655f2012-03-28 18:41:28 +01002497 */
Mike Snitzer0424caa2012-09-26 23:45:47 +01002498 if (pt->adjusted_pf.discard_passdown) {
2499 data_limits = &bdev_get_queue(pt->data_dev->bdev)->limits;
2500 limits->discard_granularity = data_limits->discard_granularity;
Mike Snitzerf13945d2013-03-01 22:45:44 +00002501 } else
Mike Snitzer0424caa2012-09-26 23:45:47 +01002502 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
Joe Thornber104655f2012-03-28 18:41:28 +01002503}
2504
Joe Thornber991d9fa2011-10-31 20:21:18 +00002505static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
2506{
2507 struct pool_c *pt = ti->private;
2508 struct pool *pool = pt->pool;
2509
2510 blk_limits_io_min(limits, 0);
2511 blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
Mike Snitzer0424caa2012-09-26 23:45:47 +01002512
2513 /*
2514 * pt->adjusted_pf is a staging area for the actual features to use.
2515 * They get transferred to the live pool in bind_control_target()
2516 * called from pool_preresume().
2517 */
2518 if (!pt->adjusted_pf.discard_enabled)
2519 return;
2520
2521 disable_passdown_if_not_supported(pt);
2522
2523 set_discard_limits(pt, limits);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002524}
2525
2526static struct target_type pool_target = {
2527 .name = "thin-pool",
2528 .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
2529 DM_TARGET_IMMUTABLE,
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002530 .version = {1, 6, 1},
Joe Thornber991d9fa2011-10-31 20:21:18 +00002531 .module = THIS_MODULE,
2532 .ctr = pool_ctr,
2533 .dtr = pool_dtr,
2534 .map = pool_map,
2535 .postsuspend = pool_postsuspend,
2536 .preresume = pool_preresume,
2537 .resume = pool_resume,
2538 .message = pool_message,
2539 .status = pool_status,
2540 .merge = pool_merge,
2541 .iterate_devices = pool_iterate_devices,
2542 .io_hints = pool_io_hints,
2543};
2544
2545/*----------------------------------------------------------------
2546 * Thin target methods
2547 *--------------------------------------------------------------*/
2548static void thin_dtr(struct dm_target *ti)
2549{
2550 struct thin_c *tc = ti->private;
2551
2552 mutex_lock(&dm_thin_pool_table.mutex);
2553
2554 __pool_dec(tc->pool);
2555 dm_pool_close_thin_device(tc->td);
2556 dm_put_device(ti, tc->pool_dev);
Joe Thornber2dd9c252012-03-28 18:41:28 +01002557 if (tc->origin_dev)
2558 dm_put_device(ti, tc->origin_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002559 kfree(tc);
2560
2561 mutex_unlock(&dm_thin_pool_table.mutex);
2562}
2563
2564/*
2565 * Thin target parameters:
2566 *
Joe Thornber2dd9c252012-03-28 18:41:28 +01002567 * <pool_dev> <dev_id> [origin_dev]
Joe Thornber991d9fa2011-10-31 20:21:18 +00002568 *
2569 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
2570 * dev_id: the internal device identifier
Joe Thornber2dd9c252012-03-28 18:41:28 +01002571 * origin_dev: a device external to the pool that should act as the origin
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002572 *
2573 * If the pool device has discards disabled, they get disabled for the thin
2574 * device as well.
Joe Thornber991d9fa2011-10-31 20:21:18 +00002575 */
2576static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
2577{
2578 int r;
2579 struct thin_c *tc;
Joe Thornber2dd9c252012-03-28 18:41:28 +01002580 struct dm_dev *pool_dev, *origin_dev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002581 struct mapped_device *pool_md;
2582
2583 mutex_lock(&dm_thin_pool_table.mutex);
2584
Joe Thornber2dd9c252012-03-28 18:41:28 +01002585 if (argc != 2 && argc != 3) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00002586 ti->error = "Invalid argument count";
2587 r = -EINVAL;
2588 goto out_unlock;
2589 }
2590
2591 tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
2592 if (!tc) {
2593 ti->error = "Out of memory";
2594 r = -ENOMEM;
2595 goto out_unlock;
2596 }
2597
Joe Thornber2dd9c252012-03-28 18:41:28 +01002598 if (argc == 3) {
2599 r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
2600 if (r) {
2601 ti->error = "Error opening origin device";
2602 goto bad_origin_dev;
2603 }
2604 tc->origin_dev = origin_dev;
2605 }
2606
Joe Thornber991d9fa2011-10-31 20:21:18 +00002607 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
2608 if (r) {
2609 ti->error = "Error opening pool device";
2610 goto bad_pool_dev;
2611 }
2612 tc->pool_dev = pool_dev;
2613
2614 if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
2615 ti->error = "Invalid device id";
2616 r = -EINVAL;
2617 goto bad_common;
2618 }
2619
2620 pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
2621 if (!pool_md) {
2622 ti->error = "Couldn't get pool mapped device";
2623 r = -EINVAL;
2624 goto bad_common;
2625 }
2626
2627 tc->pool = __pool_table_lookup(pool_md);
2628 if (!tc->pool) {
2629 ti->error = "Couldn't find pool object";
2630 r = -EINVAL;
2631 goto bad_pool_lookup;
2632 }
2633 __pool_inc(tc->pool);
2634
Joe Thornbere49e5822012-07-27 15:08:16 +01002635 if (get_pool_mode(tc->pool) == PM_FAIL) {
2636 ti->error = "Couldn't open thin device, Pool is in fail mode";
2637 goto bad_thin_open;
2638 }
2639
Joe Thornber991d9fa2011-10-31 20:21:18 +00002640 r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
2641 if (r) {
2642 ti->error = "Couldn't open thin internal device";
2643 goto bad_thin_open;
2644 }
2645
Mike Snitzer542f9032012-07-27 15:08:00 +01002646 r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
2647 if (r)
2648 goto bad_thin_open;
2649
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002650 ti->num_flush_bios = 1;
Joe Thornber16ad3d12012-07-27 15:08:07 +01002651 ti->flush_supported = true;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002652 ti->per_bio_data_size = sizeof(struct dm_thin_endio_hook);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002653
2654 /* In case the pool supports discards, pass them on. */
2655 if (tc->pool->pf.discard_enabled) {
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01002656 ti->discards_supported = true;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002657 ti->num_discard_bios = 1;
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01002658 ti->discard_zeroes_data_unsupported = true;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002659 /* Discard bios must be split on a block boundary */
2660 ti->split_discard_bios = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002661 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002662
2663 dm_put(pool_md);
2664
2665 mutex_unlock(&dm_thin_pool_table.mutex);
2666
2667 return 0;
2668
2669bad_thin_open:
2670 __pool_dec(tc->pool);
2671bad_pool_lookup:
2672 dm_put(pool_md);
2673bad_common:
2674 dm_put_device(ti, tc->pool_dev);
2675bad_pool_dev:
Joe Thornber2dd9c252012-03-28 18:41:28 +01002676 if (tc->origin_dev)
2677 dm_put_device(ti, tc->origin_dev);
2678bad_origin_dev:
Joe Thornber991d9fa2011-10-31 20:21:18 +00002679 kfree(tc);
2680out_unlock:
2681 mutex_unlock(&dm_thin_pool_table.mutex);
2682
2683 return r;
2684}
2685
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002686static int thin_map(struct dm_target *ti, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002687{
Alasdair G Kergon6efd6e82012-03-28 18:41:28 +01002688 bio->bi_sector = dm_target_offset(ti, bio->bi_sector);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002689
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002690 return thin_bio_map(ti, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002691}
2692
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002693static int thin_endio(struct dm_target *ti, struct bio *bio, int err)
Joe Thornbereb2aa482012-03-28 18:41:28 +01002694{
2695 unsigned long flags;
Mikulas Patocka59c3d2c2012-12-21 20:23:40 +00002696 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
Joe Thornbereb2aa482012-03-28 18:41:28 +01002697 struct list_head work;
Mike Snitzera24c2562012-06-03 00:30:00 +01002698 struct dm_thin_new_mapping *m, *tmp;
Joe Thornbereb2aa482012-03-28 18:41:28 +01002699 struct pool *pool = h->tc->pool;
2700
2701 if (h->shared_read_entry) {
2702 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01002703 dm_deferred_entry_dec(h->shared_read_entry, &work);
Joe Thornbereb2aa482012-03-28 18:41:28 +01002704
2705 spin_lock_irqsave(&pool->lock, flags);
2706 list_for_each_entry_safe(m, tmp, &work, list) {
2707 list_del(&m->list);
2708 m->quiesced = 1;
2709 __maybe_add_mapping(m);
2710 }
2711 spin_unlock_irqrestore(&pool->lock, flags);
2712 }
2713
Joe Thornber104655f2012-03-28 18:41:28 +01002714 if (h->all_io_entry) {
2715 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01002716 dm_deferred_entry_dec(h->all_io_entry, &work);
Joe Thornber563af182012-12-21 20:23:31 +00002717 if (!list_empty(&work)) {
2718 spin_lock_irqsave(&pool->lock, flags);
2719 list_for_each_entry_safe(m, tmp, &work, list)
2720 list_add(&m->list, &pool->prepared_discards);
2721 spin_unlock_irqrestore(&pool->lock, flags);
2722 wake_worker(pool);
2723 }
Joe Thornber104655f2012-03-28 18:41:28 +01002724 }
2725
Joe Thornbereb2aa482012-03-28 18:41:28 +01002726 return 0;
2727}
2728
Joe Thornber991d9fa2011-10-31 20:21:18 +00002729static void thin_postsuspend(struct dm_target *ti)
2730{
2731 if (dm_noflush_suspending(ti))
2732 requeue_io((struct thin_c *)ti->private);
2733}
2734
2735/*
2736 * <nr mapped sectors> <highest mapped sector>
2737 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002738static void thin_status(struct dm_target *ti, status_type_t type,
2739 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002740{
2741 int r;
2742 ssize_t sz = 0;
2743 dm_block_t mapped, highest;
2744 char buf[BDEVNAME_SIZE];
2745 struct thin_c *tc = ti->private;
2746
Joe Thornbere49e5822012-07-27 15:08:16 +01002747 if (get_pool_mode(tc->pool) == PM_FAIL) {
2748 DMEMIT("Fail");
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002749 return;
Joe Thornbere49e5822012-07-27 15:08:16 +01002750 }
2751
Joe Thornber991d9fa2011-10-31 20:21:18 +00002752 if (!tc->td)
2753 DMEMIT("-");
2754 else {
2755 switch (type) {
2756 case STATUSTYPE_INFO:
2757 r = dm_thin_get_mapped_count(tc->td, &mapped);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002758 if (r) {
2759 DMERR("dm_thin_get_mapped_count returned %d", r);
2760 goto err;
2761 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002762
2763 r = dm_thin_get_highest_mapped_block(tc->td, &highest);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002764 if (r < 0) {
2765 DMERR("dm_thin_get_highest_mapped_block returned %d", r);
2766 goto err;
2767 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002768
2769 DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
2770 if (r)
2771 DMEMIT("%llu", ((highest + 1) *
2772 tc->pool->sectors_per_block) - 1);
2773 else
2774 DMEMIT("-");
2775 break;
2776
2777 case STATUSTYPE_TABLE:
2778 DMEMIT("%s %lu",
2779 format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
2780 (unsigned long) tc->dev_id);
Joe Thornber2dd9c252012-03-28 18:41:28 +01002781 if (tc->origin_dev)
2782 DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
Joe Thornber991d9fa2011-10-31 20:21:18 +00002783 break;
2784 }
2785 }
2786
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002787 return;
2788
2789err:
2790 DMEMIT("Error");
Joe Thornber991d9fa2011-10-31 20:21:18 +00002791}
2792
2793static int thin_iterate_devices(struct dm_target *ti,
2794 iterate_devices_callout_fn fn, void *data)
2795{
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002796 sector_t blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002797 struct thin_c *tc = ti->private;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002798 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002799
2800 /*
2801 * We can't call dm_pool_get_data_dev_size() since that blocks. So
2802 * we follow a more convoluted path through to the pool's target.
2803 */
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002804 if (!pool->ti)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002805 return 0; /* nothing is bound */
2806
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002807 blocks = pool->ti->len;
2808 (void) sector_div(blocks, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002809 if (blocks)
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002810 return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002811
2812 return 0;
2813}
2814
Joe Thornber991d9fa2011-10-31 20:21:18 +00002815static struct target_type thin_target = {
2816 .name = "thin",
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002817 .version = {1, 7, 1},
Joe Thornber991d9fa2011-10-31 20:21:18 +00002818 .module = THIS_MODULE,
2819 .ctr = thin_ctr,
2820 .dtr = thin_dtr,
2821 .map = thin_map,
Joe Thornbereb2aa482012-03-28 18:41:28 +01002822 .end_io = thin_endio,
Joe Thornber991d9fa2011-10-31 20:21:18 +00002823 .postsuspend = thin_postsuspend,
2824 .status = thin_status,
2825 .iterate_devices = thin_iterate_devices,
Joe Thornber991d9fa2011-10-31 20:21:18 +00002826};
2827
2828/*----------------------------------------------------------------*/
2829
2830static int __init dm_thin_init(void)
2831{
2832 int r;
2833
2834 pool_table_init();
2835
2836 r = dm_register_target(&thin_target);
2837 if (r)
2838 return r;
2839
2840 r = dm_register_target(&pool_target);
2841 if (r)
Mike Snitzera24c2562012-06-03 00:30:00 +01002842 goto bad_pool_target;
2843
2844 r = -ENOMEM;
2845
Mike Snitzera24c2562012-06-03 00:30:00 +01002846 _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
2847 if (!_new_mapping_cache)
2848 goto bad_new_mapping_cache;
2849
Mike Snitzera24c2562012-06-03 00:30:00 +01002850 return 0;
2851
Mike Snitzera24c2562012-06-03 00:30:00 +01002852bad_new_mapping_cache:
Mike Snitzera24c2562012-06-03 00:30:00 +01002853 dm_unregister_target(&pool_target);
2854bad_pool_target:
2855 dm_unregister_target(&thin_target);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002856
2857 return r;
2858}
2859
2860static void dm_thin_exit(void)
2861{
2862 dm_unregister_target(&thin_target);
2863 dm_unregister_target(&pool_target);
Mike Snitzera24c2562012-06-03 00:30:00 +01002864
Mike Snitzera24c2562012-06-03 00:30:00 +01002865 kmem_cache_destroy(_new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002866}
2867
2868module_init(dm_thin_init);
2869module_exit(dm_thin_exit);
2870
Alasdair G Kergon7cab8bf2012-05-12 01:43:19 +01002871MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
Joe Thornber991d9fa2011-10-31 20:21:18 +00002872MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2873MODULE_LICENSE("GPL");