block: make /sys/block/<dev>/queue/discard_max_bytes writeable
[deliverable/linux.git] / drivers / md / dm-thin.c
1 /*
2 * Copyright (C) 2011-2012 Red Hat UK.
3 *
4 * This file is released under the GPL.
5 */
6
7 #include "dm-thin-metadata.h"
8 #include "dm-bio-prison.h"
9 #include "dm.h"
10
11 #include <linux/device-mapper.h>
12 #include <linux/dm-io.h>
13 #include <linux/dm-kcopyd.h>
14 #include <linux/jiffies.h>
15 #include <linux/log2.h>
16 #include <linux/list.h>
17 #include <linux/rculist.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/sort.h>
22 #include <linux/rbtree.h>
23
24 #define DM_MSG_PREFIX "thin"
25
26 /*
27 * Tunable constants
28 */
29 #define ENDIO_HOOK_POOL_SIZE 1024
30 #define MAPPING_POOL_SIZE 1024
31 #define COMMIT_PERIOD HZ
32 #define NO_SPACE_TIMEOUT_SECS 60
33
34 static unsigned no_space_timeout_secs = NO_SPACE_TIMEOUT_SECS;
35
36 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
37 "A percentage of time allocated for copy on write");
38
39 /*
40 * The block size of the device holding pool data must be
41 * between 64KB and 1GB.
42 */
43 #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
44 #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
45
46 /*
47 * Device id is restricted to 24 bits.
48 */
49 #define MAX_DEV_ID ((1 << 24) - 1)
50
51 /*
52 * How do we handle breaking sharing of data blocks?
53 * =================================================
54 *
55 * We use a standard copy-on-write btree to store the mappings for the
56 * devices (note I'm talking about copy-on-write of the metadata here, not
57 * the data). When you take an internal snapshot you clone the root node
58 * of the origin btree. After this there is no concept of an origin or a
59 * snapshot. They are just two device trees that happen to point to the
60 * same data blocks.
61 *
62 * When we get a write in we decide if it's to a shared data block using
63 * some timestamp magic. If it is, we have to break sharing.
64 *
65 * Let's say we write to a shared block in what was the origin. The
66 * steps are:
67 *
68 * i) plug io further to this physical block. (see bio_prison code).
69 *
70 * ii) quiesce any read io to that shared data block. Obviously
71 * including all devices that share this block. (see dm_deferred_set code)
72 *
73 * iii) copy the data block to a newly allocate block. This step can be
74 * missed out if the io covers the block. (schedule_copy).
75 *
76 * iv) insert the new mapping into the origin's btree
77 * (process_prepared_mapping). This act of inserting breaks some
78 * sharing of btree nodes between the two devices. Breaking sharing only
79 * effects the btree of that specific device. Btrees for the other
80 * devices that share the block never change. The btree for the origin
81 * device as it was after the last commit is untouched, ie. we're using
82 * persistent data structures in the functional programming sense.
83 *
84 * v) unplug io to this physical block, including the io that triggered
85 * the breaking of sharing.
86 *
87 * Steps (ii) and (iii) occur in parallel.
88 *
89 * The metadata _doesn't_ need to be committed before the io continues. We
90 * get away with this because the io is always written to a _new_ block.
91 * If there's a crash, then:
92 *
93 * - The origin mapping will point to the old origin block (the shared
94 * one). This will contain the data as it was before the io that triggered
95 * the breaking of sharing came in.
96 *
97 * - The snap mapping still points to the old block. As it would after
98 * the commit.
99 *
100 * The downside of this scheme is the timestamp magic isn't perfect, and
101 * will continue to think that data block in the snapshot device is shared
102 * even after the write to the origin has broken sharing. I suspect data
103 * blocks will typically be shared by many different devices, so we're
104 * breaking sharing n + 1 times, rather than n, where n is the number of
105 * devices that reference this data block. At the moment I think the
106 * benefits far, far outweigh the disadvantages.
107 */
108
109 /*----------------------------------------------------------------*/
110
111 /*
112 * Key building.
113 */
114 enum lock_space {
115 VIRTUAL,
116 PHYSICAL
117 };
118
119 static void build_key(struct dm_thin_device *td, enum lock_space ls,
120 dm_block_t b, dm_block_t e, struct dm_cell_key *key)
121 {
122 key->virtual = (ls == VIRTUAL);
123 key->dev = dm_thin_dev_id(td);
124 key->block_begin = b;
125 key->block_end = e;
126 }
127
128 static void build_data_key(struct dm_thin_device *td, dm_block_t b,
129 struct dm_cell_key *key)
130 {
131 build_key(td, PHYSICAL, b, b + 1llu, key);
132 }
133
134 static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
135 struct dm_cell_key *key)
136 {
137 build_key(td, VIRTUAL, b, b + 1llu, key);
138 }
139
140 /*----------------------------------------------------------------*/
141
142 #define THROTTLE_THRESHOLD (1 * HZ)
143
144 struct throttle {
145 struct rw_semaphore lock;
146 unsigned long threshold;
147 bool throttle_applied;
148 };
149
150 static void throttle_init(struct throttle *t)
151 {
152 init_rwsem(&t->lock);
153 t->throttle_applied = false;
154 }
155
156 static void throttle_work_start(struct throttle *t)
157 {
158 t->threshold = jiffies + THROTTLE_THRESHOLD;
159 }
160
161 static void throttle_work_update(struct throttle *t)
162 {
163 if (!t->throttle_applied && jiffies > t->threshold) {
164 down_write(&t->lock);
165 t->throttle_applied = true;
166 }
167 }
168
169 static void throttle_work_complete(struct throttle *t)
170 {
171 if (t->throttle_applied) {
172 t->throttle_applied = false;
173 up_write(&t->lock);
174 }
175 }
176
177 static void throttle_lock(struct throttle *t)
178 {
179 down_read(&t->lock);
180 }
181
182 static void throttle_unlock(struct throttle *t)
183 {
184 up_read(&t->lock);
185 }
186
187 /*----------------------------------------------------------------*/
188
189 /*
190 * A pool device ties together a metadata device and a data device. It
191 * also provides the interface for creating and destroying internal
192 * devices.
193 */
194 struct dm_thin_new_mapping;
195
196 /*
197 * The pool runs in 4 modes. Ordered in degraded order for comparisons.
198 */
199 enum pool_mode {
200 PM_WRITE, /* metadata may be changed */
201 PM_OUT_OF_DATA_SPACE, /* metadata may be changed, though data may not be allocated */
202 PM_READ_ONLY, /* metadata may not be changed */
203 PM_FAIL, /* all I/O fails */
204 };
205
206 struct pool_features {
207 enum pool_mode mode;
208
209 bool zero_new_blocks:1;
210 bool discard_enabled:1;
211 bool discard_passdown:1;
212 bool error_if_no_space:1;
213 };
214
215 struct thin_c;
216 typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio);
217 typedef void (*process_cell_fn)(struct thin_c *tc, struct dm_bio_prison_cell *cell);
218 typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m);
219
220 #define CELL_SORT_ARRAY_SIZE 8192
221
222 struct pool {
223 struct list_head list;
224 struct dm_target *ti; /* Only set if a pool target is bound */
225
226 struct mapped_device *pool_md;
227 struct block_device *md_dev;
228 struct dm_pool_metadata *pmd;
229
230 dm_block_t low_water_blocks;
231 uint32_t sectors_per_block;
232 int sectors_per_block_shift;
233
234 struct pool_features pf;
235 bool low_water_triggered:1; /* A dm event has been sent */
236 bool suspended:1;
237
238 struct dm_bio_prison *prison;
239 struct dm_kcopyd_client *copier;
240
241 struct workqueue_struct *wq;
242 struct throttle throttle;
243 struct work_struct worker;
244 struct delayed_work waker;
245 struct delayed_work no_space_timeout;
246
247 unsigned long last_commit_jiffies;
248 unsigned ref_count;
249
250 spinlock_t lock;
251 struct bio_list deferred_flush_bios;
252 struct list_head prepared_mappings;
253 struct list_head prepared_discards;
254 struct list_head active_thins;
255
256 struct dm_deferred_set *shared_read_ds;
257 struct dm_deferred_set *all_io_ds;
258
259 struct dm_thin_new_mapping *next_mapping;
260 mempool_t *mapping_pool;
261
262 process_bio_fn process_bio;
263 process_bio_fn process_discard;
264
265 process_cell_fn process_cell;
266 process_cell_fn process_discard_cell;
267
268 process_mapping_fn process_prepared_mapping;
269 process_mapping_fn process_prepared_discard;
270
271 struct dm_bio_prison_cell *cell_sort_array[CELL_SORT_ARRAY_SIZE];
272 };
273
274 static enum pool_mode get_pool_mode(struct pool *pool);
275 static void metadata_operation_failed(struct pool *pool, const char *op, int r);
276
277 /*
278 * Target context for a pool.
279 */
280 struct pool_c {
281 struct dm_target *ti;
282 struct pool *pool;
283 struct dm_dev *data_dev;
284 struct dm_dev *metadata_dev;
285 struct dm_target_callbacks callbacks;
286
287 dm_block_t low_water_blocks;
288 struct pool_features requested_pf; /* Features requested during table load */
289 struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */
290 };
291
292 /*
293 * Target context for a thin.
294 */
295 struct thin_c {
296 struct list_head list;
297 struct dm_dev *pool_dev;
298 struct dm_dev *origin_dev;
299 sector_t origin_size;
300 dm_thin_id dev_id;
301
302 struct pool *pool;
303 struct dm_thin_device *td;
304 struct mapped_device *thin_md;
305
306 bool requeue_mode:1;
307 spinlock_t lock;
308 struct list_head deferred_cells;
309 struct bio_list deferred_bio_list;
310 struct bio_list retry_on_resume_list;
311 struct rb_root sort_bio_list; /* sorted list of deferred bios */
312
313 /*
314 * Ensures the thin is not destroyed until the worker has finished
315 * iterating the active_thins list.
316 */
317 atomic_t refcount;
318 struct completion can_destroy;
319 };
320
321 /*----------------------------------------------------------------*/
322
323 /**
324 * __blkdev_issue_discard_async - queue a discard with async completion
325 * @bdev: blockdev to issue discard for
326 * @sector: start sector
327 * @nr_sects: number of sectors to discard
328 * @gfp_mask: memory allocation flags (for bio_alloc)
329 * @flags: BLKDEV_IFL_* flags to control behaviour
330 * @parent_bio: parent discard bio that all sub discards get chained to
331 *
332 * Description:
333 * Asynchronously issue a discard request for the sectors in question.
334 * NOTE: this variant of blk-core's blkdev_issue_discard() is a stop-gap
335 * that is being kept local to DM thinp until the block changes to allow
336 * late bio splitting land upstream.
337 */
338 static int __blkdev_issue_discard_async(struct block_device *bdev, sector_t sector,
339 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags,
340 struct bio *parent_bio)
341 {
342 struct request_queue *q = bdev_get_queue(bdev);
343 int type = REQ_WRITE | REQ_DISCARD;
344 unsigned int max_discard_sectors, granularity;
345 int alignment;
346 struct bio *bio;
347 int ret = 0;
348 struct blk_plug plug;
349
350 if (!q)
351 return -ENXIO;
352
353 if (!blk_queue_discard(q))
354 return -EOPNOTSUPP;
355
356 /* Zero-sector (unknown) and one-sector granularities are the same. */
357 granularity = max(q->limits.discard_granularity >> 9, 1U);
358 alignment = (bdev_discard_alignment(bdev) >> 9) % granularity;
359
360 /*
361 * Ensure that max_discard_sectors is of the proper
362 * granularity, so that requests stay aligned after a split.
363 */
364 max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9);
365 max_discard_sectors -= max_discard_sectors % granularity;
366 if (unlikely(!max_discard_sectors)) {
367 /* Avoid infinite loop below. Being cautious never hurts. */
368 return -EOPNOTSUPP;
369 }
370
371 if (flags & BLKDEV_DISCARD_SECURE) {
372 if (!blk_queue_secdiscard(q))
373 return -EOPNOTSUPP;
374 type |= REQ_SECURE;
375 }
376
377 blk_start_plug(&plug);
378 while (nr_sects) {
379 unsigned int req_sects;
380 sector_t end_sect, tmp;
381
382 /*
383 * Required bio_put occurs in bio_endio thanks to bio_chain below
384 */
385 bio = bio_alloc(gfp_mask, 1);
386 if (!bio) {
387 ret = -ENOMEM;
388 break;
389 }
390
391 req_sects = min_t(sector_t, nr_sects, max_discard_sectors);
392
393 /*
394 * If splitting a request, and the next starting sector would be
395 * misaligned, stop the discard at the previous aligned sector.
396 */
397 end_sect = sector + req_sects;
398 tmp = end_sect;
399 if (req_sects < nr_sects &&
400 sector_div(tmp, granularity) != alignment) {
401 end_sect = end_sect - alignment;
402 sector_div(end_sect, granularity);
403 end_sect = end_sect * granularity + alignment;
404 req_sects = end_sect - sector;
405 }
406
407 bio_chain(bio, parent_bio);
408
409 bio->bi_iter.bi_sector = sector;
410 bio->bi_bdev = bdev;
411
412 bio->bi_iter.bi_size = req_sects << 9;
413 nr_sects -= req_sects;
414 sector = end_sect;
415
416 submit_bio(type, bio);
417
418 /*
419 * We can loop for a long time in here, if someone does
420 * full device discards (like mkfs). Be nice and allow
421 * us to schedule out to avoid softlocking if preempt
422 * is disabled.
423 */
424 cond_resched();
425 }
426 blk_finish_plug(&plug);
427
428 return ret;
429 }
430
431 static bool block_size_is_power_of_two(struct pool *pool)
432 {
433 return pool->sectors_per_block_shift >= 0;
434 }
435
436 static sector_t block_to_sectors(struct pool *pool, dm_block_t b)
437 {
438 return block_size_is_power_of_two(pool) ?
439 (b << pool->sectors_per_block_shift) :
440 (b * pool->sectors_per_block);
441 }
442
443 static int issue_discard(struct thin_c *tc, dm_block_t data_b, dm_block_t data_e,
444 struct bio *parent_bio)
445 {
446 sector_t s = block_to_sectors(tc->pool, data_b);
447 sector_t len = block_to_sectors(tc->pool, data_e - data_b);
448
449 return __blkdev_issue_discard_async(tc->pool_dev->bdev, s, len,
450 GFP_NOWAIT, 0, parent_bio);
451 }
452
453 /*----------------------------------------------------------------*/
454
455 /*
456 * wake_worker() is used when new work is queued and when pool_resume is
457 * ready to continue deferred IO processing.
458 */
459 static void wake_worker(struct pool *pool)
460 {
461 queue_work(pool->wq, &pool->worker);
462 }
463
464 /*----------------------------------------------------------------*/
465
466 static int bio_detain(struct pool *pool, struct dm_cell_key *key, struct bio *bio,
467 struct dm_bio_prison_cell **cell_result)
468 {
469 int r;
470 struct dm_bio_prison_cell *cell_prealloc;
471
472 /*
473 * Allocate a cell from the prison's mempool.
474 * This might block but it can't fail.
475 */
476 cell_prealloc = dm_bio_prison_alloc_cell(pool->prison, GFP_NOIO);
477
478 r = dm_bio_detain(pool->prison, key, bio, cell_prealloc, cell_result);
479 if (r)
480 /*
481 * We reused an old cell; we can get rid of
482 * the new one.
483 */
484 dm_bio_prison_free_cell(pool->prison, cell_prealloc);
485
486 return r;
487 }
488
489 static void cell_release(struct pool *pool,
490 struct dm_bio_prison_cell *cell,
491 struct bio_list *bios)
492 {
493 dm_cell_release(pool->prison, cell, bios);
494 dm_bio_prison_free_cell(pool->prison, cell);
495 }
496
497 static void cell_visit_release(struct pool *pool,
498 void (*fn)(void *, struct dm_bio_prison_cell *),
499 void *context,
500 struct dm_bio_prison_cell *cell)
501 {
502 dm_cell_visit_release(pool->prison, fn, context, cell);
503 dm_bio_prison_free_cell(pool->prison, cell);
504 }
505
506 static void cell_release_no_holder(struct pool *pool,
507 struct dm_bio_prison_cell *cell,
508 struct bio_list *bios)
509 {
510 dm_cell_release_no_holder(pool->prison, cell, bios);
511 dm_bio_prison_free_cell(pool->prison, cell);
512 }
513
514 static void cell_error_with_code(struct pool *pool,
515 struct dm_bio_prison_cell *cell, int error_code)
516 {
517 dm_cell_error(pool->prison, cell, error_code);
518 dm_bio_prison_free_cell(pool->prison, cell);
519 }
520
521 static void cell_error(struct pool *pool, struct dm_bio_prison_cell *cell)
522 {
523 cell_error_with_code(pool, cell, -EIO);
524 }
525
526 static void cell_success(struct pool *pool, struct dm_bio_prison_cell *cell)
527 {
528 cell_error_with_code(pool, cell, 0);
529 }
530
531 static void cell_requeue(struct pool *pool, struct dm_bio_prison_cell *cell)
532 {
533 cell_error_with_code(pool, cell, DM_ENDIO_REQUEUE);
534 }
535
536 /*----------------------------------------------------------------*/
537
538 /*
539 * A global list of pools that uses a struct mapped_device as a key.
540 */
541 static struct dm_thin_pool_table {
542 struct mutex mutex;
543 struct list_head pools;
544 } dm_thin_pool_table;
545
546 static void pool_table_init(void)
547 {
548 mutex_init(&dm_thin_pool_table.mutex);
549 INIT_LIST_HEAD(&dm_thin_pool_table.pools);
550 }
551
552 static void __pool_table_insert(struct pool *pool)
553 {
554 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
555 list_add(&pool->list, &dm_thin_pool_table.pools);
556 }
557
558 static void __pool_table_remove(struct pool *pool)
559 {
560 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
561 list_del(&pool->list);
562 }
563
564 static struct pool *__pool_table_lookup(struct mapped_device *md)
565 {
566 struct pool *pool = NULL, *tmp;
567
568 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
569
570 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
571 if (tmp->pool_md == md) {
572 pool = tmp;
573 break;
574 }
575 }
576
577 return pool;
578 }
579
580 static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
581 {
582 struct pool *pool = NULL, *tmp;
583
584 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
585
586 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
587 if (tmp->md_dev == md_dev) {
588 pool = tmp;
589 break;
590 }
591 }
592
593 return pool;
594 }
595
596 /*----------------------------------------------------------------*/
597
598 struct dm_thin_endio_hook {
599 struct thin_c *tc;
600 struct dm_deferred_entry *shared_read_entry;
601 struct dm_deferred_entry *all_io_entry;
602 struct dm_thin_new_mapping *overwrite_mapping;
603 struct rb_node rb_node;
604 struct dm_bio_prison_cell *cell;
605 };
606
607 static void __merge_bio_list(struct bio_list *bios, struct bio_list *master)
608 {
609 bio_list_merge(bios, master);
610 bio_list_init(master);
611 }
612
613 static void error_bio_list(struct bio_list *bios, int error)
614 {
615 struct bio *bio;
616
617 while ((bio = bio_list_pop(bios)))
618 bio_endio(bio, error);
619 }
620
621 static void error_thin_bio_list(struct thin_c *tc, struct bio_list *master, int error)
622 {
623 struct bio_list bios;
624 unsigned long flags;
625
626 bio_list_init(&bios);
627
628 spin_lock_irqsave(&tc->lock, flags);
629 __merge_bio_list(&bios, master);
630 spin_unlock_irqrestore(&tc->lock, flags);
631
632 error_bio_list(&bios, error);
633 }
634
635 static void requeue_deferred_cells(struct thin_c *tc)
636 {
637 struct pool *pool = tc->pool;
638 unsigned long flags;
639 struct list_head cells;
640 struct dm_bio_prison_cell *cell, *tmp;
641
642 INIT_LIST_HEAD(&cells);
643
644 spin_lock_irqsave(&tc->lock, flags);
645 list_splice_init(&tc->deferred_cells, &cells);
646 spin_unlock_irqrestore(&tc->lock, flags);
647
648 list_for_each_entry_safe(cell, tmp, &cells, user_list)
649 cell_requeue(pool, cell);
650 }
651
652 static void requeue_io(struct thin_c *tc)
653 {
654 struct bio_list bios;
655 unsigned long flags;
656
657 bio_list_init(&bios);
658
659 spin_lock_irqsave(&tc->lock, flags);
660 __merge_bio_list(&bios, &tc->deferred_bio_list);
661 __merge_bio_list(&bios, &tc->retry_on_resume_list);
662 spin_unlock_irqrestore(&tc->lock, flags);
663
664 error_bio_list(&bios, DM_ENDIO_REQUEUE);
665 requeue_deferred_cells(tc);
666 }
667
668 static void error_retry_list(struct pool *pool)
669 {
670 struct thin_c *tc;
671
672 rcu_read_lock();
673 list_for_each_entry_rcu(tc, &pool->active_thins, list)
674 error_thin_bio_list(tc, &tc->retry_on_resume_list, -EIO);
675 rcu_read_unlock();
676 }
677
678 /*
679 * This section of code contains the logic for processing a thin device's IO.
680 * Much of the code depends on pool object resources (lists, workqueues, etc)
681 * but most is exclusively called from the thin target rather than the thin-pool
682 * target.
683 */
684
685 static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
686 {
687 struct pool *pool = tc->pool;
688 sector_t block_nr = bio->bi_iter.bi_sector;
689
690 if (block_size_is_power_of_two(pool))
691 block_nr >>= pool->sectors_per_block_shift;
692 else
693 (void) sector_div(block_nr, pool->sectors_per_block);
694
695 return block_nr;
696 }
697
698 /*
699 * Returns the _complete_ blocks that this bio covers.
700 */
701 static void get_bio_block_range(struct thin_c *tc, struct bio *bio,
702 dm_block_t *begin, dm_block_t *end)
703 {
704 struct pool *pool = tc->pool;
705 sector_t b = bio->bi_iter.bi_sector;
706 sector_t e = b + (bio->bi_iter.bi_size >> SECTOR_SHIFT);
707
708 b += pool->sectors_per_block - 1ull; /* so we round up */
709
710 if (block_size_is_power_of_two(pool)) {
711 b >>= pool->sectors_per_block_shift;
712 e >>= pool->sectors_per_block_shift;
713 } else {
714 (void) sector_div(b, pool->sectors_per_block);
715 (void) sector_div(e, pool->sectors_per_block);
716 }
717
718 if (e < b)
719 /* Can happen if the bio is within a single block. */
720 e = b;
721
722 *begin = b;
723 *end = e;
724 }
725
726 static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
727 {
728 struct pool *pool = tc->pool;
729 sector_t bi_sector = bio->bi_iter.bi_sector;
730
731 bio->bi_bdev = tc->pool_dev->bdev;
732 if (block_size_is_power_of_two(pool))
733 bio->bi_iter.bi_sector =
734 (block << pool->sectors_per_block_shift) |
735 (bi_sector & (pool->sectors_per_block - 1));
736 else
737 bio->bi_iter.bi_sector = (block * pool->sectors_per_block) +
738 sector_div(bi_sector, pool->sectors_per_block);
739 }
740
741 static void remap_to_origin(struct thin_c *tc, struct bio *bio)
742 {
743 bio->bi_bdev = tc->origin_dev->bdev;
744 }
745
746 static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
747 {
748 return (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) &&
749 dm_thin_changed_this_transaction(tc->td);
750 }
751
752 static void inc_all_io_entry(struct pool *pool, struct bio *bio)
753 {
754 struct dm_thin_endio_hook *h;
755
756 if (bio->bi_rw & REQ_DISCARD)
757 return;
758
759 h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
760 h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
761 }
762
763 static void issue(struct thin_c *tc, struct bio *bio)
764 {
765 struct pool *pool = tc->pool;
766 unsigned long flags;
767
768 if (!bio_triggers_commit(tc, bio)) {
769 generic_make_request(bio);
770 return;
771 }
772
773 /*
774 * Complete bio with an error if earlier I/O caused changes to
775 * the metadata that can't be committed e.g, due to I/O errors
776 * on the metadata device.
777 */
778 if (dm_thin_aborted_changes(tc->td)) {
779 bio_io_error(bio);
780 return;
781 }
782
783 /*
784 * Batch together any bios that trigger commits and then issue a
785 * single commit for them in process_deferred_bios().
786 */
787 spin_lock_irqsave(&pool->lock, flags);
788 bio_list_add(&pool->deferred_flush_bios, bio);
789 spin_unlock_irqrestore(&pool->lock, flags);
790 }
791
792 static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
793 {
794 remap_to_origin(tc, bio);
795 issue(tc, bio);
796 }
797
798 static void remap_and_issue(struct thin_c *tc, struct bio *bio,
799 dm_block_t block)
800 {
801 remap(tc, bio, block);
802 issue(tc, bio);
803 }
804
805 /*----------------------------------------------------------------*/
806
807 /*
808 * Bio endio functions.
809 */
810 struct dm_thin_new_mapping {
811 struct list_head list;
812
813 bool pass_discard:1;
814 bool maybe_shared:1;
815
816 /*
817 * Track quiescing, copying and zeroing preparation actions. When this
818 * counter hits zero the block is prepared and can be inserted into the
819 * btree.
820 */
821 atomic_t prepare_actions;
822
823 int err;
824 struct thin_c *tc;
825 dm_block_t virt_begin, virt_end;
826 dm_block_t data_block;
827 struct dm_bio_prison_cell *cell;
828
829 /*
830 * If the bio covers the whole area of a block then we can avoid
831 * zeroing or copying. Instead this bio is hooked. The bio will
832 * still be in the cell, so care has to be taken to avoid issuing
833 * the bio twice.
834 */
835 struct bio *bio;
836 bio_end_io_t *saved_bi_end_io;
837 };
838
839 static void __complete_mapping_preparation(struct dm_thin_new_mapping *m)
840 {
841 struct pool *pool = m->tc->pool;
842
843 if (atomic_dec_and_test(&m->prepare_actions)) {
844 list_add_tail(&m->list, &pool->prepared_mappings);
845 wake_worker(pool);
846 }
847 }
848
849 static void complete_mapping_preparation(struct dm_thin_new_mapping *m)
850 {
851 unsigned long flags;
852 struct pool *pool = m->tc->pool;
853
854 spin_lock_irqsave(&pool->lock, flags);
855 __complete_mapping_preparation(m);
856 spin_unlock_irqrestore(&pool->lock, flags);
857 }
858
859 static void copy_complete(int read_err, unsigned long write_err, void *context)
860 {
861 struct dm_thin_new_mapping *m = context;
862
863 m->err = read_err || write_err ? -EIO : 0;
864 complete_mapping_preparation(m);
865 }
866
867 static void overwrite_endio(struct bio *bio, int err)
868 {
869 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
870 struct dm_thin_new_mapping *m = h->overwrite_mapping;
871
872 bio->bi_end_io = m->saved_bi_end_io;
873
874 m->err = err;
875 complete_mapping_preparation(m);
876 }
877
878 /*----------------------------------------------------------------*/
879
880 /*
881 * Workqueue.
882 */
883
884 /*
885 * Prepared mapping jobs.
886 */
887
888 /*
889 * This sends the bios in the cell, except the original holder, back
890 * to the deferred_bios list.
891 */
892 static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
893 {
894 struct pool *pool = tc->pool;
895 unsigned long flags;
896
897 spin_lock_irqsave(&tc->lock, flags);
898 cell_release_no_holder(pool, cell, &tc->deferred_bio_list);
899 spin_unlock_irqrestore(&tc->lock, flags);
900
901 wake_worker(pool);
902 }
903
904 static void thin_defer_bio(struct thin_c *tc, struct bio *bio);
905
906 struct remap_info {
907 struct thin_c *tc;
908 struct bio_list defer_bios;
909 struct bio_list issue_bios;
910 };
911
912 static void __inc_remap_and_issue_cell(void *context,
913 struct dm_bio_prison_cell *cell)
914 {
915 struct remap_info *info = context;
916 struct bio *bio;
917
918 while ((bio = bio_list_pop(&cell->bios))) {
919 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA))
920 bio_list_add(&info->defer_bios, bio);
921 else {
922 inc_all_io_entry(info->tc->pool, bio);
923
924 /*
925 * We can't issue the bios with the bio prison lock
926 * held, so we add them to a list to issue on
927 * return from this function.
928 */
929 bio_list_add(&info->issue_bios, bio);
930 }
931 }
932 }
933
934 static void inc_remap_and_issue_cell(struct thin_c *tc,
935 struct dm_bio_prison_cell *cell,
936 dm_block_t block)
937 {
938 struct bio *bio;
939 struct remap_info info;
940
941 info.tc = tc;
942 bio_list_init(&info.defer_bios);
943 bio_list_init(&info.issue_bios);
944
945 /*
946 * We have to be careful to inc any bios we're about to issue
947 * before the cell is released, and avoid a race with new bios
948 * being added to the cell.
949 */
950 cell_visit_release(tc->pool, __inc_remap_and_issue_cell,
951 &info, cell);
952
953 while ((bio = bio_list_pop(&info.defer_bios)))
954 thin_defer_bio(tc, bio);
955
956 while ((bio = bio_list_pop(&info.issue_bios)))
957 remap_and_issue(info.tc, bio, block);
958 }
959
960 static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
961 {
962 cell_error(m->tc->pool, m->cell);
963 list_del(&m->list);
964 mempool_free(m, m->tc->pool->mapping_pool);
965 }
966
967 static void process_prepared_mapping(struct dm_thin_new_mapping *m)
968 {
969 struct thin_c *tc = m->tc;
970 struct pool *pool = tc->pool;
971 struct bio *bio = m->bio;
972 int r;
973
974 if (m->err) {
975 cell_error(pool, m->cell);
976 goto out;
977 }
978
979 /*
980 * Commit the prepared block into the mapping btree.
981 * Any I/O for this block arriving after this point will get
982 * remapped to it directly.
983 */
984 r = dm_thin_insert_block(tc->td, m->virt_begin, m->data_block);
985 if (r) {
986 metadata_operation_failed(pool, "dm_thin_insert_block", r);
987 cell_error(pool, m->cell);
988 goto out;
989 }
990
991 /*
992 * Release any bios held while the block was being provisioned.
993 * If we are processing a write bio that completely covers the block,
994 * we already processed it so can ignore it now when processing
995 * the bios in the cell.
996 */
997 if (bio) {
998 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
999 bio_endio(bio, 0);
1000 } else {
1001 inc_all_io_entry(tc->pool, m->cell->holder);
1002 remap_and_issue(tc, m->cell->holder, m->data_block);
1003 inc_remap_and_issue_cell(tc, m->cell, m->data_block);
1004 }
1005
1006 out:
1007 list_del(&m->list);
1008 mempool_free(m, pool->mapping_pool);
1009 }
1010
1011 /*----------------------------------------------------------------*/
1012
1013 static void free_discard_mapping(struct dm_thin_new_mapping *m)
1014 {
1015 struct thin_c *tc = m->tc;
1016 if (m->cell)
1017 cell_defer_no_holder(tc, m->cell);
1018 mempool_free(m, tc->pool->mapping_pool);
1019 }
1020
1021 static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
1022 {
1023 bio_io_error(m->bio);
1024 free_discard_mapping(m);
1025 }
1026
1027 static void process_prepared_discard_success(struct dm_thin_new_mapping *m)
1028 {
1029 bio_endio(m->bio, 0);
1030 free_discard_mapping(m);
1031 }
1032
1033 static void process_prepared_discard_no_passdown(struct dm_thin_new_mapping *m)
1034 {
1035 int r;
1036 struct thin_c *tc = m->tc;
1037
1038 r = dm_thin_remove_range(tc->td, m->cell->key.block_begin, m->cell->key.block_end);
1039 if (r) {
1040 metadata_operation_failed(tc->pool, "dm_thin_remove_range", r);
1041 bio_io_error(m->bio);
1042 } else
1043 bio_endio(m->bio, 0);
1044
1045 cell_defer_no_holder(tc, m->cell);
1046 mempool_free(m, tc->pool->mapping_pool);
1047 }
1048
1049 static int passdown_double_checking_shared_status(struct dm_thin_new_mapping *m)
1050 {
1051 /*
1052 * We've already unmapped this range of blocks, but before we
1053 * passdown we have to check that these blocks are now unused.
1054 */
1055 int r;
1056 bool used = true;
1057 struct thin_c *tc = m->tc;
1058 struct pool *pool = tc->pool;
1059 dm_block_t b = m->data_block, e, end = m->data_block + m->virt_end - m->virt_begin;
1060
1061 while (b != end) {
1062 /* find start of unmapped run */
1063 for (; b < end; b++) {
1064 r = dm_pool_block_is_used(pool->pmd, b, &used);
1065 if (r)
1066 return r;
1067
1068 if (!used)
1069 break;
1070 }
1071
1072 if (b == end)
1073 break;
1074
1075 /* find end of run */
1076 for (e = b + 1; e != end; e++) {
1077 r = dm_pool_block_is_used(pool->pmd, e, &used);
1078 if (r)
1079 return r;
1080
1081 if (used)
1082 break;
1083 }
1084
1085 r = issue_discard(tc, b, e, m->bio);
1086 if (r)
1087 return r;
1088
1089 b = e;
1090 }
1091
1092 return 0;
1093 }
1094
1095 static void process_prepared_discard_passdown(struct dm_thin_new_mapping *m)
1096 {
1097 int r;
1098 struct thin_c *tc = m->tc;
1099 struct pool *pool = tc->pool;
1100
1101 r = dm_thin_remove_range(tc->td, m->virt_begin, m->virt_end);
1102 if (r)
1103 metadata_operation_failed(pool, "dm_thin_remove_range", r);
1104
1105 else if (m->maybe_shared)
1106 r = passdown_double_checking_shared_status(m);
1107 else
1108 r = issue_discard(tc, m->data_block, m->data_block + (m->virt_end - m->virt_begin), m->bio);
1109
1110 /*
1111 * Even if r is set, there could be sub discards in flight that we
1112 * need to wait for.
1113 */
1114 bio_endio(m->bio, r);
1115 cell_defer_no_holder(tc, m->cell);
1116 mempool_free(m, pool->mapping_pool);
1117 }
1118
1119 static void process_prepared(struct pool *pool, struct list_head *head,
1120 process_mapping_fn *fn)
1121 {
1122 unsigned long flags;
1123 struct list_head maps;
1124 struct dm_thin_new_mapping *m, *tmp;
1125
1126 INIT_LIST_HEAD(&maps);
1127 spin_lock_irqsave(&pool->lock, flags);
1128 list_splice_init(head, &maps);
1129 spin_unlock_irqrestore(&pool->lock, flags);
1130
1131 list_for_each_entry_safe(m, tmp, &maps, list)
1132 (*fn)(m);
1133 }
1134
1135 /*
1136 * Deferred bio jobs.
1137 */
1138 static int io_overlaps_block(struct pool *pool, struct bio *bio)
1139 {
1140 return bio->bi_iter.bi_size ==
1141 (pool->sectors_per_block << SECTOR_SHIFT);
1142 }
1143
1144 static int io_overwrites_block(struct pool *pool, struct bio *bio)
1145 {
1146 return (bio_data_dir(bio) == WRITE) &&
1147 io_overlaps_block(pool, bio);
1148 }
1149
1150 static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
1151 bio_end_io_t *fn)
1152 {
1153 *save = bio->bi_end_io;
1154 bio->bi_end_io = fn;
1155 }
1156
1157 static int ensure_next_mapping(struct pool *pool)
1158 {
1159 if (pool->next_mapping)
1160 return 0;
1161
1162 pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
1163
1164 return pool->next_mapping ? 0 : -ENOMEM;
1165 }
1166
1167 static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
1168 {
1169 struct dm_thin_new_mapping *m = pool->next_mapping;
1170
1171 BUG_ON(!pool->next_mapping);
1172
1173 memset(m, 0, sizeof(struct dm_thin_new_mapping));
1174 INIT_LIST_HEAD(&m->list);
1175 m->bio = NULL;
1176
1177 pool->next_mapping = NULL;
1178
1179 return m;
1180 }
1181
1182 static void ll_zero(struct thin_c *tc, struct dm_thin_new_mapping *m,
1183 sector_t begin, sector_t end)
1184 {
1185 int r;
1186 struct dm_io_region to;
1187
1188 to.bdev = tc->pool_dev->bdev;
1189 to.sector = begin;
1190 to.count = end - begin;
1191
1192 r = dm_kcopyd_zero(tc->pool->copier, 1, &to, 0, copy_complete, m);
1193 if (r < 0) {
1194 DMERR_LIMIT("dm_kcopyd_zero() failed");
1195 copy_complete(1, 1, m);
1196 }
1197 }
1198
1199 static void remap_and_issue_overwrite(struct thin_c *tc, struct bio *bio,
1200 dm_block_t data_begin,
1201 struct dm_thin_new_mapping *m)
1202 {
1203 struct pool *pool = tc->pool;
1204 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1205
1206 h->overwrite_mapping = m;
1207 m->bio = bio;
1208 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
1209 inc_all_io_entry(pool, bio);
1210 remap_and_issue(tc, bio, data_begin);
1211 }
1212
1213 /*
1214 * A partial copy also needs to zero the uncopied region.
1215 */
1216 static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
1217 struct dm_dev *origin, dm_block_t data_origin,
1218 dm_block_t data_dest,
1219 struct dm_bio_prison_cell *cell, struct bio *bio,
1220 sector_t len)
1221 {
1222 int r;
1223 struct pool *pool = tc->pool;
1224 struct dm_thin_new_mapping *m = get_next_mapping(pool);
1225
1226 m->tc = tc;
1227 m->virt_begin = virt_block;
1228 m->virt_end = virt_block + 1u;
1229 m->data_block = data_dest;
1230 m->cell = cell;
1231
1232 /*
1233 * quiesce action + copy action + an extra reference held for the
1234 * duration of this function (we may need to inc later for a
1235 * partial zero).
1236 */
1237 atomic_set(&m->prepare_actions, 3);
1238
1239 if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
1240 complete_mapping_preparation(m); /* already quiesced */
1241
1242 /*
1243 * IO to pool_dev remaps to the pool target's data_dev.
1244 *
1245 * If the whole block of data is being overwritten, we can issue the
1246 * bio immediately. Otherwise we use kcopyd to clone the data first.
1247 */
1248 if (io_overwrites_block(pool, bio))
1249 remap_and_issue_overwrite(tc, bio, data_dest, m);
1250 else {
1251 struct dm_io_region from, to;
1252
1253 from.bdev = origin->bdev;
1254 from.sector = data_origin * pool->sectors_per_block;
1255 from.count = len;
1256
1257 to.bdev = tc->pool_dev->bdev;
1258 to.sector = data_dest * pool->sectors_per_block;
1259 to.count = len;
1260
1261 r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
1262 0, copy_complete, m);
1263 if (r < 0) {
1264 DMERR_LIMIT("dm_kcopyd_copy() failed");
1265 copy_complete(1, 1, m);
1266
1267 /*
1268 * We allow the zero to be issued, to simplify the
1269 * error path. Otherwise we'd need to start
1270 * worrying about decrementing the prepare_actions
1271 * counter.
1272 */
1273 }
1274
1275 /*
1276 * Do we need to zero a tail region?
1277 */
1278 if (len < pool->sectors_per_block && pool->pf.zero_new_blocks) {
1279 atomic_inc(&m->prepare_actions);
1280 ll_zero(tc, m,
1281 data_dest * pool->sectors_per_block + len,
1282 (data_dest + 1) * pool->sectors_per_block);
1283 }
1284 }
1285
1286 complete_mapping_preparation(m); /* drop our ref */
1287 }
1288
1289 static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
1290 dm_block_t data_origin, dm_block_t data_dest,
1291 struct dm_bio_prison_cell *cell, struct bio *bio)
1292 {
1293 schedule_copy(tc, virt_block, tc->pool_dev,
1294 data_origin, data_dest, cell, bio,
1295 tc->pool->sectors_per_block);
1296 }
1297
1298 static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
1299 dm_block_t data_block, struct dm_bio_prison_cell *cell,
1300 struct bio *bio)
1301 {
1302 struct pool *pool = tc->pool;
1303 struct dm_thin_new_mapping *m = get_next_mapping(pool);
1304
1305 atomic_set(&m->prepare_actions, 1); /* no need to quiesce */
1306 m->tc = tc;
1307 m->virt_begin = virt_block;
1308 m->virt_end = virt_block + 1u;
1309 m->data_block = data_block;
1310 m->cell = cell;
1311
1312 /*
1313 * If the whole block of data is being overwritten or we are not
1314 * zeroing pre-existing data, we can issue the bio immediately.
1315 * Otherwise we use kcopyd to zero the data first.
1316 */
1317 if (pool->pf.zero_new_blocks) {
1318 if (io_overwrites_block(pool, bio))
1319 remap_and_issue_overwrite(tc, bio, data_block, m);
1320 else
1321 ll_zero(tc, m, data_block * pool->sectors_per_block,
1322 (data_block + 1) * pool->sectors_per_block);
1323 } else
1324 process_prepared_mapping(m);
1325 }
1326
1327 static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
1328 dm_block_t data_dest,
1329 struct dm_bio_prison_cell *cell, struct bio *bio)
1330 {
1331 struct pool *pool = tc->pool;
1332 sector_t virt_block_begin = virt_block * pool->sectors_per_block;
1333 sector_t virt_block_end = (virt_block + 1) * pool->sectors_per_block;
1334
1335 if (virt_block_end <= tc->origin_size)
1336 schedule_copy(tc, virt_block, tc->origin_dev,
1337 virt_block, data_dest, cell, bio,
1338 pool->sectors_per_block);
1339
1340 else if (virt_block_begin < tc->origin_size)
1341 schedule_copy(tc, virt_block, tc->origin_dev,
1342 virt_block, data_dest, cell, bio,
1343 tc->origin_size - virt_block_begin);
1344
1345 else
1346 schedule_zero(tc, virt_block, data_dest, cell, bio);
1347 }
1348
1349 static void set_pool_mode(struct pool *pool, enum pool_mode new_mode);
1350
1351 static void check_for_space(struct pool *pool)
1352 {
1353 int r;
1354 dm_block_t nr_free;
1355
1356 if (get_pool_mode(pool) != PM_OUT_OF_DATA_SPACE)
1357 return;
1358
1359 r = dm_pool_get_free_block_count(pool->pmd, &nr_free);
1360 if (r)
1361 return;
1362
1363 if (nr_free)
1364 set_pool_mode(pool, PM_WRITE);
1365 }
1366
1367 /*
1368 * A non-zero return indicates read_only or fail_io mode.
1369 * Many callers don't care about the return value.
1370 */
1371 static int commit(struct pool *pool)
1372 {
1373 int r;
1374
1375 if (get_pool_mode(pool) >= PM_READ_ONLY)
1376 return -EINVAL;
1377
1378 r = dm_pool_commit_metadata(pool->pmd);
1379 if (r)
1380 metadata_operation_failed(pool, "dm_pool_commit_metadata", r);
1381 else
1382 check_for_space(pool);
1383
1384 return r;
1385 }
1386
1387 static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks)
1388 {
1389 unsigned long flags;
1390
1391 if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
1392 DMWARN("%s: reached low water mark for data device: sending event.",
1393 dm_device_name(pool->pool_md));
1394 spin_lock_irqsave(&pool->lock, flags);
1395 pool->low_water_triggered = true;
1396 spin_unlock_irqrestore(&pool->lock, flags);
1397 dm_table_event(pool->ti->table);
1398 }
1399 }
1400
1401 static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
1402 {
1403 int r;
1404 dm_block_t free_blocks;
1405 struct pool *pool = tc->pool;
1406
1407 if (WARN_ON(get_pool_mode(pool) != PM_WRITE))
1408 return -EINVAL;
1409
1410 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
1411 if (r) {
1412 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
1413 return r;
1414 }
1415
1416 check_low_water_mark(pool, free_blocks);
1417
1418 if (!free_blocks) {
1419 /*
1420 * Try to commit to see if that will free up some
1421 * more space.
1422 */
1423 r = commit(pool);
1424 if (r)
1425 return r;
1426
1427 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
1428 if (r) {
1429 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
1430 return r;
1431 }
1432
1433 if (!free_blocks) {
1434 set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
1435 return -ENOSPC;
1436 }
1437 }
1438
1439 r = dm_pool_alloc_data_block(pool->pmd, result);
1440 if (r) {
1441 metadata_operation_failed(pool, "dm_pool_alloc_data_block", r);
1442 return r;
1443 }
1444
1445 return 0;
1446 }
1447
1448 /*
1449 * If we have run out of space, queue bios until the device is
1450 * resumed, presumably after having been reloaded with more space.
1451 */
1452 static void retry_on_resume(struct bio *bio)
1453 {
1454 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1455 struct thin_c *tc = h->tc;
1456 unsigned long flags;
1457
1458 spin_lock_irqsave(&tc->lock, flags);
1459 bio_list_add(&tc->retry_on_resume_list, bio);
1460 spin_unlock_irqrestore(&tc->lock, flags);
1461 }
1462
1463 static int should_error_unserviceable_bio(struct pool *pool)
1464 {
1465 enum pool_mode m = get_pool_mode(pool);
1466
1467 switch (m) {
1468 case PM_WRITE:
1469 /* Shouldn't get here */
1470 DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
1471 return -EIO;
1472
1473 case PM_OUT_OF_DATA_SPACE:
1474 return pool->pf.error_if_no_space ? -ENOSPC : 0;
1475
1476 case PM_READ_ONLY:
1477 case PM_FAIL:
1478 return -EIO;
1479 default:
1480 /* Shouldn't get here */
1481 DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
1482 return -EIO;
1483 }
1484 }
1485
1486 static void handle_unserviceable_bio(struct pool *pool, struct bio *bio)
1487 {
1488 int error = should_error_unserviceable_bio(pool);
1489
1490 if (error)
1491 bio_endio(bio, error);
1492 else
1493 retry_on_resume(bio);
1494 }
1495
1496 static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell)
1497 {
1498 struct bio *bio;
1499 struct bio_list bios;
1500 int error;
1501
1502 error = should_error_unserviceable_bio(pool);
1503 if (error) {
1504 cell_error_with_code(pool, cell, error);
1505 return;
1506 }
1507
1508 bio_list_init(&bios);
1509 cell_release(pool, cell, &bios);
1510
1511 while ((bio = bio_list_pop(&bios)))
1512 retry_on_resume(bio);
1513 }
1514
1515 static void process_discard_cell_no_passdown(struct thin_c *tc,
1516 struct dm_bio_prison_cell *virt_cell)
1517 {
1518 struct pool *pool = tc->pool;
1519 struct dm_thin_new_mapping *m = get_next_mapping(pool);
1520
1521 /*
1522 * We don't need to lock the data blocks, since there's no
1523 * passdown. We only lock data blocks for allocation and breaking sharing.
1524 */
1525 m->tc = tc;
1526 m->virt_begin = virt_cell->key.block_begin;
1527 m->virt_end = virt_cell->key.block_end;
1528 m->cell = virt_cell;
1529 m->bio = virt_cell->holder;
1530
1531 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
1532 pool->process_prepared_discard(m);
1533 }
1534
1535 /*
1536 * FIXME: DM local hack to defer parent bios's end_io until we
1537 * _know_ all chained sub range discard bios have completed.
1538 * Will go away once late bio splitting lands upstream!
1539 */
1540 static inline void __bio_inc_remaining(struct bio *bio)
1541 {
1542 bio->bi_flags |= (1 << BIO_CHAIN);
1543 smp_mb__before_atomic();
1544 atomic_inc(&bio->__bi_remaining);
1545 }
1546
1547 static void break_up_discard_bio(struct thin_c *tc, dm_block_t begin, dm_block_t end,
1548 struct bio *bio)
1549 {
1550 struct pool *pool = tc->pool;
1551
1552 int r;
1553 bool maybe_shared;
1554 struct dm_cell_key data_key;
1555 struct dm_bio_prison_cell *data_cell;
1556 struct dm_thin_new_mapping *m;
1557 dm_block_t virt_begin, virt_end, data_begin;
1558
1559 while (begin != end) {
1560 r = ensure_next_mapping(pool);
1561 if (r)
1562 /* we did our best */
1563 return;
1564
1565 r = dm_thin_find_mapped_range(tc->td, begin, end, &virt_begin, &virt_end,
1566 &data_begin, &maybe_shared);
1567 if (r)
1568 /*
1569 * Silently fail, letting any mappings we've
1570 * created complete.
1571 */
1572 break;
1573
1574 build_key(tc->td, PHYSICAL, data_begin, data_begin + (virt_end - virt_begin), &data_key);
1575 if (bio_detain(tc->pool, &data_key, NULL, &data_cell)) {
1576 /* contention, we'll give up with this range */
1577 begin = virt_end;
1578 continue;
1579 }
1580
1581 /*
1582 * IO may still be going to the destination block. We must
1583 * quiesce before we can do the removal.
1584 */
1585 m = get_next_mapping(pool);
1586 m->tc = tc;
1587 m->maybe_shared = maybe_shared;
1588 m->virt_begin = virt_begin;
1589 m->virt_end = virt_end;
1590 m->data_block = data_begin;
1591 m->cell = data_cell;
1592 m->bio = bio;
1593
1594 /*
1595 * The parent bio must not complete before sub discard bios are
1596 * chained to it (see __blkdev_issue_discard_async's bio_chain)!
1597 *
1598 * This per-mapping bi_remaining increment is paired with
1599 * the implicit decrement that occurs via bio_endio() in
1600 * process_prepared_discard_{passdown,no_passdown}.
1601 */
1602 __bio_inc_remaining(bio);
1603 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
1604 pool->process_prepared_discard(m);
1605
1606 begin = virt_end;
1607 }
1608 }
1609
1610 static void process_discard_cell_passdown(struct thin_c *tc, struct dm_bio_prison_cell *virt_cell)
1611 {
1612 struct bio *bio = virt_cell->holder;
1613 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1614
1615 /*
1616 * The virt_cell will only get freed once the origin bio completes.
1617 * This means it will remain locked while all the individual
1618 * passdown bios are in flight.
1619 */
1620 h->cell = virt_cell;
1621 break_up_discard_bio(tc, virt_cell->key.block_begin, virt_cell->key.block_end, bio);
1622
1623 /*
1624 * We complete the bio now, knowing that the bi_remaining field
1625 * will prevent completion until the sub range discards have
1626 * completed.
1627 */
1628 bio_endio(bio, 0);
1629 }
1630
1631 static void process_discard_bio(struct thin_c *tc, struct bio *bio)
1632 {
1633 dm_block_t begin, end;
1634 struct dm_cell_key virt_key;
1635 struct dm_bio_prison_cell *virt_cell;
1636
1637 get_bio_block_range(tc, bio, &begin, &end);
1638 if (begin == end) {
1639 /*
1640 * The discard covers less than a block.
1641 */
1642 bio_endio(bio, 0);
1643 return;
1644 }
1645
1646 build_key(tc->td, VIRTUAL, begin, end, &virt_key);
1647 if (bio_detain(tc->pool, &virt_key, bio, &virt_cell))
1648 /*
1649 * Potential starvation issue: We're relying on the
1650 * fs/application being well behaved, and not trying to
1651 * send IO to a region at the same time as discarding it.
1652 * If they do this persistently then it's possible this
1653 * cell will never be granted.
1654 */
1655 return;
1656
1657 tc->pool->process_discard_cell(tc, virt_cell);
1658 }
1659
1660 static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
1661 struct dm_cell_key *key,
1662 struct dm_thin_lookup_result *lookup_result,
1663 struct dm_bio_prison_cell *cell)
1664 {
1665 int r;
1666 dm_block_t data_block;
1667 struct pool *pool = tc->pool;
1668
1669 r = alloc_data_block(tc, &data_block);
1670 switch (r) {
1671 case 0:
1672 schedule_internal_copy(tc, block, lookup_result->block,
1673 data_block, cell, bio);
1674 break;
1675
1676 case -ENOSPC:
1677 retry_bios_on_resume(pool, cell);
1678 break;
1679
1680 default:
1681 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1682 __func__, r);
1683 cell_error(pool, cell);
1684 break;
1685 }
1686 }
1687
1688 static void __remap_and_issue_shared_cell(void *context,
1689 struct dm_bio_prison_cell *cell)
1690 {
1691 struct remap_info *info = context;
1692 struct bio *bio;
1693
1694 while ((bio = bio_list_pop(&cell->bios))) {
1695 if ((bio_data_dir(bio) == WRITE) ||
1696 (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)))
1697 bio_list_add(&info->defer_bios, bio);
1698 else {
1699 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));;
1700
1701 h->shared_read_entry = dm_deferred_entry_inc(info->tc->pool->shared_read_ds);
1702 inc_all_io_entry(info->tc->pool, bio);
1703 bio_list_add(&info->issue_bios, bio);
1704 }
1705 }
1706 }
1707
1708 static void remap_and_issue_shared_cell(struct thin_c *tc,
1709 struct dm_bio_prison_cell *cell,
1710 dm_block_t block)
1711 {
1712 struct bio *bio;
1713 struct remap_info info;
1714
1715 info.tc = tc;
1716 bio_list_init(&info.defer_bios);
1717 bio_list_init(&info.issue_bios);
1718
1719 cell_visit_release(tc->pool, __remap_and_issue_shared_cell,
1720 &info, cell);
1721
1722 while ((bio = bio_list_pop(&info.defer_bios)))
1723 thin_defer_bio(tc, bio);
1724
1725 while ((bio = bio_list_pop(&info.issue_bios)))
1726 remap_and_issue(tc, bio, block);
1727 }
1728
1729 static void process_shared_bio(struct thin_c *tc, struct bio *bio,
1730 dm_block_t block,
1731 struct dm_thin_lookup_result *lookup_result,
1732 struct dm_bio_prison_cell *virt_cell)
1733 {
1734 struct dm_bio_prison_cell *data_cell;
1735 struct pool *pool = tc->pool;
1736 struct dm_cell_key key;
1737
1738 /*
1739 * If cell is already occupied, then sharing is already in the process
1740 * of being broken so we have nothing further to do here.
1741 */
1742 build_data_key(tc->td, lookup_result->block, &key);
1743 if (bio_detain(pool, &key, bio, &data_cell)) {
1744 cell_defer_no_holder(tc, virt_cell);
1745 return;
1746 }
1747
1748 if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size) {
1749 break_sharing(tc, bio, block, &key, lookup_result, data_cell);
1750 cell_defer_no_holder(tc, virt_cell);
1751 } else {
1752 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1753
1754 h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
1755 inc_all_io_entry(pool, bio);
1756 remap_and_issue(tc, bio, lookup_result->block);
1757
1758 remap_and_issue_shared_cell(tc, data_cell, lookup_result->block);
1759 remap_and_issue_shared_cell(tc, virt_cell, lookup_result->block);
1760 }
1761 }
1762
1763 static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
1764 struct dm_bio_prison_cell *cell)
1765 {
1766 int r;
1767 dm_block_t data_block;
1768 struct pool *pool = tc->pool;
1769
1770 /*
1771 * Remap empty bios (flushes) immediately, without provisioning.
1772 */
1773 if (!bio->bi_iter.bi_size) {
1774 inc_all_io_entry(pool, bio);
1775 cell_defer_no_holder(tc, cell);
1776
1777 remap_and_issue(tc, bio, 0);
1778 return;
1779 }
1780
1781 /*
1782 * Fill read bios with zeroes and complete them immediately.
1783 */
1784 if (bio_data_dir(bio) == READ) {
1785 zero_fill_bio(bio);
1786 cell_defer_no_holder(tc, cell);
1787 bio_endio(bio, 0);
1788 return;
1789 }
1790
1791 r = alloc_data_block(tc, &data_block);
1792 switch (r) {
1793 case 0:
1794 if (tc->origin_dev)
1795 schedule_external_copy(tc, block, data_block, cell, bio);
1796 else
1797 schedule_zero(tc, block, data_block, cell, bio);
1798 break;
1799
1800 case -ENOSPC:
1801 retry_bios_on_resume(pool, cell);
1802 break;
1803
1804 default:
1805 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
1806 __func__, r);
1807 cell_error(pool, cell);
1808 break;
1809 }
1810 }
1811
1812 static void process_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1813 {
1814 int r;
1815 struct pool *pool = tc->pool;
1816 struct bio *bio = cell->holder;
1817 dm_block_t block = get_bio_block(tc, bio);
1818 struct dm_thin_lookup_result lookup_result;
1819
1820 if (tc->requeue_mode) {
1821 cell_requeue(pool, cell);
1822 return;
1823 }
1824
1825 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1826 switch (r) {
1827 case 0:
1828 if (lookup_result.shared)
1829 process_shared_bio(tc, bio, block, &lookup_result, cell);
1830 else {
1831 inc_all_io_entry(pool, bio);
1832 remap_and_issue(tc, bio, lookup_result.block);
1833 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
1834 }
1835 break;
1836
1837 case -ENODATA:
1838 if (bio_data_dir(bio) == READ && tc->origin_dev) {
1839 inc_all_io_entry(pool, bio);
1840 cell_defer_no_holder(tc, cell);
1841
1842 if (bio_end_sector(bio) <= tc->origin_size)
1843 remap_to_origin_and_issue(tc, bio);
1844
1845 else if (bio->bi_iter.bi_sector < tc->origin_size) {
1846 zero_fill_bio(bio);
1847 bio->bi_iter.bi_size = (tc->origin_size - bio->bi_iter.bi_sector) << SECTOR_SHIFT;
1848 remap_to_origin_and_issue(tc, bio);
1849
1850 } else {
1851 zero_fill_bio(bio);
1852 bio_endio(bio, 0);
1853 }
1854 } else
1855 provision_block(tc, bio, block, cell);
1856 break;
1857
1858 default:
1859 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1860 __func__, r);
1861 cell_defer_no_holder(tc, cell);
1862 bio_io_error(bio);
1863 break;
1864 }
1865 }
1866
1867 static void process_bio(struct thin_c *tc, struct bio *bio)
1868 {
1869 struct pool *pool = tc->pool;
1870 dm_block_t block = get_bio_block(tc, bio);
1871 struct dm_bio_prison_cell *cell;
1872 struct dm_cell_key key;
1873
1874 /*
1875 * If cell is already occupied, then the block is already
1876 * being provisioned so we have nothing further to do here.
1877 */
1878 build_virtual_key(tc->td, block, &key);
1879 if (bio_detain(pool, &key, bio, &cell))
1880 return;
1881
1882 process_cell(tc, cell);
1883 }
1884
1885 static void __process_bio_read_only(struct thin_c *tc, struct bio *bio,
1886 struct dm_bio_prison_cell *cell)
1887 {
1888 int r;
1889 int rw = bio_data_dir(bio);
1890 dm_block_t block = get_bio_block(tc, bio);
1891 struct dm_thin_lookup_result lookup_result;
1892
1893 r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
1894 switch (r) {
1895 case 0:
1896 if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size) {
1897 handle_unserviceable_bio(tc->pool, bio);
1898 if (cell)
1899 cell_defer_no_holder(tc, cell);
1900 } else {
1901 inc_all_io_entry(tc->pool, bio);
1902 remap_and_issue(tc, bio, lookup_result.block);
1903 if (cell)
1904 inc_remap_and_issue_cell(tc, cell, lookup_result.block);
1905 }
1906 break;
1907
1908 case -ENODATA:
1909 if (cell)
1910 cell_defer_no_holder(tc, cell);
1911 if (rw != READ) {
1912 handle_unserviceable_bio(tc->pool, bio);
1913 break;
1914 }
1915
1916 if (tc->origin_dev) {
1917 inc_all_io_entry(tc->pool, bio);
1918 remap_to_origin_and_issue(tc, bio);
1919 break;
1920 }
1921
1922 zero_fill_bio(bio);
1923 bio_endio(bio, 0);
1924 break;
1925
1926 default:
1927 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
1928 __func__, r);
1929 if (cell)
1930 cell_defer_no_holder(tc, cell);
1931 bio_io_error(bio);
1932 break;
1933 }
1934 }
1935
1936 static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
1937 {
1938 __process_bio_read_only(tc, bio, NULL);
1939 }
1940
1941 static void process_cell_read_only(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1942 {
1943 __process_bio_read_only(tc, cell->holder, cell);
1944 }
1945
1946 static void process_bio_success(struct thin_c *tc, struct bio *bio)
1947 {
1948 bio_endio(bio, 0);
1949 }
1950
1951 static void process_bio_fail(struct thin_c *tc, struct bio *bio)
1952 {
1953 bio_io_error(bio);
1954 }
1955
1956 static void process_cell_success(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1957 {
1958 cell_success(tc->pool, cell);
1959 }
1960
1961 static void process_cell_fail(struct thin_c *tc, struct dm_bio_prison_cell *cell)
1962 {
1963 cell_error(tc->pool, cell);
1964 }
1965
1966 /*
1967 * FIXME: should we also commit due to size of transaction, measured in
1968 * metadata blocks?
1969 */
1970 static int need_commit_due_to_time(struct pool *pool)
1971 {
1972 return !time_in_range(jiffies, pool->last_commit_jiffies,
1973 pool->last_commit_jiffies + COMMIT_PERIOD);
1974 }
1975
1976 #define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node)
1977 #define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook))
1978
1979 static void __thin_bio_rb_add(struct thin_c *tc, struct bio *bio)
1980 {
1981 struct rb_node **rbp, *parent;
1982 struct dm_thin_endio_hook *pbd;
1983 sector_t bi_sector = bio->bi_iter.bi_sector;
1984
1985 rbp = &tc->sort_bio_list.rb_node;
1986 parent = NULL;
1987 while (*rbp) {
1988 parent = *rbp;
1989 pbd = thin_pbd(parent);
1990
1991 if (bi_sector < thin_bio(pbd)->bi_iter.bi_sector)
1992 rbp = &(*rbp)->rb_left;
1993 else
1994 rbp = &(*rbp)->rb_right;
1995 }
1996
1997 pbd = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
1998 rb_link_node(&pbd->rb_node, parent, rbp);
1999 rb_insert_color(&pbd->rb_node, &tc->sort_bio_list);
2000 }
2001
2002 static void __extract_sorted_bios(struct thin_c *tc)
2003 {
2004 struct rb_node *node;
2005 struct dm_thin_endio_hook *pbd;
2006 struct bio *bio;
2007
2008 for (node = rb_first(&tc->sort_bio_list); node; node = rb_next(node)) {
2009 pbd = thin_pbd(node);
2010 bio = thin_bio(pbd);
2011
2012 bio_list_add(&tc->deferred_bio_list, bio);
2013 rb_erase(&pbd->rb_node, &tc->sort_bio_list);
2014 }
2015
2016 WARN_ON(!RB_EMPTY_ROOT(&tc->sort_bio_list));
2017 }
2018
2019 static void __sort_thin_deferred_bios(struct thin_c *tc)
2020 {
2021 struct bio *bio;
2022 struct bio_list bios;
2023
2024 bio_list_init(&bios);
2025 bio_list_merge(&bios, &tc->deferred_bio_list);
2026 bio_list_init(&tc->deferred_bio_list);
2027
2028 /* Sort deferred_bio_list using rb-tree */
2029 while ((bio = bio_list_pop(&bios)))
2030 __thin_bio_rb_add(tc, bio);
2031
2032 /*
2033 * Transfer the sorted bios in sort_bio_list back to
2034 * deferred_bio_list to allow lockless submission of
2035 * all bios.
2036 */
2037 __extract_sorted_bios(tc);
2038 }
2039
2040 static void process_thin_deferred_bios(struct thin_c *tc)
2041 {
2042 struct pool *pool = tc->pool;
2043 unsigned long flags;
2044 struct bio *bio;
2045 struct bio_list bios;
2046 struct blk_plug plug;
2047 unsigned count = 0;
2048
2049 if (tc->requeue_mode) {
2050 error_thin_bio_list(tc, &tc->deferred_bio_list, DM_ENDIO_REQUEUE);
2051 return;
2052 }
2053
2054 bio_list_init(&bios);
2055
2056 spin_lock_irqsave(&tc->lock, flags);
2057
2058 if (bio_list_empty(&tc->deferred_bio_list)) {
2059 spin_unlock_irqrestore(&tc->lock, flags);
2060 return;
2061 }
2062
2063 __sort_thin_deferred_bios(tc);
2064
2065 bio_list_merge(&bios, &tc->deferred_bio_list);
2066 bio_list_init(&tc->deferred_bio_list);
2067
2068 spin_unlock_irqrestore(&tc->lock, flags);
2069
2070 blk_start_plug(&plug);
2071 while ((bio = bio_list_pop(&bios))) {
2072 /*
2073 * If we've got no free new_mapping structs, and processing
2074 * this bio might require one, we pause until there are some
2075 * prepared mappings to process.
2076 */
2077 if (ensure_next_mapping(pool)) {
2078 spin_lock_irqsave(&tc->lock, flags);
2079 bio_list_add(&tc->deferred_bio_list, bio);
2080 bio_list_merge(&tc->deferred_bio_list, &bios);
2081 spin_unlock_irqrestore(&tc->lock, flags);
2082 break;
2083 }
2084
2085 if (bio->bi_rw & REQ_DISCARD)
2086 pool->process_discard(tc, bio);
2087 else
2088 pool->process_bio(tc, bio);
2089
2090 if ((count++ & 127) == 0) {
2091 throttle_work_update(&pool->throttle);
2092 dm_pool_issue_prefetches(pool->pmd);
2093 }
2094 }
2095 blk_finish_plug(&plug);
2096 }
2097
2098 static int cmp_cells(const void *lhs, const void *rhs)
2099 {
2100 struct dm_bio_prison_cell *lhs_cell = *((struct dm_bio_prison_cell **) lhs);
2101 struct dm_bio_prison_cell *rhs_cell = *((struct dm_bio_prison_cell **) rhs);
2102
2103 BUG_ON(!lhs_cell->holder);
2104 BUG_ON(!rhs_cell->holder);
2105
2106 if (lhs_cell->holder->bi_iter.bi_sector < rhs_cell->holder->bi_iter.bi_sector)
2107 return -1;
2108
2109 if (lhs_cell->holder->bi_iter.bi_sector > rhs_cell->holder->bi_iter.bi_sector)
2110 return 1;
2111
2112 return 0;
2113 }
2114
2115 static unsigned sort_cells(struct pool *pool, struct list_head *cells)
2116 {
2117 unsigned count = 0;
2118 struct dm_bio_prison_cell *cell, *tmp;
2119
2120 list_for_each_entry_safe(cell, tmp, cells, user_list) {
2121 if (count >= CELL_SORT_ARRAY_SIZE)
2122 break;
2123
2124 pool->cell_sort_array[count++] = cell;
2125 list_del(&cell->user_list);
2126 }
2127
2128 sort(pool->cell_sort_array, count, sizeof(cell), cmp_cells, NULL);
2129
2130 return count;
2131 }
2132
2133 static void process_thin_deferred_cells(struct thin_c *tc)
2134 {
2135 struct pool *pool = tc->pool;
2136 unsigned long flags;
2137 struct list_head cells;
2138 struct dm_bio_prison_cell *cell;
2139 unsigned i, j, count;
2140
2141 INIT_LIST_HEAD(&cells);
2142
2143 spin_lock_irqsave(&tc->lock, flags);
2144 list_splice_init(&tc->deferred_cells, &cells);
2145 spin_unlock_irqrestore(&tc->lock, flags);
2146
2147 if (list_empty(&cells))
2148 return;
2149
2150 do {
2151 count = sort_cells(tc->pool, &cells);
2152
2153 for (i = 0; i < count; i++) {
2154 cell = pool->cell_sort_array[i];
2155 BUG_ON(!cell->holder);
2156
2157 /*
2158 * If we've got no free new_mapping structs, and processing
2159 * this bio might require one, we pause until there are some
2160 * prepared mappings to process.
2161 */
2162 if (ensure_next_mapping(pool)) {
2163 for (j = i; j < count; j++)
2164 list_add(&pool->cell_sort_array[j]->user_list, &cells);
2165
2166 spin_lock_irqsave(&tc->lock, flags);
2167 list_splice(&cells, &tc->deferred_cells);
2168 spin_unlock_irqrestore(&tc->lock, flags);
2169 return;
2170 }
2171
2172 if (cell->holder->bi_rw & REQ_DISCARD)
2173 pool->process_discard_cell(tc, cell);
2174 else
2175 pool->process_cell(tc, cell);
2176 }
2177 } while (!list_empty(&cells));
2178 }
2179
2180 static void thin_get(struct thin_c *tc);
2181 static void thin_put(struct thin_c *tc);
2182
2183 /*
2184 * We can't hold rcu_read_lock() around code that can block. So we
2185 * find a thin with the rcu lock held; bump a refcount; then drop
2186 * the lock.
2187 */
2188 static struct thin_c *get_first_thin(struct pool *pool)
2189 {
2190 struct thin_c *tc = NULL;
2191
2192 rcu_read_lock();
2193 if (!list_empty(&pool->active_thins)) {
2194 tc = list_entry_rcu(pool->active_thins.next, struct thin_c, list);
2195 thin_get(tc);
2196 }
2197 rcu_read_unlock();
2198
2199 return tc;
2200 }
2201
2202 static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc)
2203 {
2204 struct thin_c *old_tc = tc;
2205
2206 rcu_read_lock();
2207 list_for_each_entry_continue_rcu(tc, &pool->active_thins, list) {
2208 thin_get(tc);
2209 thin_put(old_tc);
2210 rcu_read_unlock();
2211 return tc;
2212 }
2213 thin_put(old_tc);
2214 rcu_read_unlock();
2215
2216 return NULL;
2217 }
2218
2219 static void process_deferred_bios(struct pool *pool)
2220 {
2221 unsigned long flags;
2222 struct bio *bio;
2223 struct bio_list bios;
2224 struct thin_c *tc;
2225
2226 tc = get_first_thin(pool);
2227 while (tc) {
2228 process_thin_deferred_cells(tc);
2229 process_thin_deferred_bios(tc);
2230 tc = get_next_thin(pool, tc);
2231 }
2232
2233 /*
2234 * If there are any deferred flush bios, we must commit
2235 * the metadata before issuing them.
2236 */
2237 bio_list_init(&bios);
2238 spin_lock_irqsave(&pool->lock, flags);
2239 bio_list_merge(&bios, &pool->deferred_flush_bios);
2240 bio_list_init(&pool->deferred_flush_bios);
2241 spin_unlock_irqrestore(&pool->lock, flags);
2242
2243 if (bio_list_empty(&bios) &&
2244 !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool)))
2245 return;
2246
2247 if (commit(pool)) {
2248 while ((bio = bio_list_pop(&bios)))
2249 bio_io_error(bio);
2250 return;
2251 }
2252 pool->last_commit_jiffies = jiffies;
2253
2254 while ((bio = bio_list_pop(&bios)))
2255 generic_make_request(bio);
2256 }
2257
2258 static void do_worker(struct work_struct *ws)
2259 {
2260 struct pool *pool = container_of(ws, struct pool, worker);
2261
2262 throttle_work_start(&pool->throttle);
2263 dm_pool_issue_prefetches(pool->pmd);
2264 throttle_work_update(&pool->throttle);
2265 process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
2266 throttle_work_update(&pool->throttle);
2267 process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
2268 throttle_work_update(&pool->throttle);
2269 process_deferred_bios(pool);
2270 throttle_work_complete(&pool->throttle);
2271 }
2272
2273 /*
2274 * We want to commit periodically so that not too much
2275 * unwritten data builds up.
2276 */
2277 static void do_waker(struct work_struct *ws)
2278 {
2279 struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
2280 wake_worker(pool);
2281 queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
2282 }
2283
2284 /*
2285 * We're holding onto IO to allow userland time to react. After the
2286 * timeout either the pool will have been resized (and thus back in
2287 * PM_WRITE mode), or we degrade to PM_READ_ONLY and start erroring IO.
2288 */
2289 static void do_no_space_timeout(struct work_struct *ws)
2290 {
2291 struct pool *pool = container_of(to_delayed_work(ws), struct pool,
2292 no_space_timeout);
2293
2294 if (get_pool_mode(pool) == PM_OUT_OF_DATA_SPACE && !pool->pf.error_if_no_space)
2295 set_pool_mode(pool, PM_READ_ONLY);
2296 }
2297
2298 /*----------------------------------------------------------------*/
2299
2300 struct pool_work {
2301 struct work_struct worker;
2302 struct completion complete;
2303 };
2304
2305 static struct pool_work *to_pool_work(struct work_struct *ws)
2306 {
2307 return container_of(ws, struct pool_work, worker);
2308 }
2309
2310 static void pool_work_complete(struct pool_work *pw)
2311 {
2312 complete(&pw->complete);
2313 }
2314
2315 static void pool_work_wait(struct pool_work *pw, struct pool *pool,
2316 void (*fn)(struct work_struct *))
2317 {
2318 INIT_WORK_ONSTACK(&pw->worker, fn);
2319 init_completion(&pw->complete);
2320 queue_work(pool->wq, &pw->worker);
2321 wait_for_completion(&pw->complete);
2322 }
2323
2324 /*----------------------------------------------------------------*/
2325
2326 struct noflush_work {
2327 struct pool_work pw;
2328 struct thin_c *tc;
2329 };
2330
2331 static struct noflush_work *to_noflush(struct work_struct *ws)
2332 {
2333 return container_of(to_pool_work(ws), struct noflush_work, pw);
2334 }
2335
2336 static void do_noflush_start(struct work_struct *ws)
2337 {
2338 struct noflush_work *w = to_noflush(ws);
2339 w->tc->requeue_mode = true;
2340 requeue_io(w->tc);
2341 pool_work_complete(&w->pw);
2342 }
2343
2344 static void do_noflush_stop(struct work_struct *ws)
2345 {
2346 struct noflush_work *w = to_noflush(ws);
2347 w->tc->requeue_mode = false;
2348 pool_work_complete(&w->pw);
2349 }
2350
2351 static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *))
2352 {
2353 struct noflush_work w;
2354
2355 w.tc = tc;
2356 pool_work_wait(&w.pw, tc->pool, fn);
2357 }
2358
2359 /*----------------------------------------------------------------*/
2360
2361 static enum pool_mode get_pool_mode(struct pool *pool)
2362 {
2363 return pool->pf.mode;
2364 }
2365
2366 static void notify_of_pool_mode_change(struct pool *pool, const char *new_mode)
2367 {
2368 dm_table_event(pool->ti->table);
2369 DMINFO("%s: switching pool to %s mode",
2370 dm_device_name(pool->pool_md), new_mode);
2371 }
2372
2373 static bool passdown_enabled(struct pool_c *pt)
2374 {
2375 return pt->adjusted_pf.discard_passdown;
2376 }
2377
2378 static void set_discard_callbacks(struct pool *pool)
2379 {
2380 struct pool_c *pt = pool->ti->private;
2381
2382 if (passdown_enabled(pt)) {
2383 pool->process_discard_cell = process_discard_cell_passdown;
2384 pool->process_prepared_discard = process_prepared_discard_passdown;
2385 } else {
2386 pool->process_discard_cell = process_discard_cell_no_passdown;
2387 pool->process_prepared_discard = process_prepared_discard_no_passdown;
2388 }
2389 }
2390
2391 static void set_pool_mode(struct pool *pool, enum pool_mode new_mode)
2392 {
2393 struct pool_c *pt = pool->ti->private;
2394 bool needs_check = dm_pool_metadata_needs_check(pool->pmd);
2395 enum pool_mode old_mode = get_pool_mode(pool);
2396 unsigned long no_space_timeout = ACCESS_ONCE(no_space_timeout_secs) * HZ;
2397
2398 /*
2399 * Never allow the pool to transition to PM_WRITE mode if user
2400 * intervention is required to verify metadata and data consistency.
2401 */
2402 if (new_mode == PM_WRITE && needs_check) {
2403 DMERR("%s: unable to switch pool to write mode until repaired.",
2404 dm_device_name(pool->pool_md));
2405 if (old_mode != new_mode)
2406 new_mode = old_mode;
2407 else
2408 new_mode = PM_READ_ONLY;
2409 }
2410 /*
2411 * If we were in PM_FAIL mode, rollback of metadata failed. We're
2412 * not going to recover without a thin_repair. So we never let the
2413 * pool move out of the old mode.
2414 */
2415 if (old_mode == PM_FAIL)
2416 new_mode = old_mode;
2417
2418 switch (new_mode) {
2419 case PM_FAIL:
2420 if (old_mode != new_mode)
2421 notify_of_pool_mode_change(pool, "failure");
2422 dm_pool_metadata_read_only(pool->pmd);
2423 pool->process_bio = process_bio_fail;
2424 pool->process_discard = process_bio_fail;
2425 pool->process_cell = process_cell_fail;
2426 pool->process_discard_cell = process_cell_fail;
2427 pool->process_prepared_mapping = process_prepared_mapping_fail;
2428 pool->process_prepared_discard = process_prepared_discard_fail;
2429
2430 error_retry_list(pool);
2431 break;
2432
2433 case PM_READ_ONLY:
2434 if (old_mode != new_mode)
2435 notify_of_pool_mode_change(pool, "read-only");
2436 dm_pool_metadata_read_only(pool->pmd);
2437 pool->process_bio = process_bio_read_only;
2438 pool->process_discard = process_bio_success;
2439 pool->process_cell = process_cell_read_only;
2440 pool->process_discard_cell = process_cell_success;
2441 pool->process_prepared_mapping = process_prepared_mapping_fail;
2442 pool->process_prepared_discard = process_prepared_discard_success;
2443
2444 error_retry_list(pool);
2445 break;
2446
2447 case PM_OUT_OF_DATA_SPACE:
2448 /*
2449 * Ideally we'd never hit this state; the low water mark
2450 * would trigger userland to extend the pool before we
2451 * completely run out of data space. However, many small
2452 * IOs to unprovisioned space can consume data space at an
2453 * alarming rate. Adjust your low water mark if you're
2454 * frequently seeing this mode.
2455 */
2456 if (old_mode != new_mode)
2457 notify_of_pool_mode_change(pool, "out-of-data-space");
2458 pool->process_bio = process_bio_read_only;
2459 pool->process_discard = process_discard_bio;
2460 pool->process_cell = process_cell_read_only;
2461 pool->process_prepared_mapping = process_prepared_mapping;
2462 set_discard_callbacks(pool);
2463
2464 if (!pool->pf.error_if_no_space && no_space_timeout)
2465 queue_delayed_work(pool->wq, &pool->no_space_timeout, no_space_timeout);
2466 break;
2467
2468 case PM_WRITE:
2469 if (old_mode != new_mode)
2470 notify_of_pool_mode_change(pool, "write");
2471 dm_pool_metadata_read_write(pool->pmd);
2472 pool->process_bio = process_bio;
2473 pool->process_discard = process_discard_bio;
2474 pool->process_cell = process_cell;
2475 pool->process_prepared_mapping = process_prepared_mapping;
2476 set_discard_callbacks(pool);
2477 break;
2478 }
2479
2480 pool->pf.mode = new_mode;
2481 /*
2482 * The pool mode may have changed, sync it so bind_control_target()
2483 * doesn't cause an unexpected mode transition on resume.
2484 */
2485 pt->adjusted_pf.mode = new_mode;
2486 }
2487
2488 static void abort_transaction(struct pool *pool)
2489 {
2490 const char *dev_name = dm_device_name(pool->pool_md);
2491
2492 DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
2493 if (dm_pool_abort_metadata(pool->pmd)) {
2494 DMERR("%s: failed to abort metadata transaction", dev_name);
2495 set_pool_mode(pool, PM_FAIL);
2496 }
2497
2498 if (dm_pool_metadata_set_needs_check(pool->pmd)) {
2499 DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
2500 set_pool_mode(pool, PM_FAIL);
2501 }
2502 }
2503
2504 static void metadata_operation_failed(struct pool *pool, const char *op, int r)
2505 {
2506 DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
2507 dm_device_name(pool->pool_md), op, r);
2508
2509 abort_transaction(pool);
2510 set_pool_mode(pool, PM_READ_ONLY);
2511 }
2512
2513 /*----------------------------------------------------------------*/
2514
2515 /*
2516 * Mapping functions.
2517 */
2518
2519 /*
2520 * Called only while mapping a thin bio to hand it over to the workqueue.
2521 */
2522 static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
2523 {
2524 unsigned long flags;
2525 struct pool *pool = tc->pool;
2526
2527 spin_lock_irqsave(&tc->lock, flags);
2528 bio_list_add(&tc->deferred_bio_list, bio);
2529 spin_unlock_irqrestore(&tc->lock, flags);
2530
2531 wake_worker(pool);
2532 }
2533
2534 static void thin_defer_bio_with_throttle(struct thin_c *tc, struct bio *bio)
2535 {
2536 struct pool *pool = tc->pool;
2537
2538 throttle_lock(&pool->throttle);
2539 thin_defer_bio(tc, bio);
2540 throttle_unlock(&pool->throttle);
2541 }
2542
2543 static void thin_defer_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
2544 {
2545 unsigned long flags;
2546 struct pool *pool = tc->pool;
2547
2548 throttle_lock(&pool->throttle);
2549 spin_lock_irqsave(&tc->lock, flags);
2550 list_add_tail(&cell->user_list, &tc->deferred_cells);
2551 spin_unlock_irqrestore(&tc->lock, flags);
2552 throttle_unlock(&pool->throttle);
2553
2554 wake_worker(pool);
2555 }
2556
2557 static void thin_hook_bio(struct thin_c *tc, struct bio *bio)
2558 {
2559 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
2560
2561 h->tc = tc;
2562 h->shared_read_entry = NULL;
2563 h->all_io_entry = NULL;
2564 h->overwrite_mapping = NULL;
2565 h->cell = NULL;
2566 }
2567
2568 /*
2569 * Non-blocking function called from the thin target's map function.
2570 */
2571 static int thin_bio_map(struct dm_target *ti, struct bio *bio)
2572 {
2573 int r;
2574 struct thin_c *tc = ti->private;
2575 dm_block_t block = get_bio_block(tc, bio);
2576 struct dm_thin_device *td = tc->td;
2577 struct dm_thin_lookup_result result;
2578 struct dm_bio_prison_cell *virt_cell, *data_cell;
2579 struct dm_cell_key key;
2580
2581 thin_hook_bio(tc, bio);
2582
2583 if (tc->requeue_mode) {
2584 bio_endio(bio, DM_ENDIO_REQUEUE);
2585 return DM_MAPIO_SUBMITTED;
2586 }
2587
2588 if (get_pool_mode(tc->pool) == PM_FAIL) {
2589 bio_io_error(bio);
2590 return DM_MAPIO_SUBMITTED;
2591 }
2592
2593 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) {
2594 thin_defer_bio_with_throttle(tc, bio);
2595 return DM_MAPIO_SUBMITTED;
2596 }
2597
2598 /*
2599 * We must hold the virtual cell before doing the lookup, otherwise
2600 * there's a race with discard.
2601 */
2602 build_virtual_key(tc->td, block, &key);
2603 if (bio_detain(tc->pool, &key, bio, &virt_cell))
2604 return DM_MAPIO_SUBMITTED;
2605
2606 r = dm_thin_find_block(td, block, 0, &result);
2607
2608 /*
2609 * Note that we defer readahead too.
2610 */
2611 switch (r) {
2612 case 0:
2613 if (unlikely(result.shared)) {
2614 /*
2615 * We have a race condition here between the
2616 * result.shared value returned by the lookup and
2617 * snapshot creation, which may cause new
2618 * sharing.
2619 *
2620 * To avoid this always quiesce the origin before
2621 * taking the snap. You want to do this anyway to
2622 * ensure a consistent application view
2623 * (i.e. lockfs).
2624 *
2625 * More distant ancestors are irrelevant. The
2626 * shared flag will be set in their case.
2627 */
2628 thin_defer_cell(tc, virt_cell);
2629 return DM_MAPIO_SUBMITTED;
2630 }
2631
2632 build_data_key(tc->td, result.block, &key);
2633 if (bio_detain(tc->pool, &key, bio, &data_cell)) {
2634 cell_defer_no_holder(tc, virt_cell);
2635 return DM_MAPIO_SUBMITTED;
2636 }
2637
2638 inc_all_io_entry(tc->pool, bio);
2639 cell_defer_no_holder(tc, data_cell);
2640 cell_defer_no_holder(tc, virt_cell);
2641
2642 remap(tc, bio, result.block);
2643 return DM_MAPIO_REMAPPED;
2644
2645 case -ENODATA:
2646 case -EWOULDBLOCK:
2647 thin_defer_cell(tc, virt_cell);
2648 return DM_MAPIO_SUBMITTED;
2649
2650 default:
2651 /*
2652 * Must always call bio_io_error on failure.
2653 * dm_thin_find_block can fail with -EINVAL if the
2654 * pool is switched to fail-io mode.
2655 */
2656 bio_io_error(bio);
2657 cell_defer_no_holder(tc, virt_cell);
2658 return DM_MAPIO_SUBMITTED;
2659 }
2660 }
2661
2662 static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
2663 {
2664 struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
2665 struct request_queue *q;
2666
2667 if (get_pool_mode(pt->pool) == PM_OUT_OF_DATA_SPACE)
2668 return 1;
2669
2670 q = bdev_get_queue(pt->data_dev->bdev);
2671 return bdi_congested(&q->backing_dev_info, bdi_bits);
2672 }
2673
2674 static void requeue_bios(struct pool *pool)
2675 {
2676 unsigned long flags;
2677 struct thin_c *tc;
2678
2679 rcu_read_lock();
2680 list_for_each_entry_rcu(tc, &pool->active_thins, list) {
2681 spin_lock_irqsave(&tc->lock, flags);
2682 bio_list_merge(&tc->deferred_bio_list, &tc->retry_on_resume_list);
2683 bio_list_init(&tc->retry_on_resume_list);
2684 spin_unlock_irqrestore(&tc->lock, flags);
2685 }
2686 rcu_read_unlock();
2687 }
2688
2689 /*----------------------------------------------------------------
2690 * Binding of control targets to a pool object
2691 *--------------------------------------------------------------*/
2692 static bool data_dev_supports_discard(struct pool_c *pt)
2693 {
2694 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
2695
2696 return q && blk_queue_discard(q);
2697 }
2698
2699 static bool is_factor(sector_t block_size, uint32_t n)
2700 {
2701 return !sector_div(block_size, n);
2702 }
2703
2704 /*
2705 * If discard_passdown was enabled verify that the data device
2706 * supports discards. Disable discard_passdown if not.
2707 */
2708 static void disable_passdown_if_not_supported(struct pool_c *pt)
2709 {
2710 struct pool *pool = pt->pool;
2711 struct block_device *data_bdev = pt->data_dev->bdev;
2712 struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
2713 const char *reason = NULL;
2714 char buf[BDEVNAME_SIZE];
2715
2716 if (!pt->adjusted_pf.discard_passdown)
2717 return;
2718
2719 if (!data_dev_supports_discard(pt))
2720 reason = "discard unsupported";
2721
2722 else if (data_limits->max_discard_sectors < pool->sectors_per_block)
2723 reason = "max discard sectors smaller than a block";
2724
2725 if (reason) {
2726 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
2727 pt->adjusted_pf.discard_passdown = false;
2728 }
2729 }
2730
2731 static int bind_control_target(struct pool *pool, struct dm_target *ti)
2732 {
2733 struct pool_c *pt = ti->private;
2734
2735 /*
2736 * We want to make sure that a pool in PM_FAIL mode is never upgraded.
2737 */
2738 enum pool_mode old_mode = get_pool_mode(pool);
2739 enum pool_mode new_mode = pt->adjusted_pf.mode;
2740
2741 /*
2742 * Don't change the pool's mode until set_pool_mode() below.
2743 * Otherwise the pool's process_* function pointers may
2744 * not match the desired pool mode.
2745 */
2746 pt->adjusted_pf.mode = old_mode;
2747
2748 pool->ti = ti;
2749 pool->pf = pt->adjusted_pf;
2750 pool->low_water_blocks = pt->low_water_blocks;
2751
2752 set_pool_mode(pool, new_mode);
2753
2754 return 0;
2755 }
2756
2757 static void unbind_control_target(struct pool *pool, struct dm_target *ti)
2758 {
2759 if (pool->ti == ti)
2760 pool->ti = NULL;
2761 }
2762
2763 /*----------------------------------------------------------------
2764 * Pool creation
2765 *--------------------------------------------------------------*/
2766 /* Initialize pool features. */
2767 static void pool_features_init(struct pool_features *pf)
2768 {
2769 pf->mode = PM_WRITE;
2770 pf->zero_new_blocks = true;
2771 pf->discard_enabled = true;
2772 pf->discard_passdown = true;
2773 pf->error_if_no_space = false;
2774 }
2775
2776 static void __pool_destroy(struct pool *pool)
2777 {
2778 __pool_table_remove(pool);
2779
2780 if (dm_pool_metadata_close(pool->pmd) < 0)
2781 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2782
2783 dm_bio_prison_destroy(pool->prison);
2784 dm_kcopyd_client_destroy(pool->copier);
2785
2786 if (pool->wq)
2787 destroy_workqueue(pool->wq);
2788
2789 if (pool->next_mapping)
2790 mempool_free(pool->next_mapping, pool->mapping_pool);
2791 mempool_destroy(pool->mapping_pool);
2792 dm_deferred_set_destroy(pool->shared_read_ds);
2793 dm_deferred_set_destroy(pool->all_io_ds);
2794 kfree(pool);
2795 }
2796
2797 static struct kmem_cache *_new_mapping_cache;
2798
2799 static struct pool *pool_create(struct mapped_device *pool_md,
2800 struct block_device *metadata_dev,
2801 unsigned long block_size,
2802 int read_only, char **error)
2803 {
2804 int r;
2805 void *err_p;
2806 struct pool *pool;
2807 struct dm_pool_metadata *pmd;
2808 bool format_device = read_only ? false : true;
2809
2810 pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
2811 if (IS_ERR(pmd)) {
2812 *error = "Error creating metadata object";
2813 return (struct pool *)pmd;
2814 }
2815
2816 pool = kmalloc(sizeof(*pool), GFP_KERNEL);
2817 if (!pool) {
2818 *error = "Error allocating memory for pool";
2819 err_p = ERR_PTR(-ENOMEM);
2820 goto bad_pool;
2821 }
2822
2823 pool->pmd = pmd;
2824 pool->sectors_per_block = block_size;
2825 if (block_size & (block_size - 1))
2826 pool->sectors_per_block_shift = -1;
2827 else
2828 pool->sectors_per_block_shift = __ffs(block_size);
2829 pool->low_water_blocks = 0;
2830 pool_features_init(&pool->pf);
2831 pool->prison = dm_bio_prison_create();
2832 if (!pool->prison) {
2833 *error = "Error creating pool's bio prison";
2834 err_p = ERR_PTR(-ENOMEM);
2835 goto bad_prison;
2836 }
2837
2838 pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2839 if (IS_ERR(pool->copier)) {
2840 r = PTR_ERR(pool->copier);
2841 *error = "Error creating pool's kcopyd client";
2842 err_p = ERR_PTR(r);
2843 goto bad_kcopyd_client;
2844 }
2845
2846 /*
2847 * Create singlethreaded workqueue that will service all devices
2848 * that use this metadata.
2849 */
2850 pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2851 if (!pool->wq) {
2852 *error = "Error creating pool's workqueue";
2853 err_p = ERR_PTR(-ENOMEM);
2854 goto bad_wq;
2855 }
2856
2857 throttle_init(&pool->throttle);
2858 INIT_WORK(&pool->worker, do_worker);
2859 INIT_DELAYED_WORK(&pool->waker, do_waker);
2860 INIT_DELAYED_WORK(&pool->no_space_timeout, do_no_space_timeout);
2861 spin_lock_init(&pool->lock);
2862 bio_list_init(&pool->deferred_flush_bios);
2863 INIT_LIST_HEAD(&pool->prepared_mappings);
2864 INIT_LIST_HEAD(&pool->prepared_discards);
2865 INIT_LIST_HEAD(&pool->active_thins);
2866 pool->low_water_triggered = false;
2867 pool->suspended = true;
2868
2869 pool->shared_read_ds = dm_deferred_set_create();
2870 if (!pool->shared_read_ds) {
2871 *error = "Error creating pool's shared read deferred set";
2872 err_p = ERR_PTR(-ENOMEM);
2873 goto bad_shared_read_ds;
2874 }
2875
2876 pool->all_io_ds = dm_deferred_set_create();
2877 if (!pool->all_io_ds) {
2878 *error = "Error creating pool's all io deferred set";
2879 err_p = ERR_PTR(-ENOMEM);
2880 goto bad_all_io_ds;
2881 }
2882
2883 pool->next_mapping = NULL;
2884 pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
2885 _new_mapping_cache);
2886 if (!pool->mapping_pool) {
2887 *error = "Error creating pool's mapping mempool";
2888 err_p = ERR_PTR(-ENOMEM);
2889 goto bad_mapping_pool;
2890 }
2891
2892 pool->ref_count = 1;
2893 pool->last_commit_jiffies = jiffies;
2894 pool->pool_md = pool_md;
2895 pool->md_dev = metadata_dev;
2896 __pool_table_insert(pool);
2897
2898 return pool;
2899
2900 bad_mapping_pool:
2901 dm_deferred_set_destroy(pool->all_io_ds);
2902 bad_all_io_ds:
2903 dm_deferred_set_destroy(pool->shared_read_ds);
2904 bad_shared_read_ds:
2905 destroy_workqueue(pool->wq);
2906 bad_wq:
2907 dm_kcopyd_client_destroy(pool->copier);
2908 bad_kcopyd_client:
2909 dm_bio_prison_destroy(pool->prison);
2910 bad_prison:
2911 kfree(pool);
2912 bad_pool:
2913 if (dm_pool_metadata_close(pmd))
2914 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
2915
2916 return err_p;
2917 }
2918
2919 static void __pool_inc(struct pool *pool)
2920 {
2921 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2922 pool->ref_count++;
2923 }
2924
2925 static void __pool_dec(struct pool *pool)
2926 {
2927 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
2928 BUG_ON(!pool->ref_count);
2929 if (!--pool->ref_count)
2930 __pool_destroy(pool);
2931 }
2932
2933 static struct pool *__pool_find(struct mapped_device *pool_md,
2934 struct block_device *metadata_dev,
2935 unsigned long block_size, int read_only,
2936 char **error, int *created)
2937 {
2938 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
2939
2940 if (pool) {
2941 if (pool->pool_md != pool_md) {
2942 *error = "metadata device already in use by a pool";
2943 return ERR_PTR(-EBUSY);
2944 }
2945 __pool_inc(pool);
2946
2947 } else {
2948 pool = __pool_table_lookup(pool_md);
2949 if (pool) {
2950 if (pool->md_dev != metadata_dev) {
2951 *error = "different pool cannot replace a pool";
2952 return ERR_PTR(-EINVAL);
2953 }
2954 __pool_inc(pool);
2955
2956 } else {
2957 pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
2958 *created = 1;
2959 }
2960 }
2961
2962 return pool;
2963 }
2964
2965 /*----------------------------------------------------------------
2966 * Pool target methods
2967 *--------------------------------------------------------------*/
2968 static void pool_dtr(struct dm_target *ti)
2969 {
2970 struct pool_c *pt = ti->private;
2971
2972 mutex_lock(&dm_thin_pool_table.mutex);
2973
2974 unbind_control_target(pt->pool, ti);
2975 __pool_dec(pt->pool);
2976 dm_put_device(ti, pt->metadata_dev);
2977 dm_put_device(ti, pt->data_dev);
2978 kfree(pt);
2979
2980 mutex_unlock(&dm_thin_pool_table.mutex);
2981 }
2982
2983 static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
2984 struct dm_target *ti)
2985 {
2986 int r;
2987 unsigned argc;
2988 const char *arg_name;
2989
2990 static struct dm_arg _args[] = {
2991 {0, 4, "Invalid number of pool feature arguments"},
2992 };
2993
2994 /*
2995 * No feature arguments supplied.
2996 */
2997 if (!as->argc)
2998 return 0;
2999
3000 r = dm_read_arg_group(_args, as, &argc, &ti->error);
3001 if (r)
3002 return -EINVAL;
3003
3004 while (argc && !r) {
3005 arg_name = dm_shift_arg(as);
3006 argc--;
3007
3008 if (!strcasecmp(arg_name, "skip_block_zeroing"))
3009 pf->zero_new_blocks = false;
3010
3011 else if (!strcasecmp(arg_name, "ignore_discard"))
3012 pf->discard_enabled = false;
3013
3014 else if (!strcasecmp(arg_name, "no_discard_passdown"))
3015 pf->discard_passdown = false;
3016
3017 else if (!strcasecmp(arg_name, "read_only"))
3018 pf->mode = PM_READ_ONLY;
3019
3020 else if (!strcasecmp(arg_name, "error_if_no_space"))
3021 pf->error_if_no_space = true;
3022
3023 else {
3024 ti->error = "Unrecognised pool feature requested";
3025 r = -EINVAL;
3026 break;
3027 }
3028 }
3029
3030 return r;
3031 }
3032
3033 static void metadata_low_callback(void *context)
3034 {
3035 struct pool *pool = context;
3036
3037 DMWARN("%s: reached low water mark for metadata device: sending event.",
3038 dm_device_name(pool->pool_md));
3039
3040 dm_table_event(pool->ti->table);
3041 }
3042
3043 static sector_t get_dev_size(struct block_device *bdev)
3044 {
3045 return i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
3046 }
3047
3048 static void warn_if_metadata_device_too_big(struct block_device *bdev)
3049 {
3050 sector_t metadata_dev_size = get_dev_size(bdev);
3051 char buffer[BDEVNAME_SIZE];
3052
3053 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
3054 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
3055 bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS);
3056 }
3057
3058 static sector_t get_metadata_dev_size(struct block_device *bdev)
3059 {
3060 sector_t metadata_dev_size = get_dev_size(bdev);
3061
3062 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS)
3063 metadata_dev_size = THIN_METADATA_MAX_SECTORS;
3064
3065 return metadata_dev_size;
3066 }
3067
3068 static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev)
3069 {
3070 sector_t metadata_dev_size = get_metadata_dev_size(bdev);
3071
3072 sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE);
3073
3074 return metadata_dev_size;
3075 }
3076
3077 /*
3078 * When a metadata threshold is crossed a dm event is triggered, and
3079 * userland should respond by growing the metadata device. We could let
3080 * userland set the threshold, like we do with the data threshold, but I'm
3081 * not sure they know enough to do this well.
3082 */
3083 static dm_block_t calc_metadata_threshold(struct pool_c *pt)
3084 {
3085 /*
3086 * 4M is ample for all ops with the possible exception of thin
3087 * device deletion which is harmless if it fails (just retry the
3088 * delete after you've grown the device).
3089 */
3090 dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4;
3091 return min((dm_block_t)1024ULL /* 4M */, quarter);
3092 }
3093
3094 /*
3095 * thin-pool <metadata dev> <data dev>
3096 * <data block size (sectors)>
3097 * <low water mark (blocks)>
3098 * [<#feature args> [<arg>]*]
3099 *
3100 * Optional feature arguments are:
3101 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
3102 * ignore_discard: disable discard
3103 * no_discard_passdown: don't pass discards down to the data device
3104 * read_only: Don't allow any changes to be made to the pool metadata.
3105 * error_if_no_space: error IOs, instead of queueing, if no space.
3106 */
3107 static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
3108 {
3109 int r, pool_created = 0;
3110 struct pool_c *pt;
3111 struct pool *pool;
3112 struct pool_features pf;
3113 struct dm_arg_set as;
3114 struct dm_dev *data_dev;
3115 unsigned long block_size;
3116 dm_block_t low_water_blocks;
3117 struct dm_dev *metadata_dev;
3118 fmode_t metadata_mode;
3119
3120 /*
3121 * FIXME Remove validation from scope of lock.
3122 */
3123 mutex_lock(&dm_thin_pool_table.mutex);
3124
3125 if (argc < 4) {
3126 ti->error = "Invalid argument count";
3127 r = -EINVAL;
3128 goto out_unlock;
3129 }
3130
3131 as.argc = argc;
3132 as.argv = argv;
3133
3134 /*
3135 * Set default pool features.
3136 */
3137 pool_features_init(&pf);
3138
3139 dm_consume_args(&as, 4);
3140 r = parse_pool_features(&as, &pf, ti);
3141 if (r)
3142 goto out_unlock;
3143
3144 metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE);
3145 r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
3146 if (r) {
3147 ti->error = "Error opening metadata block device";
3148 goto out_unlock;
3149 }
3150 warn_if_metadata_device_too_big(metadata_dev->bdev);
3151
3152 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
3153 if (r) {
3154 ti->error = "Error getting data device";
3155 goto out_metadata;
3156 }
3157
3158 if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
3159 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
3160 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
3161 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
3162 ti->error = "Invalid block size";
3163 r = -EINVAL;
3164 goto out;
3165 }
3166
3167 if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
3168 ti->error = "Invalid low water mark";
3169 r = -EINVAL;
3170 goto out;
3171 }
3172
3173 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
3174 if (!pt) {
3175 r = -ENOMEM;
3176 goto out;
3177 }
3178
3179 pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
3180 block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
3181 if (IS_ERR(pool)) {
3182 r = PTR_ERR(pool);
3183 goto out_free_pt;
3184 }
3185
3186 /*
3187 * 'pool_created' reflects whether this is the first table load.
3188 * Top level discard support is not allowed to be changed after
3189 * initial load. This would require a pool reload to trigger thin
3190 * device changes.
3191 */
3192 if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
3193 ti->error = "Discard support cannot be disabled once enabled";
3194 r = -EINVAL;
3195 goto out_flags_changed;
3196 }
3197
3198 pt->pool = pool;
3199 pt->ti = ti;
3200 pt->metadata_dev = metadata_dev;
3201 pt->data_dev = data_dev;
3202 pt->low_water_blocks = low_water_blocks;
3203 pt->adjusted_pf = pt->requested_pf = pf;
3204 ti->num_flush_bios = 1;
3205
3206 /*
3207 * Only need to enable discards if the pool should pass
3208 * them down to the data device. The thin device's discard
3209 * processing will cause mappings to be removed from the btree.
3210 */
3211 ti->discard_zeroes_data_unsupported = true;
3212 if (pf.discard_enabled && pf.discard_passdown) {
3213 ti->num_discard_bios = 1;
3214
3215 /*
3216 * Setting 'discards_supported' circumvents the normal
3217 * stacking of discard limits (this keeps the pool and
3218 * thin devices' discard limits consistent).
3219 */
3220 ti->discards_supported = true;
3221 }
3222 ti->private = pt;
3223
3224 r = dm_pool_register_metadata_threshold(pt->pool->pmd,
3225 calc_metadata_threshold(pt),
3226 metadata_low_callback,
3227 pool);
3228 if (r)
3229 goto out_free_pt;
3230
3231 pt->callbacks.congested_fn = pool_is_congested;
3232 dm_table_add_target_callbacks(ti->table, &pt->callbacks);
3233
3234 mutex_unlock(&dm_thin_pool_table.mutex);
3235
3236 return 0;
3237
3238 out_flags_changed:
3239 __pool_dec(pool);
3240 out_free_pt:
3241 kfree(pt);
3242 out:
3243 dm_put_device(ti, data_dev);
3244 out_metadata:
3245 dm_put_device(ti, metadata_dev);
3246 out_unlock:
3247 mutex_unlock(&dm_thin_pool_table.mutex);
3248
3249 return r;
3250 }
3251
3252 static int pool_map(struct dm_target *ti, struct bio *bio)
3253 {
3254 int r;
3255 struct pool_c *pt = ti->private;
3256 struct pool *pool = pt->pool;
3257 unsigned long flags;
3258
3259 /*
3260 * As this is a singleton target, ti->begin is always zero.
3261 */
3262 spin_lock_irqsave(&pool->lock, flags);
3263 bio->bi_bdev = pt->data_dev->bdev;
3264 r = DM_MAPIO_REMAPPED;
3265 spin_unlock_irqrestore(&pool->lock, flags);
3266
3267 return r;
3268 }
3269
3270 static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit)
3271 {
3272 int r;
3273 struct pool_c *pt = ti->private;
3274 struct pool *pool = pt->pool;
3275 sector_t data_size = ti->len;
3276 dm_block_t sb_data_size;
3277
3278 *need_commit = false;
3279
3280 (void) sector_div(data_size, pool->sectors_per_block);
3281
3282 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
3283 if (r) {
3284 DMERR("%s: failed to retrieve data device size",
3285 dm_device_name(pool->pool_md));
3286 return r;
3287 }
3288
3289 if (data_size < sb_data_size) {
3290 DMERR("%s: pool target (%llu blocks) too small: expected %llu",
3291 dm_device_name(pool->pool_md),
3292 (unsigned long long)data_size, sb_data_size);
3293 return -EINVAL;
3294
3295 } else if (data_size > sb_data_size) {
3296 if (dm_pool_metadata_needs_check(pool->pmd)) {
3297 DMERR("%s: unable to grow the data device until repaired.",
3298 dm_device_name(pool->pool_md));
3299 return 0;
3300 }
3301
3302 if (sb_data_size)
3303 DMINFO("%s: growing the data device from %llu to %llu blocks",
3304 dm_device_name(pool->pool_md),
3305 sb_data_size, (unsigned long long)data_size);
3306 r = dm_pool_resize_data_dev(pool->pmd, data_size);
3307 if (r) {
3308 metadata_operation_failed(pool, "dm_pool_resize_data_dev", r);
3309 return r;
3310 }
3311
3312 *need_commit = true;
3313 }
3314
3315 return 0;
3316 }
3317
3318 static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit)
3319 {
3320 int r;
3321 struct pool_c *pt = ti->private;
3322 struct pool *pool = pt->pool;
3323 dm_block_t metadata_dev_size, sb_metadata_dev_size;
3324
3325 *need_commit = false;
3326
3327 metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev);
3328
3329 r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size);
3330 if (r) {
3331 DMERR("%s: failed to retrieve metadata device size",
3332 dm_device_name(pool->pool_md));
3333 return r;
3334 }
3335
3336 if (metadata_dev_size < sb_metadata_dev_size) {
3337 DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
3338 dm_device_name(pool->pool_md),
3339 metadata_dev_size, sb_metadata_dev_size);
3340 return -EINVAL;
3341
3342 } else if (metadata_dev_size > sb_metadata_dev_size) {
3343 if (dm_pool_metadata_needs_check(pool->pmd)) {
3344 DMERR("%s: unable to grow the metadata device until repaired.",
3345 dm_device_name(pool->pool_md));
3346 return 0;
3347 }
3348
3349 warn_if_metadata_device_too_big(pool->md_dev);
3350 DMINFO("%s: growing the metadata device from %llu to %llu blocks",
3351 dm_device_name(pool->pool_md),
3352 sb_metadata_dev_size, metadata_dev_size);
3353 r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size);
3354 if (r) {
3355 metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r);
3356 return r;
3357 }
3358
3359 *need_commit = true;
3360 }
3361
3362 return 0;
3363 }
3364
3365 /*
3366 * Retrieves the number of blocks of the data device from
3367 * the superblock and compares it to the actual device size,
3368 * thus resizing the data device in case it has grown.
3369 *
3370 * This both copes with opening preallocated data devices in the ctr
3371 * being followed by a resume
3372 * -and-
3373 * calling the resume method individually after userspace has
3374 * grown the data device in reaction to a table event.
3375 */
3376 static int pool_preresume(struct dm_target *ti)
3377 {
3378 int r;
3379 bool need_commit1, need_commit2;
3380 struct pool_c *pt = ti->private;
3381 struct pool *pool = pt->pool;
3382
3383 /*
3384 * Take control of the pool object.
3385 */
3386 r = bind_control_target(pool, ti);
3387 if (r)
3388 return r;
3389
3390 r = maybe_resize_data_dev(ti, &need_commit1);
3391 if (r)
3392 return r;
3393
3394 r = maybe_resize_metadata_dev(ti, &need_commit2);
3395 if (r)
3396 return r;
3397
3398 if (need_commit1 || need_commit2)
3399 (void) commit(pool);
3400
3401 return 0;
3402 }
3403
3404 static void pool_suspend_active_thins(struct pool *pool)
3405 {
3406 struct thin_c *tc;
3407
3408 /* Suspend all active thin devices */
3409 tc = get_first_thin(pool);
3410 while (tc) {
3411 dm_internal_suspend_noflush(tc->thin_md);
3412 tc = get_next_thin(pool, tc);
3413 }
3414 }
3415
3416 static void pool_resume_active_thins(struct pool *pool)
3417 {
3418 struct thin_c *tc;
3419
3420 /* Resume all active thin devices */
3421 tc = get_first_thin(pool);
3422 while (tc) {
3423 dm_internal_resume(tc->thin_md);
3424 tc = get_next_thin(pool, tc);
3425 }
3426 }
3427
3428 static void pool_resume(struct dm_target *ti)
3429 {
3430 struct pool_c *pt = ti->private;
3431 struct pool *pool = pt->pool;
3432 unsigned long flags;
3433
3434 /*
3435 * Must requeue active_thins' bios and then resume
3436 * active_thins _before_ clearing 'suspend' flag.
3437 */
3438 requeue_bios(pool);
3439 pool_resume_active_thins(pool);
3440
3441 spin_lock_irqsave(&pool->lock, flags);
3442 pool->low_water_triggered = false;
3443 pool->suspended = false;
3444 spin_unlock_irqrestore(&pool->lock, flags);
3445
3446 do_waker(&pool->waker.work);
3447 }
3448
3449 static void pool_presuspend(struct dm_target *ti)
3450 {
3451 struct pool_c *pt = ti->private;
3452 struct pool *pool = pt->pool;
3453 unsigned long flags;
3454
3455 spin_lock_irqsave(&pool->lock, flags);
3456 pool->suspended = true;
3457 spin_unlock_irqrestore(&pool->lock, flags);
3458
3459 pool_suspend_active_thins(pool);
3460 }
3461
3462 static void pool_presuspend_undo(struct dm_target *ti)
3463 {
3464 struct pool_c *pt = ti->private;
3465 struct pool *pool = pt->pool;
3466 unsigned long flags;
3467
3468 pool_resume_active_thins(pool);
3469
3470 spin_lock_irqsave(&pool->lock, flags);
3471 pool->suspended = false;
3472 spin_unlock_irqrestore(&pool->lock, flags);
3473 }
3474
3475 static void pool_postsuspend(struct dm_target *ti)
3476 {
3477 struct pool_c *pt = ti->private;
3478 struct pool *pool = pt->pool;
3479
3480 cancel_delayed_work(&pool->waker);
3481 cancel_delayed_work(&pool->no_space_timeout);
3482 flush_workqueue(pool->wq);
3483 (void) commit(pool);
3484 }
3485
3486 static int check_arg_count(unsigned argc, unsigned args_required)
3487 {
3488 if (argc != args_required) {
3489 DMWARN("Message received with %u arguments instead of %u.",
3490 argc, args_required);
3491 return -EINVAL;
3492 }
3493
3494 return 0;
3495 }
3496
3497 static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
3498 {
3499 if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
3500 *dev_id <= MAX_DEV_ID)
3501 return 0;
3502
3503 if (warning)
3504 DMWARN("Message received with invalid device id: %s", arg);
3505
3506 return -EINVAL;
3507 }
3508
3509 static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
3510 {
3511 dm_thin_id dev_id;
3512 int r;
3513
3514 r = check_arg_count(argc, 2);
3515 if (r)
3516 return r;
3517
3518 r = read_dev_id(argv[1], &dev_id, 1);
3519 if (r)
3520 return r;
3521
3522 r = dm_pool_create_thin(pool->pmd, dev_id);
3523 if (r) {
3524 DMWARN("Creation of new thinly-provisioned device with id %s failed.",
3525 argv[1]);
3526 return r;
3527 }
3528
3529 return 0;
3530 }
3531
3532 static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3533 {
3534 dm_thin_id dev_id;
3535 dm_thin_id origin_dev_id;
3536 int r;
3537
3538 r = check_arg_count(argc, 3);
3539 if (r)
3540 return r;
3541
3542 r = read_dev_id(argv[1], &dev_id, 1);
3543 if (r)
3544 return r;
3545
3546 r = read_dev_id(argv[2], &origin_dev_id, 1);
3547 if (r)
3548 return r;
3549
3550 r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
3551 if (r) {
3552 DMWARN("Creation of new snapshot %s of device %s failed.",
3553 argv[1], argv[2]);
3554 return r;
3555 }
3556
3557 return 0;
3558 }
3559
3560 static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
3561 {
3562 dm_thin_id dev_id;
3563 int r;
3564
3565 r = check_arg_count(argc, 2);
3566 if (r)
3567 return r;
3568
3569 r = read_dev_id(argv[1], &dev_id, 1);
3570 if (r)
3571 return r;
3572
3573 r = dm_pool_delete_thin_device(pool->pmd, dev_id);
3574 if (r)
3575 DMWARN("Deletion of thin device %s failed.", argv[1]);
3576
3577 return r;
3578 }
3579
3580 static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
3581 {
3582 dm_thin_id old_id, new_id;
3583 int r;
3584
3585 r = check_arg_count(argc, 3);
3586 if (r)
3587 return r;
3588
3589 if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
3590 DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
3591 return -EINVAL;
3592 }
3593
3594 if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
3595 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
3596 return -EINVAL;
3597 }
3598
3599 r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
3600 if (r) {
3601 DMWARN("Failed to change transaction id from %s to %s.",
3602 argv[1], argv[2]);
3603 return r;
3604 }
3605
3606 return 0;
3607 }
3608
3609 static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3610 {
3611 int r;
3612
3613 r = check_arg_count(argc, 1);
3614 if (r)
3615 return r;
3616
3617 (void) commit(pool);
3618
3619 r = dm_pool_reserve_metadata_snap(pool->pmd);
3620 if (r)
3621 DMWARN("reserve_metadata_snap message failed.");
3622
3623 return r;
3624 }
3625
3626 static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
3627 {
3628 int r;
3629
3630 r = check_arg_count(argc, 1);
3631 if (r)
3632 return r;
3633
3634 r = dm_pool_release_metadata_snap(pool->pmd);
3635 if (r)
3636 DMWARN("release_metadata_snap message failed.");
3637
3638 return r;
3639 }
3640
3641 /*
3642 * Messages supported:
3643 * create_thin <dev_id>
3644 * create_snap <dev_id> <origin_id>
3645 * delete <dev_id>
3646 * set_transaction_id <current_trans_id> <new_trans_id>
3647 * reserve_metadata_snap
3648 * release_metadata_snap
3649 */
3650 static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
3651 {
3652 int r = -EINVAL;
3653 struct pool_c *pt = ti->private;
3654 struct pool *pool = pt->pool;
3655
3656 if (get_pool_mode(pool) >= PM_READ_ONLY) {
3657 DMERR("%s: unable to service pool target messages in READ_ONLY or FAIL mode",
3658 dm_device_name(pool->pool_md));
3659 return -EOPNOTSUPP;
3660 }
3661
3662 if (!strcasecmp(argv[0], "create_thin"))
3663 r = process_create_thin_mesg(argc, argv, pool);
3664
3665 else if (!strcasecmp(argv[0], "create_snap"))
3666 r = process_create_snap_mesg(argc, argv, pool);
3667
3668 else if (!strcasecmp(argv[0], "delete"))
3669 r = process_delete_mesg(argc, argv, pool);
3670
3671 else if (!strcasecmp(argv[0], "set_transaction_id"))
3672 r = process_set_transaction_id_mesg(argc, argv, pool);
3673
3674 else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
3675 r = process_reserve_metadata_snap_mesg(argc, argv, pool);
3676
3677 else if (!strcasecmp(argv[0], "release_metadata_snap"))
3678 r = process_release_metadata_snap_mesg(argc, argv, pool);
3679
3680 else
3681 DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
3682
3683 if (!r)
3684 (void) commit(pool);
3685
3686 return r;
3687 }
3688
3689 static void emit_flags(struct pool_features *pf, char *result,
3690 unsigned sz, unsigned maxlen)
3691 {
3692 unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
3693 !pf->discard_passdown + (pf->mode == PM_READ_ONLY) +
3694 pf->error_if_no_space;
3695 DMEMIT("%u ", count);
3696
3697 if (!pf->zero_new_blocks)
3698 DMEMIT("skip_block_zeroing ");
3699
3700 if (!pf->discard_enabled)
3701 DMEMIT("ignore_discard ");
3702
3703 if (!pf->discard_passdown)
3704 DMEMIT("no_discard_passdown ");
3705
3706 if (pf->mode == PM_READ_ONLY)
3707 DMEMIT("read_only ");
3708
3709 if (pf->error_if_no_space)
3710 DMEMIT("error_if_no_space ");
3711 }
3712
3713 /*
3714 * Status line is:
3715 * <transaction id> <used metadata sectors>/<total metadata sectors>
3716 * <used data sectors>/<total data sectors> <held metadata root>
3717 */
3718 static void pool_status(struct dm_target *ti, status_type_t type,
3719 unsigned status_flags, char *result, unsigned maxlen)
3720 {
3721 int r;
3722 unsigned sz = 0;
3723 uint64_t transaction_id;
3724 dm_block_t nr_free_blocks_data;
3725 dm_block_t nr_free_blocks_metadata;
3726 dm_block_t nr_blocks_data;
3727 dm_block_t nr_blocks_metadata;
3728 dm_block_t held_root;
3729 char buf[BDEVNAME_SIZE];
3730 char buf2[BDEVNAME_SIZE];
3731 struct pool_c *pt = ti->private;
3732 struct pool *pool = pt->pool;
3733
3734 switch (type) {
3735 case STATUSTYPE_INFO:
3736 if (get_pool_mode(pool) == PM_FAIL) {
3737 DMEMIT("Fail");
3738 break;
3739 }
3740
3741 /* Commit to ensure statistics aren't out-of-date */
3742 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
3743 (void) commit(pool);
3744
3745 r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id);
3746 if (r) {
3747 DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
3748 dm_device_name(pool->pool_md), r);
3749 goto err;
3750 }
3751
3752 r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata);
3753 if (r) {
3754 DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
3755 dm_device_name(pool->pool_md), r);
3756 goto err;
3757 }
3758
3759 r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
3760 if (r) {
3761 DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
3762 dm_device_name(pool->pool_md), r);
3763 goto err;
3764 }
3765
3766 r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data);
3767 if (r) {
3768 DMERR("%s: dm_pool_get_free_block_count returned %d",
3769 dm_device_name(pool->pool_md), r);
3770 goto err;
3771 }
3772
3773 r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
3774 if (r) {
3775 DMERR("%s: dm_pool_get_data_dev_size returned %d",
3776 dm_device_name(pool->pool_md), r);
3777 goto err;
3778 }
3779
3780 r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
3781 if (r) {
3782 DMERR("%s: dm_pool_get_metadata_snap returned %d",
3783 dm_device_name(pool->pool_md), r);
3784 goto err;
3785 }
3786
3787 DMEMIT("%llu %llu/%llu %llu/%llu ",
3788 (unsigned long long)transaction_id,
3789 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
3790 (unsigned long long)nr_blocks_metadata,
3791 (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
3792 (unsigned long long)nr_blocks_data);
3793
3794 if (held_root)
3795 DMEMIT("%llu ", held_root);
3796 else
3797 DMEMIT("- ");
3798
3799 if (pool->pf.mode == PM_OUT_OF_DATA_SPACE)
3800 DMEMIT("out_of_data_space ");
3801 else if (pool->pf.mode == PM_READ_ONLY)
3802 DMEMIT("ro ");
3803 else
3804 DMEMIT("rw ");
3805
3806 if (!pool->pf.discard_enabled)
3807 DMEMIT("ignore_discard ");
3808 else if (pool->pf.discard_passdown)
3809 DMEMIT("discard_passdown ");
3810 else
3811 DMEMIT("no_discard_passdown ");
3812
3813 if (pool->pf.error_if_no_space)
3814 DMEMIT("error_if_no_space ");
3815 else
3816 DMEMIT("queue_if_no_space ");
3817
3818 break;
3819
3820 case STATUSTYPE_TABLE:
3821 DMEMIT("%s %s %lu %llu ",
3822 format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
3823 format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
3824 (unsigned long)pool->sectors_per_block,
3825 (unsigned long long)pt->low_water_blocks);
3826 emit_flags(&pt->requested_pf, result, sz, maxlen);
3827 break;
3828 }
3829 return;
3830
3831 err:
3832 DMEMIT("Error");
3833 }
3834
3835 static int pool_iterate_devices(struct dm_target *ti,
3836 iterate_devices_callout_fn fn, void *data)
3837 {
3838 struct pool_c *pt = ti->private;
3839
3840 return fn(ti, pt->data_dev, 0, ti->len, data);
3841 }
3842
3843 static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
3844 struct bio_vec *biovec, int max_size)
3845 {
3846 struct pool_c *pt = ti->private;
3847 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
3848
3849 if (!q->merge_bvec_fn)
3850 return max_size;
3851
3852 bvm->bi_bdev = pt->data_dev->bdev;
3853
3854 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
3855 }
3856
3857 static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
3858 {
3859 struct pool_c *pt = ti->private;
3860 struct pool *pool = pt->pool;
3861 sector_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
3862
3863 /*
3864 * If max_sectors is smaller than pool->sectors_per_block adjust it
3865 * to the highest possible power-of-2 factor of pool->sectors_per_block.
3866 * This is especially beneficial when the pool's data device is a RAID
3867 * device that has a full stripe width that matches pool->sectors_per_block
3868 * -- because even though partial RAID stripe-sized IOs will be issued to a
3869 * single RAID stripe; when aggregated they will end on a full RAID stripe
3870 * boundary.. which avoids additional partial RAID stripe writes cascading
3871 */
3872 if (limits->max_sectors < pool->sectors_per_block) {
3873 while (!is_factor(pool->sectors_per_block, limits->max_sectors)) {
3874 if ((limits->max_sectors & (limits->max_sectors - 1)) == 0)
3875 limits->max_sectors--;
3876 limits->max_sectors = rounddown_pow_of_two(limits->max_sectors);
3877 }
3878 }
3879
3880 /*
3881 * If the system-determined stacked limits are compatible with the
3882 * pool's blocksize (io_opt is a factor) do not override them.
3883 */
3884 if (io_opt_sectors < pool->sectors_per_block ||
3885 !is_factor(io_opt_sectors, pool->sectors_per_block)) {
3886 if (is_factor(pool->sectors_per_block, limits->max_sectors))
3887 blk_limits_io_min(limits, limits->max_sectors << SECTOR_SHIFT);
3888 else
3889 blk_limits_io_min(limits, pool->sectors_per_block << SECTOR_SHIFT);
3890 blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
3891 }
3892
3893 /*
3894 * pt->adjusted_pf is a staging area for the actual features to use.
3895 * They get transferred to the live pool in bind_control_target()
3896 * called from pool_preresume().
3897 */
3898 if (!pt->adjusted_pf.discard_enabled) {
3899 /*
3900 * Must explicitly disallow stacking discard limits otherwise the
3901 * block layer will stack them if pool's data device has support.
3902 * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the
3903 * user to see that, so make sure to set all discard limits to 0.
3904 */
3905 limits->discard_granularity = 0;
3906 return;
3907 }
3908
3909 disable_passdown_if_not_supported(pt);
3910
3911 /*
3912 * The pool uses the same discard limits as the underlying data
3913 * device. DM core has already set this up.
3914 */
3915 }
3916
3917 static struct target_type pool_target = {
3918 .name = "thin-pool",
3919 .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
3920 DM_TARGET_IMMUTABLE,
3921 .version = {1, 15, 0},
3922 .module = THIS_MODULE,
3923 .ctr = pool_ctr,
3924 .dtr = pool_dtr,
3925 .map = pool_map,
3926 .presuspend = pool_presuspend,
3927 .presuspend_undo = pool_presuspend_undo,
3928 .postsuspend = pool_postsuspend,
3929 .preresume = pool_preresume,
3930 .resume = pool_resume,
3931 .message = pool_message,
3932 .status = pool_status,
3933 .merge = pool_merge,
3934 .iterate_devices = pool_iterate_devices,
3935 .io_hints = pool_io_hints,
3936 };
3937
3938 /*----------------------------------------------------------------
3939 * Thin target methods
3940 *--------------------------------------------------------------*/
3941 static void thin_get(struct thin_c *tc)
3942 {
3943 atomic_inc(&tc->refcount);
3944 }
3945
3946 static void thin_put(struct thin_c *tc)
3947 {
3948 if (atomic_dec_and_test(&tc->refcount))
3949 complete(&tc->can_destroy);
3950 }
3951
3952 static void thin_dtr(struct dm_target *ti)
3953 {
3954 struct thin_c *tc = ti->private;
3955 unsigned long flags;
3956
3957 spin_lock_irqsave(&tc->pool->lock, flags);
3958 list_del_rcu(&tc->list);
3959 spin_unlock_irqrestore(&tc->pool->lock, flags);
3960 synchronize_rcu();
3961
3962 thin_put(tc);
3963 wait_for_completion(&tc->can_destroy);
3964
3965 mutex_lock(&dm_thin_pool_table.mutex);
3966
3967 __pool_dec(tc->pool);
3968 dm_pool_close_thin_device(tc->td);
3969 dm_put_device(ti, tc->pool_dev);
3970 if (tc->origin_dev)
3971 dm_put_device(ti, tc->origin_dev);
3972 kfree(tc);
3973
3974 mutex_unlock(&dm_thin_pool_table.mutex);
3975 }
3976
3977 /*
3978 * Thin target parameters:
3979 *
3980 * <pool_dev> <dev_id> [origin_dev]
3981 *
3982 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
3983 * dev_id: the internal device identifier
3984 * origin_dev: a device external to the pool that should act as the origin
3985 *
3986 * If the pool device has discards disabled, they get disabled for the thin
3987 * device as well.
3988 */
3989 static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
3990 {
3991 int r;
3992 struct thin_c *tc;
3993 struct dm_dev *pool_dev, *origin_dev;
3994 struct mapped_device *pool_md;
3995 unsigned long flags;
3996
3997 mutex_lock(&dm_thin_pool_table.mutex);
3998
3999 if (argc != 2 && argc != 3) {
4000 ti->error = "Invalid argument count";
4001 r = -EINVAL;
4002 goto out_unlock;
4003 }
4004
4005 tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
4006 if (!tc) {
4007 ti->error = "Out of memory";
4008 r = -ENOMEM;
4009 goto out_unlock;
4010 }
4011 tc->thin_md = dm_table_get_md(ti->table);
4012 spin_lock_init(&tc->lock);
4013 INIT_LIST_HEAD(&tc->deferred_cells);
4014 bio_list_init(&tc->deferred_bio_list);
4015 bio_list_init(&tc->retry_on_resume_list);
4016 tc->sort_bio_list = RB_ROOT;
4017
4018 if (argc == 3) {
4019 r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
4020 if (r) {
4021 ti->error = "Error opening origin device";
4022 goto bad_origin_dev;
4023 }
4024 tc->origin_dev = origin_dev;
4025 }
4026
4027 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
4028 if (r) {
4029 ti->error = "Error opening pool device";
4030 goto bad_pool_dev;
4031 }
4032 tc->pool_dev = pool_dev;
4033
4034 if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
4035 ti->error = "Invalid device id";
4036 r = -EINVAL;
4037 goto bad_common;
4038 }
4039
4040 pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
4041 if (!pool_md) {
4042 ti->error = "Couldn't get pool mapped device";
4043 r = -EINVAL;
4044 goto bad_common;
4045 }
4046
4047 tc->pool = __pool_table_lookup(pool_md);
4048 if (!tc->pool) {
4049 ti->error = "Couldn't find pool object";
4050 r = -EINVAL;
4051 goto bad_pool_lookup;
4052 }
4053 __pool_inc(tc->pool);
4054
4055 if (get_pool_mode(tc->pool) == PM_FAIL) {
4056 ti->error = "Couldn't open thin device, Pool is in fail mode";
4057 r = -EINVAL;
4058 goto bad_pool;
4059 }
4060
4061 r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
4062 if (r) {
4063 ti->error = "Couldn't open thin internal device";
4064 goto bad_pool;
4065 }
4066
4067 r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
4068 if (r)
4069 goto bad;
4070
4071 ti->num_flush_bios = 1;
4072 ti->flush_supported = true;
4073 ti->per_bio_data_size = sizeof(struct dm_thin_endio_hook);
4074
4075 /* In case the pool supports discards, pass them on. */
4076 ti->discard_zeroes_data_unsupported = true;
4077 if (tc->pool->pf.discard_enabled) {
4078 ti->discards_supported = true;
4079 ti->num_discard_bios = 1;
4080 ti->split_discard_bios = false;
4081 }
4082
4083 mutex_unlock(&dm_thin_pool_table.mutex);
4084
4085 spin_lock_irqsave(&tc->pool->lock, flags);
4086 if (tc->pool->suspended) {
4087 spin_unlock_irqrestore(&tc->pool->lock, flags);
4088 mutex_lock(&dm_thin_pool_table.mutex); /* reacquire for __pool_dec */
4089 ti->error = "Unable to activate thin device while pool is suspended";
4090 r = -EINVAL;
4091 goto bad;
4092 }
4093 atomic_set(&tc->refcount, 1);
4094 init_completion(&tc->can_destroy);
4095 list_add_tail_rcu(&tc->list, &tc->pool->active_thins);
4096 spin_unlock_irqrestore(&tc->pool->lock, flags);
4097 /*
4098 * This synchronize_rcu() call is needed here otherwise we risk a
4099 * wake_worker() call finding no bios to process (because the newly
4100 * added tc isn't yet visible). So this reduces latency since we
4101 * aren't then dependent on the periodic commit to wake_worker().
4102 */
4103 synchronize_rcu();
4104
4105 dm_put(pool_md);
4106
4107 return 0;
4108
4109 bad:
4110 dm_pool_close_thin_device(tc->td);
4111 bad_pool:
4112 __pool_dec(tc->pool);
4113 bad_pool_lookup:
4114 dm_put(pool_md);
4115 bad_common:
4116 dm_put_device(ti, tc->pool_dev);
4117 bad_pool_dev:
4118 if (tc->origin_dev)
4119 dm_put_device(ti, tc->origin_dev);
4120 bad_origin_dev:
4121 kfree(tc);
4122 out_unlock:
4123 mutex_unlock(&dm_thin_pool_table.mutex);
4124
4125 return r;
4126 }
4127
4128 static int thin_map(struct dm_target *ti, struct bio *bio)
4129 {
4130 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
4131
4132 return thin_bio_map(ti, bio);
4133 }
4134
4135 static int thin_endio(struct dm_target *ti, struct bio *bio, int err)
4136 {
4137 unsigned long flags;
4138 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
4139 struct list_head work;
4140 struct dm_thin_new_mapping *m, *tmp;
4141 struct pool *pool = h->tc->pool;
4142
4143 if (h->shared_read_entry) {
4144 INIT_LIST_HEAD(&work);
4145 dm_deferred_entry_dec(h->shared_read_entry, &work);
4146
4147 spin_lock_irqsave(&pool->lock, flags);
4148 list_for_each_entry_safe(m, tmp, &work, list) {
4149 list_del(&m->list);
4150 __complete_mapping_preparation(m);
4151 }
4152 spin_unlock_irqrestore(&pool->lock, flags);
4153 }
4154
4155 if (h->all_io_entry) {
4156 INIT_LIST_HEAD(&work);
4157 dm_deferred_entry_dec(h->all_io_entry, &work);
4158 if (!list_empty(&work)) {
4159 spin_lock_irqsave(&pool->lock, flags);
4160 list_for_each_entry_safe(m, tmp, &work, list)
4161 list_add_tail(&m->list, &pool->prepared_discards);
4162 spin_unlock_irqrestore(&pool->lock, flags);
4163 wake_worker(pool);
4164 }
4165 }
4166
4167 if (h->cell)
4168 cell_defer_no_holder(h->tc, h->cell);
4169
4170 return 0;
4171 }
4172
4173 static void thin_presuspend(struct dm_target *ti)
4174 {
4175 struct thin_c *tc = ti->private;
4176
4177 if (dm_noflush_suspending(ti))
4178 noflush_work(tc, do_noflush_start);
4179 }
4180
4181 static void thin_postsuspend(struct dm_target *ti)
4182 {
4183 struct thin_c *tc = ti->private;
4184
4185 /*
4186 * The dm_noflush_suspending flag has been cleared by now, so
4187 * unfortunately we must always run this.
4188 */
4189 noflush_work(tc, do_noflush_stop);
4190 }
4191
4192 static int thin_preresume(struct dm_target *ti)
4193 {
4194 struct thin_c *tc = ti->private;
4195
4196 if (tc->origin_dev)
4197 tc->origin_size = get_dev_size(tc->origin_dev->bdev);
4198
4199 return 0;
4200 }
4201
4202 /*
4203 * <nr mapped sectors> <highest mapped sector>
4204 */
4205 static void thin_status(struct dm_target *ti, status_type_t type,
4206 unsigned status_flags, char *result, unsigned maxlen)
4207 {
4208 int r;
4209 ssize_t sz = 0;
4210 dm_block_t mapped, highest;
4211 char buf[BDEVNAME_SIZE];
4212 struct thin_c *tc = ti->private;
4213
4214 if (get_pool_mode(tc->pool) == PM_FAIL) {
4215 DMEMIT("Fail");
4216 return;
4217 }
4218
4219 if (!tc->td)
4220 DMEMIT("-");
4221 else {
4222 switch (type) {
4223 case STATUSTYPE_INFO:
4224 r = dm_thin_get_mapped_count(tc->td, &mapped);
4225 if (r) {
4226 DMERR("dm_thin_get_mapped_count returned %d", r);
4227 goto err;
4228 }
4229
4230 r = dm_thin_get_highest_mapped_block(tc->td, &highest);
4231 if (r < 0) {
4232 DMERR("dm_thin_get_highest_mapped_block returned %d", r);
4233 goto err;
4234 }
4235
4236 DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
4237 if (r)
4238 DMEMIT("%llu", ((highest + 1) *
4239 tc->pool->sectors_per_block) - 1);
4240 else
4241 DMEMIT("-");
4242 break;
4243
4244 case STATUSTYPE_TABLE:
4245 DMEMIT("%s %lu",
4246 format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
4247 (unsigned long) tc->dev_id);
4248 if (tc->origin_dev)
4249 DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
4250 break;
4251 }
4252 }
4253
4254 return;
4255
4256 err:
4257 DMEMIT("Error");
4258 }
4259
4260 static int thin_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
4261 struct bio_vec *biovec, int max_size)
4262 {
4263 struct thin_c *tc = ti->private;
4264 struct request_queue *q = bdev_get_queue(tc->pool_dev->bdev);
4265
4266 if (!q->merge_bvec_fn)
4267 return max_size;
4268
4269 bvm->bi_bdev = tc->pool_dev->bdev;
4270 bvm->bi_sector = dm_target_offset(ti, bvm->bi_sector);
4271
4272 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
4273 }
4274
4275 static int thin_iterate_devices(struct dm_target *ti,
4276 iterate_devices_callout_fn fn, void *data)
4277 {
4278 sector_t blocks;
4279 struct thin_c *tc = ti->private;
4280 struct pool *pool = tc->pool;
4281
4282 /*
4283 * We can't call dm_pool_get_data_dev_size() since that blocks. So
4284 * we follow a more convoluted path through to the pool's target.
4285 */
4286 if (!pool->ti)
4287 return 0; /* nothing is bound */
4288
4289 blocks = pool->ti->len;
4290 (void) sector_div(blocks, pool->sectors_per_block);
4291 if (blocks)
4292 return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
4293
4294 return 0;
4295 }
4296
4297 static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)
4298 {
4299 struct thin_c *tc = ti->private;
4300 struct pool *pool = tc->pool;
4301
4302 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
4303 limits->max_discard_sectors = 2048 * 1024 * 16; /* 16G */
4304 }
4305
4306 static struct target_type thin_target = {
4307 .name = "thin",
4308 .version = {1, 15, 0},
4309 .module = THIS_MODULE,
4310 .ctr = thin_ctr,
4311 .dtr = thin_dtr,
4312 .map = thin_map,
4313 .end_io = thin_endio,
4314 .preresume = thin_preresume,
4315 .presuspend = thin_presuspend,
4316 .postsuspend = thin_postsuspend,
4317 .status = thin_status,
4318 .merge = thin_merge,
4319 .iterate_devices = thin_iterate_devices,
4320 .io_hints = thin_io_hints,
4321 };
4322
4323 /*----------------------------------------------------------------*/
4324
4325 static int __init dm_thin_init(void)
4326 {
4327 int r;
4328
4329 pool_table_init();
4330
4331 r = dm_register_target(&thin_target);
4332 if (r)
4333 return r;
4334
4335 r = dm_register_target(&pool_target);
4336 if (r)
4337 goto bad_pool_target;
4338
4339 r = -ENOMEM;
4340
4341 _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
4342 if (!_new_mapping_cache)
4343 goto bad_new_mapping_cache;
4344
4345 return 0;
4346
4347 bad_new_mapping_cache:
4348 dm_unregister_target(&pool_target);
4349 bad_pool_target:
4350 dm_unregister_target(&thin_target);
4351
4352 return r;
4353 }
4354
4355 static void dm_thin_exit(void)
4356 {
4357 dm_unregister_target(&thin_target);
4358 dm_unregister_target(&pool_target);
4359
4360 kmem_cache_destroy(_new_mapping_cache);
4361 }
4362
4363 module_init(dm_thin_init);
4364 module_exit(dm_thin_exit);
4365
4366 module_param_named(no_space_timeout, no_space_timeout_secs, uint, S_IRUGO | S_IWUSR);
4367 MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds");
4368
4369 MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
4370 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
4371 MODULE_LICENSE("GPL");
This page took 0.136436 seconds and 6 git commands to generate.