blob: 25dfd2311a61259422c04454fc882f70bd37ecd2 [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 Thornber2dd9c252012-03-28 18:41:28 +0100371static void issue(struct thin_c *tc, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000372{
373 struct pool *pool = tc->pool;
374 unsigned long flags;
375
Joe Thornbere49e5822012-07-27 15:08:16 +0100376 if (!bio_triggers_commit(tc, bio)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000377 generic_make_request(bio);
Joe Thornbere49e5822012-07-27 15:08:16 +0100378 return;
379 }
380
381 /*
382 * Complete bio with an error if earlier I/O caused changes to
383 * the metadata that can't be committed e.g, due to I/O errors
384 * on the metadata device.
385 */
386 if (dm_thin_aborted_changes(tc->td)) {
387 bio_io_error(bio);
388 return;
389 }
390
391 /*
392 * Batch together any bios that trigger commits and then issue a
393 * single commit for them in process_deferred_bios().
394 */
395 spin_lock_irqsave(&pool->lock, flags);
396 bio_list_add(&pool->deferred_flush_bios, bio);
397 spin_unlock_irqrestore(&pool->lock, flags);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000398}
399
Joe Thornber2dd9c252012-03-28 18:41:28 +0100400static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
401{
402 remap_to_origin(tc, bio);
403 issue(tc, bio);
404}
405
406static void remap_and_issue(struct thin_c *tc, struct bio *bio,
407 dm_block_t block)
408{
409 remap(tc, bio, block);
410 issue(tc, bio);
411}
412
Joe Thornber991d9fa2011-10-31 20:21:18 +0000413/*
414 * wake_worker() is used when new work is queued and when pool_resume is
415 * ready to continue deferred IO processing.
416 */
417static void wake_worker(struct pool *pool)
418{
419 queue_work(pool->wq, &pool->worker);
420}
421
422/*----------------------------------------------------------------*/
423
424/*
425 * Bio endio functions.
426 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100427struct dm_thin_new_mapping {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000428 struct list_head list;
429
Joe Thornbereb2aa482012-03-28 18:41:28 +0100430 unsigned quiesced:1;
431 unsigned prepared:1;
Joe Thornber104655f2012-03-28 18:41:28 +0100432 unsigned pass_discard:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000433
434 struct thin_c *tc;
435 dm_block_t virt_block;
436 dm_block_t data_block;
Mike Snitzera24c2562012-06-03 00:30:00 +0100437 struct dm_bio_prison_cell *cell, *cell2;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000438 int err;
439
440 /*
441 * If the bio covers the whole area of a block then we can avoid
442 * zeroing or copying. Instead this bio is hooked. The bio will
443 * still be in the cell, so care has to be taken to avoid issuing
444 * the bio twice.
445 */
446 struct bio *bio;
447 bio_end_io_t *saved_bi_end_io;
448};
449
Mike Snitzera24c2562012-06-03 00:30:00 +0100450static void __maybe_add_mapping(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000451{
452 struct pool *pool = m->tc->pool;
453
Joe Thornbereb2aa482012-03-28 18:41:28 +0100454 if (m->quiesced && m->prepared) {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000455 list_add(&m->list, &pool->prepared_mappings);
456 wake_worker(pool);
457 }
458}
459
460static void copy_complete(int read_err, unsigned long write_err, void *context)
461{
462 unsigned long flags;
Mike Snitzera24c2562012-06-03 00:30:00 +0100463 struct dm_thin_new_mapping *m = context;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000464 struct pool *pool = m->tc->pool;
465
466 m->err = read_err || write_err ? -EIO : 0;
467
468 spin_lock_irqsave(&pool->lock, flags);
469 m->prepared = 1;
470 __maybe_add_mapping(m);
471 spin_unlock_irqrestore(&pool->lock, flags);
472}
473
474static void overwrite_endio(struct bio *bio, int err)
475{
476 unsigned long flags;
Mike Snitzera24c2562012-06-03 00:30:00 +0100477 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
478 struct dm_thin_new_mapping *m = h->overwrite_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000479 struct pool *pool = m->tc->pool;
480
481 m->err = err;
482
483 spin_lock_irqsave(&pool->lock, flags);
484 m->prepared = 1;
485 __maybe_add_mapping(m);
486 spin_unlock_irqrestore(&pool->lock, flags);
487}
488
Joe Thornber991d9fa2011-10-31 20:21:18 +0000489/*----------------------------------------------------------------*/
490
491/*
492 * Workqueue.
493 */
494
495/*
496 * Prepared mapping jobs.
497 */
498
499/*
500 * This sends the bios in the cell back to the deferred_bios list.
501 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100502static void cell_defer(struct thin_c *tc, struct dm_bio_prison_cell *cell,
Joe Thornber991d9fa2011-10-31 20:21:18 +0000503 dm_block_t data_block)
504{
505 struct pool *pool = tc->pool;
506 unsigned long flags;
507
508 spin_lock_irqsave(&pool->lock, flags);
Mike Snitzer44feb382012-10-12 21:02:10 +0100509 dm_cell_release(cell, &pool->deferred_bios);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000510 spin_unlock_irqrestore(&tc->pool->lock, flags);
511
512 wake_worker(pool);
513}
514
515/*
Joe Thornberb7ca9c92012-12-21 20:23:31 +0000516 * Same as cell_defer except it omits the original holder of the cell.
Joe Thornber991d9fa2011-10-31 20:21:18 +0000517 */
Mike Snitzera24c2562012-06-03 00:30:00 +0100518static void cell_defer_except(struct thin_c *tc, struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000519{
520 struct bio_list bios;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000521 struct pool *pool = tc->pool;
522 unsigned long flags;
523
524 bio_list_init(&bios);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000525
526 spin_lock_irqsave(&pool->lock, flags);
Mike Snitzer44feb382012-10-12 21:02:10 +0100527 dm_cell_release_no_holder(cell, &pool->deferred_bios);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000528 spin_unlock_irqrestore(&pool->lock, flags);
529
530 wake_worker(pool);
531}
532
Joe Thornbere49e5822012-07-27 15:08:16 +0100533static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
534{
535 if (m->bio)
536 m->bio->bi_end_io = m->saved_bi_end_io;
Mike Snitzer44feb382012-10-12 21:02:10 +0100537 dm_cell_error(m->cell);
Joe Thornbere49e5822012-07-27 15:08:16 +0100538 list_del(&m->list);
539 mempool_free(m, m->tc->pool->mapping_pool);
540}
Mike Snitzera24c2562012-06-03 00:30:00 +0100541static void process_prepared_mapping(struct dm_thin_new_mapping *m)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000542{
543 struct thin_c *tc = m->tc;
544 struct bio *bio;
545 int r;
546
547 bio = m->bio;
548 if (bio)
549 bio->bi_end_io = m->saved_bi_end_io;
550
551 if (m->err) {
Mike Snitzer44feb382012-10-12 21:02:10 +0100552 dm_cell_error(m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100553 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000554 }
555
556 /*
557 * Commit the prepared block into the mapping btree.
558 * Any I/O for this block arriving after this point will get
559 * remapped to it directly.
560 */
561 r = dm_thin_insert_block(tc->td, m->virt_block, m->data_block);
562 if (r) {
563 DMERR("dm_thin_insert_block() failed");
Mike Snitzer44feb382012-10-12 21:02:10 +0100564 dm_cell_error(m->cell);
Joe Thornber905386f2012-07-27 15:08:05 +0100565 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000566 }
567
568 /*
569 * Release any bios held while the block was being provisioned.
570 * If we are processing a write bio that completely covers the block,
571 * we already processed it so can ignore it now when processing
572 * the bios in the cell.
573 */
574 if (bio) {
Joe Thornber6f94a4c2012-03-28 18:41:23 +0100575 cell_defer_except(tc, m->cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000576 bio_endio(bio, 0);
577 } else
578 cell_defer(tc, m->cell, m->data_block);
579
Joe Thornber905386f2012-07-27 15:08:05 +0100580out:
Joe Thornber991d9fa2011-10-31 20:21:18 +0000581 list_del(&m->list);
582 mempool_free(m, tc->pool->mapping_pool);
583}
584
Joe Thornbere49e5822012-07-27 15:08:16 +0100585static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
Joe Thornber104655f2012-03-28 18:41:28 +0100586{
Joe Thornber104655f2012-03-28 18:41:28 +0100587 struct thin_c *tc = m->tc;
588
Joe Thornbere49e5822012-07-27 15:08:16 +0100589 bio_io_error(m->bio);
590 cell_defer_except(tc, m->cell);
591 cell_defer_except(tc, m->cell2);
592 mempool_free(m, tc->pool->mapping_pool);
593}
Joe Thornber104655f2012-03-28 18:41:28 +0100594
Joe Thornbere49e5822012-07-27 15:08:16 +0100595static void process_prepared_discard_passdown(struct dm_thin_new_mapping *m)
596{
597 struct thin_c *tc = m->tc;
598
Joe Thornber104655f2012-03-28 18:41:28 +0100599 if (m->pass_discard)
600 remap_and_issue(tc, m->bio, m->data_block);
601 else
602 bio_endio(m->bio, 0);
603
604 cell_defer_except(tc, m->cell);
605 cell_defer_except(tc, m->cell2);
606 mempool_free(m, tc->pool->mapping_pool);
607}
608
Joe Thornbere49e5822012-07-27 15:08:16 +0100609static void process_prepared_discard(struct dm_thin_new_mapping *m)
610{
611 int r;
612 struct thin_c *tc = m->tc;
613
614 r = dm_thin_remove_block(tc->td, m->virt_block);
615 if (r)
616 DMERR("dm_thin_remove_block() failed");
617
618 process_prepared_discard_passdown(m);
619}
620
Joe Thornber104655f2012-03-28 18:41:28 +0100621static void process_prepared(struct pool *pool, struct list_head *head,
Joe Thornbere49e5822012-07-27 15:08:16 +0100622 process_mapping_fn *fn)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000623{
624 unsigned long flags;
625 struct list_head maps;
Mike Snitzera24c2562012-06-03 00:30:00 +0100626 struct dm_thin_new_mapping *m, *tmp;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000627
628 INIT_LIST_HEAD(&maps);
629 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +0100630 list_splice_init(head, &maps);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000631 spin_unlock_irqrestore(&pool->lock, flags);
632
633 list_for_each_entry_safe(m, tmp, &maps, list)
Joe Thornbere49e5822012-07-27 15:08:16 +0100634 (*fn)(m);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000635}
636
637/*
638 * Deferred bio jobs.
639 */
Joe Thornber104655f2012-03-28 18:41:28 +0100640static int io_overlaps_block(struct pool *pool, struct bio *bio)
641{
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +0100642 return bio->bi_size == (pool->sectors_per_block << SECTOR_SHIFT);
Joe Thornber104655f2012-03-28 18:41:28 +0100643}
644
Joe Thornber991d9fa2011-10-31 20:21:18 +0000645static int io_overwrites_block(struct pool *pool, struct bio *bio)
646{
Joe Thornber104655f2012-03-28 18:41:28 +0100647 return (bio_data_dir(bio) == WRITE) &&
648 io_overlaps_block(pool, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000649}
650
651static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
652 bio_end_io_t *fn)
653{
654 *save = bio->bi_end_io;
655 bio->bi_end_io = fn;
656}
657
658static int ensure_next_mapping(struct pool *pool)
659{
660 if (pool->next_mapping)
661 return 0;
662
663 pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
664
665 return pool->next_mapping ? 0 : -ENOMEM;
666}
667
Mike Snitzera24c2562012-06-03 00:30:00 +0100668static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000669{
Mike Snitzera24c2562012-06-03 00:30:00 +0100670 struct dm_thin_new_mapping *r = pool->next_mapping;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000671
672 BUG_ON(!pool->next_mapping);
673
674 pool->next_mapping = NULL;
675
676 return r;
677}
678
679static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
Joe Thornber2dd9c252012-03-28 18:41:28 +0100680 struct dm_dev *origin, dm_block_t data_origin,
681 dm_block_t data_dest,
Mike Snitzera24c2562012-06-03 00:30:00 +0100682 struct dm_bio_prison_cell *cell, struct bio *bio)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000683{
684 int r;
685 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +0100686 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000687
688 INIT_LIST_HEAD(&m->list);
Joe Thornbereb2aa482012-03-28 18:41:28 +0100689 m->quiesced = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000690 m->prepared = 0;
691 m->tc = tc;
692 m->virt_block = virt_block;
693 m->data_block = data_dest;
694 m->cell = cell;
695 m->err = 0;
696 m->bio = NULL;
697
Mike Snitzer44feb382012-10-12 21:02:10 +0100698 if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
Joe Thornbereb2aa482012-03-28 18:41:28 +0100699 m->quiesced = 1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000700
701 /*
702 * IO to pool_dev remaps to the pool target's data_dev.
703 *
704 * If the whole block of data is being overwritten, we can issue the
705 * bio immediately. Otherwise we use kcopyd to clone the data first.
706 */
707 if (io_overwrites_block(pool, bio)) {
Mike Snitzera24c2562012-06-03 00:30:00 +0100708 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
709
Joe Thornbereb2aa482012-03-28 18:41:28 +0100710 h->overwrite_mapping = m;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000711 m->bio = bio;
712 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000713 remap_and_issue(tc, bio, data_dest);
714 } else {
715 struct dm_io_region from, to;
716
Joe Thornber2dd9c252012-03-28 18:41:28 +0100717 from.bdev = origin->bdev;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000718 from.sector = data_origin * pool->sectors_per_block;
719 from.count = pool->sectors_per_block;
720
721 to.bdev = tc->pool_dev->bdev;
722 to.sector = data_dest * pool->sectors_per_block;
723 to.count = pool->sectors_per_block;
724
725 r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
726 0, copy_complete, m);
727 if (r < 0) {
728 mempool_free(m, pool->mapping_pool);
729 DMERR("dm_kcopyd_copy() failed");
Mike Snitzer44feb382012-10-12 21:02:10 +0100730 dm_cell_error(cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000731 }
732 }
733}
734
Joe Thornber2dd9c252012-03-28 18:41:28 +0100735static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
736 dm_block_t data_origin, dm_block_t data_dest,
Mike Snitzera24c2562012-06-03 00:30:00 +0100737 struct dm_bio_prison_cell *cell, struct bio *bio)
Joe Thornber2dd9c252012-03-28 18:41:28 +0100738{
739 schedule_copy(tc, virt_block, tc->pool_dev,
740 data_origin, data_dest, cell, bio);
741}
742
743static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
744 dm_block_t data_dest,
Mike Snitzera24c2562012-06-03 00:30:00 +0100745 struct dm_bio_prison_cell *cell, struct bio *bio)
Joe Thornber2dd9c252012-03-28 18:41:28 +0100746{
747 schedule_copy(tc, virt_block, tc->origin_dev,
748 virt_block, data_dest, cell, bio);
749}
750
Joe Thornber991d9fa2011-10-31 20:21:18 +0000751static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
Mike Snitzera24c2562012-06-03 00:30:00 +0100752 dm_block_t data_block, struct dm_bio_prison_cell *cell,
Joe Thornber991d9fa2011-10-31 20:21:18 +0000753 struct bio *bio)
754{
755 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +0100756 struct dm_thin_new_mapping *m = get_next_mapping(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000757
758 INIT_LIST_HEAD(&m->list);
Joe Thornbereb2aa482012-03-28 18:41:28 +0100759 m->quiesced = 1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000760 m->prepared = 0;
761 m->tc = tc;
762 m->virt_block = virt_block;
763 m->data_block = data_block;
764 m->cell = cell;
765 m->err = 0;
766 m->bio = NULL;
767
768 /*
769 * If the whole block of data is being overwritten or we are not
770 * zeroing pre-existing data, we can issue the bio immediately.
771 * Otherwise we use kcopyd to zero the data first.
772 */
Joe Thornber67e2e2b2012-03-28 18:41:29 +0100773 if (!pool->pf.zero_new_blocks)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000774 process_prepared_mapping(m);
775
776 else if (io_overwrites_block(pool, bio)) {
Mike Snitzera24c2562012-06-03 00:30:00 +0100777 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
778
Joe Thornbereb2aa482012-03-28 18:41:28 +0100779 h->overwrite_mapping = m;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000780 m->bio = bio;
781 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000782 remap_and_issue(tc, bio, data_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000783 } else {
784 int r;
785 struct dm_io_region to;
786
787 to.bdev = tc->pool_dev->bdev;
788 to.sector = data_block * pool->sectors_per_block;
789 to.count = pool->sectors_per_block;
790
791 r = dm_kcopyd_zero(pool->copier, 1, &to, 0, copy_complete, m);
792 if (r < 0) {
793 mempool_free(m, pool->mapping_pool);
794 DMERR("dm_kcopyd_zero() failed");
Mike Snitzer44feb382012-10-12 21:02:10 +0100795 dm_cell_error(cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000796 }
797 }
798}
799
Joe Thornbere49e5822012-07-27 15:08:16 +0100800static int commit(struct pool *pool)
801{
802 int r;
803
804 r = dm_pool_commit_metadata(pool->pmd);
805 if (r)
806 DMERR("commit failed, error = %d", r);
807
808 return r;
809}
810
811/*
812 * A non-zero return indicates read_only or fail_io mode.
813 * Many callers don't care about the return value.
814 */
815static int commit_or_fallback(struct pool *pool)
816{
817 int r;
818
819 if (get_pool_mode(pool) != PM_WRITE)
820 return -EINVAL;
821
822 r = commit(pool);
823 if (r)
824 set_pool_mode(pool, PM_READ_ONLY);
825
826 return r;
827}
828
Joe Thornber991d9fa2011-10-31 20:21:18 +0000829static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
830{
831 int r;
832 dm_block_t free_blocks;
833 unsigned long flags;
834 struct pool *pool = tc->pool;
835
836 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
837 if (r)
838 return r;
839
840 if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
841 DMWARN("%s: reached low water mark, sending event.",
842 dm_device_name(pool->pool_md));
843 spin_lock_irqsave(&pool->lock, flags);
844 pool->low_water_triggered = 1;
845 spin_unlock_irqrestore(&pool->lock, flags);
846 dm_table_event(pool->ti->table);
847 }
848
849 if (!free_blocks) {
850 if (pool->no_free_space)
851 return -ENOSPC;
852 else {
853 /*
854 * Try to commit to see if that will free up some
855 * more space.
856 */
Joe Thornbere49e5822012-07-27 15:08:16 +0100857 (void) commit_or_fallback(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000858
859 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
860 if (r)
861 return r;
862
863 /*
864 * If we still have no space we set a flag to avoid
865 * doing all this checking and return -ENOSPC.
866 */
867 if (!free_blocks) {
868 DMWARN("%s: no free space available.",
869 dm_device_name(pool->pool_md));
870 spin_lock_irqsave(&pool->lock, flags);
871 pool->no_free_space = 1;
872 spin_unlock_irqrestore(&pool->lock, flags);
873 return -ENOSPC;
874 }
875 }
876 }
877
878 r = dm_pool_alloc_data_block(pool->pmd, result);
879 if (r)
880 return r;
881
882 return 0;
883}
884
885/*
886 * If we have run out of space, queue bios until the device is
887 * resumed, presumably after having been reloaded with more space.
888 */
889static void retry_on_resume(struct bio *bio)
890{
Mike Snitzera24c2562012-06-03 00:30:00 +0100891 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
Joe Thornbereb2aa482012-03-28 18:41:28 +0100892 struct thin_c *tc = h->tc;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000893 struct pool *pool = tc->pool;
894 unsigned long flags;
895
896 spin_lock_irqsave(&pool->lock, flags);
897 bio_list_add(&pool->retry_on_resume_list, bio);
898 spin_unlock_irqrestore(&pool->lock, flags);
899}
900
Mike Snitzera24c2562012-06-03 00:30:00 +0100901static void no_space(struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000902{
903 struct bio *bio;
904 struct bio_list bios;
905
906 bio_list_init(&bios);
Mike Snitzer44feb382012-10-12 21:02:10 +0100907 dm_cell_release(cell, &bios);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000908
909 while ((bio = bio_list_pop(&bios)))
910 retry_on_resume(bio);
911}
912
Joe Thornber104655f2012-03-28 18:41:28 +0100913static void process_discard(struct thin_c *tc, struct bio *bio)
914{
915 int r;
Mike Snitzerc3a0ce22012-05-12 01:43:16 +0100916 unsigned long flags;
Joe Thornber104655f2012-03-28 18:41:28 +0100917 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +0100918 struct dm_bio_prison_cell *cell, *cell2;
Mike Snitzer44feb382012-10-12 21:02:10 +0100919 struct dm_cell_key key, key2;
Joe Thornber104655f2012-03-28 18:41:28 +0100920 dm_block_t block = get_bio_block(tc, bio);
921 struct dm_thin_lookup_result lookup_result;
Mike Snitzera24c2562012-06-03 00:30:00 +0100922 struct dm_thin_new_mapping *m;
Joe Thornber104655f2012-03-28 18:41:28 +0100923
924 build_virtual_key(tc->td, block, &key);
Mike Snitzer44feb382012-10-12 21:02:10 +0100925 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell))
Joe Thornber104655f2012-03-28 18:41:28 +0100926 return;
927
928 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
929 switch (r) {
930 case 0:
931 /*
932 * Check nobody is fiddling with this pool block. This can
933 * happen if someone's in the process of breaking sharing
934 * on this block.
935 */
936 build_data_key(tc->td, lookup_result.block, &key2);
Mike Snitzer44feb382012-10-12 21:02:10 +0100937 if (dm_bio_detain(tc->pool->prison, &key2, bio, &cell2)) {
Joe Thornberb7ca9c92012-12-21 20:23:31 +0000938 cell_defer_except(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +0100939 break;
940 }
941
942 if (io_overlaps_block(pool, bio)) {
943 /*
944 * IO may still be going to the destination block. We must
945 * quiesce before we can do the removal.
946 */
947 m = get_next_mapping(pool);
948 m->tc = tc;
Mike Snitzer17b7d632012-07-27 15:07:57 +0100949 m->pass_discard = (!lookup_result.shared) && pool->pf.discard_passdown;
Joe Thornber104655f2012-03-28 18:41:28 +0100950 m->virt_block = block;
951 m->data_block = lookup_result.block;
952 m->cell = cell;
953 m->cell2 = cell2;
954 m->err = 0;
955 m->bio = bio;
956
Mike Snitzer44feb382012-10-12 21:02:10 +0100957 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list)) {
Mike Snitzerc3a0ce22012-05-12 01:43:16 +0100958 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +0100959 list_add(&m->list, &pool->prepared_discards);
Mike Snitzerc3a0ce22012-05-12 01:43:16 +0100960 spin_unlock_irqrestore(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +0100961 wake_worker(pool);
962 }
963 } else {
964 /*
Mikulas Patocka49296302012-07-27 15:08:03 +0100965 * The DM core makes sure that the discard doesn't span
966 * a block boundary. So we submit the discard of a
967 * partial block appropriately.
Joe Thornber104655f2012-03-28 18:41:28 +0100968 */
Joe Thornberb7ca9c92012-12-21 20:23:31 +0000969 cell_defer_except(tc, cell);
970 cell_defer_except(tc, cell2);
Mikulas Patocka650d2a02012-07-20 14:25:05 +0100971 if ((!lookup_result.shared) && pool->pf.discard_passdown)
972 remap_and_issue(tc, bio, lookup_result.block);
973 else
974 bio_endio(bio, 0);
Joe Thornber104655f2012-03-28 18:41:28 +0100975 }
976 break;
977
978 case -ENODATA:
979 /*
980 * It isn't provisioned, just forget it.
981 */
Joe Thornberb7ca9c92012-12-21 20:23:31 +0000982 cell_defer_except(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +0100983 bio_endio(bio, 0);
984 break;
985
986 default:
987 DMERR("discard: find block unexpectedly returned %d", r);
Joe Thornberb7ca9c92012-12-21 20:23:31 +0000988 cell_defer_except(tc, cell);
Joe Thornber104655f2012-03-28 18:41:28 +0100989 bio_io_error(bio);
990 break;
991 }
992}
993
Joe Thornber991d9fa2011-10-31 20:21:18 +0000994static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzer44feb382012-10-12 21:02:10 +0100995 struct dm_cell_key *key,
Joe Thornber991d9fa2011-10-31 20:21:18 +0000996 struct dm_thin_lookup_result *lookup_result,
Mike Snitzera24c2562012-06-03 00:30:00 +0100997 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000998{
999 int r;
1000 dm_block_t data_block;
1001
1002 r = alloc_data_block(tc, &data_block);
1003 switch (r) {
1004 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001005 schedule_internal_copy(tc, block, lookup_result->block,
1006 data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001007 break;
1008
1009 case -ENOSPC:
1010 no_space(cell);
1011 break;
1012
1013 default:
1014 DMERR("%s: alloc_data_block() failed, error = %d", __func__, r);
Mike Snitzer44feb382012-10-12 21:02:10 +01001015 dm_cell_error(cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001016 break;
1017 }
1018}
1019
1020static void process_shared_bio(struct thin_c *tc, struct bio *bio,
1021 dm_block_t block,
1022 struct dm_thin_lookup_result *lookup_result)
1023{
Mike Snitzera24c2562012-06-03 00:30:00 +01001024 struct dm_bio_prison_cell *cell;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001025 struct pool *pool = tc->pool;
Mike Snitzer44feb382012-10-12 21:02:10 +01001026 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001027
1028 /*
1029 * If cell is already occupied, then sharing is already in the process
1030 * of being broken so we have nothing further to do here.
1031 */
1032 build_data_key(tc->td, lookup_result->block, &key);
Mike Snitzer44feb382012-10-12 21:02:10 +01001033 if (dm_bio_detain(pool->prison, &key, bio, &cell))
Joe Thornber991d9fa2011-10-31 20:21:18 +00001034 return;
1035
Joe Thornber60049702012-07-27 15:08:06 +01001036 if (bio_data_dir(bio) == WRITE && bio->bi_size)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001037 break_sharing(tc, bio, block, &key, lookup_result, cell);
1038 else {
Mike Snitzera24c2562012-06-03 00:30:00 +01001039 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001040
Mike Snitzer44feb382012-10-12 21:02:10 +01001041 h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001042
Joe Thornberb7ca9c92012-12-21 20:23:31 +00001043 cell_defer_except(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001044 remap_and_issue(tc, bio, lookup_result->block);
1045 }
1046}
1047
1048static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
Mike Snitzera24c2562012-06-03 00:30:00 +01001049 struct dm_bio_prison_cell *cell)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001050{
1051 int r;
1052 dm_block_t data_block;
1053
1054 /*
1055 * Remap empty bios (flushes) immediately, without provisioning.
1056 */
1057 if (!bio->bi_size) {
Joe Thornberb7ca9c92012-12-21 20:23:31 +00001058 cell_defer_except(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001059 remap_and_issue(tc, bio, 0);
1060 return;
1061 }
1062
1063 /*
1064 * Fill read bios with zeroes and complete them immediately.
1065 */
1066 if (bio_data_dir(bio) == READ) {
1067 zero_fill_bio(bio);
Joe Thornberb7ca9c92012-12-21 20:23:31 +00001068 cell_defer_except(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001069 bio_endio(bio, 0);
1070 return;
1071 }
1072
1073 r = alloc_data_block(tc, &data_block);
1074 switch (r) {
1075 case 0:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001076 if (tc->origin_dev)
1077 schedule_external_copy(tc, block, data_block, cell, bio);
1078 else
1079 schedule_zero(tc, block, data_block, cell, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001080 break;
1081
1082 case -ENOSPC:
1083 no_space(cell);
1084 break;
1085
1086 default:
1087 DMERR("%s: alloc_data_block() failed, error = %d", __func__, r);
Joe Thornbere49e5822012-07-27 15:08:16 +01001088 set_pool_mode(tc->pool, PM_READ_ONLY);
Mike Snitzer44feb382012-10-12 21:02:10 +01001089 dm_cell_error(cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001090 break;
1091 }
1092}
1093
1094static void process_bio(struct thin_c *tc, struct bio *bio)
1095{
1096 int r;
1097 dm_block_t block = get_bio_block(tc, bio);
Mike Snitzera24c2562012-06-03 00:30:00 +01001098 struct dm_bio_prison_cell *cell;
Mike Snitzer44feb382012-10-12 21:02:10 +01001099 struct dm_cell_key key;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001100 struct dm_thin_lookup_result lookup_result;
1101
1102 /*
1103 * If cell is already occupied, then the block is already
1104 * being provisioned so we have nothing further to do here.
1105 */
1106 build_virtual_key(tc->td, block, &key);
Mike Snitzer44feb382012-10-12 21:02:10 +01001107 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell))
Joe Thornber991d9fa2011-10-31 20:21:18 +00001108 return;
1109
1110 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1111 switch (r) {
1112 case 0:
1113 /*
1114 * We can release this cell now. This thread is the only
1115 * one that puts bios into a cell, and we know there were
1116 * no preceding bios.
1117 */
1118 /*
1119 * TODO: this will probably have to change when discard goes
1120 * back in.
1121 */
Joe Thornberb7ca9c92012-12-21 20:23:31 +00001122 cell_defer_except(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001123
1124 if (lookup_result.shared)
1125 process_shared_bio(tc, bio, block, &lookup_result);
1126 else
1127 remap_and_issue(tc, bio, lookup_result.block);
1128 break;
1129
1130 case -ENODATA:
Joe Thornber2dd9c252012-03-28 18:41:28 +01001131 if (bio_data_dir(bio) == READ && tc->origin_dev) {
Joe Thornberb7ca9c92012-12-21 20:23:31 +00001132 cell_defer_except(tc, cell);
Joe Thornber2dd9c252012-03-28 18:41:28 +01001133 remap_to_origin_and_issue(tc, bio);
1134 } else
1135 provision_block(tc, bio, block, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001136 break;
1137
1138 default:
1139 DMERR("dm_thin_find_block() failed, error = %d", r);
Joe Thornberb7ca9c92012-12-21 20:23:31 +00001140 cell_defer_except(tc, cell);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001141 bio_io_error(bio);
1142 break;
1143 }
1144}
1145
Joe Thornbere49e5822012-07-27 15:08:16 +01001146static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
1147{
1148 int r;
1149 int rw = bio_data_dir(bio);
1150 dm_block_t block = get_bio_block(tc, bio);
1151 struct dm_thin_lookup_result lookup_result;
1152
1153 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1154 switch (r) {
1155 case 0:
1156 if (lookup_result.shared && (rw == WRITE) && bio->bi_size)
1157 bio_io_error(bio);
1158 else
1159 remap_and_issue(tc, bio, lookup_result.block);
1160 break;
1161
1162 case -ENODATA:
1163 if (rw != READ) {
1164 bio_io_error(bio);
1165 break;
1166 }
1167
1168 if (tc->origin_dev) {
1169 remap_to_origin_and_issue(tc, bio);
1170 break;
1171 }
1172
1173 zero_fill_bio(bio);
1174 bio_endio(bio, 0);
1175 break;
1176
1177 default:
1178 DMERR("dm_thin_find_block() failed, error = %d", r);
1179 bio_io_error(bio);
1180 break;
1181 }
1182}
1183
1184static void process_bio_fail(struct thin_c *tc, struct bio *bio)
1185{
1186 bio_io_error(bio);
1187}
1188
Joe Thornber905e51b2012-03-28 18:41:27 +01001189static int need_commit_due_to_time(struct pool *pool)
1190{
1191 return jiffies < pool->last_commit_jiffies ||
1192 jiffies > pool->last_commit_jiffies + COMMIT_PERIOD;
1193}
1194
Joe Thornber991d9fa2011-10-31 20:21:18 +00001195static void process_deferred_bios(struct pool *pool)
1196{
1197 unsigned long flags;
1198 struct bio *bio;
1199 struct bio_list bios;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001200
1201 bio_list_init(&bios);
1202
1203 spin_lock_irqsave(&pool->lock, flags);
1204 bio_list_merge(&bios, &pool->deferred_bios);
1205 bio_list_init(&pool->deferred_bios);
1206 spin_unlock_irqrestore(&pool->lock, flags);
1207
1208 while ((bio = bio_list_pop(&bios))) {
Mike Snitzera24c2562012-06-03 00:30:00 +01001209 struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
Joe Thornbereb2aa482012-03-28 18:41:28 +01001210 struct thin_c *tc = h->tc;
1211
Joe Thornber991d9fa2011-10-31 20:21:18 +00001212 /*
1213 * If we've got no free new_mapping structs, and processing
1214 * this bio might require one, we pause until there are some
1215 * prepared mappings to process.
1216 */
1217 if (ensure_next_mapping(pool)) {
1218 spin_lock_irqsave(&pool->lock, flags);
1219 bio_list_merge(&pool->deferred_bios, &bios);
1220 spin_unlock_irqrestore(&pool->lock, flags);
1221
1222 break;
1223 }
Joe Thornber104655f2012-03-28 18:41:28 +01001224
1225 if (bio->bi_rw & REQ_DISCARD)
Joe Thornbere49e5822012-07-27 15:08:16 +01001226 pool->process_discard(tc, bio);
Joe Thornber104655f2012-03-28 18:41:28 +01001227 else
Joe Thornbere49e5822012-07-27 15:08:16 +01001228 pool->process_bio(tc, bio);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001229 }
1230
1231 /*
1232 * If there are any deferred flush bios, we must commit
1233 * the metadata before issuing them.
1234 */
1235 bio_list_init(&bios);
1236 spin_lock_irqsave(&pool->lock, flags);
1237 bio_list_merge(&bios, &pool->deferred_flush_bios);
1238 bio_list_init(&pool->deferred_flush_bios);
1239 spin_unlock_irqrestore(&pool->lock, flags);
1240
Joe Thornber905e51b2012-03-28 18:41:27 +01001241 if (bio_list_empty(&bios) && !need_commit_due_to_time(pool))
Joe Thornber991d9fa2011-10-31 20:21:18 +00001242 return;
1243
Joe Thornbere49e5822012-07-27 15:08:16 +01001244 if (commit_or_fallback(pool)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001245 while ((bio = bio_list_pop(&bios)))
1246 bio_io_error(bio);
1247 return;
1248 }
Joe Thornber905e51b2012-03-28 18:41:27 +01001249 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001250
1251 while ((bio = bio_list_pop(&bios)))
1252 generic_make_request(bio);
1253}
1254
1255static void do_worker(struct work_struct *ws)
1256{
1257 struct pool *pool = container_of(ws, struct pool, worker);
1258
Joe Thornbere49e5822012-07-27 15:08:16 +01001259 process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
1260 process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001261 process_deferred_bios(pool);
1262}
1263
Joe Thornber905e51b2012-03-28 18:41:27 +01001264/*
1265 * We want to commit periodically so that not too much
1266 * unwritten data builds up.
1267 */
1268static void do_waker(struct work_struct *ws)
1269{
1270 struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
1271 wake_worker(pool);
1272 queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
1273}
1274
Joe Thornber991d9fa2011-10-31 20:21:18 +00001275/*----------------------------------------------------------------*/
1276
Joe Thornbere49e5822012-07-27 15:08:16 +01001277static enum pool_mode get_pool_mode(struct pool *pool)
1278{
1279 return pool->pf.mode;
1280}
1281
1282static void set_pool_mode(struct pool *pool, enum pool_mode mode)
1283{
1284 int r;
1285
1286 pool->pf.mode = mode;
1287
1288 switch (mode) {
1289 case PM_FAIL:
1290 DMERR("switching pool to failure mode");
1291 pool->process_bio = process_bio_fail;
1292 pool->process_discard = process_bio_fail;
1293 pool->process_prepared_mapping = process_prepared_mapping_fail;
1294 pool->process_prepared_discard = process_prepared_discard_fail;
1295 break;
1296
1297 case PM_READ_ONLY:
1298 DMERR("switching pool to read-only mode");
1299 r = dm_pool_abort_metadata(pool->pmd);
1300 if (r) {
1301 DMERR("aborting transaction failed");
1302 set_pool_mode(pool, PM_FAIL);
1303 } else {
1304 dm_pool_metadata_read_only(pool->pmd);
1305 pool->process_bio = process_bio_read_only;
1306 pool->process_discard = process_discard;
1307 pool->process_prepared_mapping = process_prepared_mapping_fail;
1308 pool->process_prepared_discard = process_prepared_discard_passdown;
1309 }
1310 break;
1311
1312 case PM_WRITE:
1313 pool->process_bio = process_bio;
1314 pool->process_discard = process_discard;
1315 pool->process_prepared_mapping = process_prepared_mapping;
1316 pool->process_prepared_discard = process_prepared_discard;
1317 break;
1318 }
1319}
1320
1321/*----------------------------------------------------------------*/
1322
Joe Thornber991d9fa2011-10-31 20:21:18 +00001323/*
1324 * Mapping functions.
1325 */
1326
1327/*
1328 * Called only while mapping a thin bio to hand it over to the workqueue.
1329 */
1330static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
1331{
1332 unsigned long flags;
1333 struct pool *pool = tc->pool;
1334
1335 spin_lock_irqsave(&pool->lock, flags);
1336 bio_list_add(&pool->deferred_bios, bio);
1337 spin_unlock_irqrestore(&pool->lock, flags);
1338
1339 wake_worker(pool);
1340}
1341
Mike Snitzera24c2562012-06-03 00:30:00 +01001342static struct dm_thin_endio_hook *thin_hook_bio(struct thin_c *tc, struct bio *bio)
Joe Thornbereb2aa482012-03-28 18:41:28 +01001343{
1344 struct pool *pool = tc->pool;
Mike Snitzera24c2562012-06-03 00:30:00 +01001345 struct dm_thin_endio_hook *h = mempool_alloc(pool->endio_hook_pool, GFP_NOIO);
Joe Thornbereb2aa482012-03-28 18:41:28 +01001346
1347 h->tc = tc;
1348 h->shared_read_entry = NULL;
Mike Snitzer44feb382012-10-12 21:02:10 +01001349 h->all_io_entry = bio->bi_rw & REQ_DISCARD ? NULL : dm_deferred_entry_inc(pool->all_io_ds);
Joe Thornbereb2aa482012-03-28 18:41:28 +01001350 h->overwrite_mapping = NULL;
1351
1352 return h;
1353}
1354
Joe Thornber991d9fa2011-10-31 20:21:18 +00001355/*
1356 * Non-blocking function called from the thin target's map function.
1357 */
1358static int thin_bio_map(struct dm_target *ti, struct bio *bio,
1359 union map_info *map_context)
1360{
1361 int r;
1362 struct thin_c *tc = ti->private;
1363 dm_block_t block = get_bio_block(tc, bio);
1364 struct dm_thin_device *td = tc->td;
1365 struct dm_thin_lookup_result result;
1366
Joe Thornbereb2aa482012-03-28 18:41:28 +01001367 map_context->ptr = thin_hook_bio(tc, bio);
Joe Thornbere49e5822012-07-27 15:08:16 +01001368
1369 if (get_pool_mode(tc->pool) == PM_FAIL) {
1370 bio_io_error(bio);
1371 return DM_MAPIO_SUBMITTED;
1372 }
1373
Joe Thornber104655f2012-03-28 18:41:28 +01001374 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001375 thin_defer_bio(tc, bio);
1376 return DM_MAPIO_SUBMITTED;
1377 }
1378
1379 r = dm_thin_find_block(td, block, 0, &result);
1380
1381 /*
1382 * Note that we defer readahead too.
1383 */
1384 switch (r) {
1385 case 0:
1386 if (unlikely(result.shared)) {
1387 /*
1388 * We have a race condition here between the
1389 * result.shared value returned by the lookup and
1390 * snapshot creation, which may cause new
1391 * sharing.
1392 *
1393 * To avoid this always quiesce the origin before
1394 * taking the snap. You want to do this anyway to
1395 * ensure a consistent application view
1396 * (i.e. lockfs).
1397 *
1398 * More distant ancestors are irrelevant. The
1399 * shared flag will be set in their case.
1400 */
1401 thin_defer_bio(tc, bio);
1402 r = DM_MAPIO_SUBMITTED;
1403 } else {
1404 remap(tc, bio, result.block);
1405 r = DM_MAPIO_REMAPPED;
1406 }
1407 break;
1408
1409 case -ENODATA:
Joe Thornbere49e5822012-07-27 15:08:16 +01001410 if (get_pool_mode(tc->pool) == PM_READ_ONLY) {
1411 /*
1412 * This block isn't provisioned, and we have no way
1413 * of doing so. Just error it.
1414 */
1415 bio_io_error(bio);
1416 r = DM_MAPIO_SUBMITTED;
1417 break;
1418 }
1419 /* fall through */
1420
1421 case -EWOULDBLOCK:
Joe Thornber991d9fa2011-10-31 20:21:18 +00001422 /*
1423 * In future, the failed dm_thin_find_block above could
1424 * provide the hint to load the metadata into cache.
1425 */
Joe Thornber991d9fa2011-10-31 20:21:18 +00001426 thin_defer_bio(tc, bio);
1427 r = DM_MAPIO_SUBMITTED;
1428 break;
Joe Thornbere49e5822012-07-27 15:08:16 +01001429
1430 default:
1431 /*
1432 * Must always call bio_io_error on failure.
1433 * dm_thin_find_block can fail with -EINVAL if the
1434 * pool is switched to fail-io mode.
1435 */
1436 bio_io_error(bio);
1437 r = DM_MAPIO_SUBMITTED;
1438 break;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001439 }
1440
1441 return r;
1442}
1443
1444static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1445{
1446 int r;
1447 unsigned long flags;
1448 struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
1449
1450 spin_lock_irqsave(&pt->pool->lock, flags);
1451 r = !bio_list_empty(&pt->pool->retry_on_resume_list);
1452 spin_unlock_irqrestore(&pt->pool->lock, flags);
1453
1454 if (!r) {
1455 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
1456 r = bdi_congested(&q->backing_dev_info, bdi_bits);
1457 }
1458
1459 return r;
1460}
1461
1462static void __requeue_bios(struct pool *pool)
1463{
1464 bio_list_merge(&pool->deferred_bios, &pool->retry_on_resume_list);
1465 bio_list_init(&pool->retry_on_resume_list);
1466}
1467
1468/*----------------------------------------------------------------
1469 * Binding of control targets to a pool object
1470 *--------------------------------------------------------------*/
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001471static bool data_dev_supports_discard(struct pool_c *pt)
1472{
1473 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
1474
1475 return q && blk_queue_discard(q);
1476}
1477
1478/*
1479 * If discard_passdown was enabled verify that the data device
Mike Snitzer0424caa2012-09-26 23:45:47 +01001480 * supports discards. Disable discard_passdown if not.
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001481 */
Mike Snitzer0424caa2012-09-26 23:45:47 +01001482static void disable_passdown_if_not_supported(struct pool_c *pt)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001483{
Mike Snitzer0424caa2012-09-26 23:45:47 +01001484 struct pool *pool = pt->pool;
1485 struct block_device *data_bdev = pt->data_dev->bdev;
1486 struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
1487 sector_t block_size = pool->sectors_per_block << SECTOR_SHIFT;
1488 const char *reason = NULL;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001489 char buf[BDEVNAME_SIZE];
1490
Mike Snitzer0424caa2012-09-26 23:45:47 +01001491 if (!pt->adjusted_pf.discard_passdown)
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001492 return;
1493
Mike Snitzer0424caa2012-09-26 23:45:47 +01001494 if (!data_dev_supports_discard(pt))
1495 reason = "discard unsupported";
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001496
Mike Snitzer0424caa2012-09-26 23:45:47 +01001497 else if (data_limits->max_discard_sectors < pool->sectors_per_block)
1498 reason = "max discard sectors smaller than a block";
1499
1500 else if (data_limits->discard_granularity > block_size)
1501 reason = "discard granularity larger than a block";
1502
1503 else if (block_size & (data_limits->discard_granularity - 1))
1504 reason = "discard granularity not a factor of block size";
1505
1506 if (reason) {
1507 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
1508 pt->adjusted_pf.discard_passdown = false;
1509 }
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001510}
1511
Joe Thornber991d9fa2011-10-31 20:21:18 +00001512static int bind_control_target(struct pool *pool, struct dm_target *ti)
1513{
1514 struct pool_c *pt = ti->private;
1515
Joe Thornbere49e5822012-07-27 15:08:16 +01001516 /*
1517 * We want to make sure that degraded pools are never upgraded.
1518 */
1519 enum pool_mode old_mode = pool->pf.mode;
Mike Snitzer0424caa2012-09-26 23:45:47 +01001520 enum pool_mode new_mode = pt->adjusted_pf.mode;
Joe Thornbere49e5822012-07-27 15:08:16 +01001521
1522 if (old_mode > new_mode)
1523 new_mode = old_mode;
1524
Joe Thornber991d9fa2011-10-31 20:21:18 +00001525 pool->ti = ti;
1526 pool->low_water_blocks = pt->low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +01001527 pool->pf = pt->adjusted_pf;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001528
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001529 set_pool_mode(pool, new_mode);
Mike Snitzerf4026932012-05-19 01:01:01 +01001530
Joe Thornber991d9fa2011-10-31 20:21:18 +00001531 return 0;
1532}
1533
1534static void unbind_control_target(struct pool *pool, struct dm_target *ti)
1535{
1536 if (pool->ti == ti)
1537 pool->ti = NULL;
1538}
1539
1540/*----------------------------------------------------------------
1541 * Pool creation
1542 *--------------------------------------------------------------*/
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001543/* Initialize pool features. */
1544static void pool_features_init(struct pool_features *pf)
1545{
Joe Thornbere49e5822012-07-27 15:08:16 +01001546 pf->mode = PM_WRITE;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001547 pf->zero_new_blocks = true;
1548 pf->discard_enabled = true;
1549 pf->discard_passdown = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001550}
1551
Joe Thornber991d9fa2011-10-31 20:21:18 +00001552static void __pool_destroy(struct pool *pool)
1553{
1554 __pool_table_remove(pool);
1555
1556 if (dm_pool_metadata_close(pool->pmd) < 0)
1557 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
1558
Mike Snitzer44feb382012-10-12 21:02:10 +01001559 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001560 dm_kcopyd_client_destroy(pool->copier);
1561
1562 if (pool->wq)
1563 destroy_workqueue(pool->wq);
1564
1565 if (pool->next_mapping)
1566 mempool_free(pool->next_mapping, pool->mapping_pool);
1567 mempool_destroy(pool->mapping_pool);
1568 mempool_destroy(pool->endio_hook_pool);
Mike Snitzer44feb382012-10-12 21:02:10 +01001569 dm_deferred_set_destroy(pool->shared_read_ds);
1570 dm_deferred_set_destroy(pool->all_io_ds);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001571 kfree(pool);
1572}
1573
Mike Snitzera24c2562012-06-03 00:30:00 +01001574static struct kmem_cache *_new_mapping_cache;
1575static struct kmem_cache *_endio_hook_cache;
1576
Joe Thornber991d9fa2011-10-31 20:21:18 +00001577static struct pool *pool_create(struct mapped_device *pool_md,
1578 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01001579 unsigned long block_size,
1580 int read_only, char **error)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001581{
1582 int r;
1583 void *err_p;
1584 struct pool *pool;
1585 struct dm_pool_metadata *pmd;
Joe Thornbere49e5822012-07-27 15:08:16 +01001586 bool format_device = read_only ? false : true;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001587
Joe Thornbere49e5822012-07-27 15:08:16 +01001588 pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001589 if (IS_ERR(pmd)) {
1590 *error = "Error creating metadata object";
1591 return (struct pool *)pmd;
1592 }
1593
1594 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
1595 if (!pool) {
1596 *error = "Error allocating memory for pool";
1597 err_p = ERR_PTR(-ENOMEM);
1598 goto bad_pool;
1599 }
1600
1601 pool->pmd = pmd;
1602 pool->sectors_per_block = block_size;
Mikulas Patockaf9a8e0c2012-07-27 15:08:03 +01001603 if (block_size & (block_size - 1))
1604 pool->sectors_per_block_shift = -1;
1605 else
1606 pool->sectors_per_block_shift = __ffs(block_size);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001607 pool->low_water_blocks = 0;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001608 pool_features_init(&pool->pf);
Mike Snitzer44feb382012-10-12 21:02:10 +01001609 pool->prison = dm_bio_prison_create(PRISON_CELLS);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001610 if (!pool->prison) {
1611 *error = "Error creating pool's bio prison";
1612 err_p = ERR_PTR(-ENOMEM);
1613 goto bad_prison;
1614 }
1615
1616 pool->copier = dm_kcopyd_client_create();
1617 if (IS_ERR(pool->copier)) {
1618 r = PTR_ERR(pool->copier);
1619 *error = "Error creating pool's kcopyd client";
1620 err_p = ERR_PTR(r);
1621 goto bad_kcopyd_client;
1622 }
1623
1624 /*
1625 * Create singlethreaded workqueue that will service all devices
1626 * that use this metadata.
1627 */
1628 pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
1629 if (!pool->wq) {
1630 *error = "Error creating pool's workqueue";
1631 err_p = ERR_PTR(-ENOMEM);
1632 goto bad_wq;
1633 }
1634
1635 INIT_WORK(&pool->worker, do_worker);
Joe Thornber905e51b2012-03-28 18:41:27 +01001636 INIT_DELAYED_WORK(&pool->waker, do_waker);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001637 spin_lock_init(&pool->lock);
1638 bio_list_init(&pool->deferred_bios);
1639 bio_list_init(&pool->deferred_flush_bios);
1640 INIT_LIST_HEAD(&pool->prepared_mappings);
Joe Thornber104655f2012-03-28 18:41:28 +01001641 INIT_LIST_HEAD(&pool->prepared_discards);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001642 pool->low_water_triggered = 0;
1643 pool->no_free_space = 0;
1644 bio_list_init(&pool->retry_on_resume_list);
Mike Snitzer44feb382012-10-12 21:02:10 +01001645
1646 pool->shared_read_ds = dm_deferred_set_create();
1647 if (!pool->shared_read_ds) {
1648 *error = "Error creating pool's shared read deferred set";
1649 err_p = ERR_PTR(-ENOMEM);
1650 goto bad_shared_read_ds;
1651 }
1652
1653 pool->all_io_ds = dm_deferred_set_create();
1654 if (!pool->all_io_ds) {
1655 *error = "Error creating pool's all io deferred set";
1656 err_p = ERR_PTR(-ENOMEM);
1657 goto bad_all_io_ds;
1658 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001659
1660 pool->next_mapping = NULL;
Mike Snitzera24c2562012-06-03 00:30:00 +01001661 pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
1662 _new_mapping_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001663 if (!pool->mapping_pool) {
1664 *error = "Error creating pool's mapping mempool";
1665 err_p = ERR_PTR(-ENOMEM);
1666 goto bad_mapping_pool;
1667 }
1668
Mike Snitzera24c2562012-06-03 00:30:00 +01001669 pool->endio_hook_pool = mempool_create_slab_pool(ENDIO_HOOK_POOL_SIZE,
1670 _endio_hook_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001671 if (!pool->endio_hook_pool) {
1672 *error = "Error creating pool's endio_hook mempool";
1673 err_p = ERR_PTR(-ENOMEM);
1674 goto bad_endio_hook_pool;
1675 }
1676 pool->ref_count = 1;
Joe Thornber905e51b2012-03-28 18:41:27 +01001677 pool->last_commit_jiffies = jiffies;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001678 pool->pool_md = pool_md;
1679 pool->md_dev = metadata_dev;
1680 __pool_table_insert(pool);
1681
1682 return pool;
1683
1684bad_endio_hook_pool:
1685 mempool_destroy(pool->mapping_pool);
1686bad_mapping_pool:
Mike Snitzer44feb382012-10-12 21:02:10 +01001687 dm_deferred_set_destroy(pool->all_io_ds);
1688bad_all_io_ds:
1689 dm_deferred_set_destroy(pool->shared_read_ds);
1690bad_shared_read_ds:
Joe Thornber991d9fa2011-10-31 20:21:18 +00001691 destroy_workqueue(pool->wq);
1692bad_wq:
1693 dm_kcopyd_client_destroy(pool->copier);
1694bad_kcopyd_client:
Mike Snitzer44feb382012-10-12 21:02:10 +01001695 dm_bio_prison_destroy(pool->prison);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001696bad_prison:
1697 kfree(pool);
1698bad_pool:
1699 if (dm_pool_metadata_close(pmd))
1700 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
1701
1702 return err_p;
1703}
1704
1705static void __pool_inc(struct pool *pool)
1706{
1707 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
1708 pool->ref_count++;
1709}
1710
1711static void __pool_dec(struct pool *pool)
1712{
1713 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
1714 BUG_ON(!pool->ref_count);
1715 if (!--pool->ref_count)
1716 __pool_destroy(pool);
1717}
1718
1719static struct pool *__pool_find(struct mapped_device *pool_md,
1720 struct block_device *metadata_dev,
Joe Thornbere49e5822012-07-27 15:08:16 +01001721 unsigned long block_size, int read_only,
1722 char **error, int *created)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001723{
1724 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
1725
1726 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01001727 if (pool->pool_md != pool_md) {
1728 *error = "metadata device already in use by a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00001729 return ERR_PTR(-EBUSY);
Mike Snitzerf09996c2012-07-27 15:07:59 +01001730 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001731 __pool_inc(pool);
1732
1733 } else {
1734 pool = __pool_table_lookup(pool_md);
1735 if (pool) {
Mike Snitzerf09996c2012-07-27 15:07:59 +01001736 if (pool->md_dev != metadata_dev) {
1737 *error = "different pool cannot replace a pool";
Joe Thornber991d9fa2011-10-31 20:21:18 +00001738 return ERR_PTR(-EINVAL);
Mike Snitzerf09996c2012-07-27 15:07:59 +01001739 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001740 __pool_inc(pool);
1741
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001742 } else {
Joe Thornbere49e5822012-07-27 15:08:16 +01001743 pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001744 *created = 1;
1745 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001746 }
1747
1748 return pool;
1749}
1750
1751/*----------------------------------------------------------------
1752 * Pool target methods
1753 *--------------------------------------------------------------*/
1754static void pool_dtr(struct dm_target *ti)
1755{
1756 struct pool_c *pt = ti->private;
1757
1758 mutex_lock(&dm_thin_pool_table.mutex);
1759
1760 unbind_control_target(pt->pool, ti);
1761 __pool_dec(pt->pool);
1762 dm_put_device(ti, pt->metadata_dev);
1763 dm_put_device(ti, pt->data_dev);
1764 kfree(pt);
1765
1766 mutex_unlock(&dm_thin_pool_table.mutex);
1767}
1768
Joe Thornber991d9fa2011-10-31 20:21:18 +00001769static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
1770 struct dm_target *ti)
1771{
1772 int r;
1773 unsigned argc;
1774 const char *arg_name;
1775
1776 static struct dm_arg _args[] = {
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001777 {0, 3, "Invalid number of pool feature arguments"},
Joe Thornber991d9fa2011-10-31 20:21:18 +00001778 };
1779
1780 /*
1781 * No feature arguments supplied.
1782 */
1783 if (!as->argc)
1784 return 0;
1785
1786 r = dm_read_arg_group(_args, as, &argc, &ti->error);
1787 if (r)
1788 return -EINVAL;
1789
1790 while (argc && !r) {
1791 arg_name = dm_shift_arg(as);
1792 argc--;
1793
Joe Thornbere49e5822012-07-27 15:08:16 +01001794 if (!strcasecmp(arg_name, "skip_block_zeroing"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001795 pf->zero_new_blocks = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001796
Joe Thornbere49e5822012-07-27 15:08:16 +01001797 else if (!strcasecmp(arg_name, "ignore_discard"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001798 pf->discard_enabled = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01001799
1800 else if (!strcasecmp(arg_name, "no_discard_passdown"))
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001801 pf->discard_passdown = false;
Joe Thornbere49e5822012-07-27 15:08:16 +01001802
1803 else if (!strcasecmp(arg_name, "read_only"))
1804 pf->mode = PM_READ_ONLY;
1805
1806 else {
1807 ti->error = "Unrecognised pool feature requested";
1808 r = -EINVAL;
1809 break;
1810 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001811 }
1812
1813 return r;
1814}
1815
1816/*
1817 * thin-pool <metadata dev> <data dev>
1818 * <data block size (sectors)>
1819 * <low water mark (blocks)>
1820 * [<#feature args> [<arg>]*]
1821 *
1822 * Optional feature arguments are:
1823 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001824 * ignore_discard: disable discard
1825 * no_discard_passdown: don't pass discards down to the data device
Joe Thornber991d9fa2011-10-31 20:21:18 +00001826 */
1827static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
1828{
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001829 int r, pool_created = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001830 struct pool_c *pt;
1831 struct pool *pool;
1832 struct pool_features pf;
1833 struct dm_arg_set as;
1834 struct dm_dev *data_dev;
1835 unsigned long block_size;
1836 dm_block_t low_water_blocks;
1837 struct dm_dev *metadata_dev;
1838 sector_t metadata_dev_size;
Mike Snitzerc4a69ec2012-03-28 18:41:28 +01001839 char b[BDEVNAME_SIZE];
Joe Thornber991d9fa2011-10-31 20:21:18 +00001840
1841 /*
1842 * FIXME Remove validation from scope of lock.
1843 */
1844 mutex_lock(&dm_thin_pool_table.mutex);
1845
1846 if (argc < 4) {
1847 ti->error = "Invalid argument count";
1848 r = -EINVAL;
1849 goto out_unlock;
1850 }
1851 as.argc = argc;
1852 as.argv = argv;
1853
1854 r = dm_get_device(ti, argv[0], FMODE_READ | FMODE_WRITE, &metadata_dev);
1855 if (r) {
1856 ti->error = "Error opening metadata block device";
1857 goto out_unlock;
1858 }
1859
1860 metadata_dev_size = i_size_read(metadata_dev->bdev->bd_inode) >> SECTOR_SHIFT;
Mike Snitzerc4a69ec2012-03-28 18:41:28 +01001861 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
1862 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
1863 bdevname(metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001864
1865 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
1866 if (r) {
1867 ti->error = "Error getting data device";
1868 goto out_metadata;
1869 }
1870
1871 if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
1872 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
1873 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01001874 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001875 ti->error = "Invalid block size";
1876 r = -EINVAL;
1877 goto out;
1878 }
1879
1880 if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
1881 ti->error = "Invalid low water mark";
1882 r = -EINVAL;
1883 goto out;
1884 }
1885
1886 /*
1887 * Set default pool features.
1888 */
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001889 pool_features_init(&pf);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001890
1891 dm_consume_args(&as, 4);
1892 r = parse_pool_features(&as, &pf, ti);
1893 if (r)
1894 goto out;
1895
1896 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
1897 if (!pt) {
1898 r = -ENOMEM;
1899 goto out;
1900 }
1901
1902 pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
Joe Thornbere49e5822012-07-27 15:08:16 +01001903 block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001904 if (IS_ERR(pool)) {
1905 r = PTR_ERR(pool);
1906 goto out_free_pt;
1907 }
1908
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001909 /*
1910 * 'pool_created' reflects whether this is the first table load.
1911 * Top level discard support is not allowed to be changed after
1912 * initial load. This would require a pool reload to trigger thin
1913 * device changes.
1914 */
1915 if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
1916 ti->error = "Discard support cannot be disabled once enabled";
1917 r = -EINVAL;
1918 goto out_flags_changed;
1919 }
1920
Joe Thornber991d9fa2011-10-31 20:21:18 +00001921 pt->pool = pool;
1922 pt->ti = ti;
1923 pt->metadata_dev = metadata_dev;
1924 pt->data_dev = data_dev;
1925 pt->low_water_blocks = low_water_blocks;
Mike Snitzer0424caa2012-09-26 23:45:47 +01001926 pt->adjusted_pf = pt->requested_pf = pf;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001927 ti->num_flush_requests = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001928
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001929 /*
1930 * Only need to enable discards if the pool should pass
1931 * them down to the data device. The thin device's discard
1932 * processing will cause mappings to be removed from the btree.
1933 */
1934 if (pf.discard_enabled && pf.discard_passdown) {
1935 ti->num_discard_requests = 1;
Mike Snitzer9bc142d2012-09-26 23:45:46 +01001936
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001937 /*
1938 * Setting 'discards_supported' circumvents the normal
1939 * stacking of discard limits (this keeps the pool and
1940 * thin devices' discard limits consistent).
1941 */
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01001942 ti->discards_supported = true;
Mike Snitzer307615a2012-09-26 23:45:39 +01001943 ti->discard_zeroes_data_unsupported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001944 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001945 ti->private = pt;
1946
1947 pt->callbacks.congested_fn = pool_is_congested;
1948 dm_table_add_target_callbacks(ti->table, &pt->callbacks);
1949
1950 mutex_unlock(&dm_thin_pool_table.mutex);
1951
1952 return 0;
1953
Joe Thornber67e2e2b2012-03-28 18:41:29 +01001954out_flags_changed:
1955 __pool_dec(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001956out_free_pt:
1957 kfree(pt);
1958out:
1959 dm_put_device(ti, data_dev);
1960out_metadata:
1961 dm_put_device(ti, metadata_dev);
1962out_unlock:
1963 mutex_unlock(&dm_thin_pool_table.mutex);
1964
1965 return r;
1966}
1967
1968static int pool_map(struct dm_target *ti, struct bio *bio,
1969 union map_info *map_context)
1970{
1971 int r;
1972 struct pool_c *pt = ti->private;
1973 struct pool *pool = pt->pool;
1974 unsigned long flags;
1975
1976 /*
1977 * As this is a singleton target, ti->begin is always zero.
1978 */
1979 spin_lock_irqsave(&pool->lock, flags);
1980 bio->bi_bdev = pt->data_dev->bdev;
1981 r = DM_MAPIO_REMAPPED;
1982 spin_unlock_irqrestore(&pool->lock, flags);
1983
1984 return r;
1985}
1986
1987/*
1988 * Retrieves the number of blocks of the data device from
1989 * the superblock and compares it to the actual device size,
1990 * thus resizing the data device in case it has grown.
1991 *
1992 * This both copes with opening preallocated data devices in the ctr
1993 * being followed by a resume
1994 * -and-
1995 * calling the resume method individually after userspace has
1996 * grown the data device in reaction to a table event.
1997 */
1998static int pool_preresume(struct dm_target *ti)
1999{
2000 int r;
2001 struct pool_c *pt = ti->private;
2002 struct pool *pool = pt->pool;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002003 sector_t data_size = ti->len;
2004 dm_block_t sb_data_size;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002005
2006 /*
2007 * Take control of the pool object.
2008 */
2009 r = bind_control_target(pool, ti);
2010 if (r)
2011 return r;
2012
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002013 (void) sector_div(data_size, pool->sectors_per_block);
2014
Joe Thornber991d9fa2011-10-31 20:21:18 +00002015 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
2016 if (r) {
2017 DMERR("failed to retrieve data device size");
2018 return r;
2019 }
2020
2021 if (data_size < sb_data_size) {
2022 DMERR("pool target too small, is %llu blocks (expected %llu)",
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002023 (unsigned long long)data_size, sb_data_size);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002024 return -EINVAL;
2025
2026 } else if (data_size > sb_data_size) {
2027 r = dm_pool_resize_data_dev(pool->pmd, data_size);
2028 if (r) {
2029 DMERR("failed to resize data device");
Joe Thornbere49e5822012-07-27 15:08:16 +01002030 /* FIXME Stricter than necessary: Rollback transaction instead here */
2031 set_pool_mode(pool, PM_READ_ONLY);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002032 return r;
2033 }
2034
Joe Thornbere49e5822012-07-27 15:08:16 +01002035 (void) commit_or_fallback(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002036 }
2037
2038 return 0;
2039}
2040
2041static void pool_resume(struct dm_target *ti)
2042{
2043 struct pool_c *pt = ti->private;
2044 struct pool *pool = pt->pool;
2045 unsigned long flags;
2046
2047 spin_lock_irqsave(&pool->lock, flags);
2048 pool->low_water_triggered = 0;
2049 pool->no_free_space = 0;
2050 __requeue_bios(pool);
2051 spin_unlock_irqrestore(&pool->lock, flags);
2052
Joe Thornber905e51b2012-03-28 18:41:27 +01002053 do_waker(&pool->waker.work);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002054}
2055
2056static void pool_postsuspend(struct dm_target *ti)
2057{
Joe Thornber991d9fa2011-10-31 20:21:18 +00002058 struct pool_c *pt = ti->private;
2059 struct pool *pool = pt->pool;
2060
Joe Thornber905e51b2012-03-28 18:41:27 +01002061 cancel_delayed_work(&pool->waker);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002062 flush_workqueue(pool->wq);
Joe Thornbere49e5822012-07-27 15:08:16 +01002063 (void) commit_or_fallback(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002064}
2065
2066static int check_arg_count(unsigned argc, unsigned args_required)
2067{
2068 if (argc != args_required) {
2069 DMWARN("Message received with %u arguments instead of %u.",
2070 argc, args_required);
2071 return -EINVAL;
2072 }
2073
2074 return 0;
2075}
2076
2077static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
2078{
2079 if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
2080 *dev_id <= MAX_DEV_ID)
2081 return 0;
2082
2083 if (warning)
2084 DMWARN("Message received with invalid device id: %s", arg);
2085
2086 return -EINVAL;
2087}
2088
2089static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
2090{
2091 dm_thin_id dev_id;
2092 int r;
2093
2094 r = check_arg_count(argc, 2);
2095 if (r)
2096 return r;
2097
2098 r = read_dev_id(argv[1], &dev_id, 1);
2099 if (r)
2100 return r;
2101
2102 r = dm_pool_create_thin(pool->pmd, dev_id);
2103 if (r) {
2104 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
2105 argv[1]);
2106 return r;
2107 }
2108
2109 return 0;
2110}
2111
2112static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2113{
2114 dm_thin_id dev_id;
2115 dm_thin_id origin_dev_id;
2116 int r;
2117
2118 r = check_arg_count(argc, 3);
2119 if (r)
2120 return r;
2121
2122 r = read_dev_id(argv[1], &dev_id, 1);
2123 if (r)
2124 return r;
2125
2126 r = read_dev_id(argv[2], &origin_dev_id, 1);
2127 if (r)
2128 return r;
2129
2130 r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
2131 if (r) {
2132 DMWARN("Creation of new snapshot %s of device %s failed.",
2133 argv[1], argv[2]);
2134 return r;
2135 }
2136
2137 return 0;
2138}
2139
2140static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
2141{
2142 dm_thin_id dev_id;
2143 int r;
2144
2145 r = check_arg_count(argc, 2);
2146 if (r)
2147 return r;
2148
2149 r = read_dev_id(argv[1], &dev_id, 1);
2150 if (r)
2151 return r;
2152
2153 r = dm_pool_delete_thin_device(pool->pmd, dev_id);
2154 if (r)
2155 DMWARN("Deletion of thin device %s failed.", argv[1]);
2156
2157 return r;
2158}
2159
2160static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
2161{
2162 dm_thin_id old_id, new_id;
2163 int r;
2164
2165 r = check_arg_count(argc, 3);
2166 if (r)
2167 return r;
2168
2169 if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
2170 DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
2171 return -EINVAL;
2172 }
2173
2174 if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
2175 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
2176 return -EINVAL;
2177 }
2178
2179 r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
2180 if (r) {
2181 DMWARN("Failed to change transaction id from %s to %s.",
2182 argv[1], argv[2]);
2183 return r;
2184 }
2185
2186 return 0;
2187}
2188
Joe Thornbercc8394d2012-06-03 00:30:01 +01002189static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2190{
2191 int r;
2192
2193 r = check_arg_count(argc, 1);
2194 if (r)
2195 return r;
2196
Joe Thornbere49e5822012-07-27 15:08:16 +01002197 (void) commit_or_fallback(pool);
Joe Thornber0d200ae2012-07-03 12:55:31 +01002198
Joe Thornbercc8394d2012-06-03 00:30:01 +01002199 r = dm_pool_reserve_metadata_snap(pool->pmd);
2200 if (r)
2201 DMWARN("reserve_metadata_snap message failed.");
2202
2203 return r;
2204}
2205
2206static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
2207{
2208 int r;
2209
2210 r = check_arg_count(argc, 1);
2211 if (r)
2212 return r;
2213
2214 r = dm_pool_release_metadata_snap(pool->pmd);
2215 if (r)
2216 DMWARN("release_metadata_snap message failed.");
2217
2218 return r;
2219}
2220
Joe Thornber991d9fa2011-10-31 20:21:18 +00002221/*
2222 * Messages supported:
2223 * create_thin <dev_id>
2224 * create_snap <dev_id> <origin_id>
2225 * delete <dev_id>
2226 * trim <dev_id> <new_size_in_sectors>
2227 * set_transaction_id <current_trans_id> <new_trans_id>
Joe Thornbercc8394d2012-06-03 00:30:01 +01002228 * reserve_metadata_snap
2229 * release_metadata_snap
Joe Thornber991d9fa2011-10-31 20:21:18 +00002230 */
2231static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
2232{
2233 int r = -EINVAL;
2234 struct pool_c *pt = ti->private;
2235 struct pool *pool = pt->pool;
2236
2237 if (!strcasecmp(argv[0], "create_thin"))
2238 r = process_create_thin_mesg(argc, argv, pool);
2239
2240 else if (!strcasecmp(argv[0], "create_snap"))
2241 r = process_create_snap_mesg(argc, argv, pool);
2242
2243 else if (!strcasecmp(argv[0], "delete"))
2244 r = process_delete_mesg(argc, argv, pool);
2245
2246 else if (!strcasecmp(argv[0], "set_transaction_id"))
2247 r = process_set_transaction_id_mesg(argc, argv, pool);
2248
Joe Thornbercc8394d2012-06-03 00:30:01 +01002249 else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
2250 r = process_reserve_metadata_snap_mesg(argc, argv, pool);
2251
2252 else if (!strcasecmp(argv[0], "release_metadata_snap"))
2253 r = process_release_metadata_snap_mesg(argc, argv, pool);
2254
Joe Thornber991d9fa2011-10-31 20:21:18 +00002255 else
2256 DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
2257
Joe Thornbere49e5822012-07-27 15:08:16 +01002258 if (!r)
2259 (void) commit_or_fallback(pool);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002260
2261 return r;
2262}
2263
Joe Thornbere49e5822012-07-27 15:08:16 +01002264static void emit_flags(struct pool_features *pf, char *result,
2265 unsigned sz, unsigned maxlen)
2266{
2267 unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
2268 !pf->discard_passdown + (pf->mode == PM_READ_ONLY);
2269 DMEMIT("%u ", count);
2270
2271 if (!pf->zero_new_blocks)
2272 DMEMIT("skip_block_zeroing ");
2273
2274 if (!pf->discard_enabled)
2275 DMEMIT("ignore_discard ");
2276
2277 if (!pf->discard_passdown)
2278 DMEMIT("no_discard_passdown ");
2279
2280 if (pf->mode == PM_READ_ONLY)
2281 DMEMIT("read_only ");
2282}
2283
Joe Thornber991d9fa2011-10-31 20:21:18 +00002284/*
2285 * Status line is:
2286 * <transaction id> <used metadata sectors>/<total metadata sectors>
2287 * <used data sectors>/<total data sectors> <held metadata root>
2288 */
2289static int pool_status(struct dm_target *ti, status_type_t type,
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01002290 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002291{
Joe Thornbere49e5822012-07-27 15:08:16 +01002292 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002293 unsigned sz = 0;
2294 uint64_t transaction_id;
2295 dm_block_t nr_free_blocks_data;
2296 dm_block_t nr_free_blocks_metadata;
2297 dm_block_t nr_blocks_data;
2298 dm_block_t nr_blocks_metadata;
2299 dm_block_t held_root;
2300 char buf[BDEVNAME_SIZE];
2301 char buf2[BDEVNAME_SIZE];
2302 struct pool_c *pt = ti->private;
2303 struct pool *pool = pt->pool;
2304
2305 switch (type) {
2306 case STATUSTYPE_INFO:
Joe Thornbere49e5822012-07-27 15:08:16 +01002307 if (get_pool_mode(pool) == PM_FAIL) {
2308 DMEMIT("Fail");
2309 break;
2310 }
2311
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01002312 /* Commit to ensure statistics aren't out-of-date */
2313 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
2314 (void) commit_or_fallback(pool);
2315
Joe Thornber991d9fa2011-10-31 20:21:18 +00002316 r = dm_pool_get_metadata_transaction_id(pool->pmd,
2317 &transaction_id);
2318 if (r)
2319 return r;
2320
2321 r = dm_pool_get_free_metadata_block_count(pool->pmd,
2322 &nr_free_blocks_metadata);
2323 if (r)
2324 return r;
2325
2326 r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
2327 if (r)
2328 return r;
2329
2330 r = dm_pool_get_free_block_count(pool->pmd,
2331 &nr_free_blocks_data);
2332 if (r)
2333 return r;
2334
2335 r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
2336 if (r)
2337 return r;
2338
Joe Thornbercc8394d2012-06-03 00:30:01 +01002339 r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002340 if (r)
2341 return r;
2342
2343 DMEMIT("%llu %llu/%llu %llu/%llu ",
2344 (unsigned long long)transaction_id,
2345 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
2346 (unsigned long long)nr_blocks_metadata,
2347 (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
2348 (unsigned long long)nr_blocks_data);
2349
2350 if (held_root)
Joe Thornbere49e5822012-07-27 15:08:16 +01002351 DMEMIT("%llu ", held_root);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002352 else
Joe Thornbere49e5822012-07-27 15:08:16 +01002353 DMEMIT("- ");
2354
2355 if (pool->pf.mode == PM_READ_ONLY)
2356 DMEMIT("ro ");
2357 else
2358 DMEMIT("rw ");
2359
2360 if (pool->pf.discard_enabled && pool->pf.discard_passdown)
2361 DMEMIT("discard_passdown");
2362 else
2363 DMEMIT("no_discard_passdown");
Joe Thornber991d9fa2011-10-31 20:21:18 +00002364
2365 break;
2366
2367 case STATUSTYPE_TABLE:
2368 DMEMIT("%s %s %lu %llu ",
2369 format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
2370 format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
2371 (unsigned long)pool->sectors_per_block,
2372 (unsigned long long)pt->low_water_blocks);
Mike Snitzer0424caa2012-09-26 23:45:47 +01002373 emit_flags(&pt->requested_pf, result, sz, maxlen);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002374 break;
2375 }
2376
2377 return 0;
2378}
2379
2380static int pool_iterate_devices(struct dm_target *ti,
2381 iterate_devices_callout_fn fn, void *data)
2382{
2383 struct pool_c *pt = ti->private;
2384
2385 return fn(ti, pt->data_dev, 0, ti->len, data);
2386}
2387
2388static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
2389 struct bio_vec *biovec, int max_size)
2390{
2391 struct pool_c *pt = ti->private;
2392 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
2393
2394 if (!q->merge_bvec_fn)
2395 return max_size;
2396
2397 bvm->bi_bdev = pt->data_dev->bdev;
2398
2399 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
2400}
2401
Mike Snitzer28eed342012-10-12 21:02:07 +01002402static bool block_size_is_power_of_two(struct pool *pool)
2403{
2404 return pool->sectors_per_block_shift >= 0;
2405}
2406
Mike Snitzer0424caa2012-09-26 23:45:47 +01002407static void set_discard_limits(struct pool_c *pt, struct queue_limits *limits)
Joe Thornber104655f2012-03-28 18:41:28 +01002408{
Mike Snitzer0424caa2012-09-26 23:45:47 +01002409 struct pool *pool = pt->pool;
2410 struct queue_limits *data_limits;
2411
Joe Thornber104655f2012-03-28 18:41:28 +01002412 limits->max_discard_sectors = pool->sectors_per_block;
2413
2414 /*
Mike Snitzer0424caa2012-09-26 23:45:47 +01002415 * discard_granularity is just a hint, and not enforced.
Joe Thornber104655f2012-03-28 18:41:28 +01002416 */
Mike Snitzer0424caa2012-09-26 23:45:47 +01002417 if (pt->adjusted_pf.discard_passdown) {
2418 data_limits = &bdev_get_queue(pt->data_dev->bdev)->limits;
2419 limits->discard_granularity = data_limits->discard_granularity;
Mike Snitzer28eed342012-10-12 21:02:07 +01002420 } else if (block_size_is_power_of_two(pool))
Mike Snitzer0424caa2012-09-26 23:45:47 +01002421 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
Mike Snitzer28eed342012-10-12 21:02:07 +01002422 else
2423 /*
2424 * Use largest power of 2 that is a factor of sectors_per_block
2425 * but at least DATA_DEV_BLOCK_SIZE_MIN_SECTORS.
2426 */
2427 limits->discard_granularity = max(1 << (ffs(pool->sectors_per_block) - 1),
2428 DATA_DEV_BLOCK_SIZE_MIN_SECTORS) << SECTOR_SHIFT;
Joe Thornber104655f2012-03-28 18:41:28 +01002429}
2430
Joe Thornber991d9fa2011-10-31 20:21:18 +00002431static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
2432{
2433 struct pool_c *pt = ti->private;
2434 struct pool *pool = pt->pool;
2435
2436 blk_limits_io_min(limits, 0);
2437 blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
Mike Snitzer0424caa2012-09-26 23:45:47 +01002438
2439 /*
2440 * pt->adjusted_pf is a staging area for the actual features to use.
2441 * They get transferred to the live pool in bind_control_target()
2442 * called from pool_preresume().
2443 */
2444 if (!pt->adjusted_pf.discard_enabled)
2445 return;
2446
2447 disable_passdown_if_not_supported(pt);
2448
2449 set_discard_limits(pt, limits);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002450}
2451
2452static struct target_type pool_target = {
2453 .name = "thin-pool",
2454 .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
2455 DM_TARGET_IMMUTABLE,
Mike Snitzer4f81a412012-10-12 21:02:13 +01002456 .version = {1, 5, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00002457 .module = THIS_MODULE,
2458 .ctr = pool_ctr,
2459 .dtr = pool_dtr,
2460 .map = pool_map,
2461 .postsuspend = pool_postsuspend,
2462 .preresume = pool_preresume,
2463 .resume = pool_resume,
2464 .message = pool_message,
2465 .status = pool_status,
2466 .merge = pool_merge,
2467 .iterate_devices = pool_iterate_devices,
2468 .io_hints = pool_io_hints,
2469};
2470
2471/*----------------------------------------------------------------
2472 * Thin target methods
2473 *--------------------------------------------------------------*/
2474static void thin_dtr(struct dm_target *ti)
2475{
2476 struct thin_c *tc = ti->private;
2477
2478 mutex_lock(&dm_thin_pool_table.mutex);
2479
2480 __pool_dec(tc->pool);
2481 dm_pool_close_thin_device(tc->td);
2482 dm_put_device(ti, tc->pool_dev);
Joe Thornber2dd9c252012-03-28 18:41:28 +01002483 if (tc->origin_dev)
2484 dm_put_device(ti, tc->origin_dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002485 kfree(tc);
2486
2487 mutex_unlock(&dm_thin_pool_table.mutex);
2488}
2489
2490/*
2491 * Thin target parameters:
2492 *
Joe Thornber2dd9c252012-03-28 18:41:28 +01002493 * <pool_dev> <dev_id> [origin_dev]
Joe Thornber991d9fa2011-10-31 20:21:18 +00002494 *
2495 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
2496 * dev_id: the internal device identifier
Joe Thornber2dd9c252012-03-28 18:41:28 +01002497 * origin_dev: a device external to the pool that should act as the origin
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002498 *
2499 * If the pool device has discards disabled, they get disabled for the thin
2500 * device as well.
Joe Thornber991d9fa2011-10-31 20:21:18 +00002501 */
2502static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
2503{
2504 int r;
2505 struct thin_c *tc;
Joe Thornber2dd9c252012-03-28 18:41:28 +01002506 struct dm_dev *pool_dev, *origin_dev;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002507 struct mapped_device *pool_md;
2508
2509 mutex_lock(&dm_thin_pool_table.mutex);
2510
Joe Thornber2dd9c252012-03-28 18:41:28 +01002511 if (argc != 2 && argc != 3) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00002512 ti->error = "Invalid argument count";
2513 r = -EINVAL;
2514 goto out_unlock;
2515 }
2516
2517 tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
2518 if (!tc) {
2519 ti->error = "Out of memory";
2520 r = -ENOMEM;
2521 goto out_unlock;
2522 }
2523
Joe Thornber2dd9c252012-03-28 18:41:28 +01002524 if (argc == 3) {
2525 r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
2526 if (r) {
2527 ti->error = "Error opening origin device";
2528 goto bad_origin_dev;
2529 }
2530 tc->origin_dev = origin_dev;
2531 }
2532
Joe Thornber991d9fa2011-10-31 20:21:18 +00002533 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
2534 if (r) {
2535 ti->error = "Error opening pool device";
2536 goto bad_pool_dev;
2537 }
2538 tc->pool_dev = pool_dev;
2539
2540 if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
2541 ti->error = "Invalid device id";
2542 r = -EINVAL;
2543 goto bad_common;
2544 }
2545
2546 pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
2547 if (!pool_md) {
2548 ti->error = "Couldn't get pool mapped device";
2549 r = -EINVAL;
2550 goto bad_common;
2551 }
2552
2553 tc->pool = __pool_table_lookup(pool_md);
2554 if (!tc->pool) {
2555 ti->error = "Couldn't find pool object";
2556 r = -EINVAL;
2557 goto bad_pool_lookup;
2558 }
2559 __pool_inc(tc->pool);
2560
Joe Thornbere49e5822012-07-27 15:08:16 +01002561 if (get_pool_mode(tc->pool) == PM_FAIL) {
2562 ti->error = "Couldn't open thin device, Pool is in fail mode";
2563 goto bad_thin_open;
2564 }
2565
Joe Thornber991d9fa2011-10-31 20:21:18 +00002566 r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
2567 if (r) {
2568 ti->error = "Couldn't open thin internal device";
2569 goto bad_thin_open;
2570 }
2571
Mike Snitzer542f9032012-07-27 15:08:00 +01002572 r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
2573 if (r)
2574 goto bad_thin_open;
2575
Joe Thornber991d9fa2011-10-31 20:21:18 +00002576 ti->num_flush_requests = 1;
Joe Thornber16ad3d12012-07-27 15:08:07 +01002577 ti->flush_supported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002578
2579 /* In case the pool supports discards, pass them on. */
2580 if (tc->pool->pf.discard_enabled) {
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01002581 ti->discards_supported = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002582 ti->num_discard_requests = 1;
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01002583 ti->discard_zeroes_data_unsupported = true;
Mikulas Patocka49296302012-07-27 15:08:03 +01002584 /* Discard requests must be split on a block boundary */
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01002585 ti->split_discard_requests = true;
Joe Thornber67e2e2b2012-03-28 18:41:29 +01002586 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00002587
2588 dm_put(pool_md);
2589
2590 mutex_unlock(&dm_thin_pool_table.mutex);
2591
2592 return 0;
2593
2594bad_thin_open:
2595 __pool_dec(tc->pool);
2596bad_pool_lookup:
2597 dm_put(pool_md);
2598bad_common:
2599 dm_put_device(ti, tc->pool_dev);
2600bad_pool_dev:
Joe Thornber2dd9c252012-03-28 18:41:28 +01002601 if (tc->origin_dev)
2602 dm_put_device(ti, tc->origin_dev);
2603bad_origin_dev:
Joe Thornber991d9fa2011-10-31 20:21:18 +00002604 kfree(tc);
2605out_unlock:
2606 mutex_unlock(&dm_thin_pool_table.mutex);
2607
2608 return r;
2609}
2610
2611static int thin_map(struct dm_target *ti, struct bio *bio,
2612 union map_info *map_context)
2613{
Alasdair G Kergon6efd6e82012-03-28 18:41:28 +01002614 bio->bi_sector = dm_target_offset(ti, bio->bi_sector);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002615
2616 return thin_bio_map(ti, bio, map_context);
2617}
2618
Joe Thornbereb2aa482012-03-28 18:41:28 +01002619static int thin_endio(struct dm_target *ti,
2620 struct bio *bio, int err,
2621 union map_info *map_context)
2622{
2623 unsigned long flags;
Mike Snitzera24c2562012-06-03 00:30:00 +01002624 struct dm_thin_endio_hook *h = map_context->ptr;
Joe Thornbereb2aa482012-03-28 18:41:28 +01002625 struct list_head work;
Mike Snitzera24c2562012-06-03 00:30:00 +01002626 struct dm_thin_new_mapping *m, *tmp;
Joe Thornbereb2aa482012-03-28 18:41:28 +01002627 struct pool *pool = h->tc->pool;
2628
2629 if (h->shared_read_entry) {
2630 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01002631 dm_deferred_entry_dec(h->shared_read_entry, &work);
Joe Thornbereb2aa482012-03-28 18:41:28 +01002632
2633 spin_lock_irqsave(&pool->lock, flags);
2634 list_for_each_entry_safe(m, tmp, &work, list) {
2635 list_del(&m->list);
2636 m->quiesced = 1;
2637 __maybe_add_mapping(m);
2638 }
2639 spin_unlock_irqrestore(&pool->lock, flags);
2640 }
2641
Joe Thornber104655f2012-03-28 18:41:28 +01002642 if (h->all_io_entry) {
2643 INIT_LIST_HEAD(&work);
Mike Snitzer44feb382012-10-12 21:02:10 +01002644 dm_deferred_entry_dec(h->all_io_entry, &work);
Mike Snitzerc3a0ce22012-05-12 01:43:16 +01002645 spin_lock_irqsave(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +01002646 list_for_each_entry_safe(m, tmp, &work, list)
2647 list_add(&m->list, &pool->prepared_discards);
Mike Snitzerc3a0ce22012-05-12 01:43:16 +01002648 spin_unlock_irqrestore(&pool->lock, flags);
Joe Thornber104655f2012-03-28 18:41:28 +01002649 }
2650
Joe Thornbereb2aa482012-03-28 18:41:28 +01002651 mempool_free(h, pool->endio_hook_pool);
2652
2653 return 0;
2654}
2655
Joe Thornber991d9fa2011-10-31 20:21:18 +00002656static void thin_postsuspend(struct dm_target *ti)
2657{
2658 if (dm_noflush_suspending(ti))
2659 requeue_io((struct thin_c *)ti->private);
2660}
2661
2662/*
2663 * <nr mapped sectors> <highest mapped sector>
2664 */
2665static int thin_status(struct dm_target *ti, status_type_t type,
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01002666 unsigned status_flags, char *result, unsigned maxlen)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002667{
2668 int r;
2669 ssize_t sz = 0;
2670 dm_block_t mapped, highest;
2671 char buf[BDEVNAME_SIZE];
2672 struct thin_c *tc = ti->private;
2673
Joe Thornbere49e5822012-07-27 15:08:16 +01002674 if (get_pool_mode(tc->pool) == PM_FAIL) {
2675 DMEMIT("Fail");
2676 return 0;
2677 }
2678
Joe Thornber991d9fa2011-10-31 20:21:18 +00002679 if (!tc->td)
2680 DMEMIT("-");
2681 else {
2682 switch (type) {
2683 case STATUSTYPE_INFO:
2684 r = dm_thin_get_mapped_count(tc->td, &mapped);
2685 if (r)
2686 return r;
2687
2688 r = dm_thin_get_highest_mapped_block(tc->td, &highest);
2689 if (r < 0)
2690 return r;
2691
2692 DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
2693 if (r)
2694 DMEMIT("%llu", ((highest + 1) *
2695 tc->pool->sectors_per_block) - 1);
2696 else
2697 DMEMIT("-");
2698 break;
2699
2700 case STATUSTYPE_TABLE:
2701 DMEMIT("%s %lu",
2702 format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
2703 (unsigned long) tc->dev_id);
Joe Thornber2dd9c252012-03-28 18:41:28 +01002704 if (tc->origin_dev)
2705 DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
Joe Thornber991d9fa2011-10-31 20:21:18 +00002706 break;
2707 }
2708 }
2709
2710 return 0;
2711}
2712
2713static int thin_iterate_devices(struct dm_target *ti,
2714 iterate_devices_callout_fn fn, void *data)
2715{
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002716 sector_t blocks;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002717 struct thin_c *tc = ti->private;
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002718 struct pool *pool = tc->pool;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002719
2720 /*
2721 * We can't call dm_pool_get_data_dev_size() since that blocks. So
2722 * we follow a more convoluted path through to the pool's target.
2723 */
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002724 if (!pool->ti)
Joe Thornber991d9fa2011-10-31 20:21:18 +00002725 return 0; /* nothing is bound */
2726
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002727 blocks = pool->ti->len;
2728 (void) sector_div(blocks, pool->sectors_per_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002729 if (blocks)
Mike Snitzer55f2b8b2012-07-27 15:08:02 +01002730 return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002731
2732 return 0;
2733}
2734
Mike Snitzer0424caa2012-09-26 23:45:47 +01002735/*
2736 * A thin device always inherits its queue limits from its pool.
2737 */
Joe Thornber991d9fa2011-10-31 20:21:18 +00002738static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)
2739{
2740 struct thin_c *tc = ti->private;
2741
Mike Snitzer0424caa2012-09-26 23:45:47 +01002742 *limits = bdev_get_queue(tc->pool_dev->bdev)->limits;
Joe Thornber991d9fa2011-10-31 20:21:18 +00002743}
2744
2745static struct target_type thin_target = {
2746 .name = "thin",
Mike Snitzer4f81a412012-10-12 21:02:13 +01002747 .version = {1, 5, 0},
Joe Thornber991d9fa2011-10-31 20:21:18 +00002748 .module = THIS_MODULE,
2749 .ctr = thin_ctr,
2750 .dtr = thin_dtr,
2751 .map = thin_map,
Joe Thornbereb2aa482012-03-28 18:41:28 +01002752 .end_io = thin_endio,
Joe Thornber991d9fa2011-10-31 20:21:18 +00002753 .postsuspend = thin_postsuspend,
2754 .status = thin_status,
2755 .iterate_devices = thin_iterate_devices,
2756 .io_hints = thin_io_hints,
2757};
2758
2759/*----------------------------------------------------------------*/
2760
2761static int __init dm_thin_init(void)
2762{
2763 int r;
2764
2765 pool_table_init();
2766
2767 r = dm_register_target(&thin_target);
2768 if (r)
2769 return r;
2770
2771 r = dm_register_target(&pool_target);
2772 if (r)
Mike Snitzera24c2562012-06-03 00:30:00 +01002773 goto bad_pool_target;
2774
2775 r = -ENOMEM;
2776
Mike Snitzera24c2562012-06-03 00:30:00 +01002777 _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
2778 if (!_new_mapping_cache)
2779 goto bad_new_mapping_cache;
2780
2781 _endio_hook_cache = KMEM_CACHE(dm_thin_endio_hook, 0);
2782 if (!_endio_hook_cache)
2783 goto bad_endio_hook_cache;
2784
2785 return 0;
2786
2787bad_endio_hook_cache:
2788 kmem_cache_destroy(_new_mapping_cache);
2789bad_new_mapping_cache:
Mike Snitzera24c2562012-06-03 00:30:00 +01002790 dm_unregister_target(&pool_target);
2791bad_pool_target:
2792 dm_unregister_target(&thin_target);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002793
2794 return r;
2795}
2796
2797static void dm_thin_exit(void)
2798{
2799 dm_unregister_target(&thin_target);
2800 dm_unregister_target(&pool_target);
Mike Snitzera24c2562012-06-03 00:30:00 +01002801
Mike Snitzera24c2562012-06-03 00:30:00 +01002802 kmem_cache_destroy(_new_mapping_cache);
2803 kmem_cache_destroy(_endio_hook_cache);
Joe Thornber991d9fa2011-10-31 20:21:18 +00002804}
2805
2806module_init(dm_thin_init);
2807module_exit(dm_thin_exit);
2808
Alasdair G Kergon7cab8bf2012-05-12 01:43:19 +01002809MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
Joe Thornber991d9fa2011-10-31 20:21:18 +00002810MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2811MODULE_LICENSE("GPL");