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