blob: 6b9322e63651eef4fe61a0e12748ecfe44b190d7 [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
29/*
30 * The block size of the device holding pool data must be
31 * between 64KB and 1GB.
32 */
33#define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
34#define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
35
36/*
Joe Thornber991d9fa2011-10-31 20:21:18 +000037 * Device id is restricted to 24 bits.
38 */
39#define MAX_DEV_ID ((1 << 24) - 1)
40
41/*
42 * How do we handle breaking sharing of data blocks?
43 * =================================================
44 *
45 * We use a standard copy-on-write btree to store the mappings for the
46 * devices (note I'm talking about copy-on-write of the metadata here, not
47 * the data). When you take an internal snapshot you clone the root node
48 * of the origin btree. After this there is no concept of an origin or a
49 * snapshot. They are just two device trees that happen to point to the
50 * same data blocks.
51 *
52 * When we get a write in we decide if it's to a shared data block using
53 * some timestamp magic. If it is, we have to break sharing.
54 *
55 * Let's say we write to a shared block in what was the origin. The
56 * steps are:
57 *
58 * i) plug io further to this physical block. (see bio_prison code).
59 *
60 * ii) quiesce any read io to that shared data block. Obviously
Mike Snitzer44feb382012-10-12 21:02:10 +010061 * including all devices that share this block. (see dm_deferred_set code)
Joe Thornber991d9fa2011-10-31 20:21:18 +000062 *
63 * iii) copy the data block to a newly allocate block. This step can be
64 * missed out if the io covers the block. (schedule_copy).
65 *
66 * iv) insert the new mapping into the origin's btree
Joe Thornberfe878f32012-03-28 18:41:24 +010067 * (process_prepared_mapping). This act of inserting breaks some
Joe Thornber991d9fa2011-10-31 20:21:18 +000068 * sharing of btree nodes between the two devices. Breaking sharing only
69 * effects the btree of that specific device. Btrees for the other
70 * devices that share the block never change. The btree for the origin
71 * device as it was after the last commit is untouched, ie. we're using
72 * persistent data structures in the functional programming sense.
73 *
74 * v) unplug io to this physical block, including the io that triggered
75 * the breaking of sharing.
76 *
77 * Steps (ii) and (iii) occur in parallel.
78 *
79 * The metadata _doesn't_ need to be committed before the io continues. We
80 * get away with this because the io is always written to a _new_ block.
81 * If there's a crash, then:
82 *
83 * - The origin mapping will point to the old origin block (the shared
84 * one). This will contain the data as it was before the io that triggered
85 * the breaking of sharing came in.
86 *
87 * - The snap mapping still points to the old block. As it would after
88 * the commit.
89 *
90 * The downside of this scheme is the timestamp magic isn't perfect, and
91 * will continue to think that data block in the snapshot device is shared
92 * even after the write to the origin has broken sharing. I suspect data
93 * blocks will typically be shared by many different devices, so we're
94 * breaking sharing n + 1 times, rather than n, where n is the number of
95 * devices that reference this data block. At the moment I think the
96 * benefits far, far outweigh the disadvantages.
97 */
98
99/*----------------------------------------------------------------*/
100
101/*
Joe Thornber991d9fa2011-10-31 20:21:18 +0000102 * Key building.
103 */
104static void build_data_key(struct dm_thin_device *td,
Mike Snitzer44feb382012-10-12 21:02:10 +0100105 dm_block_t b, struct dm_cell_key *key)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000106{
107 key->virtual = 0;
108 key->dev = dm_thin_dev_id(td);
109 key->block = b;
110}
111
112static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
Mike Snitzer44feb382012-10-12 21:02:10 +0100113 struct dm_cell_key *key)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000114{
115 key->virtual = 1;
116 key->dev = dm_thin_dev_id(td);
117 key->block = b;
118}
119
120/*----------------------------------------------------------------*/
121
122/*
123 * A pool device ties together a metadata device and a data device. It
124 * also provides the interface for creating and destroying internal
125 * devices.
126 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100127struct dm_thin_new_mapping;
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100128
Joe Thornbere49e5822012-07-27 15:08:16 +0100129/*
130 * The pool runs in 3 modes. Ordered in degraded order for comparisons.
131 */
132enum pool_mode {
133 PM_WRITE, /* metadata may be changed */
134 PM_READ_ONLY, /* metadata may not be changed */
135 PM_FAIL, /* all I/O fails */
136};
137
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100138struct pool_features {
Joe Thornbere49e5822012-07-27 15:08:16 +0100139 enum pool_mode mode;
140
Mike Snitzer9bc142d2012-09-26 23:45:46 +0100141 bool zero_new_blocks:1;
142 bool discard_enabled:1;
143 bool discard_passdown:1;
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100144};
145
Joe Thornbere49e5822012-07-27 15:08:16 +0100146struct thin_c;
147typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio);
148typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m);
149
Joe Thornber991d9fa2011-10-31 20:21:18 +0000150struct pool {
151 struct list_head list;
152 struct dm_target *ti; /* Only set if a pool target is bound */
153
154 struct mapped_device *pool_md;
155 struct block_device *md_dev;
156 struct dm_pool_metadata *pmd;
157
Joe Thornber991d9fa2011-10-31 20:21:18 +0000158 dm_block_t low_water_blocks;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100159 uint32_t sectors_per_block;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100160 int sectors_per_block_shift;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000161
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100162 struct pool_features pf;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000163 unsigned low_water_triggered:1; /* A dm event has been sent */
164 unsigned no_free_space:1; /* A -ENOSPC warning has been issued */
165
Mike Snitzer44feb382012-10-12 21:02:10 +0100166 struct dm_bio_prison *prison;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000167 struct dm_kcopyd_client *copier;
168
169 struct workqueue_struct *wq;
170 struct work_struct worker;
Joe Thornber905e51b2012-03-28 18:41:27 +0100171 struct delayed_work waker;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000172
Joe Thornber905e51b2012-03-28 18:41:27 +0100173 unsigned long last_commit_jiffies;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100174 unsigned ref_count;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000175
176 spinlock_t lock;
177 struct bio_list deferred_bios;
178 struct bio_list deferred_flush_bios;
179 struct list_head prepared_mappings;
Joe Thornber104655f2012-03-28 18:41:28 +0100180 struct list_head prepared_discards;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000181
182 struct bio_list retry_on_resume_list;
183
Mike Snitzer44feb382012-10-12 21:02:10 +0100184 struct dm_deferred_set *shared_read_ds;
185 struct dm_deferred_set *all_io_ds;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000186
Mike Snitzera24c2562012-06-03 00:30:00 +0100187 struct dm_thin_new_mapping *next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000188 mempool_t *mapping_pool;
189 mempool_t *endio_hook_pool;
Joe Thornbere49e5822012-07-27 15:08:16 +0100190
191 process_bio_fn process_bio;
192 process_bio_fn process_discard;
193
194 process_mapping_fn process_prepared_mapping;
195 process_mapping_fn process_prepared_discard;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000196};
197
Joe Thornbere49e5822012-07-27 15:08:16 +0100198static enum pool_mode get_pool_mode(struct pool *pool);
199static void set_pool_mode(struct pool *pool, enum pool_mode mode);
200
Joe Thornber991d9fa2011-10-31 20:21:18 +0000201/*
202 * Target context for a pool.
203 */
204struct pool_c {
205 struct dm_target *ti;
206 struct pool *pool;
207 struct dm_dev *data_dev;
208 struct dm_dev *metadata_dev;
209 struct dm_target_callbacks callbacks;
210
211 dm_block_t low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +0100212 struct pool_features requested_pf; /* Features requested during table load */
213 struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000214};
215
216/*
217 * Target context for a thin.
218 */
219struct thin_c {
220 struct dm_dev *pool_dev;
Joe Thornber2dd9c252012-03-28 18:41:28 +0100221 struct dm_dev *origin_dev;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000222 dm_thin_id dev_id;
223
224 struct pool *pool;
225 struct dm_thin_device *td;
226};
227
228/*----------------------------------------------------------------*/
229
230/*
231 * A global list of pools that uses a struct mapped_device as a key.
232 */
233static struct dm_thin_pool_table {
234 struct mutex mutex;
235 struct list_head pools;
236} dm_thin_pool_table;
237
238static void pool_table_init(void)
239{
240 mutex_init(&dm_thin_pool_table.mutex);
241 INIT_LIST_HEAD(&dm_thin_pool_table.pools);
242}
243
244static void __pool_table_insert(struct pool *pool)
245{
246 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
247 list_add(&pool->list, &dm_thin_pool_table.pools);
248}
249
250static void __pool_table_remove(struct pool *pool)
251{
252 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
253 list_del(&pool->list);
254}
255
256static struct pool *__pool_table_lookup(struct mapped_device *md)
257{
258 struct pool *pool = NULL, *tmp;
259
260 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
261
262 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
263 if (tmp->pool_md == md) {
264 pool = tmp;
265 break;
266 }
267 }
268
269 return pool;
270}
271
272static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
273{
274 struct pool *pool = NULL, *tmp;
275
276 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
277
278 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
279 if (tmp->md_dev == md_dev) {
280 pool = tmp;
281 break;
282 }
283 }
284
285 return pool;
286}
287
288/*----------------------------------------------------------------*/
289
Mike Snitzera24c2562012-06-03 00:30:00 +0100290struct dm_thin_endio_hook {
Joe Thornbereb2aa482012-03-28 18:41:28 +0100291 struct thin_c *tc;
Mike Snitzer44feb382012-10-12 21:02:10 +0100292 struct dm_deferred_entry *shared_read_entry;
293 struct dm_deferred_entry *all_io_entry;
Mike Snitzera24c2562012-06-03 00:30:00 +0100294 struct dm_thin_new_mapping *overwrite_mapping;
Joe Thornbereb2aa482012-03-28 18:41:28 +0100295};
296
Joe Thornber991d9fa2011-10-31 20:21:18 +0000297static void __requeue_bio_list(struct thin_c *tc, struct bio_list *master)
298{
299 struct bio *bio;
300 struct bio_list bios;
301
302 bio_list_init(&bios);
303 bio_list_merge(&bios, master);
304 bio_list_init(master);
305
306 while ((bio = bio_list_pop(&bios))) {
Mike Snitzera24c2562012-06-03 00:30:00 +0100307 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
308
Joe Thornbereb2aa482012-03-28 18:41:28 +0100309 if (h->tc == tc)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000310 bio_endio(bio, DM_ENDIO_REQUEUE);
311 else
312 bio_list_add(master, bio);
313 }
314}
315
316static void requeue_io(struct thin_c *tc)
317{
318 struct pool *pool = tc->pool;
319 unsigned long flags;
320
321 spin_lock_irqsave(&pool->lock, flags);
322 __requeue_bio_list(tc, &pool->deferred_bios);
323 __requeue_bio_list(tc, &pool->retry_on_resume_list);
324 spin_unlock_irqrestore(&pool->lock, flags);
325}
326
327/*
328 * This section of code contains the logic for processing a thin device's IO.
329 * Much of the code depends on pool object resources (lists, workqueues, etc)
330 * but most is exclusively called from the thin target rather than the thin-pool
331 * target.
332 */
333
334static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
335{
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100336 sector_t block_nr = bio->bi_sector;
337
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100338 if (tc->pool->sectors_per_block_shift < 0)
339 (void) sector_div(block_nr, tc->pool->sectors_per_block);
340 else
341 block_nr >>= tc->pool->sectors_per_block_shift;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100342
343 return block_nr;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000344}
345
346static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
347{
348 struct pool *pool = tc->pool;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +0100349 sector_t bi_sector = bio->bi_sector;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000350
351 bio->bi_bdev = tc->pool_dev->bdev;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100352 if (tc->pool->sectors_per_block_shift < 0)
353 bio->bi_sector = (block * pool->sectors_per_block) +
354 sector_div(bi_sector, pool->sectors_per_block);
355 else
356 bio->bi_sector = (block << pool->sectors_per_block_shift) |
357 (bi_sector & (pool->sectors_per_block - 1));
Joe Thornber991d9fa2011-10-31 20:21:18 +0000358}
359
Joe Thornber2dd9c252012-03-28 18:41:28 +0100360static void remap_to_origin(struct thin_c *tc, struct bio *bio)
361{
362 bio->bi_bdev = tc->origin_dev->bdev;
363}
364
Joe Thornber4afdd682012-07-27 15:08:14 +0100365static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
366{
367 return (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) &&
368 dm_thin_changed_this_transaction(tc->td);
369}
370
Joe Thornbere8088072012-12-21 20:23:31 +0000371static void inc_all_io_entry(struct pool *pool, struct bio *bio)
372{
373 struct dm_thin_endio_hook *h;
374
375 if (bio->bi_rw & REQ_DISCARD)
376 return;
377
378 h = dm_get_mapinfo(bio)->ptr;
379 h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
380}
381
Joe Thornber2dd9c252012-03-28 18:41:28 +0100382static void issue(struct thin_c *tc, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000383{
384 struct pool *pool = tc->pool;
385 unsigned long flags;
386
Joe Thornbere49e5822012-07-27 15:08:16 +0100387 if (!bio_triggers_commit(tc, bio)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000388 generic_make_request(bio);
Joe Thornbere49e5822012-07-27 15:08:16 +0100389 return;
390 }
391
392 /*
393 * Complete bio with an error if earlier I/O caused changes to
394 * the metadata that can't be committed e.g, due to I/O errors
395 * on the metadata device.
396 */
397 if (dm_thin_aborted_changes(tc->td)) {
398 bio_io_error(bio);
399 return;
400 }
401
402 /*
403 * Batch together any bios that trigger commits and then issue a
404 * single commit for them in process_deferred_bios().
405 */
406 spin_lock_irqsave(&pool->lock, flags);
407 bio_list_add(&pool->deferred_flush_bios, bio);
408 spin_unlock_irqrestore(&pool->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000409}
410
Joe Thornber2dd9c252012-03-28 18:41:28 +0100411static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
412{
413 remap_to_origin(tc, bio);
414 issue(tc, bio);
415}
416
417static void remap_and_issue(struct thin_c *tc, struct bio *bio,
418 dm_block_t block)
419{
420 remap(tc, bio, block);
421 issue(tc, bio);
422}
423
Joe Thornber991d9fa2011-10-31 20:21:18 +0000424/*
425 * wake_worker() is used when new work is queued and when pool_resume is
426 * ready to continue deferred IO processing.
427 */
428static void wake_worker(struct pool *pool)
429{
430 queue_work(pool->wq, &pool->worker);
431}
432
433/*----------------------------------------------------------------*/
434
435/*
436 * Bio endio functions.
437 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100438struct dm_thin_new_mapping {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000439 struct list_head list;
440
Joe Thornbereb2aa482012-03-28 18:41:28 +0100441 unsigned quiesced:1;
442 unsigned prepared:1;
Joe Thornber104655f2012-03-28 18:41:28 +0100443 unsigned pass_discard:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000444
445 struct thin_c *tc;
446 dm_block_t virt_block;
447 dm_block_t data_block;
Mike Snitzera24c2562012-06-03 00:30:00 +0100448 struct dm_bio_prison_cell *cell, *cell2;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000449 int err;
450
451 /*
452 * If the bio covers the whole area of a block then we can avoid
453 * zeroing or copying. Instead this bio is hooked. The bio will
454 * still be in the cell, so care has to be taken to avoid issuing
455 * the bio twice.
456 */
457 struct bio *bio;
458 bio_end_io_t *saved_bi_end_io;
459};
460
Mike Snitzera24c2562012-06-03 00:30:00 +0100461static void __maybe_add_mapping(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000462{
463 struct pool *pool = m->tc->pool;
464
Joe Thornbereb2aa482012-03-28 18:41:28 +0100465 if (m->quiesced && m->prepared) {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000466 list_add(&m->list, &pool->prepared_mappings);
467 wake_worker(pool);
468 }
469}
470
471static void copy_complete(int read_err, unsigned long write_err, void *context)
472{
473 unsigned long flags;
Mike Snitzera24c2562012-06-03 00:30:00 +0100474 struct dm_thin_new_mapping *m = context;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000475 struct pool *pool = m->tc->pool;
476
477 m->err = read_err || write_err ? -EIO : 0;
478
479 spin_lock_irqsave(&pool->lock, flags);
480 m->prepared = 1;
481 __maybe_add_mapping(m);
482 spin_unlock_irqrestore(&pool->lock, flags);
483}
484
485static void overwrite_endio(struct bio *bio, int err)
486{
487 unsigned long flags;
Mike Snitzera24c2562012-06-03 00:30:00 +0100488 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
489 struct dm_thin_new_mapping *m = h->overwrite_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000490 struct pool *pool = m->tc->pool;
491
492 m->err = err;
493
494 spin_lock_irqsave(&pool->lock, flags);
495 m->prepared = 1;
496 __maybe_add_mapping(m);
497 spin_unlock_irqrestore(&pool->lock, flags);
498}
499
Joe Thornber991d9fa2011-10-31 20:21:18 +0000500/*----------------------------------------------------------------*/
501
502/*
503 * Workqueue.
504 */
505
506/*
507 * Prepared mapping jobs.
508 */
509
510/*
511 * This sends the bios in the cell back to the deferred_bios list.
512 */
Joe Thornber2aab3852012-12-21 20:23:33 +0000513static void cell_defer(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000514{
515 struct pool *pool = tc->pool;
516 unsigned long flags;
517
518 spin_lock_irqsave(&pool->lock, flags);
Mike Snitzer44feb382012-10-12 21:02:10 +0100519 dm_cell_release(cell, &pool->deferred_bios);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000520 spin_unlock_irqrestore(&tc->pool->lock, flags);
521
522 wake_worker(pool);
523}
524
525/*
Joe Thornberb7ca9c92012-12-21 20:23:31 +0000526 * Same as cell_defer except it omits the original holder of the cell.
Joe Thornber991d9fa2011-10-31 20:21:18 +0000527 */
Joe Thornberf286ba02012-12-21 20:23:33 +0000528static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000529{
Joe Thornber991d9fa2011-10-31 20:21:18 +0000530 struct pool *pool = tc->pool;
531 unsigned long flags;
532
Joe Thornber991d9fa2011-10-31 20:21:18 +0000533 spin_lock_irqsave(&pool->lock, flags);
Mike Snitzer44feb382012-10-12 21:02:10 +0100534 dm_cell_release_no_holder(cell, &pool->deferred_bios);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000535 spin_unlock_irqrestore(&pool->lock, flags);
536
537 wake_worker(pool);
538}
539
Joe Thornbere49e5822012-07-27 15:08:16 +0100540static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
541{
542 if (m->bio)
543 m->bio->bi_end_io = m->saved_bi_end_io;
Mike Snitzer44feb382012-10-12 21:02:10 +0100544 dm_cell_error(m->cell);
Joe Thornbere49e5822012-07-27 15:08:16 +0100545 list_del(&m->list);
546 mempool_free(m, m->tc->pool->mapping_pool);
547}
Mike Snitzera24c2562012-06-03 00:30:00 +0100548static void process_prepared_mapping(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000549{
550 struct thin_c *tc = m->tc;
551 struct bio *bio;
552 int r;
553
554 bio = m->bio;
555 if (bio)
556 bio->bi_end_io = m->saved_bi_end_io;
557
558 if (m->err) {
Mike Snitzer44feb382012-10-12 21:02:10 +0100559 dm_cell_error(m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100560 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000561 }
562
563 /*
564 * Commit the prepared block into the mapping btree.
565 * Any I/O for this block arriving after this point will get
566 * remapped to it directly.
567 */
568 r = dm_thin_insert_block(tc->td, m->virt_block, m->data_block);
569 if (r) {
570 DMERR("dm_thin_insert_block() failed");
Mike Snitzer44feb382012-10-12 21:02:10 +0100571 dm_cell_error(m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100572 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000573 }
574
575 /*
576 * Release any bios held while the block was being provisioned.
577 * If we are processing a write bio that completely covers the block,
578 * we already processed it so can ignore it now when processing
579 * the bios in the cell.
580 */
581 if (bio) {
Joe Thornberf286ba02012-12-21 20:23:33 +0000582 cell_defer_no_holder(tc, m->cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000583 bio_endio(bio, 0);
584 } else
Joe Thornber2aab3852012-12-21 20:23:33 +0000585 cell_defer(tc, m->cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000586
Joe Thornber905386f2012-07-27 15:08:05 +0100587out:
Joe Thornber991d9fa2011-10-31 20:21:18 +0000588 list_del(&m->list);
589 mempool_free(m, tc->pool->mapping_pool);
590}
591
Joe Thornbere49e5822012-07-27 15:08:16 +0100592static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
Joe Thornber104655f2012-03-28 18:41:28 +0100593{
Joe Thornber104655f2012-03-28 18:41:28 +0100594 struct thin_c *tc = m->tc;
595
Joe Thornbere49e5822012-07-27 15:08:16 +0100596 bio_io_error(m->bio);
Joe Thornberf286ba02012-12-21 20:23:33 +0000597 cell_defer_no_holder(tc, m->cell);
598 cell_defer_no_holder(tc, m->cell2);
Joe Thornbere49e5822012-07-27 15:08:16 +0100599 mempool_free(m, tc->pool->mapping_pool);
600}
Joe Thornber104655f2012-03-28 18:41:28 +0100601
Joe Thornbere49e5822012-07-27 15:08:16 +0100602static void process_prepared_discard_passdown(struct dm_thin_new_mapping *m)
603{
604 struct thin_c *tc = m->tc;
605
Joe Thornbere8088072012-12-21 20:23:31 +0000606 inc_all_io_entry(tc->pool, m->bio);
Joe Thornberf286ba02012-12-21 20:23:33 +0000607 cell_defer_no_holder(tc, m->cell);
608 cell_defer_no_holder(tc, m->cell2);
Joe Thornbere8088072012-12-21 20:23:31 +0000609
Joe Thornber104655f2012-03-28 18:41:28 +0100610 if (m->pass_discard)
611 remap_and_issue(tc, m->bio, m->data_block);
612 else
613 bio_endio(m->bio, 0);
614
Joe Thornber104655f2012-03-28 18:41:28 +0100615 mempool_free(m, tc->pool->mapping_pool);
616}
617
Joe Thornbere49e5822012-07-27 15:08:16 +0100618static void process_prepared_discard(struct dm_thin_new_mapping *m)
619{
620 int r;
621 struct thin_c *tc = m->tc;
622
623 r = dm_thin_remove_block(tc->td, m->virt_block);
624 if (r)
625 DMERR("dm_thin_remove_block() failed");
626
627 process_prepared_discard_passdown(m);
628}
629
Joe Thornber104655f2012-03-28 18:41:28 +0100630static void process_prepared(struct pool *pool, struct list_head *head,
Joe Thornbere49e5822012-07-27 15:08:16 +0100631 process_mapping_fn *fn)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000632{
633 unsigned long flags;
634 struct list_head maps;
Mike Snitzera24c2562012-06-03 00:30:00 +0100635 struct dm_thin_new_mapping *m, *tmp;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000636
637 INIT_LIST_HEAD(&maps);
638 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +0100639 list_splice_init(head, &maps);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000640 spin_unlock_irqrestore(&pool->lock, flags);
641
642 list_for_each_entry_safe(m, tmp, &maps, list)
Joe Thornbere49e5822012-07-27 15:08:16 +0100643 (*fn)(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000644}
645
646/*
647 * Deferred bio jobs.
648 */
Joe Thornber104655f2012-03-28 18:41:28 +0100649static int io_overlaps_block(struct pool *pool, struct bio *bio)
650{
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100651 return bio->bi_size == (pool->sectors_per_block << SECTOR_SHIFT);
Joe Thornber104655f2012-03-28 18:41:28 +0100652}
653
Joe Thornber991d9fa2011-10-31 20:21:18 +0000654static int io_overwrites_block(struct pool *pool, struct bio *bio)
655{
Joe Thornber104655f2012-03-28 18:41:28 +0100656 return (bio_data_dir(bio) == WRITE) &&
657 io_overlaps_block(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000658}
659
660static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
661 bio_end_io_t *fn)
662{
663 *save = bio->bi_end_io;
664 bio->bi_end_io = fn;
665}
666
667static int ensure_next_mapping(struct pool *pool)
668{
669 if (pool->next_mapping)
670 return 0;
671
672 pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
673
674 return pool->next_mapping ? 0 : -ENOMEM;
675}
676
Mike Snitzera24c2562012-06-03 00:30:00 +0100677static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000678{
Mike Snitzera24c2562012-06-03 00:30:00 +0100679 struct dm_thin_new_mapping *r = pool->next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000680
681 BUG_ON(!pool->next_mapping);
682
683 pool->next_mapping = NULL;
684
685 return r;
686}
687
688static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
Joe Thornber2dd9c252012-03-28 18:41:28 +0100689 struct dm_dev *origin, dm_block_t data_origin,
690 dm_block_t data_dest,
Mike Snitzera24c2562012-06-03 00:30:00 +0100691 struct dm_bio_prison_cell *cell, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000692{
693 int r;
694 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +0100695 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000696
697 INIT_LIST_HEAD(&m->list);
Joe Thornbereb2aa482012-03-28 18:41:28 +0100698 m->quiesced = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000699 m->prepared = 0;
700 m->tc = tc;
701 m->virt_block = virt_block;
702 m->data_block = data_dest;
703 m->cell = cell;
704 m->err = 0;
705 m->bio = NULL;
706
Mike Snitzer44feb382012-10-12 21:02:10 +0100707 if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
Joe Thornbereb2aa482012-03-28 18:41:28 +0100708 m->quiesced = 1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000709
710 /*
711 * IO to pool_dev remaps to the pool target's data_dev.
712 *
713 * If the whole block of data is being overwritten, we can issue the
714 * bio immediately. Otherwise we use kcopyd to clone the data first.
715 */
716 if (io_overwrites_block(pool, bio)) {
Mike Snitzera24c2562012-06-03 00:30:00 +0100717 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
718
Joe Thornbereb2aa482012-03-28 18:41:28 +0100719 h->overwrite_mapping = m;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000720 m->bio = bio;
721 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
Joe Thornbere8088072012-12-21 20:23:31 +0000722 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000723 remap_and_issue(tc, bio, data_dest);
724 } else {
725 struct dm_io_region from, to;
726
Joe Thornber2dd9c252012-03-28 18:41:28 +0100727 from.bdev = origin->bdev;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000728 from.sector = data_origin * pool->sectors_per_block;
729 from.count = pool->sectors_per_block;
730
731 to.bdev = tc->pool_dev->bdev;
732 to.sector = data_dest * pool->sectors_per_block;
733 to.count = pool->sectors_per_block;
734
735 r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
736 0, copy_complete, m);
737 if (r < 0) {
738 mempool_free(m, pool->mapping_pool);
739 DMERR("dm_kcopyd_copy() failed");
Mike Snitzer44feb382012-10-12 21:02:10 +0100740 dm_cell_error(cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000741 }
742 }
743}
744
Joe Thornber2dd9c252012-03-28 18:41:28 +0100745static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
746 dm_block_t data_origin, dm_block_t data_dest,
Mike Snitzera24c2562012-06-03 00:30:00 +0100747 struct dm_bio_prison_cell *cell, struct bio *bio)
Joe Thornber2dd9c252012-03-28 18:41:28 +0100748{
749 schedule_copy(tc, virt_block, tc->pool_dev,
750 data_origin, data_dest, cell, bio);
751}
752
753static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
754 dm_block_t data_dest,
Mike Snitzera24c2562012-06-03 00:30:00 +0100755 struct dm_bio_prison_cell *cell, struct bio *bio)
Joe Thornber2dd9c252012-03-28 18:41:28 +0100756{
757 schedule_copy(tc, virt_block, tc->origin_dev,
758 virt_block, data_dest, cell, bio);
759}
760
Joe Thornber991d9fa2011-10-31 20:21:18 +0000761static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
Mike Snitzera24c2562012-06-03 00:30:00 +0100762 dm_block_t data_block, struct dm_bio_prison_cell *cell,
Joe Thornber991d9fa2011-10-31 20:21:18 +0000763 struct bio *bio)
764{
765 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +0100766 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000767
768 INIT_LIST_HEAD(&m->list);
Joe Thornbereb2aa482012-03-28 18:41:28 +0100769 m->quiesced = 1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000770 m->prepared = 0;
771 m->tc = tc;
772 m->virt_block = virt_block;
773 m->data_block = data_block;
774 m->cell = cell;
775 m->err = 0;
776 m->bio = NULL;
777
778 /*
779 * If the whole block of data is being overwritten or we are not
780 * zeroing pre-existing data, we can issue the bio immediately.
781 * Otherwise we use kcopyd to zero the data first.
782 */
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100783 if (!pool->pf.zero_new_blocks)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000784 process_prepared_mapping(m);
785
786 else if (io_overwrites_block(pool, bio)) {
Mike Snitzera24c2562012-06-03 00:30:00 +0100787 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
788
Joe Thornbereb2aa482012-03-28 18:41:28 +0100789 h->overwrite_mapping = m;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000790 m->bio = bio;
791 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
Joe Thornbere8088072012-12-21 20:23:31 +0000792 inc_all_io_entry(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000793 remap_and_issue(tc, bio, data_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000794 } else {
795 int r;
796 struct dm_io_region to;
797
798 to.bdev = tc->pool_dev->bdev;
799 to.sector = data_block * pool->sectors_per_block;
800 to.count = pool->sectors_per_block;
801
802 r = dm_kcopyd_zero(pool->copier, 1, &to, 0, copy_complete, m);
803 if (r < 0) {
804 mempool_free(m, pool->mapping_pool);
805 DMERR("dm_kcopyd_zero() failed");
Mike Snitzer44feb382012-10-12 21:02:10 +0100806 dm_cell_error(cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000807 }
808 }
809}
810
Joe Thornbere49e5822012-07-27 15:08:16 +0100811static int commit(struct pool *pool)
812{
813 int r;
814
815 r = dm_pool_commit_metadata(pool->pmd);
816 if (r)
817 DMERR("commit failed, error = %d", r);
818
819 return r;
820}
821
822/*
823 * A non-zero return indicates read_only or fail_io mode.
824 * Many callers don't care about the return value.
825 */
826static int commit_or_fallback(struct pool *pool)
827{
828 int r;
829
830 if (get_pool_mode(pool) != PM_WRITE)
831 return -EINVAL;
832
833 r = commit(pool);
834 if (r)
835 set_pool_mode(pool, PM_READ_ONLY);
836
837 return r;
838}
839
Joe Thornber991d9fa2011-10-31 20:21:18 +0000840static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
841{
842 int r;
843 dm_block_t free_blocks;
844 unsigned long flags;
845 struct pool *pool = tc->pool;
846
847 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
848 if (r)
849 return r;
850
851 if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
852 DMWARN("%s: reached low water mark, sending event.",
853 dm_device_name(pool->pool_md));
854 spin_lock_irqsave(&pool->lock, flags);
855 pool->low_water_triggered = 1;
856 spin_unlock_irqrestore(&pool->lock, flags);
857 dm_table_event(pool->ti->table);
858 }
859
860 if (!free_blocks) {
861 if (pool->no_free_space)
862 return -ENOSPC;
863 else {
864 /*
865 * Try to commit to see if that will free up some
866 * more space.
867 */
Joe Thornbere49e5822012-07-27 15:08:16 +0100868 (void) commit_or_fallback(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000869
870 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
871 if (r)
872 return r;
873
874 /*
875 * If we still have no space we set a flag to avoid
876 * doing all this checking and return -ENOSPC.
877 */
878 if (!free_blocks) {
879 DMWARN("%s: no free space available.",
880 dm_device_name(pool->pool_md));
881 spin_lock_irqsave(&pool->lock, flags);
882 pool->no_free_space = 1;
883 spin_unlock_irqrestore(&pool->lock, flags);
884 return -ENOSPC;
885 }
886 }
887 }
888
889 r = dm_pool_alloc_data_block(pool->pmd, result);
890 if (r)
891 return r;
892
893 return 0;
894}
895
896/*
897 * If we have run out of space, queue bios until the device is
898 * resumed, presumably after having been reloaded with more space.
899 */
900static void retry_on_resume(struct bio *bio)
901{
Mike Snitzera24c2562012-06-03 00:30:00 +0100902 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
Joe Thornbereb2aa482012-03-28 18:41:28 +0100903 struct thin_c *tc = h->tc;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000904 struct pool *pool = tc->pool;
905 unsigned long flags;
906
907 spin_lock_irqsave(&pool->lock, flags);
908 bio_list_add(&pool->retry_on_resume_list, bio);
909 spin_unlock_irqrestore(&pool->lock, flags);
910}
911
Mike Snitzera24c2562012-06-03 00:30:00 +0100912static void no_space(struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000913{
914 struct bio *bio;
915 struct bio_list bios;
916
917 bio_list_init(&bios);
Mike Snitzer44feb382012-10-12 21:02:10 +0100918 dm_cell_release(cell, &bios);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000919
920 while ((bio = bio_list_pop(&bios)))
921 retry_on_resume(bio);
922}
923
Joe Thornber104655f2012-03-28 18:41:28 +0100924static void process_discard(struct thin_c *tc, struct bio *bio)
925{
926 int r;
Mike Snitzerc3a0ce22012-05-12 01:43:16 +0100927 unsigned long flags;
Joe Thornber104655f2012-03-28 18:41:28 +0100928 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +0100929 struct dm_bio_prison_cell *cell, *cell2;
Mike Snitzer44feb382012-10-12 21:02:10 +0100930 struct dm_cell_key key, key2;
Joe Thornber104655f2012-03-28 18:41:28 +0100931 dm_block_t block = get_bio_block(tc, bio);
932 struct dm_thin_lookup_result lookup_result;
Mike Snitzera24c2562012-06-03 00:30:00 +0100933 struct dm_thin_new_mapping *m;
Joe Thornber104655f2012-03-28 18:41:28 +0100934
935 build_virtual_key(tc->td, block, &key);
Mike Snitzer44feb382012-10-12 21:02:10 +0100936 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell))
Joe Thornber104655f2012-03-28 18:41:28 +0100937 return;
938
939 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
940 switch (r) {
941 case 0:
942 /*
943 * Check nobody is fiddling with this pool block. This can
944 * happen if someone's in the process of breaking sharing
945 * on this block.
946 */
947 build_data_key(tc->td, lookup_result.block, &key2);
Mike Snitzer44feb382012-10-12 21:02:10 +0100948 if (dm_bio_detain(tc->pool->prison, &key2, bio, &cell2)) {
Joe Thornberf286ba02012-12-21 20:23:33 +0000949 cell_defer_no_holder(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +0100950 break;
951 }
952
953 if (io_overlaps_block(pool, bio)) {
954 /*
955 * IO may still be going to the destination block. We must
956 * quiesce before we can do the removal.
957 */
958 m = get_next_mapping(pool);
959 m->tc = tc;
Mike Snitzer17b7d632012-07-27 15:07:57 +0100960 m->pass_discard = (!lookup_result.shared) && pool->pf.discard_passdown;
Joe Thornber104655f2012-03-28 18:41:28 +0100961 m->virt_block = block;
962 m->data_block = lookup_result.block;
963 m->cell = cell;
964 m->cell2 = cell2;
965 m->err = 0;
966 m->bio = bio;
967
Mike Snitzer44feb382012-10-12 21:02:10 +0100968 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list)) {
Mike Snitzerc3a0ce22012-05-12 01:43:16 +0100969 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +0100970 list_add(&m->list, &pool->prepared_discards);
Mike Snitzerc3a0ce22012-05-12 01:43:16 +0100971 spin_unlock_irqrestore(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +0100972 wake_worker(pool);
973 }
974 } else {
Joe Thornbere8088072012-12-21 20:23:31 +0000975 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +0000976 cell_defer_no_holder(tc, cell);
977 cell_defer_no_holder(tc, cell2);
Joe Thornbere8088072012-12-21 20:23:31 +0000978
Joe Thornber104655f2012-03-28 18:41:28 +0100979 /*
Mikulas Patocka49296302012-07-27 15:08:03 +0100980 * The DM core makes sure that the discard doesn't span
981 * a block boundary. So we submit the discard of a
982 * partial block appropriately.
Joe Thornber104655f2012-03-28 18:41:28 +0100983 */
Mikulas Patocka650d2a02012-07-20 14:25:05 +0100984 if ((!lookup_result.shared) && pool->pf.discard_passdown)
985 remap_and_issue(tc, bio, lookup_result.block);
986 else
987 bio_endio(bio, 0);
Joe Thornber104655f2012-03-28 18:41:28 +0100988 }
989 break;
990
991 case -ENODATA:
992 /*
993 * It isn't provisioned, just forget it.
994 */
Joe Thornberf286ba02012-12-21 20:23:33 +0000995 cell_defer_no_holder(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +0100996 bio_endio(bio, 0);
997 break;
998
999 default:
1000 DMERR("discard: find block unexpectedly returned %d", r);
Joe Thornberf286ba02012-12-21 20:23:33 +00001001 cell_defer_no_holder(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +01001002 bio_io_error(bio);
1003 break;
1004 }
1005}
1006
Joe Thornber991d9fa2011-10-31 20:21:18 +00001007static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzer44feb382012-10-12 21:02:10 +01001008 struct dm_cell_key *key,
Joe Thornber991d9fa2011-10-31 20:21:18 +00001009 struct dm_thin_lookup_result *lookup_result,
Mike Snitzera24c2562012-06-03 00:30:00 +01001010 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001011{
1012 int r;
1013 dm_block_t data_block;
1014
1015 r = alloc_data_block(tc, &data_block);
1016 switch (r) {
1017 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001018 schedule_internal_copy(tc, block, lookup_result->block,
1019 data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001020 break;
1021
1022 case -ENOSPC:
1023 no_space(cell);
1024 break;
1025
1026 default:
1027 DMERR("%s: alloc_data_block() failed, error = %d", __func__, r);
Mike Snitzer44feb382012-10-12 21:02:10 +01001028 dm_cell_error(cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001029 break;
1030 }
1031}
1032
1033static void process_shared_bio(struct thin_c *tc, struct bio *bio,
1034 dm_block_t block,
1035 struct dm_thin_lookup_result *lookup_result)
1036{
Mike Snitzera24c2562012-06-03 00:30:00 +01001037 struct dm_bio_prison_cell *cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001038 struct pool *pool = tc->pool;
Mike Snitzer44feb382012-10-12 21:02:10 +01001039 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001040
1041 /*
1042 * If cell is already occupied, then sharing is already in the process
1043 * of being broken so we have nothing further to do here.
1044 */
1045 build_data_key(tc->td, lookup_result->block, &key);
Mike Snitzer44feb382012-10-12 21:02:10 +01001046 if (dm_bio_detain(pool->prison, &key, bio, &cell))
Joe Thornber991d9fa2011-10-31 20:21:18 +00001047 return;
1048
Joe Thornber60049702012-07-27 15:08:06 +01001049 if (bio_data_dir(bio) == WRITE && bio->bi_size)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001050 break_sharing(tc, bio, block, &key, lookup_result, cell);
1051 else {
Mike Snitzera24c2562012-06-03 00:30:00 +01001052 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001053
Mike Snitzer44feb382012-10-12 21:02:10 +01001054 h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
Joe Thornbere8088072012-12-21 20:23:31 +00001055 inc_all_io_entry(pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001056 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001057
Joe Thornber991d9fa2011-10-31 20:21:18 +00001058 remap_and_issue(tc, bio, lookup_result->block);
1059 }
1060}
1061
1062static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzera24c2562012-06-03 00:30:00 +01001063 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001064{
1065 int r;
1066 dm_block_t data_block;
1067
1068 /*
1069 * Remap empty bios (flushes) immediately, without provisioning.
1070 */
1071 if (!bio->bi_size) {
Joe Thornbere8088072012-12-21 20:23:31 +00001072 inc_all_io_entry(tc->pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001073 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001074
Joe Thornber991d9fa2011-10-31 20:21:18 +00001075 remap_and_issue(tc, bio, 0);
1076 return;
1077 }
1078
1079 /*
1080 * Fill read bios with zeroes and complete them immediately.
1081 */
1082 if (bio_data_dir(bio) == READ) {
1083 zero_fill_bio(bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001084 cell_defer_no_holder(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001085 bio_endio(bio, 0);
1086 return;
1087 }
1088
1089 r = alloc_data_block(tc, &data_block);
1090 switch (r) {
1091 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001092 if (tc->origin_dev)
1093 schedule_external_copy(tc, block, data_block, cell, bio);
1094 else
1095 schedule_zero(tc, block, data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001096 break;
1097
1098 case -ENOSPC:
1099 no_space(cell);
1100 break;
1101
1102 default:
1103 DMERR("%s: alloc_data_block() failed, error = %d", __func__, r);
Joe Thornbere49e5822012-07-27 15:08:16 +01001104 set_pool_mode(tc->pool, PM_READ_ONLY);
Mike Snitzer44feb382012-10-12 21:02:10 +01001105 dm_cell_error(cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001106 break;
1107 }
1108}
1109
1110static void process_bio(struct thin_c *tc, struct bio *bio)
1111{
1112 int r;
1113 dm_block_t block = get_bio_block(tc, bio);
Mike Snitzera24c2562012-06-03 00:30:00 +01001114 struct dm_bio_prison_cell *cell;
Mike Snitzer44feb382012-10-12 21:02:10 +01001115 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001116 struct dm_thin_lookup_result lookup_result;
1117
1118 /*
1119 * If cell is already occupied, then the block is already
1120 * being provisioned so we have nothing further to do here.
1121 */
1122 build_virtual_key(tc->td, block, &key);
Mike Snitzer44feb382012-10-12 21:02:10 +01001123 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell))
Joe Thornber991d9fa2011-10-31 20:21:18 +00001124 return;
1125
1126 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1127 switch (r) {
1128 case 0:
Joe Thornbere8088072012-12-21 20:23:31 +00001129 if (lookup_result.shared) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001130 process_shared_bio(tc, bio, block, &lookup_result);
Joe Thornberf286ba02012-12-21 20:23:33 +00001131 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001132 } else {
1133 inc_all_io_entry(tc->pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001134 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001135
Joe Thornber991d9fa2011-10-31 20:21:18 +00001136 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001137 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001138 break;
1139
1140 case -ENODATA:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001141 if (bio_data_dir(bio) == READ && tc->origin_dev) {
Joe Thornbere8088072012-12-21 20:23:31 +00001142 inc_all_io_entry(tc->pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001143 cell_defer_no_holder(tc, cell);
Joe Thornbere8088072012-12-21 20:23:31 +00001144
Joe Thornber2dd9c252012-03-28 18:41:28 +01001145 remap_to_origin_and_issue(tc, bio);
1146 } else
1147 provision_block(tc, bio, block, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001148 break;
1149
1150 default:
1151 DMERR("dm_thin_find_block() failed, error = %d", r);
Joe Thornberf286ba02012-12-21 20:23:33 +00001152 cell_defer_no_holder(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001153 bio_io_error(bio);
1154 break;
1155 }
1156}
1157
Joe Thornbere49e5822012-07-27 15:08:16 +01001158static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
1159{
1160 int r;
1161 int rw = bio_data_dir(bio);
1162 dm_block_t block = get_bio_block(tc, bio);
1163 struct dm_thin_lookup_result lookup_result;
1164
1165 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1166 switch (r) {
1167 case 0:
1168 if (lookup_result.shared && (rw == WRITE) && bio->bi_size)
1169 bio_io_error(bio);
Joe Thornbere8088072012-12-21 20:23:31 +00001170 else {
1171 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001172 remap_and_issue(tc, bio, lookup_result.block);
Joe Thornbere8088072012-12-21 20:23:31 +00001173 }
Joe Thornbere49e5822012-07-27 15:08:16 +01001174 break;
1175
1176 case -ENODATA:
1177 if (rw != READ) {
1178 bio_io_error(bio);
1179 break;
1180 }
1181
1182 if (tc->origin_dev) {
Joe Thornbere8088072012-12-21 20:23:31 +00001183 inc_all_io_entry(tc->pool, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001184 remap_to_origin_and_issue(tc, bio);
1185 break;
1186 }
1187
1188 zero_fill_bio(bio);
1189 bio_endio(bio, 0);
1190 break;
1191
1192 default:
1193 DMERR("dm_thin_find_block() failed, error = %d", r);
1194 bio_io_error(bio);
1195 break;
1196 }
1197}
1198
1199static void process_bio_fail(struct thin_c *tc, struct bio *bio)
1200{
1201 bio_io_error(bio);
1202}
1203
Joe Thornber905e51b2012-03-28 18:41:27 +01001204static int need_commit_due_to_time(struct pool *pool)
1205{
1206 return jiffies < pool->last_commit_jiffies ||
1207 jiffies > pool->last_commit_jiffies + COMMIT_PERIOD;
1208}
1209
Joe Thornber991d9fa2011-10-31 20:21:18 +00001210static void process_deferred_bios(struct pool *pool)
1211{
1212 unsigned long flags;
1213 struct bio *bio;
1214 struct bio_list bios;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001215
1216 bio_list_init(&bios);
1217
1218 spin_lock_irqsave(&pool->lock, flags);
1219 bio_list_merge(&bios, &pool->deferred_bios);
1220 bio_list_init(&pool->deferred_bios);
1221 spin_unlock_irqrestore(&pool->lock, flags);
1222
1223 while ((bio = bio_list_pop(&bios))) {
Mike Snitzera24c2562012-06-03 00:30:00 +01001224 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
Joe Thornbereb2aa482012-03-28 18:41:28 +01001225 struct thin_c *tc = h->tc;
1226
Joe Thornber991d9fa2011-10-31 20:21:18 +00001227 /*
1228 * If we've got no free new_mapping structs, and processing
1229 * this bio might require one, we pause until there are some
1230 * prepared mappings to process.
1231 */
1232 if (ensure_next_mapping(pool)) {
1233 spin_lock_irqsave(&pool->lock, flags);
1234 bio_list_merge(&pool->deferred_bios, &bios);
1235 spin_unlock_irqrestore(&pool->lock, flags);
1236
1237 break;
1238 }
Joe Thornber104655f2012-03-28 18:41:28 +01001239
1240 if (bio->bi_rw & REQ_DISCARD)
Joe Thornbere49e5822012-07-27 15:08:16 +01001241 pool->process_discard(tc, bio);
Joe Thornber104655f2012-03-28 18:41:28 +01001242 else
Joe Thornbere49e5822012-07-27 15:08:16 +01001243 pool->process_bio(tc, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001244 }
1245
1246 /*
1247 * If there are any deferred flush bios, we must commit
1248 * the metadata before issuing them.
1249 */
1250 bio_list_init(&bios);
1251 spin_lock_irqsave(&pool->lock, flags);
1252 bio_list_merge(&bios, &pool->deferred_flush_bios);
1253 bio_list_init(&pool->deferred_flush_bios);
1254 spin_unlock_irqrestore(&pool->lock, flags);
1255
Joe Thornber905e51b2012-03-28 18:41:27 +01001256 if (bio_list_empty(&bios) && !need_commit_due_to_time(pool))
Joe Thornber991d9fa2011-10-31 20:21:18 +00001257 return;
1258
Joe Thornbere49e5822012-07-27 15:08:16 +01001259 if (commit_or_fallback(pool)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001260 while ((bio = bio_list_pop(&bios)))
1261 bio_io_error(bio);
1262 return;
1263 }
Joe Thornber905e51b2012-03-28 18:41:27 +01001264 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001265
1266 while ((bio = bio_list_pop(&bios)))
1267 generic_make_request(bio);
1268}
1269
1270static void do_worker(struct work_struct *ws)
1271{
1272 struct pool *pool = container_of(ws, struct pool, worker);
1273
Joe Thornbere49e5822012-07-27 15:08:16 +01001274 process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
1275 process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001276 process_deferred_bios(pool);
1277}
1278
Joe Thornber905e51b2012-03-28 18:41:27 +01001279/*
1280 * We want to commit periodically so that not too much
1281 * unwritten data builds up.
1282 */
1283static void do_waker(struct work_struct *ws)
1284{
1285 struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
1286 wake_worker(pool);
1287 queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
1288}
1289
Joe Thornber991d9fa2011-10-31 20:21:18 +00001290/*----------------------------------------------------------------*/
1291
Joe Thornbere49e5822012-07-27 15:08:16 +01001292static enum pool_mode get_pool_mode(struct pool *pool)
1293{
1294 return pool->pf.mode;
1295}
1296
1297static void set_pool_mode(struct pool *pool, enum pool_mode mode)
1298{
1299 int r;
1300
1301 pool->pf.mode = mode;
1302
1303 switch (mode) {
1304 case PM_FAIL:
1305 DMERR("switching pool to failure mode");
1306 pool->process_bio = process_bio_fail;
1307 pool->process_discard = process_bio_fail;
1308 pool->process_prepared_mapping = process_prepared_mapping_fail;
1309 pool->process_prepared_discard = process_prepared_discard_fail;
1310 break;
1311
1312 case PM_READ_ONLY:
1313 DMERR("switching pool to read-only mode");
1314 r = dm_pool_abort_metadata(pool->pmd);
1315 if (r) {
1316 DMERR("aborting transaction failed");
1317 set_pool_mode(pool, PM_FAIL);
1318 } else {
1319 dm_pool_metadata_read_only(pool->pmd);
1320 pool->process_bio = process_bio_read_only;
1321 pool->process_discard = process_discard;
1322 pool->process_prepared_mapping = process_prepared_mapping_fail;
1323 pool->process_prepared_discard = process_prepared_discard_passdown;
1324 }
1325 break;
1326
1327 case PM_WRITE:
1328 pool->process_bio = process_bio;
1329 pool->process_discard = process_discard;
1330 pool->process_prepared_mapping = process_prepared_mapping;
1331 pool->process_prepared_discard = process_prepared_discard;
1332 break;
1333 }
1334}
1335
1336/*----------------------------------------------------------------*/
1337
Joe Thornber991d9fa2011-10-31 20:21:18 +00001338/*
1339 * Mapping functions.
1340 */
1341
1342/*
1343 * Called only while mapping a thin bio to hand it over to the workqueue.
1344 */
1345static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
1346{
1347 unsigned long flags;
1348 struct pool *pool = tc->pool;
1349
1350 spin_lock_irqsave(&pool->lock, flags);
1351 bio_list_add(&pool->deferred_bios, bio);
1352 spin_unlock_irqrestore(&pool->lock, flags);
1353
1354 wake_worker(pool);
1355}
1356
Mike Snitzera24c2562012-06-03 00:30:00 +01001357static struct dm_thin_endio_hook *thin_hook_bio(struct thin_c *tc, struct bio *bio)
Joe Thornbereb2aa482012-03-28 18:41:28 +01001358{
1359 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +01001360 struct dm_thin_endio_hook *h = mempool_alloc(pool->endio_hook_pool, GFP_NOIO);
Joe Thornbereb2aa482012-03-28 18:41:28 +01001361
1362 h->tc = tc;
1363 h->shared_read_entry = NULL;
Joe Thornbere8088072012-12-21 20:23:31 +00001364 h->all_io_entry = NULL;
Joe Thornbereb2aa482012-03-28 18:41:28 +01001365 h->overwrite_mapping = NULL;
1366
1367 return h;
1368}
1369
Joe Thornber991d9fa2011-10-31 20:21:18 +00001370/*
1371 * Non-blocking function called from the thin target's map function.
1372 */
1373static int thin_bio_map(struct dm_target *ti, struct bio *bio,
1374 union map_info *map_context)
1375{
1376 int r;
1377 struct thin_c *tc = ti->private;
1378 dm_block_t block = get_bio_block(tc, bio);
1379 struct dm_thin_device *td = tc->td;
1380 struct dm_thin_lookup_result result;
Joe Thornbere8088072012-12-21 20:23:31 +00001381 struct dm_bio_prison_cell *cell1, *cell2;
1382 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001383
Joe Thornbereb2aa482012-03-28 18:41:28 +01001384 map_context->ptr = thin_hook_bio(tc, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001385
1386 if (get_pool_mode(tc->pool) == PM_FAIL) {
1387 bio_io_error(bio);
1388 return DM_MAPIO_SUBMITTED;
1389 }
1390
Joe Thornber104655f2012-03-28 18:41:28 +01001391 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001392 thin_defer_bio(tc, bio);
1393 return DM_MAPIO_SUBMITTED;
1394 }
1395
1396 r = dm_thin_find_block(td, block, 0, &result);
1397
1398 /*
1399 * Note that we defer readahead too.
1400 */
1401 switch (r) {
1402 case 0:
1403 if (unlikely(result.shared)) {
1404 /*
1405 * We have a race condition here between the
1406 * result.shared value returned by the lookup and
1407 * snapshot creation, which may cause new
1408 * sharing.
1409 *
1410 * To avoid this always quiesce the origin before
1411 * taking the snap. You want to do this anyway to
1412 * ensure a consistent application view
1413 * (i.e. lockfs).
1414 *
1415 * More distant ancestors are irrelevant. The
1416 * shared flag will be set in their case.
1417 */
1418 thin_defer_bio(tc, bio);
Joe Thornbere8088072012-12-21 20:23:31 +00001419 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001420 }
Joe Thornbere8088072012-12-21 20:23:31 +00001421
1422 build_virtual_key(tc->td, block, &key);
1423 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell1))
1424 return DM_MAPIO_SUBMITTED;
1425
1426 build_data_key(tc->td, result.block, &key);
1427 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell2)) {
Joe Thornberf286ba02012-12-21 20:23:33 +00001428 cell_defer_no_holder(tc, cell1);
Joe Thornbere8088072012-12-21 20:23:31 +00001429 return DM_MAPIO_SUBMITTED;
1430 }
1431
1432 inc_all_io_entry(tc->pool, bio);
Joe Thornberf286ba02012-12-21 20:23:33 +00001433 cell_defer_no_holder(tc, cell2);
1434 cell_defer_no_holder(tc, cell1);
Joe Thornbere8088072012-12-21 20:23:31 +00001435
1436 remap(tc, bio, result.block);
1437 return DM_MAPIO_REMAPPED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001438
1439 case -ENODATA:
Joe Thornbere49e5822012-07-27 15:08:16 +01001440 if (get_pool_mode(tc->pool) == PM_READ_ONLY) {
1441 /*
1442 * This block isn't provisioned, and we have no way
1443 * of doing so. Just error it.
1444 */
1445 bio_io_error(bio);
Joe Thornber2aab3852012-12-21 20:23:33 +00001446 return DM_MAPIO_SUBMITTED;
Joe Thornbere49e5822012-07-27 15:08:16 +01001447 }
1448 /* fall through */
1449
1450 case -EWOULDBLOCK:
Joe Thornber991d9fa2011-10-31 20:21:18 +00001451 /*
1452 * In future, the failed dm_thin_find_block above could
1453 * provide the hint to load the metadata into cache.
1454 */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001455 thin_defer_bio(tc, bio);
Joe Thornber2aab3852012-12-21 20:23:33 +00001456 return DM_MAPIO_SUBMITTED;
Joe Thornbere49e5822012-07-27 15:08:16 +01001457
1458 default:
1459 /*
1460 * Must always call bio_io_error on failure.
1461 * dm_thin_find_block can fail with -EINVAL if the
1462 * pool is switched to fail-io mode.
1463 */
1464 bio_io_error(bio);
Joe Thornber2aab3852012-12-21 20:23:33 +00001465 return DM_MAPIO_SUBMITTED;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001466 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001467}
1468
1469static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1470{
1471 int r;
1472 unsigned long flags;
1473 struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
1474
1475 spin_lock_irqsave(&pt->pool->lock, flags);
1476 r = !bio_list_empty(&pt->pool->retry_on_resume_list);
1477 spin_unlock_irqrestore(&pt->pool->lock, flags);
1478
1479 if (!r) {
1480 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
1481 r = bdi_congested(&q->backing_dev_info, bdi_bits);
1482 }
1483
1484 return r;
1485}
1486
1487static void __requeue_bios(struct pool *pool)
1488{
1489 bio_list_merge(&pool->deferred_bios, &pool->retry_on_resume_list);
1490 bio_list_init(&pool->retry_on_resume_list);
1491}
1492
1493/*----------------------------------------------------------------
1494 * Binding of control targets to a pool object
1495 *--------------------------------------------------------------*/
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001496static bool data_dev_supports_discard(struct pool_c *pt)
1497{
1498 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
1499
1500 return q && blk_queue_discard(q);
1501}
1502
1503/*
1504 * If discard_passdown was enabled verify that the data device
Mike Snitzer0424caa2012-09-26 23:45:47 +01001505 * supports discards. Disable discard_passdown if not.
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001506 */
Mike Snitzer0424caa2012-09-26 23:45:47 +01001507static void disable_passdown_if_not_supported(struct pool_c *pt)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001508{
Mike Snitzer0424caa2012-09-26 23:45:47 +01001509 struct pool *pool = pt->pool;
1510 struct block_device *data_bdev = pt->data_dev->bdev;
1511 struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
1512 sector_t block_size = pool->sectors_per_block << SECTOR_SHIFT;
1513 const char *reason = NULL;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001514 char buf[BDEVNAME_SIZE];
1515
Mike Snitzer0424caa2012-09-26 23:45:47 +01001516 if (!pt->adjusted_pf.discard_passdown)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001517 return;
1518
Mike Snitzer0424caa2012-09-26 23:45:47 +01001519 if (!data_dev_supports_discard(pt))
1520 reason = "discard unsupported";
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001521
Mike Snitzer0424caa2012-09-26 23:45:47 +01001522 else if (data_limits->max_discard_sectors < pool->sectors_per_block)
1523 reason = "max discard sectors smaller than a block";
1524
1525 else if (data_limits->discard_granularity > block_size)
1526 reason = "discard granularity larger than a block";
1527
1528 else if (block_size & (data_limits->discard_granularity - 1))
1529 reason = "discard granularity not a factor of block size";
1530
1531 if (reason) {
1532 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
1533 pt->adjusted_pf.discard_passdown = false;
1534 }
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001535}
1536
Joe Thornber991d9fa2011-10-31 20:21:18 +00001537static int bind_control_target(struct pool *pool, struct dm_target *ti)
1538{
1539 struct pool_c *pt = ti->private;
1540
Joe Thornbere49e5822012-07-27 15:08:16 +01001541 /*
1542 * We want to make sure that degraded pools are never upgraded.
1543 */
1544 enum pool_mode old_mode = pool->pf.mode;
Mike Snitzer0424caa2012-09-26 23:45:47 +01001545 enum pool_mode new_mode = pt->adjusted_pf.mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01001546
1547 if (old_mode > new_mode)
1548 new_mode = old_mode;
1549
Joe Thornber991d9fa2011-10-31 20:21:18 +00001550 pool->ti = ti;
1551 pool->low_water_blocks = pt->low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +01001552 pool->pf = pt->adjusted_pf;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001553
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001554 set_pool_mode(pool, new_mode);
Mike Snitzerf4026932012-05-19 01:01:01 +01001555
Joe Thornber991d9fa2011-10-31 20:21:18 +00001556 return 0;
1557}
1558
1559static void unbind_control_target(struct pool *pool, struct dm_target *ti)
1560{
1561 if (pool->ti == ti)
1562 pool->ti = NULL;
1563}
1564
1565/*----------------------------------------------------------------
1566 * Pool creation
1567 *--------------------------------------------------------------*/
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001568/* Initialize pool features. */
1569static void pool_features_init(struct pool_features *pf)
1570{
Joe Thornbere49e5822012-07-27 15:08:16 +01001571 pf->mode = PM_WRITE;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001572 pf->zero_new_blocks = true;
1573 pf->discard_enabled = true;
1574 pf->discard_passdown = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001575}
1576
Joe Thornber991d9fa2011-10-31 20:21:18 +00001577static void __pool_destroy(struct pool *pool)
1578{
1579 __pool_table_remove(pool);
1580
1581 if (dm_pool_metadata_close(pool->pmd) < 0)
1582 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
1583
Mike Snitzer44feb382012-10-12 21:02:10 +01001584 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001585 dm_kcopyd_client_destroy(pool->copier);
1586
1587 if (pool->wq)
1588 destroy_workqueue(pool->wq);
1589
1590 if (pool->next_mapping)
1591 mempool_free(pool->next_mapping, pool->mapping_pool);
1592 mempool_destroy(pool->mapping_pool);
1593 mempool_destroy(pool->endio_hook_pool);
Mike Snitzer44feb382012-10-12 21:02:10 +01001594 dm_deferred_set_destroy(pool->shared_read_ds);
1595 dm_deferred_set_destroy(pool->all_io_ds);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001596 kfree(pool);
1597}
1598
Mike Snitzera24c2562012-06-03 00:30:00 +01001599static struct kmem_cache *_new_mapping_cache;
1600static struct kmem_cache *_endio_hook_cache;
1601
Joe Thornber991d9fa2011-10-31 20:21:18 +00001602static struct pool *pool_create(struct mapped_device *pool_md,
1603 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01001604 unsigned long block_size,
1605 int read_only, char **error)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001606{
1607 int r;
1608 void *err_p;
1609 struct pool *pool;
1610 struct dm_pool_metadata *pmd;
Joe Thornbere49e5822012-07-27 15:08:16 +01001611 bool format_device = read_only ? false : true;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001612
Joe Thornbere49e5822012-07-27 15:08:16 +01001613 pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001614 if (IS_ERR(pmd)) {
1615 *error = "Error creating metadata object";
1616 return (struct pool *)pmd;
1617 }
1618
1619 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
1620 if (!pool) {
1621 *error = "Error allocating memory for pool";
1622 err_p = ERR_PTR(-ENOMEM);
1623 goto bad_pool;
1624 }
1625
1626 pool->pmd = pmd;
1627 pool->sectors_per_block = block_size;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +01001628 if (block_size & (block_size - 1))
1629 pool->sectors_per_block_shift = -1;
1630 else
1631 pool->sectors_per_block_shift = __ffs(block_size);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001632 pool->low_water_blocks = 0;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001633 pool_features_init(&pool->pf);
Mike Snitzer44feb382012-10-12 21:02:10 +01001634 pool->prison = dm_bio_prison_create(PRISON_CELLS);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001635 if (!pool->prison) {
1636 *error = "Error creating pool's bio prison";
1637 err_p = ERR_PTR(-ENOMEM);
1638 goto bad_prison;
1639 }
1640
1641 pool->copier = dm_kcopyd_client_create();
1642 if (IS_ERR(pool->copier)) {
1643 r = PTR_ERR(pool->copier);
1644 *error = "Error creating pool's kcopyd client";
1645 err_p = ERR_PTR(r);
1646 goto bad_kcopyd_client;
1647 }
1648
1649 /*
1650 * Create singlethreaded workqueue that will service all devices
1651 * that use this metadata.
1652 */
1653 pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
1654 if (!pool->wq) {
1655 *error = "Error creating pool's workqueue";
1656 err_p = ERR_PTR(-ENOMEM);
1657 goto bad_wq;
1658 }
1659
1660 INIT_WORK(&pool->worker, do_worker);
Joe Thornber905e51b2012-03-28 18:41:27 +01001661 INIT_DELAYED_WORK(&pool->waker, do_waker);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001662 spin_lock_init(&pool->lock);
1663 bio_list_init(&pool->deferred_bios);
1664 bio_list_init(&pool->deferred_flush_bios);
1665 INIT_LIST_HEAD(&pool->prepared_mappings);
Joe Thornber104655f2012-03-28 18:41:28 +01001666 INIT_LIST_HEAD(&pool->prepared_discards);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001667 pool->low_water_triggered = 0;
1668 pool->no_free_space = 0;
1669 bio_list_init(&pool->retry_on_resume_list);
Mike Snitzer44feb382012-10-12 21:02:10 +01001670
1671 pool->shared_read_ds = dm_deferred_set_create();
1672 if (!pool->shared_read_ds) {
1673 *error = "Error creating pool's shared read deferred set";
1674 err_p = ERR_PTR(-ENOMEM);
1675 goto bad_shared_read_ds;
1676 }
1677
1678 pool->all_io_ds = dm_deferred_set_create();
1679 if (!pool->all_io_ds) {
1680 *error = "Error creating pool's all io deferred set";
1681 err_p = ERR_PTR(-ENOMEM);
1682 goto bad_all_io_ds;
1683 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001684
1685 pool->next_mapping = NULL;
Mike Snitzera24c2562012-06-03 00:30:00 +01001686 pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
1687 _new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001688 if (!pool->mapping_pool) {
1689 *error = "Error creating pool's mapping mempool";
1690 err_p = ERR_PTR(-ENOMEM);
1691 goto bad_mapping_pool;
1692 }
1693
Mike Snitzera24c2562012-06-03 00:30:00 +01001694 pool->endio_hook_pool = mempool_create_slab_pool(ENDIO_HOOK_POOL_SIZE,
1695 _endio_hook_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001696 if (!pool->endio_hook_pool) {
1697 *error = "Error creating pool's endio_hook mempool";
1698 err_p = ERR_PTR(-ENOMEM);
1699 goto bad_endio_hook_pool;
1700 }
1701 pool->ref_count = 1;
Joe Thornber905e51b2012-03-28 18:41:27 +01001702 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001703 pool->pool_md = pool_md;
1704 pool->md_dev = metadata_dev;
1705 __pool_table_insert(pool);
1706
1707 return pool;
1708
1709bad_endio_hook_pool:
1710 mempool_destroy(pool->mapping_pool);
1711bad_mapping_pool:
Mike Snitzer44feb382012-10-12 21:02:10 +01001712 dm_deferred_set_destroy(pool->all_io_ds);
1713bad_all_io_ds:
1714 dm_deferred_set_destroy(pool->shared_read_ds);
1715bad_shared_read_ds:
Joe Thornber991d9fa2011-10-31 20:21:18 +00001716 destroy_workqueue(pool->wq);
1717bad_wq:
1718 dm_kcopyd_client_destroy(pool->copier);
1719bad_kcopyd_client:
Mike Snitzer44feb382012-10-12 21:02:10 +01001720 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001721bad_prison:
1722 kfree(pool);
1723bad_pool:
1724 if (dm_pool_metadata_close(pmd))
1725 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
1726
1727 return err_p;
1728}
1729
1730static void __pool_inc(struct pool *pool)
1731{
1732 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
1733 pool->ref_count++;
1734}
1735
1736static void __pool_dec(struct pool *pool)
1737{
1738 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
1739 BUG_ON(!pool->ref_count);
1740 if (!--pool->ref_count)
1741 __pool_destroy(pool);
1742}
1743
1744static struct pool *__pool_find(struct mapped_device *pool_md,
1745 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01001746 unsigned long block_size, int read_only,
1747 char **error, int *created)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001748{
1749 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
1750
1751 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01001752 if (pool->pool_md != pool_md) {
1753 *error = "metadata device already in use by a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00001754 return ERR_PTR(-EBUSY);
Mike Snitzerf09996c2012-07-27 15:07:59 +01001755 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001756 __pool_inc(pool);
1757
1758 } else {
1759 pool = __pool_table_lookup(pool_md);
1760 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01001761 if (pool->md_dev != metadata_dev) {
1762 *error = "different pool cannot replace a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00001763 return ERR_PTR(-EINVAL);
Mike Snitzerf09996c2012-07-27 15:07:59 +01001764 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001765 __pool_inc(pool);
1766
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001767 } else {
Joe Thornbere49e5822012-07-27 15:08:16 +01001768 pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001769 *created = 1;
1770 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001771 }
1772
1773 return pool;
1774}
1775
1776/*----------------------------------------------------------------
1777 * Pool target methods
1778 *--------------------------------------------------------------*/
1779static void pool_dtr(struct dm_target *ti)
1780{
1781 struct pool_c *pt = ti->private;
1782
1783 mutex_lock(&dm_thin_pool_table.mutex);
1784
1785 unbind_control_target(pt->pool, ti);
1786 __pool_dec(pt->pool);
1787 dm_put_device(ti, pt->metadata_dev);
1788 dm_put_device(ti, pt->data_dev);
1789 kfree(pt);
1790
1791 mutex_unlock(&dm_thin_pool_table.mutex);
1792}
1793
Joe Thornber991d9fa2011-10-31 20:21:18 +00001794static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
1795 struct dm_target *ti)
1796{
1797 int r;
1798 unsigned argc;
1799 const char *arg_name;
1800
1801 static struct dm_arg _args[] = {
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001802 {0, 3, "Invalid number of pool feature arguments"},
Joe Thornber991d9fa2011-10-31 20:21:18 +00001803 };
1804
1805 /*
1806 * No feature arguments supplied.
1807 */
1808 if (!as->argc)
1809 return 0;
1810
1811 r = dm_read_arg_group(_args, as, &argc, &ti->error);
1812 if (r)
1813 return -EINVAL;
1814
1815 while (argc && !r) {
1816 arg_name = dm_shift_arg(as);
1817 argc--;
1818
Joe Thornbere49e5822012-07-27 15:08:16 +01001819 if (!strcasecmp(arg_name, "skip_block_zeroing"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001820 pf->zero_new_blocks = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001821
Joe Thornbere49e5822012-07-27 15:08:16 +01001822 else if (!strcasecmp(arg_name, "ignore_discard"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001823 pf->discard_enabled = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01001824
1825 else if (!strcasecmp(arg_name, "no_discard_passdown"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001826 pf->discard_passdown = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01001827
1828 else if (!strcasecmp(arg_name, "read_only"))
1829 pf->mode = PM_READ_ONLY;
1830
1831 else {
1832 ti->error = "Unrecognised pool feature requested";
1833 r = -EINVAL;
1834 break;
1835 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001836 }
1837
1838 return r;
1839}
1840
1841/*
1842 * thin-pool <metadata dev> <data dev>
1843 * <data block size (sectors)>
1844 * <low water mark (blocks)>
1845 * [<#feature args> [<arg>]*]
1846 *
1847 * Optional feature arguments are:
1848 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001849 * ignore_discard: disable discard
1850 * no_discard_passdown: don't pass discards down to the data device
Joe Thornber991d9fa2011-10-31 20:21:18 +00001851 */
1852static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
1853{
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001854 int r, pool_created = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001855 struct pool_c *pt;
1856 struct pool *pool;
1857 struct pool_features pf;
1858 struct dm_arg_set as;
1859 struct dm_dev *data_dev;
1860 unsigned long block_size;
1861 dm_block_t low_water_blocks;
1862 struct dm_dev *metadata_dev;
1863 sector_t metadata_dev_size;
Mike Snitzerc4a69ec2012-03-28 18:41:28 +01001864 char b[BDEVNAME_SIZE];
Joe Thornber991d9fa2011-10-31 20:21:18 +00001865
1866 /*
1867 * FIXME Remove validation from scope of lock.
1868 */
1869 mutex_lock(&dm_thin_pool_table.mutex);
1870
1871 if (argc < 4) {
1872 ti->error = "Invalid argument count";
1873 r = -EINVAL;
1874 goto out_unlock;
1875 }
1876 as.argc = argc;
1877 as.argv = argv;
1878
1879 r = dm_get_device(ti, argv[0], FMODE_READ | FMODE_WRITE, &metadata_dev);
1880 if (r) {
1881 ti->error = "Error opening metadata block device";
1882 goto out_unlock;
1883 }
1884
1885 metadata_dev_size = i_size_read(metadata_dev->bdev->bd_inode) >> SECTOR_SHIFT;
Mike Snitzerc4a69ec2012-03-28 18:41:28 +01001886 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
1887 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
1888 bdevname(metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001889
1890 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
1891 if (r) {
1892 ti->error = "Error getting data device";
1893 goto out_metadata;
1894 }
1895
1896 if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
1897 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
1898 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01001899 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001900 ti->error = "Invalid block size";
1901 r = -EINVAL;
1902 goto out;
1903 }
1904
1905 if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
1906 ti->error = "Invalid low water mark";
1907 r = -EINVAL;
1908 goto out;
1909 }
1910
1911 /*
1912 * Set default pool features.
1913 */
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001914 pool_features_init(&pf);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001915
1916 dm_consume_args(&as, 4);
1917 r = parse_pool_features(&as, &pf, ti);
1918 if (r)
1919 goto out;
1920
1921 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
1922 if (!pt) {
1923 r = -ENOMEM;
1924 goto out;
1925 }
1926
1927 pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
Joe Thornbere49e5822012-07-27 15:08:16 +01001928 block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001929 if (IS_ERR(pool)) {
1930 r = PTR_ERR(pool);
1931 goto out_free_pt;
1932 }
1933
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001934 /*
1935 * 'pool_created' reflects whether this is the first table load.
1936 * Top level discard support is not allowed to be changed after
1937 * initial load. This would require a pool reload to trigger thin
1938 * device changes.
1939 */
1940 if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
1941 ti->error = "Discard support cannot be disabled once enabled";
1942 r = -EINVAL;
1943 goto out_flags_changed;
1944 }
1945
Joe Thornber991d9fa2011-10-31 20:21:18 +00001946 pt->pool = pool;
1947 pt->ti = ti;
1948 pt->metadata_dev = metadata_dev;
1949 pt->data_dev = data_dev;
1950 pt->low_water_blocks = low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +01001951 pt->adjusted_pf = pt->requested_pf = pf;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001952 ti->num_flush_requests = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001953
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001954 /*
1955 * Only need to enable discards if the pool should pass
1956 * them down to the data device. The thin device's discard
1957 * processing will cause mappings to be removed from the btree.
1958 */
1959 if (pf.discard_enabled && pf.discard_passdown) {
1960 ti->num_discard_requests = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001961
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001962 /*
1963 * Setting 'discards_supported' circumvents the normal
1964 * stacking of discard limits (this keeps the pool and
1965 * thin devices' discard limits consistent).
1966 */
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01001967 ti->discards_supported = true;
Mike Snitzer307615a2012-09-26 23:45:39 +01001968 ti->discard_zeroes_data_unsupported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001969 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001970 ti->private = pt;
1971
1972 pt->callbacks.congested_fn = pool_is_congested;
1973 dm_table_add_target_callbacks(ti->table, &pt->callbacks);
1974
1975 mutex_unlock(&dm_thin_pool_table.mutex);
1976
1977 return 0;
1978
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001979out_flags_changed:
1980 __pool_dec(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001981out_free_pt:
1982 kfree(pt);
1983out:
1984 dm_put_device(ti, data_dev);
1985out_metadata:
1986 dm_put_device(ti, metadata_dev);
1987out_unlock:
1988 mutex_unlock(&dm_thin_pool_table.mutex);
1989
1990 return r;
1991}
1992
1993static int pool_map(struct dm_target *ti, struct bio *bio,
1994 union map_info *map_context)
1995{
1996 int r;
1997 struct pool_c *pt = ti->private;
1998 struct pool *pool = pt->pool;
1999 unsigned long flags;
2000
2001 /*
2002 * As this is a singleton target, ti->begin is always zero.
2003 */
2004 spin_lock_irqsave(&pool->lock, flags);
2005 bio->bi_bdev = pt->data_dev->bdev;
2006 r = DM_MAPIO_REMAPPED;
2007 spin_unlock_irqrestore(&pool->lock, flags);
2008
2009 return r;
2010}
2011
2012/*
2013 * Retrieves the number of blocks of the data device from
2014 * the superblock and compares it to the actual device size,
2015 * thus resizing the data device in case it has grown.
2016 *
2017 * This both copes with opening preallocated data devices in the ctr
2018 * being followed by a resume
2019 * -and-
2020 * calling the resume method individually after userspace has
2021 * grown the data device in reaction to a table event.
2022 */
2023static int pool_preresume(struct dm_target *ti)
2024{
2025 int r;
2026 struct pool_c *pt = ti->private;
2027 struct pool *pool = pt->pool;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002028 sector_t data_size = ti->len;
2029 dm_block_t sb_data_size;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002030
2031 /*
2032 * Take control of the pool object.
2033 */
2034 r = bind_control_target(pool, ti);
2035 if (r)
2036 return r;
2037
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002038 (void) sector_div(data_size, pool->sectors_per_block);
2039
Joe Thornber991d9fa2011-10-31 20:21:18 +00002040 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
2041 if (r) {
2042 DMERR("failed to retrieve data device size");
2043 return r;
2044 }
2045
2046 if (data_size < sb_data_size) {
2047 DMERR("pool target too small, is %llu blocks (expected %llu)",
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002048 (unsigned long long)data_size, sb_data_size);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002049 return -EINVAL;
2050
2051 } else if (data_size > sb_data_size) {
2052 r = dm_pool_resize_data_dev(pool->pmd, data_size);
2053 if (r) {
2054 DMERR("failed to resize data device");
Joe Thornbere49e5822012-07-27 15:08:16 +01002055 /* FIXME Stricter than necessary: Rollback transaction instead here */
2056 set_pool_mode(pool, PM_READ_ONLY);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002057 return r;
2058 }
2059
Joe Thornbere49e5822012-07-27 15:08:16 +01002060 (void) commit_or_fallback(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002061 }
2062
2063 return 0;
2064}
2065
2066static void pool_resume(struct dm_target *ti)
2067{
2068 struct pool_c *pt = ti->private;
2069 struct pool *pool = pt->pool;
2070 unsigned long flags;
2071
2072 spin_lock_irqsave(&pool->lock, flags);
2073 pool->low_water_triggered = 0;
2074 pool->no_free_space = 0;
2075 __requeue_bios(pool);
2076 spin_unlock_irqrestore(&pool->lock, flags);
2077
Joe Thornber905e51b2012-03-28 18:41:27 +01002078 do_waker(&pool->waker.work);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002079}
2080
2081static void pool_postsuspend(struct dm_target *ti)
2082{
Joe Thornber991d9fa2011-10-31 20:21:18 +00002083 struct pool_c *pt = ti->private;
2084 struct pool *pool = pt->pool;
2085
Joe Thornber905e51b2012-03-28 18:41:27 +01002086 cancel_delayed_work(&pool->waker);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002087 flush_workqueue(pool->wq);
Joe Thornbere49e5822012-07-27 15:08:16 +01002088 (void) commit_or_fallback(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002089}
2090
2091static int check_arg_count(unsigned argc, unsigned args_required)
2092{
2093 if (argc != args_required) {
2094 DMWARN("Message received with %u arguments instead of %u.",
2095 argc, args_required);
2096 return -EINVAL;
2097 }
2098
2099 return 0;
2100}
2101
2102static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
2103{
2104 if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
2105 *dev_id <= MAX_DEV_ID)
2106 return 0;
2107
2108 if (warning)
2109 DMWARN("Message received with invalid device id: %s", arg);
2110
2111 return -EINVAL;
2112}
2113
2114static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
2115{
2116 dm_thin_id dev_id;
2117 int r;
2118
2119 r = check_arg_count(argc, 2);
2120 if (r)
2121 return r;
2122
2123 r = read_dev_id(argv[1], &dev_id, 1);
2124 if (r)
2125 return r;
2126
2127 r = dm_pool_create_thin(pool->pmd, dev_id);
2128 if (r) {
2129 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
2130 argv[1]);
2131 return r;
2132 }
2133
2134 return 0;
2135}
2136
2137static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2138{
2139 dm_thin_id dev_id;
2140 dm_thin_id origin_dev_id;
2141 int r;
2142
2143 r = check_arg_count(argc, 3);
2144 if (r)
2145 return r;
2146
2147 r = read_dev_id(argv[1], &dev_id, 1);
2148 if (r)
2149 return r;
2150
2151 r = read_dev_id(argv[2], &origin_dev_id, 1);
2152 if (r)
2153 return r;
2154
2155 r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
2156 if (r) {
2157 DMWARN("Creation of new snapshot %s of device %s failed.",
2158 argv[1], argv[2]);
2159 return r;
2160 }
2161
2162 return 0;
2163}
2164
2165static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
2166{
2167 dm_thin_id dev_id;
2168 int r;
2169
2170 r = check_arg_count(argc, 2);
2171 if (r)
2172 return r;
2173
2174 r = read_dev_id(argv[1], &dev_id, 1);
2175 if (r)
2176 return r;
2177
2178 r = dm_pool_delete_thin_device(pool->pmd, dev_id);
2179 if (r)
2180 DMWARN("Deletion of thin device %s failed.", argv[1]);
2181
2182 return r;
2183}
2184
2185static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
2186{
2187 dm_thin_id old_id, new_id;
2188 int r;
2189
2190 r = check_arg_count(argc, 3);
2191 if (r)
2192 return r;
2193
2194 if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
2195 DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
2196 return -EINVAL;
2197 }
2198
2199 if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
2200 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
2201 return -EINVAL;
2202 }
2203
2204 r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
2205 if (r) {
2206 DMWARN("Failed to change transaction id from %s to %s.",
2207 argv[1], argv[2]);
2208 return r;
2209 }
2210
2211 return 0;
2212}
2213
Joe Thornbercc8394d2012-06-03 00:30:01 +01002214static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2215{
2216 int r;
2217
2218 r = check_arg_count(argc, 1);
2219 if (r)
2220 return r;
2221
Joe Thornbere49e5822012-07-27 15:08:16 +01002222 (void) commit_or_fallback(pool);
Joe Thornber0d200ae2012-07-03 12:55:31 +01002223
Joe Thornbercc8394d2012-06-03 00:30:01 +01002224 r = dm_pool_reserve_metadata_snap(pool->pmd);
2225 if (r)
2226 DMWARN("reserve_metadata_snap message failed.");
2227
2228 return r;
2229}
2230
2231static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2232{
2233 int r;
2234
2235 r = check_arg_count(argc, 1);
2236 if (r)
2237 return r;
2238
2239 r = dm_pool_release_metadata_snap(pool->pmd);
2240 if (r)
2241 DMWARN("release_metadata_snap message failed.");
2242
2243 return r;
2244}
2245
Joe Thornber991d9fa2011-10-31 20:21:18 +00002246/*
2247 * Messages supported:
2248 * create_thin <dev_id>
2249 * create_snap <dev_id> <origin_id>
2250 * delete <dev_id>
2251 * trim <dev_id> <new_size_in_sectors>
2252 * set_transaction_id <current_trans_id> <new_trans_id>
Joe Thornbercc8394d2012-06-03 00:30:01 +01002253 * reserve_metadata_snap
2254 * release_metadata_snap
Joe Thornber991d9fa2011-10-31 20:21:18 +00002255 */
2256static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
2257{
2258 int r = -EINVAL;
2259 struct pool_c *pt = ti->private;
2260 struct pool *pool = pt->pool;
2261
2262 if (!strcasecmp(argv[0], "create_thin"))
2263 r = process_create_thin_mesg(argc, argv, pool);
2264
2265 else if (!strcasecmp(argv[0], "create_snap"))
2266 r = process_create_snap_mesg(argc, argv, pool);
2267
2268 else if (!strcasecmp(argv[0], "delete"))
2269 r = process_delete_mesg(argc, argv, pool);
2270
2271 else if (!strcasecmp(argv[0], "set_transaction_id"))
2272 r = process_set_transaction_id_mesg(argc, argv, pool);
2273
Joe Thornbercc8394d2012-06-03 00:30:01 +01002274 else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
2275 r = process_reserve_metadata_snap_mesg(argc, argv, pool);
2276
2277 else if (!strcasecmp(argv[0], "release_metadata_snap"))
2278 r = process_release_metadata_snap_mesg(argc, argv, pool);
2279
Joe Thornber991d9fa2011-10-31 20:21:18 +00002280 else
2281 DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
2282
Joe Thornbere49e5822012-07-27 15:08:16 +01002283 if (!r)
2284 (void) commit_or_fallback(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002285
2286 return r;
2287}
2288
Joe Thornbere49e5822012-07-27 15:08:16 +01002289static void emit_flags(struct pool_features *pf, char *result,
2290 unsigned sz, unsigned maxlen)
2291{
2292 unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
2293 !pf->discard_passdown + (pf->mode == PM_READ_ONLY);
2294 DMEMIT("%u ", count);
2295
2296 if (!pf->zero_new_blocks)
2297 DMEMIT("skip_block_zeroing ");
2298
2299 if (!pf->discard_enabled)
2300 DMEMIT("ignore_discard ");
2301
2302 if (!pf->discard_passdown)
2303 DMEMIT("no_discard_passdown ");
2304
2305 if (pf->mode == PM_READ_ONLY)
2306 DMEMIT("read_only ");
2307}
2308
Joe Thornber991d9fa2011-10-31 20:21:18 +00002309/*
2310 * Status line is:
2311 * <transaction id> <used metadata sectors>/<total metadata sectors>
2312 * <used data sectors>/<total data sectors> <held metadata root>
2313 */
2314static int pool_status(struct dm_target *ti, status_type_t type,
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01002315 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002316{
Joe Thornbere49e5822012-07-27 15:08:16 +01002317 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002318 unsigned sz = 0;
2319 uint64_t transaction_id;
2320 dm_block_t nr_free_blocks_data;
2321 dm_block_t nr_free_blocks_metadata;
2322 dm_block_t nr_blocks_data;
2323 dm_block_t nr_blocks_metadata;
2324 dm_block_t held_root;
2325 char buf[BDEVNAME_SIZE];
2326 char buf2[BDEVNAME_SIZE];
2327 struct pool_c *pt = ti->private;
2328 struct pool *pool = pt->pool;
2329
2330 switch (type) {
2331 case STATUSTYPE_INFO:
Joe Thornbere49e5822012-07-27 15:08:16 +01002332 if (get_pool_mode(pool) == PM_FAIL) {
2333 DMEMIT("Fail");
2334 break;
2335 }
2336
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01002337 /* Commit to ensure statistics aren't out-of-date */
2338 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
2339 (void) commit_or_fallback(pool);
2340
Joe Thornber991d9fa2011-10-31 20:21:18 +00002341 r = dm_pool_get_metadata_transaction_id(pool->pmd,
2342 &transaction_id);
2343 if (r)
2344 return r;
2345
2346 r = dm_pool_get_free_metadata_block_count(pool->pmd,
2347 &nr_free_blocks_metadata);
2348 if (r)
2349 return r;
2350
2351 r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
2352 if (r)
2353 return r;
2354
2355 r = dm_pool_get_free_block_count(pool->pmd,
2356 &nr_free_blocks_data);
2357 if (r)
2358 return r;
2359
2360 r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
2361 if (r)
2362 return r;
2363
Joe Thornbercc8394d2012-06-03 00:30:01 +01002364 r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002365 if (r)
2366 return r;
2367
2368 DMEMIT("%llu %llu/%llu %llu/%llu ",
2369 (unsigned long long)transaction_id,
2370 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
2371 (unsigned long long)nr_blocks_metadata,
2372 (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
2373 (unsigned long long)nr_blocks_data);
2374
2375 if (held_root)
Joe Thornbere49e5822012-07-27 15:08:16 +01002376 DMEMIT("%llu ", held_root);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002377 else
Joe Thornbere49e5822012-07-27 15:08:16 +01002378 DMEMIT("- ");
2379
2380 if (pool->pf.mode == PM_READ_ONLY)
2381 DMEMIT("ro ");
2382 else
2383 DMEMIT("rw ");
2384
Mike Snitzer018debe2012-12-21 20:23:32 +00002385 if (!pool->pf.discard_enabled)
2386 DMEMIT("ignore_discard");
2387 else if (pool->pf.discard_passdown)
Joe Thornbere49e5822012-07-27 15:08:16 +01002388 DMEMIT("discard_passdown");
2389 else
2390 DMEMIT("no_discard_passdown");
Joe Thornber991d9fa2011-10-31 20:21:18 +00002391
2392 break;
2393
2394 case STATUSTYPE_TABLE:
2395 DMEMIT("%s %s %lu %llu ",
2396 format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
2397 format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
2398 (unsigned long)pool->sectors_per_block,
2399 (unsigned long long)pt->low_water_blocks);
Mike Snitzer0424caa2012-09-26 23:45:47 +01002400 emit_flags(&pt->requested_pf, result, sz, maxlen);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002401 break;
2402 }
2403
2404 return 0;
2405}
2406
2407static int pool_iterate_devices(struct dm_target *ti,
2408 iterate_devices_callout_fn fn, void *data)
2409{
2410 struct pool_c *pt = ti->private;
2411
2412 return fn(ti, pt->data_dev, 0, ti->len, data);
2413}
2414
2415static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
2416 struct bio_vec *biovec, int max_size)
2417{
2418 struct pool_c *pt = ti->private;
2419 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
2420
2421 if (!q->merge_bvec_fn)
2422 return max_size;
2423
2424 bvm->bi_bdev = pt->data_dev->bdev;
2425
2426 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
2427}
2428
Mike Snitzer28eed342012-10-12 21:02:07 +01002429static bool block_size_is_power_of_two(struct pool *pool)
2430{
2431 return pool->sectors_per_block_shift >= 0;
2432}
2433
Mike Snitzer0424caa2012-09-26 23:45:47 +01002434static void set_discard_limits(struct pool_c *pt, struct queue_limits *limits)
Joe Thornber104655f2012-03-28 18:41:28 +01002435{
Mike Snitzer0424caa2012-09-26 23:45:47 +01002436 struct pool *pool = pt->pool;
2437 struct queue_limits *data_limits;
2438
Joe Thornber104655f2012-03-28 18:41:28 +01002439 limits->max_discard_sectors = pool->sectors_per_block;
2440
2441 /*
Mike Snitzer0424caa2012-09-26 23:45:47 +01002442 * discard_granularity is just a hint, and not enforced.
Joe Thornber104655f2012-03-28 18:41:28 +01002443 */
Mike Snitzer0424caa2012-09-26 23:45:47 +01002444 if (pt->adjusted_pf.discard_passdown) {
2445 data_limits = &bdev_get_queue(pt->data_dev->bdev)->limits;
2446 limits->discard_granularity = data_limits->discard_granularity;
Mike Snitzer28eed342012-10-12 21:02:07 +01002447 } else if (block_size_is_power_of_two(pool))
Mike Snitzer0424caa2012-09-26 23:45:47 +01002448 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
Mike Snitzer28eed342012-10-12 21:02:07 +01002449 else
2450 /*
2451 * Use largest power of 2 that is a factor of sectors_per_block
2452 * but at least DATA_DEV_BLOCK_SIZE_MIN_SECTORS.
2453 */
2454 limits->discard_granularity = max(1 << (ffs(pool->sectors_per_block) - 1),
2455 DATA_DEV_BLOCK_SIZE_MIN_SECTORS) << SECTOR_SHIFT;
Joe Thornber104655f2012-03-28 18:41:28 +01002456}
2457
Joe Thornber991d9fa2011-10-31 20:21:18 +00002458static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
2459{
2460 struct pool_c *pt = ti->private;
2461 struct pool *pool = pt->pool;
2462
2463 blk_limits_io_min(limits, 0);
2464 blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
Mike Snitzer0424caa2012-09-26 23:45:47 +01002465
2466 /*
2467 * pt->adjusted_pf is a staging area for the actual features to use.
2468 * They get transferred to the live pool in bind_control_target()
2469 * called from pool_preresume().
2470 */
2471 if (!pt->adjusted_pf.discard_enabled)
2472 return;
2473
2474 disable_passdown_if_not_supported(pt);
2475
2476 set_discard_limits(pt, limits);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002477}
2478
2479static struct target_type pool_target = {
2480 .name = "thin-pool",
2481 .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
2482 DM_TARGET_IMMUTABLE,
Mike Snitzer018debe2012-12-21 20:23:32 +00002483 .version = {1, 6, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00002484 .module = THIS_MODULE,
2485 .ctr = pool_ctr,
2486 .dtr = pool_dtr,
2487 .map = pool_map,
2488 .postsuspend = pool_postsuspend,
2489 .preresume = pool_preresume,
2490 .resume = pool_resume,
2491 .message = pool_message,
2492 .status = pool_status,
2493 .merge = pool_merge,
2494 .iterate_devices = pool_iterate_devices,
2495 .io_hints = pool_io_hints,
2496};
2497
2498/*----------------------------------------------------------------
2499 * Thin target methods
2500 *--------------------------------------------------------------*/
2501static void thin_dtr(struct dm_target *ti)
2502{
2503 struct thin_c *tc = ti->private;
2504
2505 mutex_lock(&dm_thin_pool_table.mutex);
2506
2507 __pool_dec(tc->pool);
2508 dm_pool_close_thin_device(tc->td);
2509 dm_put_device(ti, tc->pool_dev);
Joe Thornber2dd9c252012-03-28 18:41:28 +01002510 if (tc->origin_dev)
2511 dm_put_device(ti, tc->origin_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002512 kfree(tc);
2513
2514 mutex_unlock(&dm_thin_pool_table.mutex);
2515}
2516
2517/*
2518 * Thin target parameters:
2519 *
Joe Thornber2dd9c252012-03-28 18:41:28 +01002520 * <pool_dev> <dev_id> [origin_dev]
Joe Thornber991d9fa2011-10-31 20:21:18 +00002521 *
2522 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
2523 * dev_id: the internal device identifier
Joe Thornber2dd9c252012-03-28 18:41:28 +01002524 * origin_dev: a device external to the pool that should act as the origin
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002525 *
2526 * If the pool device has discards disabled, they get disabled for the thin
2527 * device as well.
Joe Thornber991d9fa2011-10-31 20:21:18 +00002528 */
2529static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
2530{
2531 int r;
2532 struct thin_c *tc;
Joe Thornber2dd9c252012-03-28 18:41:28 +01002533 struct dm_dev *pool_dev, *origin_dev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002534 struct mapped_device *pool_md;
2535
2536 mutex_lock(&dm_thin_pool_table.mutex);
2537
Joe Thornber2dd9c252012-03-28 18:41:28 +01002538 if (argc != 2 && argc != 3) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00002539 ti->error = "Invalid argument count";
2540 r = -EINVAL;
2541 goto out_unlock;
2542 }
2543
2544 tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
2545 if (!tc) {
2546 ti->error = "Out of memory";
2547 r = -ENOMEM;
2548 goto out_unlock;
2549 }
2550
Joe Thornber2dd9c252012-03-28 18:41:28 +01002551 if (argc == 3) {
2552 r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
2553 if (r) {
2554 ti->error = "Error opening origin device";
2555 goto bad_origin_dev;
2556 }
2557 tc->origin_dev = origin_dev;
2558 }
2559
Joe Thornber991d9fa2011-10-31 20:21:18 +00002560 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
2561 if (r) {
2562 ti->error = "Error opening pool device";
2563 goto bad_pool_dev;
2564 }
2565 tc->pool_dev = pool_dev;
2566
2567 if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
2568 ti->error = "Invalid device id";
2569 r = -EINVAL;
2570 goto bad_common;
2571 }
2572
2573 pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
2574 if (!pool_md) {
2575 ti->error = "Couldn't get pool mapped device";
2576 r = -EINVAL;
2577 goto bad_common;
2578 }
2579
2580 tc->pool = __pool_table_lookup(pool_md);
2581 if (!tc->pool) {
2582 ti->error = "Couldn't find pool object";
2583 r = -EINVAL;
2584 goto bad_pool_lookup;
2585 }
2586 __pool_inc(tc->pool);
2587
Joe Thornbere49e5822012-07-27 15:08:16 +01002588 if (get_pool_mode(tc->pool) == PM_FAIL) {
2589 ti->error = "Couldn't open thin device, Pool is in fail mode";
2590 goto bad_thin_open;
2591 }
2592
Joe Thornber991d9fa2011-10-31 20:21:18 +00002593 r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
2594 if (r) {
2595 ti->error = "Couldn't open thin internal device";
2596 goto bad_thin_open;
2597 }
2598
Mike Snitzer542f9032012-07-27 15:08:00 +01002599 r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
2600 if (r)
2601 goto bad_thin_open;
2602
Joe Thornber991d9fa2011-10-31 20:21:18 +00002603 ti->num_flush_requests = 1;
Joe Thornber16ad3d12012-07-27 15:08:07 +01002604 ti->flush_supported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002605
2606 /* In case the pool supports discards, pass them on. */
2607 if (tc->pool->pf.discard_enabled) {
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01002608 ti->discards_supported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002609 ti->num_discard_requests = 1;
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01002610 ti->discard_zeroes_data_unsupported = true;
Mikulas Patocka49296302012-07-27 15:08:03 +01002611 /* Discard requests must be split on a block boundary */
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01002612 ti->split_discard_requests = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002613 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002614
2615 dm_put(pool_md);
2616
2617 mutex_unlock(&dm_thin_pool_table.mutex);
2618
2619 return 0;
2620
2621bad_thin_open:
2622 __pool_dec(tc->pool);
2623bad_pool_lookup:
2624 dm_put(pool_md);
2625bad_common:
2626 dm_put_device(ti, tc->pool_dev);
2627bad_pool_dev:
Joe Thornber2dd9c252012-03-28 18:41:28 +01002628 if (tc->origin_dev)
2629 dm_put_device(ti, tc->origin_dev);
2630bad_origin_dev:
Joe Thornber991d9fa2011-10-31 20:21:18 +00002631 kfree(tc);
2632out_unlock:
2633 mutex_unlock(&dm_thin_pool_table.mutex);
2634
2635 return r;
2636}
2637
2638static int thin_map(struct dm_target *ti, struct bio *bio,
2639 union map_info *map_context)
2640{
Alasdair G Kergon6efd6e82012-03-28 18:41:28 +01002641 bio->bi_sector = dm_target_offset(ti, bio->bi_sector);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002642
2643 return thin_bio_map(ti, bio, map_context);
2644}
2645
Joe Thornbereb2aa482012-03-28 18:41:28 +01002646static int thin_endio(struct dm_target *ti,
2647 struct bio *bio, int err,
2648 union map_info *map_context)
2649{
2650 unsigned long flags;
Mike Snitzera24c2562012-06-03 00:30:00 +01002651 struct dm_thin_endio_hook *h = map_context->ptr;
Joe Thornbereb2aa482012-03-28 18:41:28 +01002652 struct list_head work;
Mike Snitzera24c2562012-06-03 00:30:00 +01002653 struct dm_thin_new_mapping *m, *tmp;
Joe Thornbereb2aa482012-03-28 18:41:28 +01002654 struct pool *pool = h->tc->pool;
2655
2656 if (h->shared_read_entry) {
2657 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01002658 dm_deferred_entry_dec(h->shared_read_entry, &work);
Joe Thornbereb2aa482012-03-28 18:41:28 +01002659
2660 spin_lock_irqsave(&pool->lock, flags);
2661 list_for_each_entry_safe(m, tmp, &work, list) {
2662 list_del(&m->list);
2663 m->quiesced = 1;
2664 __maybe_add_mapping(m);
2665 }
2666 spin_unlock_irqrestore(&pool->lock, flags);
2667 }
2668
Joe Thornber104655f2012-03-28 18:41:28 +01002669 if (h->all_io_entry) {
2670 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01002671 dm_deferred_entry_dec(h->all_io_entry, &work);
Joe Thornber563af182012-12-21 20:23:31 +00002672 if (!list_empty(&work)) {
2673 spin_lock_irqsave(&pool->lock, flags);
2674 list_for_each_entry_safe(m, tmp, &work, list)
2675 list_add(&m->list, &pool->prepared_discards);
2676 spin_unlock_irqrestore(&pool->lock, flags);
2677 wake_worker(pool);
2678 }
Joe Thornber104655f2012-03-28 18:41:28 +01002679 }
2680
Joe Thornbereb2aa482012-03-28 18:41:28 +01002681 mempool_free(h, pool->endio_hook_pool);
2682
2683 return 0;
2684}
2685
Joe Thornber991d9fa2011-10-31 20:21:18 +00002686static void thin_postsuspend(struct dm_target *ti)
2687{
2688 if (dm_noflush_suspending(ti))
2689 requeue_io((struct thin_c *)ti->private);
2690}
2691
2692/*
2693 * <nr mapped sectors> <highest mapped sector>
2694 */
2695static int thin_status(struct dm_target *ti, status_type_t type,
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01002696 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002697{
2698 int r;
2699 ssize_t sz = 0;
2700 dm_block_t mapped, highest;
2701 char buf[BDEVNAME_SIZE];
2702 struct thin_c *tc = ti->private;
2703
Joe Thornbere49e5822012-07-27 15:08:16 +01002704 if (get_pool_mode(tc->pool) == PM_FAIL) {
2705 DMEMIT("Fail");
2706 return 0;
2707 }
2708
Joe Thornber991d9fa2011-10-31 20:21:18 +00002709 if (!tc->td)
2710 DMEMIT("-");
2711 else {
2712 switch (type) {
2713 case STATUSTYPE_INFO:
2714 r = dm_thin_get_mapped_count(tc->td, &mapped);
2715 if (r)
2716 return r;
2717
2718 r = dm_thin_get_highest_mapped_block(tc->td, &highest);
2719 if (r < 0)
2720 return r;
2721
2722 DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
2723 if (r)
2724 DMEMIT("%llu", ((highest + 1) *
2725 tc->pool->sectors_per_block) - 1);
2726 else
2727 DMEMIT("-");
2728 break;
2729
2730 case STATUSTYPE_TABLE:
2731 DMEMIT("%s %lu",
2732 format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
2733 (unsigned long) tc->dev_id);
Joe Thornber2dd9c252012-03-28 18:41:28 +01002734 if (tc->origin_dev)
2735 DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
Joe Thornber991d9fa2011-10-31 20:21:18 +00002736 break;
2737 }
2738 }
2739
2740 return 0;
2741}
2742
2743static int thin_iterate_devices(struct dm_target *ti,
2744 iterate_devices_callout_fn fn, void *data)
2745{
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002746 sector_t blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002747 struct thin_c *tc = ti->private;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002748 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002749
2750 /*
2751 * We can't call dm_pool_get_data_dev_size() since that blocks. So
2752 * we follow a more convoluted path through to the pool's target.
2753 */
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002754 if (!pool->ti)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002755 return 0; /* nothing is bound */
2756
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002757 blocks = pool->ti->len;
2758 (void) sector_div(blocks, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002759 if (blocks)
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002760 return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002761
2762 return 0;
2763}
2764
Mike Snitzer0424caa2012-09-26 23:45:47 +01002765/*
2766 * A thin device always inherits its queue limits from its pool.
2767 */
Joe Thornber991d9fa2011-10-31 20:21:18 +00002768static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)
2769{
2770 struct thin_c *tc = ti->private;
2771
Mike Snitzer0424caa2012-09-26 23:45:47 +01002772 *limits = bdev_get_queue(tc->pool_dev->bdev)->limits;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002773}
2774
2775static struct target_type thin_target = {
2776 .name = "thin",
Mike Snitzer4f81a412012-10-12 21:02:13 +01002777 .version = {1, 5, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00002778 .module = THIS_MODULE,
2779 .ctr = thin_ctr,
2780 .dtr = thin_dtr,
2781 .map = thin_map,
Joe Thornbereb2aa482012-03-28 18:41:28 +01002782 .end_io = thin_endio,
Joe Thornber991d9fa2011-10-31 20:21:18 +00002783 .postsuspend = thin_postsuspend,
2784 .status = thin_status,
2785 .iterate_devices = thin_iterate_devices,
2786 .io_hints = thin_io_hints,
2787};
2788
2789/*----------------------------------------------------------------*/
2790
2791static int __init dm_thin_init(void)
2792{
2793 int r;
2794
2795 pool_table_init();
2796
2797 r = dm_register_target(&thin_target);
2798 if (r)
2799 return r;
2800
2801 r = dm_register_target(&pool_target);
2802 if (r)
Mike Snitzera24c2562012-06-03 00:30:00 +01002803 goto bad_pool_target;
2804
2805 r = -ENOMEM;
2806
Mike Snitzera24c2562012-06-03 00:30:00 +01002807 _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
2808 if (!_new_mapping_cache)
2809 goto bad_new_mapping_cache;
2810
2811 _endio_hook_cache = KMEM_CACHE(dm_thin_endio_hook, 0);
2812 if (!_endio_hook_cache)
2813 goto bad_endio_hook_cache;
2814
2815 return 0;
2816
2817bad_endio_hook_cache:
2818 kmem_cache_destroy(_new_mapping_cache);
2819bad_new_mapping_cache:
Mike Snitzera24c2562012-06-03 00:30:00 +01002820 dm_unregister_target(&pool_target);
2821bad_pool_target:
2822 dm_unregister_target(&thin_target);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002823
2824 return r;
2825}
2826
2827static void dm_thin_exit(void)
2828{
2829 dm_unregister_target(&thin_target);
2830 dm_unregister_target(&pool_target);
Mike Snitzera24c2562012-06-03 00:30:00 +01002831
Mike Snitzera24c2562012-06-03 00:30:00 +01002832 kmem_cache_destroy(_new_mapping_cache);
2833 kmem_cache_destroy(_endio_hook_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002834}
2835
2836module_init(dm_thin_init);
2837module_exit(dm_thin_exit);
2838
Alasdair G Kergon7cab8bf2012-05-12 01:43:19 +01002839MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
Joe Thornber991d9fa2011-10-31 20:21:18 +00002840MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2841MODULE_LICENSE("GPL");