Correct .gbs.conf settings
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / md / dm.c
1 /*
2  * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3  * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
4  *
5  * This file is released under the GPL.
6  */
7
8 #include "dm.h"
9 #include "dm-uevent.h"
10
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/moduleparam.h>
15 #include <linux/blkpg.h>
16 #include <linux/bio.h>
17 #include <linux/mempool.h>
18 #include <linux/slab.h>
19 #include <linux/idr.h>
20 #include <linux/hdreg.h>
21 #include <linux/delay.h>
22
23 #include <trace/events/block.h>
24
25 #define DM_MSG_PREFIX "core"
26
27 #ifdef CONFIG_PRINTK
28 /*
29  * ratelimit state to be used in DMXXX_LIMIT().
30  */
31 DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
32                        DEFAULT_RATELIMIT_INTERVAL,
33                        DEFAULT_RATELIMIT_BURST);
34 EXPORT_SYMBOL(dm_ratelimit_state);
35 #endif
36
37 /*
38  * Cookies are numeric values sent with CHANGE and REMOVE
39  * uevents while resuming, removing or renaming the device.
40  */
41 #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
42 #define DM_COOKIE_LENGTH 24
43
44 static const char *_name = DM_NAME;
45
46 static unsigned int major = 0;
47 static unsigned int _major = 0;
48
49 static DEFINE_IDR(_minor_idr);
50
51 static DEFINE_SPINLOCK(_minor_lock);
52
53 static void do_deferred_remove(struct work_struct *w);
54
55 static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
56
57 static struct workqueue_struct *deferred_remove_workqueue;
58
59 /*
60  * For bio-based dm.
61  * One of these is allocated per bio.
62  */
63 struct dm_io {
64         struct mapped_device *md;
65         int error;
66         atomic_t io_count;
67         struct bio *bio;
68         unsigned long start_time;
69         spinlock_t endio_lock;
70         struct dm_stats_aux stats_aux;
71 };
72
73 /*
74  * For request-based dm.
75  * One of these is allocated per request.
76  */
77 struct dm_rq_target_io {
78         struct mapped_device *md;
79         struct dm_target *ti;
80         struct request *orig, clone;
81         int error;
82         union map_info info;
83 };
84
85 /*
86  * For request-based dm - the bio clones we allocate are embedded in these
87  * structs.
88  *
89  * We allocate these with bio_alloc_bioset, using the front_pad parameter when
90  * the bioset is created - this means the bio has to come at the end of the
91  * struct.
92  */
93 struct dm_rq_clone_bio_info {
94         struct bio *orig;
95         struct dm_rq_target_io *tio;
96         struct bio clone;
97 };
98
99 union map_info *dm_get_mapinfo(struct bio *bio)
100 {
101         if (bio && bio->bi_private)
102                 return &((struct dm_target_io *)bio->bi_private)->info;
103         return NULL;
104 }
105
106 union map_info *dm_get_rq_mapinfo(struct request *rq)
107 {
108         if (rq && rq->end_io_data)
109                 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
110         return NULL;
111 }
112 EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
113
114 #define MINOR_ALLOCED ((void *)-1)
115
116 /*
117  * Bits for the md->flags field.
118  */
119 #define DMF_BLOCK_IO_FOR_SUSPEND 0
120 #define DMF_SUSPENDED 1
121 #define DMF_FROZEN 2
122 #define DMF_FREEING 3
123 #define DMF_DELETING 4
124 #define DMF_NOFLUSH_SUSPENDING 5
125 #define DMF_MERGE_IS_OPTIONAL 6
126 #define DMF_DEFERRED_REMOVE 7
127
128 /*
129  * A dummy definition to make RCU happy.
130  * struct dm_table should never be dereferenced in this file.
131  */
132 struct dm_table {
133         int undefined__;
134 };
135
136 /*
137  * Work processed by per-device workqueue.
138  */
139 struct mapped_device {
140         struct srcu_struct io_barrier;
141         struct mutex suspend_lock;
142         atomic_t holders;
143         atomic_t open_count;
144
145         /*
146          * The current mapping.
147          * Use dm_get_live_table{_fast} or take suspend_lock for
148          * dereference.
149          */
150         struct dm_table *map;
151
152         unsigned long flags;
153
154         struct request_queue *queue;
155         unsigned type;
156         /* Protect queue and type against concurrent access. */
157         struct mutex type_lock;
158
159         struct target_type *immutable_target_type;
160
161         struct gendisk *disk;
162         char name[16];
163
164         void *interface_ptr;
165
166         /*
167          * A list of ios that arrived while we were suspended.
168          */
169         atomic_t pending[2];
170         wait_queue_head_t wait;
171         struct work_struct work;
172         struct bio_list deferred;
173         spinlock_t deferred_lock;
174
175         /*
176          * Processing queue (flush)
177          */
178         struct workqueue_struct *wq;
179
180         /*
181          * io objects are allocated from here.
182          */
183         mempool_t *io_pool;
184
185         struct bio_set *bs;
186
187         /*
188          * Event handling.
189          */
190         atomic_t event_nr;
191         wait_queue_head_t eventq;
192         atomic_t uevent_seq;
193         struct list_head uevent_list;
194         spinlock_t uevent_lock; /* Protect access to uevent_list */
195
196         /*
197          * freeze/thaw support require holding onto a super block
198          */
199         struct super_block *frozen_sb;
200         struct block_device *bdev;
201
202         /* forced geometry settings */
203         struct hd_geometry geometry;
204
205         /* kobject and completion */
206         struct dm_kobject_holder kobj_holder;
207
208         /* zero-length flush that will be cloned and submitted to targets */
209         struct bio flush_bio;
210
211         struct dm_stats stats;
212 };
213
214 /*
215  * For mempools pre-allocation at the table loading time.
216  */
217 struct dm_md_mempools {
218         mempool_t *io_pool;
219         struct bio_set *bs;
220 };
221
222 #define RESERVED_BIO_BASED_IOS          16
223 #define RESERVED_REQUEST_BASED_IOS      256
224 #define RESERVED_MAX_IOS                1024
225 static struct kmem_cache *_io_cache;
226 static struct kmem_cache *_rq_tio_cache;
227
228 /*
229  * Bio-based DM's mempools' reserved IOs set by the user.
230  */
231 static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
232
233 /*
234  * Request-based DM's mempools' reserved IOs set by the user.
235  */
236 static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
237
238 static unsigned __dm_get_reserved_ios(unsigned *reserved_ios,
239                                       unsigned def, unsigned max)
240 {
241         unsigned ios = ACCESS_ONCE(*reserved_ios);
242         unsigned modified_ios = 0;
243
244         if (!ios)
245                 modified_ios = def;
246         else if (ios > max)
247                 modified_ios = max;
248
249         if (modified_ios) {
250                 (void)cmpxchg(reserved_ios, ios, modified_ios);
251                 ios = modified_ios;
252         }
253
254         return ios;
255 }
256
257 unsigned dm_get_reserved_bio_based_ios(void)
258 {
259         return __dm_get_reserved_ios(&reserved_bio_based_ios,
260                                      RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
261 }
262 EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
263
264 unsigned dm_get_reserved_rq_based_ios(void)
265 {
266         return __dm_get_reserved_ios(&reserved_rq_based_ios,
267                                      RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
268 }
269 EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
270
271 static int __init local_init(void)
272 {
273         int r = -ENOMEM;
274
275         /* allocate a slab for the dm_ios */
276         _io_cache = KMEM_CACHE(dm_io, 0);
277         if (!_io_cache)
278                 return r;
279
280         _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
281         if (!_rq_tio_cache)
282                 goto out_free_io_cache;
283
284         r = dm_uevent_init();
285         if (r)
286                 goto out_free_rq_tio_cache;
287
288         deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
289         if (!deferred_remove_workqueue) {
290                 r = -ENOMEM;
291                 goto out_uevent_exit;
292         }
293
294         _major = major;
295         r = register_blkdev(_major, _name);
296         if (r < 0)
297                 goto out_free_workqueue;
298
299         if (!_major)
300                 _major = r;
301
302         return 0;
303
304 out_free_workqueue:
305         destroy_workqueue(deferred_remove_workqueue);
306 out_uevent_exit:
307         dm_uevent_exit();
308 out_free_rq_tio_cache:
309         kmem_cache_destroy(_rq_tio_cache);
310 out_free_io_cache:
311         kmem_cache_destroy(_io_cache);
312
313         return r;
314 }
315
316 static void local_exit(void)
317 {
318         flush_scheduled_work();
319         destroy_workqueue(deferred_remove_workqueue);
320
321         kmem_cache_destroy(_rq_tio_cache);
322         kmem_cache_destroy(_io_cache);
323         unregister_blkdev(_major, _name);
324         dm_uevent_exit();
325
326         _major = 0;
327
328         DMINFO("cleaned up");
329 }
330
331 static int (*_inits[])(void) __initdata = {
332         local_init,
333         dm_target_init,
334         dm_linear_init,
335         dm_stripe_init,
336         dm_io_init,
337         dm_kcopyd_init,
338         dm_interface_init,
339         dm_statistics_init,
340 };
341
342 static void (*_exits[])(void) = {
343         local_exit,
344         dm_target_exit,
345         dm_linear_exit,
346         dm_stripe_exit,
347         dm_io_exit,
348         dm_kcopyd_exit,
349         dm_interface_exit,
350         dm_statistics_exit,
351 };
352
353 static int __init dm_init(void)
354 {
355         const int count = ARRAY_SIZE(_inits);
356
357         int r, i;
358
359         for (i = 0; i < count; i++) {
360                 r = _inits[i]();
361                 if (r)
362                         goto bad;
363         }
364
365         return 0;
366
367       bad:
368         while (i--)
369                 _exits[i]();
370
371         return r;
372 }
373
374 static void __exit dm_exit(void)
375 {
376         int i = ARRAY_SIZE(_exits);
377
378         while (i--)
379                 _exits[i]();
380
381         /*
382          * Should be empty by this point.
383          */
384         idr_destroy(&_minor_idr);
385 }
386
387 /*
388  * Block device functions
389  */
390 int dm_deleting_md(struct mapped_device *md)
391 {
392         return test_bit(DMF_DELETING, &md->flags);
393 }
394
395 static int dm_blk_open(struct block_device *bdev, fmode_t mode)
396 {
397         struct mapped_device *md;
398
399         spin_lock(&_minor_lock);
400
401         md = bdev->bd_disk->private_data;
402         if (!md)
403                 goto out;
404
405         if (test_bit(DMF_FREEING, &md->flags) ||
406             dm_deleting_md(md)) {
407                 md = NULL;
408                 goto out;
409         }
410
411         dm_get(md);
412         atomic_inc(&md->open_count);
413
414 out:
415         spin_unlock(&_minor_lock);
416
417         return md ? 0 : -ENXIO;
418 }
419
420 static void dm_blk_close(struct gendisk *disk, fmode_t mode)
421 {
422         struct mapped_device *md = disk->private_data;
423
424         spin_lock(&_minor_lock);
425
426         if (atomic_dec_and_test(&md->open_count) &&
427             (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
428                 queue_work(deferred_remove_workqueue, &deferred_remove_work);
429
430         dm_put(md);
431
432         spin_unlock(&_minor_lock);
433 }
434
435 int dm_open_count(struct mapped_device *md)
436 {
437         return atomic_read(&md->open_count);
438 }
439
440 /*
441  * Guarantees nothing is using the device before it's deleted.
442  */
443 int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
444 {
445         int r = 0;
446
447         spin_lock(&_minor_lock);
448
449         if (dm_open_count(md)) {
450                 r = -EBUSY;
451                 if (mark_deferred)
452                         set_bit(DMF_DEFERRED_REMOVE, &md->flags);
453         } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
454                 r = -EEXIST;
455         else
456                 set_bit(DMF_DELETING, &md->flags);
457
458         spin_unlock(&_minor_lock);
459
460         return r;
461 }
462
463 int dm_cancel_deferred_remove(struct mapped_device *md)
464 {
465         int r = 0;
466
467         spin_lock(&_minor_lock);
468
469         if (test_bit(DMF_DELETING, &md->flags))
470                 r = -EBUSY;
471         else
472                 clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
473
474         spin_unlock(&_minor_lock);
475
476         return r;
477 }
478
479 static void do_deferred_remove(struct work_struct *w)
480 {
481         dm_deferred_remove();
482 }
483
484 sector_t dm_get_size(struct mapped_device *md)
485 {
486         return get_capacity(md->disk);
487 }
488
489 struct dm_stats *dm_get_stats(struct mapped_device *md)
490 {
491         return &md->stats;
492 }
493
494 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
495 {
496         struct mapped_device *md = bdev->bd_disk->private_data;
497
498         return dm_get_geometry(md, geo);
499 }
500
501 static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
502                         unsigned int cmd, unsigned long arg)
503 {
504         struct mapped_device *md = bdev->bd_disk->private_data;
505         int srcu_idx;
506         struct dm_table *map;
507         struct dm_target *tgt;
508         int r = -ENOTTY;
509
510 retry:
511         map = dm_get_live_table(md, &srcu_idx);
512
513         if (!map || !dm_table_get_size(map))
514                 goto out;
515
516         /* We only support devices that have a single target */
517         if (dm_table_get_num_targets(map) != 1)
518                 goto out;
519
520         tgt = dm_table_get_target(map, 0);
521
522         if (dm_suspended_md(md)) {
523                 r = -EAGAIN;
524                 goto out;
525         }
526
527         if (tgt->type->ioctl)
528                 r = tgt->type->ioctl(tgt, cmd, arg);
529
530 out:
531         dm_put_live_table(md, srcu_idx);
532
533         if (r == -ENOTCONN) {
534                 msleep(10);
535                 goto retry;
536         }
537
538         return r;
539 }
540
541 static struct dm_io *alloc_io(struct mapped_device *md)
542 {
543         return mempool_alloc(md->io_pool, GFP_NOIO);
544 }
545
546 static void free_io(struct mapped_device *md, struct dm_io *io)
547 {
548         mempool_free(io, md->io_pool);
549 }
550
551 static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
552 {
553         bio_put(&tio->clone);
554 }
555
556 static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
557                                             gfp_t gfp_mask)
558 {
559         return mempool_alloc(md->io_pool, gfp_mask);
560 }
561
562 static void free_rq_tio(struct dm_rq_target_io *tio)
563 {
564         mempool_free(tio, tio->md->io_pool);
565 }
566
567 static int md_in_flight(struct mapped_device *md)
568 {
569         return atomic_read(&md->pending[READ]) +
570                atomic_read(&md->pending[WRITE]);
571 }
572
573 static void start_io_acct(struct dm_io *io)
574 {
575         struct mapped_device *md = io->md;
576         struct bio *bio = io->bio;
577         int cpu;
578         int rw = bio_data_dir(bio);
579
580         io->start_time = jiffies;
581
582         cpu = part_stat_lock();
583         part_round_stats(cpu, &dm_disk(md)->part0);
584         part_stat_unlock();
585         atomic_set(&dm_disk(md)->part0.in_flight[rw],
586                 atomic_inc_return(&md->pending[rw]));
587
588         if (unlikely(dm_stats_used(&md->stats)))
589                 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
590                                     bio_sectors(bio), false, 0, &io->stats_aux);
591 }
592
593 static void end_io_acct(struct dm_io *io)
594 {
595         struct mapped_device *md = io->md;
596         struct bio *bio = io->bio;
597         unsigned long duration = jiffies - io->start_time;
598         int pending, cpu;
599         int rw = bio_data_dir(bio);
600
601         cpu = part_stat_lock();
602         part_round_stats(cpu, &dm_disk(md)->part0);
603         part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
604         part_stat_unlock();
605
606         if (unlikely(dm_stats_used(&md->stats)))
607                 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
608                                     bio_sectors(bio), true, duration, &io->stats_aux);
609
610         /*
611          * After this is decremented the bio must not be touched if it is
612          * a flush.
613          */
614         pending = atomic_dec_return(&md->pending[rw]);
615         atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
616         pending += atomic_read(&md->pending[rw^0x1]);
617
618         /* nudge anyone waiting on suspend queue */
619         if (!pending)
620                 wake_up(&md->wait);
621 }
622
623 /*
624  * Add the bio to the list of deferred io.
625  */
626 static void queue_io(struct mapped_device *md, struct bio *bio)
627 {
628         unsigned long flags;
629
630         spin_lock_irqsave(&md->deferred_lock, flags);
631         bio_list_add(&md->deferred, bio);
632         spin_unlock_irqrestore(&md->deferred_lock, flags);
633         queue_work(md->wq, &md->work);
634 }
635
636 /*
637  * Everyone (including functions in this file), should use this
638  * function to access the md->map field, and make sure they call
639  * dm_put_live_table() when finished.
640  */
641 struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
642 {
643         *srcu_idx = srcu_read_lock(&md->io_barrier);
644
645         return srcu_dereference(md->map, &md->io_barrier);
646 }
647
648 void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
649 {
650         srcu_read_unlock(&md->io_barrier, srcu_idx);
651 }
652
653 void dm_sync_table(struct mapped_device *md)
654 {
655         synchronize_srcu(&md->io_barrier);
656         synchronize_rcu_expedited();
657 }
658
659 /*
660  * A fast alternative to dm_get_live_table/dm_put_live_table.
661  * The caller must not block between these two functions.
662  */
663 static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
664 {
665         rcu_read_lock();
666         return rcu_dereference(md->map);
667 }
668
669 static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
670 {
671         rcu_read_unlock();
672 }
673
674 /*
675  * Get the geometry associated with a dm device
676  */
677 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
678 {
679         *geo = md->geometry;
680
681         return 0;
682 }
683
684 /*
685  * Set the geometry of a device.
686  */
687 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
688 {
689         sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
690
691         if (geo->start > sz) {
692                 DMWARN("Start sector is beyond the geometry limits.");
693                 return -EINVAL;
694         }
695
696         md->geometry = *geo;
697
698         return 0;
699 }
700
701 /*-----------------------------------------------------------------
702  * CRUD START:
703  *   A more elegant soln is in the works that uses the queue
704  *   merge fn, unfortunately there are a couple of changes to
705  *   the block layer that I want to make for this.  So in the
706  *   interests of getting something for people to use I give
707  *   you this clearly demarcated crap.
708  *---------------------------------------------------------------*/
709
710 static int __noflush_suspending(struct mapped_device *md)
711 {
712         return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
713 }
714
715 /*
716  * Decrements the number of outstanding ios that a bio has been
717  * cloned into, completing the original io if necc.
718  */
719 static void dec_pending(struct dm_io *io, int error)
720 {
721         unsigned long flags;
722         int io_error;
723         struct bio *bio;
724         struct mapped_device *md = io->md;
725
726         /* Push-back supersedes any I/O errors */
727         if (unlikely(error)) {
728                 spin_lock_irqsave(&io->endio_lock, flags);
729                 if (!(io->error > 0 && __noflush_suspending(md)))
730                         io->error = error;
731                 spin_unlock_irqrestore(&io->endio_lock, flags);
732         }
733
734         if (atomic_dec_and_test(&io->io_count)) {
735                 if (io->error == DM_ENDIO_REQUEUE) {
736                         /*
737                          * Target requested pushing back the I/O.
738                          */
739                         spin_lock_irqsave(&md->deferred_lock, flags);
740                         if (__noflush_suspending(md))
741                                 bio_list_add_head(&md->deferred, io->bio);
742                         else
743                                 /* noflush suspend was interrupted. */
744                                 io->error = -EIO;
745                         spin_unlock_irqrestore(&md->deferred_lock, flags);
746                 }
747
748                 io_error = io->error;
749                 bio = io->bio;
750                 end_io_acct(io);
751                 free_io(md, io);
752
753                 if (io_error == DM_ENDIO_REQUEUE)
754                         return;
755
756                 if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) {
757                         /*
758                          * Preflush done for flush with data, reissue
759                          * without REQ_FLUSH.
760                          */
761                         bio->bi_rw &= ~REQ_FLUSH;
762                         queue_io(md, bio);
763                 } else {
764                         /* done with normal IO or empty flush */
765                         trace_block_bio_complete(md->queue, bio, io_error);
766                         bio_endio(bio, io_error);
767                 }
768         }
769 }
770
771 static void clone_endio(struct bio *bio, int error)
772 {
773         int r = 0;
774         struct dm_target_io *tio = bio->bi_private;
775         struct dm_io *io = tio->io;
776         struct mapped_device *md = tio->io->md;
777         dm_endio_fn endio = tio->ti->type->end_io;
778
779         if (!bio_flagged(bio, BIO_UPTODATE) && !error)
780                 error = -EIO;
781
782         if (endio) {
783                 r = endio(tio->ti, bio, error);
784                 if (r < 0 || r == DM_ENDIO_REQUEUE)
785                         /*
786                          * error and requeue request are handled
787                          * in dec_pending().
788                          */
789                         error = r;
790                 else if (r == DM_ENDIO_INCOMPLETE)
791                         /* The target will handle the io */
792                         return;
793                 else if (r) {
794                         DMWARN("unimplemented target endio return value: %d", r);
795                         BUG();
796                 }
797         }
798
799         free_tio(md, tio);
800         dec_pending(io, error);
801 }
802
803 /*
804  * Partial completion handling for request-based dm
805  */
806 static void end_clone_bio(struct bio *clone, int error)
807 {
808         struct dm_rq_clone_bio_info *info = clone->bi_private;
809         struct dm_rq_target_io *tio = info->tio;
810         struct bio *bio = info->orig;
811         unsigned int nr_bytes = info->orig->bi_iter.bi_size;
812
813         bio_put(clone);
814
815         if (tio->error)
816                 /*
817                  * An error has already been detected on the request.
818                  * Once error occurred, just let clone->end_io() handle
819                  * the remainder.
820                  */
821                 return;
822         else if (error) {
823                 /*
824                  * Don't notice the error to the upper layer yet.
825                  * The error handling decision is made by the target driver,
826                  * when the request is completed.
827                  */
828                 tio->error = error;
829                 return;
830         }
831
832         /*
833          * I/O for the bio successfully completed.
834          * Notice the data completion to the upper layer.
835          */
836
837         /*
838          * bios are processed from the head of the list.
839          * So the completing bio should always be rq->bio.
840          * If it's not, something wrong is happening.
841          */
842         if (tio->orig->bio != bio)
843                 DMERR("bio completion is going in the middle of the request");
844
845         /*
846          * Update the original request.
847          * Do not use blk_end_request() here, because it may complete
848          * the original request before the clone, and break the ordering.
849          */
850         blk_update_request(tio->orig, 0, nr_bytes);
851 }
852
853 /*
854  * Don't touch any member of the md after calling this function because
855  * the md may be freed in dm_put() at the end of this function.
856  * Or do dm_get() before calling this function and dm_put() later.
857  */
858 static void rq_completed(struct mapped_device *md, int rw, int run_queue)
859 {
860         atomic_dec(&md->pending[rw]);
861
862         /* nudge anyone waiting on suspend queue */
863         if (!md_in_flight(md))
864                 wake_up(&md->wait);
865
866         /*
867          * Run this off this callpath, as drivers could invoke end_io while
868          * inside their request_fn (and holding the queue lock). Calling
869          * back into ->request_fn() could deadlock attempting to grab the
870          * queue lock again.
871          */
872         if (run_queue)
873                 blk_run_queue_async(md->queue);
874
875         /*
876          * dm_put() must be at the end of this function. See the comment above
877          */
878         dm_put(md);
879 }
880
881 static void free_rq_clone(struct request *clone)
882 {
883         struct dm_rq_target_io *tio = clone->end_io_data;
884
885         blk_rq_unprep_clone(clone);
886         free_rq_tio(tio);
887 }
888
889 /*
890  * Complete the clone and the original request.
891  * Must be called without queue lock.
892  */
893 static void dm_end_request(struct request *clone, int error)
894 {
895         int rw = rq_data_dir(clone);
896         struct dm_rq_target_io *tio = clone->end_io_data;
897         struct mapped_device *md = tio->md;
898         struct request *rq = tio->orig;
899
900         if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
901                 rq->errors = clone->errors;
902                 rq->resid_len = clone->resid_len;
903
904                 if (rq->sense)
905                         /*
906                          * We are using the sense buffer of the original
907                          * request.
908                          * So setting the length of the sense data is enough.
909                          */
910                         rq->sense_len = clone->sense_len;
911         }
912
913         free_rq_clone(clone);
914         blk_end_request_all(rq, error);
915         rq_completed(md, rw, true);
916 }
917
918 static void dm_unprep_request(struct request *rq)
919 {
920         struct request *clone = rq->special;
921
922         rq->special = NULL;
923         rq->cmd_flags &= ~REQ_DONTPREP;
924
925         free_rq_clone(clone);
926 }
927
928 /*
929  * Requeue the original request of a clone.
930  */
931 void dm_requeue_unmapped_request(struct request *clone)
932 {
933         int rw = rq_data_dir(clone);
934         struct dm_rq_target_io *tio = clone->end_io_data;
935         struct mapped_device *md = tio->md;
936         struct request *rq = tio->orig;
937         struct request_queue *q = rq->q;
938         unsigned long flags;
939
940         dm_unprep_request(rq);
941
942         spin_lock_irqsave(q->queue_lock, flags);
943         blk_requeue_request(q, rq);
944         spin_unlock_irqrestore(q->queue_lock, flags);
945
946         rq_completed(md, rw, 0);
947 }
948 EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
949
950 static void __stop_queue(struct request_queue *q)
951 {
952         blk_stop_queue(q);
953 }
954
955 static void stop_queue(struct request_queue *q)
956 {
957         unsigned long flags;
958
959         spin_lock_irqsave(q->queue_lock, flags);
960         __stop_queue(q);
961         spin_unlock_irqrestore(q->queue_lock, flags);
962 }
963
964 static void __start_queue(struct request_queue *q)
965 {
966         if (blk_queue_stopped(q))
967                 blk_start_queue(q);
968 }
969
970 static void start_queue(struct request_queue *q)
971 {
972         unsigned long flags;
973
974         spin_lock_irqsave(q->queue_lock, flags);
975         __start_queue(q);
976         spin_unlock_irqrestore(q->queue_lock, flags);
977 }
978
979 static void dm_done(struct request *clone, int error, bool mapped)
980 {
981         int r = error;
982         struct dm_rq_target_io *tio = clone->end_io_data;
983         dm_request_endio_fn rq_end_io = NULL;
984
985         if (tio->ti) {
986                 rq_end_io = tio->ti->type->rq_end_io;
987
988                 if (mapped && rq_end_io)
989                         r = rq_end_io(tio->ti, clone, error, &tio->info);
990         }
991
992         if (r <= 0)
993                 /* The target wants to complete the I/O */
994                 dm_end_request(clone, r);
995         else if (r == DM_ENDIO_INCOMPLETE)
996                 /* The target will handle the I/O */
997                 return;
998         else if (r == DM_ENDIO_REQUEUE)
999                 /* The target wants to requeue the I/O */
1000                 dm_requeue_unmapped_request(clone);
1001         else {
1002                 DMWARN("unimplemented target endio return value: %d", r);
1003                 BUG();
1004         }
1005 }
1006
1007 /*
1008  * Request completion handler for request-based dm
1009  */
1010 static void dm_softirq_done(struct request *rq)
1011 {
1012         bool mapped = true;
1013         struct request *clone = rq->completion_data;
1014         struct dm_rq_target_io *tio = clone->end_io_data;
1015
1016         if (rq->cmd_flags & REQ_FAILED)
1017                 mapped = false;
1018
1019         dm_done(clone, tio->error, mapped);
1020 }
1021
1022 /*
1023  * Complete the clone and the original request with the error status
1024  * through softirq context.
1025  */
1026 static void dm_complete_request(struct request *clone, int error)
1027 {
1028         struct dm_rq_target_io *tio = clone->end_io_data;
1029         struct request *rq = tio->orig;
1030
1031         tio->error = error;
1032         rq->completion_data = clone;
1033         blk_complete_request(rq);
1034 }
1035
1036 /*
1037  * Complete the not-mapped clone and the original request with the error status
1038  * through softirq context.
1039  * Target's rq_end_io() function isn't called.
1040  * This may be used when the target's map_rq() function fails.
1041  */
1042 void dm_kill_unmapped_request(struct request *clone, int error)
1043 {
1044         struct dm_rq_target_io *tio = clone->end_io_data;
1045         struct request *rq = tio->orig;
1046
1047         rq->cmd_flags |= REQ_FAILED;
1048         dm_complete_request(clone, error);
1049 }
1050 EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
1051
1052 /*
1053  * Called with the queue lock held
1054  */
1055 static void end_clone_request(struct request *clone, int error)
1056 {
1057         /*
1058          * For just cleaning up the information of the queue in which
1059          * the clone was dispatched.
1060          * The clone is *NOT* freed actually here because it is alloced from
1061          * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
1062          */
1063         __blk_put_request(clone->q, clone);
1064
1065         /*
1066          * Actual request completion is done in a softirq context which doesn't
1067          * hold the queue lock.  Otherwise, deadlock could occur because:
1068          *     - another request may be submitted by the upper level driver
1069          *       of the stacking during the completion
1070          *     - the submission which requires queue lock may be done
1071          *       against this queue
1072          */
1073         dm_complete_request(clone, error);
1074 }
1075
1076 /*
1077  * Return maximum size of I/O possible at the supplied sector up to the current
1078  * target boundary.
1079  */
1080 static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
1081 {
1082         sector_t target_offset = dm_target_offset(ti, sector);
1083
1084         return ti->len - target_offset;
1085 }
1086
1087 static sector_t max_io_len(sector_t sector, struct dm_target *ti)
1088 {
1089         sector_t len = max_io_len_target_boundary(sector, ti);
1090         sector_t offset, max_len;
1091
1092         /*
1093          * Does the target need to split even further?
1094          */
1095         if (ti->max_io_len) {
1096                 offset = dm_target_offset(ti, sector);
1097                 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1098                         max_len = sector_div(offset, ti->max_io_len);
1099                 else
1100                         max_len = offset & (ti->max_io_len - 1);
1101                 max_len = ti->max_io_len - max_len;
1102
1103                 if (len > max_len)
1104                         len = max_len;
1105         }
1106
1107         return len;
1108 }
1109
1110 int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1111 {
1112         if (len > UINT_MAX) {
1113                 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1114                       (unsigned long long)len, UINT_MAX);
1115                 ti->error = "Maximum size of target IO is too large";
1116                 return -EINVAL;
1117         }
1118
1119         ti->max_io_len = (uint32_t) len;
1120
1121         return 0;
1122 }
1123 EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1124
1125 static void __map_bio(struct dm_target_io *tio)
1126 {
1127         int r;
1128         sector_t sector;
1129         struct mapped_device *md;
1130         struct bio *clone = &tio->clone;
1131         struct dm_target *ti = tio->ti;
1132
1133         clone->bi_end_io = clone_endio;
1134         clone->bi_private = tio;
1135
1136         /*
1137          * Map the clone.  If r == 0 we don't need to do
1138          * anything, the target has assumed ownership of
1139          * this io.
1140          */
1141         atomic_inc(&tio->io->io_count);
1142         sector = clone->bi_iter.bi_sector;
1143         r = ti->type->map(ti, clone);
1144         if (r == DM_MAPIO_REMAPPED) {
1145                 /* the bio has been remapped so dispatch it */
1146
1147                 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1148                                       tio->io->bio->bi_bdev->bd_dev, sector);
1149
1150                 generic_make_request(clone);
1151         } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1152                 /* error the io and bail out, or requeue it if needed */
1153                 md = tio->io->md;
1154                 dec_pending(tio->io, r);
1155                 free_tio(md, tio);
1156         } else if (r) {
1157                 DMWARN("unimplemented target map return value: %d", r);
1158                 BUG();
1159         }
1160 }
1161
1162 struct clone_info {
1163         struct mapped_device *md;
1164         struct dm_table *map;
1165         struct bio *bio;
1166         struct dm_io *io;
1167         sector_t sector;
1168         sector_t sector_count;
1169 };
1170
1171 static void bio_setup_sector(struct bio *bio, sector_t sector, sector_t len)
1172 {
1173         bio->bi_iter.bi_sector = sector;
1174         bio->bi_iter.bi_size = to_bytes(len);
1175 }
1176
1177 /*
1178  * Creates a bio that consists of range of complete bvecs.
1179  */
1180 static void clone_bio(struct dm_target_io *tio, struct bio *bio,
1181                       sector_t sector, unsigned len)
1182 {
1183         struct bio *clone = &tio->clone;
1184
1185         __bio_clone_fast(clone, bio);
1186
1187         if (bio_integrity(bio))
1188                 bio_integrity_clone(clone, bio, GFP_NOIO);
1189
1190         bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
1191         clone->bi_iter.bi_size = to_bytes(len);
1192
1193         if (bio_integrity(bio))
1194                 bio_integrity_trim(clone, 0, len);
1195 }
1196
1197 static struct dm_target_io *alloc_tio(struct clone_info *ci,
1198                                       struct dm_target *ti, int nr_iovecs,
1199                                       unsigned target_bio_nr)
1200 {
1201         struct dm_target_io *tio;
1202         struct bio *clone;
1203
1204         clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, ci->md->bs);
1205         tio = container_of(clone, struct dm_target_io, clone);
1206
1207         tio->io = ci->io;
1208         tio->ti = ti;
1209         memset(&tio->info, 0, sizeof(tio->info));
1210         tio->target_bio_nr = target_bio_nr;
1211
1212         return tio;
1213 }
1214
1215 static void __clone_and_map_simple_bio(struct clone_info *ci,
1216                                        struct dm_target *ti,
1217                                        unsigned target_bio_nr, sector_t len)
1218 {
1219         struct dm_target_io *tio = alloc_tio(ci, ti, ci->bio->bi_max_vecs, target_bio_nr);
1220         struct bio *clone = &tio->clone;
1221
1222         /*
1223          * Discard requests require the bio's inline iovecs be initialized.
1224          * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
1225          * and discard, so no need for concern about wasted bvec allocations.
1226          */
1227          __bio_clone_fast(clone, ci->bio);
1228         if (len)
1229                 bio_setup_sector(clone, ci->sector, len);
1230
1231         __map_bio(tio);
1232 }
1233
1234 static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
1235                                   unsigned num_bios, sector_t len)
1236 {
1237         unsigned target_bio_nr;
1238
1239         for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
1240                 __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
1241 }
1242
1243 static int __send_empty_flush(struct clone_info *ci)
1244 {
1245         unsigned target_nr = 0;
1246         struct dm_target *ti;
1247
1248         BUG_ON(bio_has_data(ci->bio));
1249         while ((ti = dm_table_get_target(ci->map, target_nr++)))
1250                 __send_duplicate_bios(ci, ti, ti->num_flush_bios, 0);
1251
1252         return 0;
1253 }
1254
1255 static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
1256                                      sector_t sector, unsigned len)
1257 {
1258         struct bio *bio = ci->bio;
1259         struct dm_target_io *tio;
1260         unsigned target_bio_nr;
1261         unsigned num_target_bios = 1;
1262
1263         /*
1264          * Does the target want to receive duplicate copies of the bio?
1265          */
1266         if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1267                 num_target_bios = ti->num_write_bios(ti, bio);
1268
1269         for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
1270                 tio = alloc_tio(ci, ti, 0, target_bio_nr);
1271                 clone_bio(tio, bio, sector, len);
1272                 __map_bio(tio);
1273         }
1274 }
1275
1276 typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
1277
1278 static unsigned get_num_discard_bios(struct dm_target *ti)
1279 {
1280         return ti->num_discard_bios;
1281 }
1282
1283 static unsigned get_num_write_same_bios(struct dm_target *ti)
1284 {
1285         return ti->num_write_same_bios;
1286 }
1287
1288 typedef bool (*is_split_required_fn)(struct dm_target *ti);
1289
1290 static bool is_split_required_for_discard(struct dm_target *ti)
1291 {
1292         return ti->split_discard_bios;
1293 }
1294
1295 static int __send_changing_extent_only(struct clone_info *ci,
1296                                        get_num_bios_fn get_num_bios,
1297                                        is_split_required_fn is_split_required)
1298 {
1299         struct dm_target *ti;
1300         sector_t len;
1301         unsigned num_bios;
1302
1303         do {
1304                 ti = dm_table_find_target(ci->map, ci->sector);
1305                 if (!dm_target_is_valid(ti))
1306                         return -EIO;
1307
1308                 /*
1309                  * Even though the device advertised support for this type of
1310                  * request, that does not mean every target supports it, and
1311                  * reconfiguration might also have changed that since the
1312                  * check was performed.
1313                  */
1314                 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1315                 if (!num_bios)
1316                         return -EOPNOTSUPP;
1317
1318                 if (is_split_required && !is_split_required(ti))
1319                         len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
1320                 else
1321                         len = min(ci->sector_count, max_io_len(ci->sector, ti));
1322
1323                 __send_duplicate_bios(ci, ti, num_bios, len);
1324
1325                 ci->sector += len;
1326         } while (ci->sector_count -= len);
1327
1328         return 0;
1329 }
1330
1331 static int __send_discard(struct clone_info *ci)
1332 {
1333         return __send_changing_extent_only(ci, get_num_discard_bios,
1334                                            is_split_required_for_discard);
1335 }
1336
1337 static int __send_write_same(struct clone_info *ci)
1338 {
1339         return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
1340 }
1341
1342 /*
1343  * Select the correct strategy for processing a non-flush bio.
1344  */
1345 static int __split_and_process_non_flush(struct clone_info *ci)
1346 {
1347         struct bio *bio = ci->bio;
1348         struct dm_target *ti;
1349         unsigned len;
1350
1351         if (unlikely(bio->bi_rw & REQ_DISCARD))
1352                 return __send_discard(ci);
1353         else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
1354                 return __send_write_same(ci);
1355
1356         ti = dm_table_find_target(ci->map, ci->sector);
1357         if (!dm_target_is_valid(ti))
1358                 return -EIO;
1359
1360         len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
1361
1362         __clone_and_map_data_bio(ci, ti, ci->sector, len);
1363
1364         ci->sector += len;
1365         ci->sector_count -= len;
1366
1367         return 0;
1368 }
1369
1370 /*
1371  * Entry point to split a bio into clones and submit them to the targets.
1372  */
1373 static void __split_and_process_bio(struct mapped_device *md,
1374                                     struct dm_table *map, struct bio *bio)
1375 {
1376         struct clone_info ci;
1377         int error = 0;
1378
1379         if (unlikely(!map)) {
1380                 bio_io_error(bio);
1381                 return;
1382         }
1383
1384         ci.map = map;
1385         ci.md = md;
1386         ci.io = alloc_io(md);
1387         ci.io->error = 0;
1388         atomic_set(&ci.io->io_count, 1);
1389         ci.io->bio = bio;
1390         ci.io->md = md;
1391         spin_lock_init(&ci.io->endio_lock);
1392         ci.sector = bio->bi_iter.bi_sector;
1393
1394         start_io_acct(ci.io);
1395
1396         if (bio->bi_rw & REQ_FLUSH) {
1397                 ci.bio = &ci.md->flush_bio;
1398                 ci.sector_count = 0;
1399                 error = __send_empty_flush(&ci);
1400                 /* dec_pending submits any data associated with flush */
1401         } else {
1402                 ci.bio = bio;
1403                 ci.sector_count = bio_sectors(bio);
1404                 while (ci.sector_count && !error)
1405                         error = __split_and_process_non_flush(&ci);
1406         }
1407
1408         /* drop the extra reference count */
1409         dec_pending(ci.io, error);
1410 }
1411 /*-----------------------------------------------------------------
1412  * CRUD END
1413  *---------------------------------------------------------------*/
1414
1415 static int dm_merge_bvec(struct request_queue *q,
1416                          struct bvec_merge_data *bvm,
1417                          struct bio_vec *biovec)
1418 {
1419         struct mapped_device *md = q->queuedata;
1420         struct dm_table *map = dm_get_live_table_fast(md);
1421         struct dm_target *ti;
1422         sector_t max_sectors;
1423         int max_size = 0;
1424
1425         if (unlikely(!map))
1426                 goto out;
1427
1428         ti = dm_table_find_target(map, bvm->bi_sector);
1429         if (!dm_target_is_valid(ti))
1430                 goto out;
1431
1432         /*
1433          * Find maximum amount of I/O that won't need splitting
1434          */
1435         max_sectors = min(max_io_len(bvm->bi_sector, ti),
1436                           (sector_t) BIO_MAX_SECTORS);
1437         max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1438         if (max_size < 0)
1439                 max_size = 0;
1440
1441         /*
1442          * merge_bvec_fn() returns number of bytes
1443          * it can accept at this offset
1444          * max is precomputed maximal io size
1445          */
1446         if (max_size && ti->type->merge)
1447                 max_size = ti->type->merge(ti, bvm, biovec, max_size);
1448         /*
1449          * If the target doesn't support merge method and some of the devices
1450          * provided their merge_bvec method (we know this by looking at
1451          * queue_max_hw_sectors), then we can't allow bios with multiple vector
1452          * entries.  So always set max_size to 0, and the code below allows
1453          * just one page.
1454          */
1455         else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1456
1457                 max_size = 0;
1458
1459 out:
1460         dm_put_live_table_fast(md);
1461         /*
1462          * Always allow an entire first page
1463          */
1464         if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1465                 max_size = biovec->bv_len;
1466
1467         return max_size;
1468 }
1469
1470 /*
1471  * The request function that just remaps the bio built up by
1472  * dm_merge_bvec.
1473  */
1474 static void _dm_request(struct request_queue *q, struct bio *bio)
1475 {
1476         int rw = bio_data_dir(bio);
1477         struct mapped_device *md = q->queuedata;
1478         int cpu;
1479         int srcu_idx;
1480         struct dm_table *map;
1481
1482         map = dm_get_live_table(md, &srcu_idx);
1483
1484         cpu = part_stat_lock();
1485         part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1486         part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1487         part_stat_unlock();
1488
1489         /* if we're suspended, we have to queue this io for later */
1490         if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
1491                 dm_put_live_table(md, srcu_idx);
1492
1493                 if (bio_rw(bio) != READA)
1494                         queue_io(md, bio);
1495                 else
1496                         bio_io_error(bio);
1497                 return;
1498         }
1499
1500         __split_and_process_bio(md, map, bio);
1501         dm_put_live_table(md, srcu_idx);
1502         return;
1503 }
1504
1505 int dm_request_based(struct mapped_device *md)
1506 {
1507         return blk_queue_stackable(md->queue);
1508 }
1509
1510 static void dm_request(struct request_queue *q, struct bio *bio)
1511 {
1512         struct mapped_device *md = q->queuedata;
1513
1514         if (dm_request_based(md))
1515                 blk_queue_bio(q, bio);
1516         else
1517                 _dm_request(q, bio);
1518 }
1519
1520 void dm_dispatch_request(struct request *rq)
1521 {
1522         int r;
1523
1524         if (blk_queue_io_stat(rq->q))
1525                 rq->cmd_flags |= REQ_IO_STAT;
1526
1527         rq->start_time = jiffies;
1528         r = blk_insert_cloned_request(rq->q, rq);
1529         if (r)
1530                 dm_complete_request(rq, r);
1531 }
1532 EXPORT_SYMBOL_GPL(dm_dispatch_request);
1533
1534 static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1535                                  void *data)
1536 {
1537         struct dm_rq_target_io *tio = data;
1538         struct dm_rq_clone_bio_info *info =
1539                 container_of(bio, struct dm_rq_clone_bio_info, clone);
1540
1541         info->orig = bio_orig;
1542         info->tio = tio;
1543         bio->bi_end_io = end_clone_bio;
1544         bio->bi_private = info;
1545
1546         return 0;
1547 }
1548
1549 static int setup_clone(struct request *clone, struct request *rq,
1550                        struct dm_rq_target_io *tio)
1551 {
1552         int r;
1553
1554         r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1555                               dm_rq_bio_constructor, tio);
1556         if (r)
1557                 return r;
1558
1559         clone->cmd = rq->cmd;
1560         clone->cmd_len = rq->cmd_len;
1561         clone->sense = rq->sense;
1562         clone->buffer = rq->buffer;
1563         clone->end_io = end_clone_request;
1564         clone->end_io_data = tio;
1565
1566         return 0;
1567 }
1568
1569 static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1570                                 gfp_t gfp_mask)
1571 {
1572         struct request *clone;
1573         struct dm_rq_target_io *tio;
1574
1575         tio = alloc_rq_tio(md, gfp_mask);
1576         if (!tio)
1577                 return NULL;
1578
1579         tio->md = md;
1580         tio->ti = NULL;
1581         tio->orig = rq;
1582         tio->error = 0;
1583         memset(&tio->info, 0, sizeof(tio->info));
1584
1585         clone = &tio->clone;
1586         if (setup_clone(clone, rq, tio)) {
1587                 /* -ENOMEM */
1588                 free_rq_tio(tio);
1589                 return NULL;
1590         }
1591
1592         return clone;
1593 }
1594
1595 /*
1596  * Called with the queue lock held.
1597  */
1598 static int dm_prep_fn(struct request_queue *q, struct request *rq)
1599 {
1600         struct mapped_device *md = q->queuedata;
1601         struct request *clone;
1602
1603         if (unlikely(rq->special)) {
1604                 DMWARN("Already has something in rq->special.");
1605                 return BLKPREP_KILL;
1606         }
1607
1608         clone = clone_rq(rq, md, GFP_ATOMIC);
1609         if (!clone)
1610                 return BLKPREP_DEFER;
1611
1612         rq->special = clone;
1613         rq->cmd_flags |= REQ_DONTPREP;
1614
1615         return BLKPREP_OK;
1616 }
1617
1618 /*
1619  * Returns:
1620  * 0  : the request has been processed (not requeued)
1621  * !0 : the request has been requeued
1622  */
1623 static int map_request(struct dm_target *ti, struct request *clone,
1624                        struct mapped_device *md)
1625 {
1626         int r, requeued = 0;
1627         struct dm_rq_target_io *tio = clone->end_io_data;
1628
1629         tio->ti = ti;
1630         r = ti->type->map_rq(ti, clone, &tio->info);
1631         switch (r) {
1632         case DM_MAPIO_SUBMITTED:
1633                 /* The target has taken the I/O to submit by itself later */
1634                 break;
1635         case DM_MAPIO_REMAPPED:
1636                 /* The target has remapped the I/O so dispatch it */
1637                 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1638                                      blk_rq_pos(tio->orig));
1639                 dm_dispatch_request(clone);
1640                 break;
1641         case DM_MAPIO_REQUEUE:
1642                 /* The target wants to requeue the I/O */
1643                 dm_requeue_unmapped_request(clone);
1644                 requeued = 1;
1645                 break;
1646         default:
1647                 if (r > 0) {
1648                         DMWARN("unimplemented target map return value: %d", r);
1649                         BUG();
1650                 }
1651
1652                 /* The target wants to complete the I/O */
1653                 dm_kill_unmapped_request(clone, r);
1654                 break;
1655         }
1656
1657         return requeued;
1658 }
1659
1660 static struct request *dm_start_request(struct mapped_device *md, struct request *orig)
1661 {
1662         struct request *clone;
1663
1664         blk_start_request(orig);
1665         clone = orig->special;
1666         atomic_inc(&md->pending[rq_data_dir(clone)]);
1667
1668         /*
1669          * Hold the md reference here for the in-flight I/O.
1670          * We can't rely on the reference count by device opener,
1671          * because the device may be closed during the request completion
1672          * when all bios are completed.
1673          * See the comment in rq_completed() too.
1674          */
1675         dm_get(md);
1676
1677         return clone;
1678 }
1679
1680 /*
1681  * q->request_fn for request-based dm.
1682  * Called with the queue lock held.
1683  */
1684 static void dm_request_fn(struct request_queue *q)
1685 {
1686         struct mapped_device *md = q->queuedata;
1687         int srcu_idx;
1688         struct dm_table *map = dm_get_live_table(md, &srcu_idx);
1689         struct dm_target *ti;
1690         struct request *rq, *clone;
1691         sector_t pos;
1692
1693         /*
1694          * For suspend, check blk_queue_stopped() and increment
1695          * ->pending within a single queue_lock not to increment the
1696          * number of in-flight I/Os after the queue is stopped in
1697          * dm_suspend().
1698          */
1699         while (!blk_queue_stopped(q)) {
1700                 rq = blk_peek_request(q);
1701                 if (!rq)
1702                         goto delay_and_out;
1703
1704                 /* always use block 0 to find the target for flushes for now */
1705                 pos = 0;
1706                 if (!(rq->cmd_flags & REQ_FLUSH))
1707                         pos = blk_rq_pos(rq);
1708
1709                 ti = dm_table_find_target(map, pos);
1710                 if (!dm_target_is_valid(ti)) {
1711                         /*
1712                          * Must perform setup, that dm_done() requires,
1713                          * before calling dm_kill_unmapped_request
1714                          */
1715                         DMERR_LIMIT("request attempted access beyond the end of device");
1716                         clone = dm_start_request(md, rq);
1717                         dm_kill_unmapped_request(clone, -EIO);
1718                         continue;
1719                 }
1720
1721                 if (ti->type->busy && ti->type->busy(ti))
1722                         goto delay_and_out;
1723
1724                 clone = dm_start_request(md, rq);
1725
1726                 spin_unlock(q->queue_lock);
1727                 if (map_request(ti, clone, md))
1728                         goto requeued;
1729
1730                 BUG_ON(!irqs_disabled());
1731                 spin_lock(q->queue_lock);
1732         }
1733
1734         goto out;
1735
1736 requeued:
1737         BUG_ON(!irqs_disabled());
1738         spin_lock(q->queue_lock);
1739
1740 delay_and_out:
1741         blk_delay_queue(q, HZ / 10);
1742 out:
1743         dm_put_live_table(md, srcu_idx);
1744 }
1745
1746 int dm_underlying_device_busy(struct request_queue *q)
1747 {
1748         return blk_lld_busy(q);
1749 }
1750 EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1751
1752 static int dm_lld_busy(struct request_queue *q)
1753 {
1754         int r;
1755         struct mapped_device *md = q->queuedata;
1756         struct dm_table *map = dm_get_live_table_fast(md);
1757
1758         if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1759                 r = 1;
1760         else
1761                 r = dm_table_any_busy_target(map);
1762
1763         dm_put_live_table_fast(md);
1764
1765         return r;
1766 }
1767
1768 static int dm_any_congested(void *congested_data, int bdi_bits)
1769 {
1770         int r = bdi_bits;
1771         struct mapped_device *md = congested_data;
1772         struct dm_table *map;
1773
1774         if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
1775                 map = dm_get_live_table_fast(md);
1776                 if (map) {
1777                         /*
1778                          * Request-based dm cares about only own queue for
1779                          * the query about congestion status of request_queue
1780                          */
1781                         if (dm_request_based(md))
1782                                 r = md->queue->backing_dev_info.state &
1783                                     bdi_bits;
1784                         else
1785                                 r = dm_table_any_congested(map, bdi_bits);
1786                 }
1787                 dm_put_live_table_fast(md);
1788         }
1789
1790         return r;
1791 }
1792
1793 /*-----------------------------------------------------------------
1794  * An IDR is used to keep track of allocated minor numbers.
1795  *---------------------------------------------------------------*/
1796 static void free_minor(int minor)
1797 {
1798         spin_lock(&_minor_lock);
1799         idr_remove(&_minor_idr, minor);
1800         spin_unlock(&_minor_lock);
1801 }
1802
1803 /*
1804  * See if the device with a specific minor # is free.
1805  */
1806 static int specific_minor(int minor)
1807 {
1808         int r;
1809
1810         if (minor >= (1 << MINORBITS))
1811                 return -EINVAL;
1812
1813         idr_preload(GFP_KERNEL);
1814         spin_lock(&_minor_lock);
1815
1816         r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
1817
1818         spin_unlock(&_minor_lock);
1819         idr_preload_end();
1820         if (r < 0)
1821                 return r == -ENOSPC ? -EBUSY : r;
1822         return 0;
1823 }
1824
1825 static int next_free_minor(int *minor)
1826 {
1827         int r;
1828
1829         idr_preload(GFP_KERNEL);
1830         spin_lock(&_minor_lock);
1831
1832         r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
1833
1834         spin_unlock(&_minor_lock);
1835         idr_preload_end();
1836         if (r < 0)
1837                 return r;
1838         *minor = r;
1839         return 0;
1840 }
1841
1842 static const struct block_device_operations dm_blk_dops;
1843
1844 static void dm_wq_work(struct work_struct *work);
1845
1846 static void dm_init_md_queue(struct mapped_device *md)
1847 {
1848         /*
1849          * Request-based dm devices cannot be stacked on top of bio-based dm
1850          * devices.  The type of this dm device has not been decided yet.
1851          * The type is decided at the first table loading time.
1852          * To prevent problematic device stacking, clear the queue flag
1853          * for request stacking support until then.
1854          *
1855          * This queue is new, so no concurrency on the queue_flags.
1856          */
1857         queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1858
1859         md->queue->queuedata = md;
1860         md->queue->backing_dev_info.congested_fn = dm_any_congested;
1861         md->queue->backing_dev_info.congested_data = md;
1862         blk_queue_make_request(md->queue, dm_request);
1863         blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
1864         blk_queue_merge_bvec(md->queue, dm_merge_bvec);
1865 }
1866
1867 /*
1868  * Allocate and initialise a blank device with a given minor.
1869  */
1870 static struct mapped_device *alloc_dev(int minor)
1871 {
1872         int r;
1873         struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
1874         void *old_md;
1875
1876         if (!md) {
1877                 DMWARN("unable to allocate device, out of memory.");
1878                 return NULL;
1879         }
1880
1881         if (!try_module_get(THIS_MODULE))
1882                 goto bad_module_get;
1883
1884         /* get a minor number for the dev */
1885         if (minor == DM_ANY_MINOR)
1886                 r = next_free_minor(&minor);
1887         else
1888                 r = specific_minor(minor);
1889         if (r < 0)
1890                 goto bad_minor;
1891
1892         r = init_srcu_struct(&md->io_barrier);
1893         if (r < 0)
1894                 goto bad_io_barrier;
1895
1896         md->type = DM_TYPE_NONE;
1897         mutex_init(&md->suspend_lock);
1898         mutex_init(&md->type_lock);
1899         spin_lock_init(&md->deferred_lock);
1900         atomic_set(&md->holders, 1);
1901         atomic_set(&md->open_count, 0);
1902         atomic_set(&md->event_nr, 0);
1903         atomic_set(&md->uevent_seq, 0);
1904         INIT_LIST_HEAD(&md->uevent_list);
1905         spin_lock_init(&md->uevent_lock);
1906
1907         md->queue = blk_alloc_queue(GFP_KERNEL);
1908         if (!md->queue)
1909                 goto bad_queue;
1910
1911         dm_init_md_queue(md);
1912
1913         md->disk = alloc_disk(1);
1914         if (!md->disk)
1915                 goto bad_disk;
1916
1917         atomic_set(&md->pending[0], 0);
1918         atomic_set(&md->pending[1], 0);
1919         init_waitqueue_head(&md->wait);
1920         INIT_WORK(&md->work, dm_wq_work);
1921         init_waitqueue_head(&md->eventq);
1922         init_completion(&md->kobj_holder.completion);
1923
1924         md->disk->major = _major;
1925         md->disk->first_minor = minor;
1926         md->disk->fops = &dm_blk_dops;
1927         md->disk->queue = md->queue;
1928         md->disk->private_data = md;
1929         sprintf(md->disk->disk_name, "dm-%d", minor);
1930         add_disk(md->disk);
1931         format_dev_t(md->name, MKDEV(_major, minor));
1932
1933         md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
1934         if (!md->wq)
1935                 goto bad_thread;
1936
1937         md->bdev = bdget_disk(md->disk, 0);
1938         if (!md->bdev)
1939                 goto bad_bdev;
1940
1941         bio_init(&md->flush_bio);
1942         md->flush_bio.bi_bdev = md->bdev;
1943         md->flush_bio.bi_rw = WRITE_FLUSH;
1944
1945         dm_stats_init(&md->stats);
1946
1947         /* Populate the mapping, nobody knows we exist yet */
1948         spin_lock(&_minor_lock);
1949         old_md = idr_replace(&_minor_idr, md, minor);
1950         spin_unlock(&_minor_lock);
1951
1952         BUG_ON(old_md != MINOR_ALLOCED);
1953
1954         return md;
1955
1956 bad_bdev:
1957         destroy_workqueue(md->wq);
1958 bad_thread:
1959         del_gendisk(md->disk);
1960         put_disk(md->disk);
1961 bad_disk:
1962         blk_cleanup_queue(md->queue);
1963 bad_queue:
1964         cleanup_srcu_struct(&md->io_barrier);
1965 bad_io_barrier:
1966         free_minor(minor);
1967 bad_minor:
1968         module_put(THIS_MODULE);
1969 bad_module_get:
1970         kfree(md);
1971         return NULL;
1972 }
1973
1974 static void unlock_fs(struct mapped_device *md);
1975
1976 static void free_dev(struct mapped_device *md)
1977 {
1978         int minor = MINOR(disk_devt(md->disk));
1979
1980         unlock_fs(md);
1981         bdput(md->bdev);
1982         destroy_workqueue(md->wq);
1983         if (md->io_pool)
1984                 mempool_destroy(md->io_pool);
1985         if (md->bs)
1986                 bioset_free(md->bs);
1987         blk_integrity_unregister(md->disk);
1988         del_gendisk(md->disk);
1989         cleanup_srcu_struct(&md->io_barrier);
1990         free_minor(minor);
1991
1992         spin_lock(&_minor_lock);
1993         md->disk->private_data = NULL;
1994         spin_unlock(&_minor_lock);
1995
1996         put_disk(md->disk);
1997         blk_cleanup_queue(md->queue);
1998         dm_stats_cleanup(&md->stats);
1999         module_put(THIS_MODULE);
2000         kfree(md);
2001 }
2002
2003 static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2004 {
2005         struct dm_md_mempools *p = dm_table_get_md_mempools(t);
2006
2007         if (md->io_pool && md->bs) {
2008                 /* The md already has necessary mempools. */
2009                 if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
2010                         /*
2011                          * Reload bioset because front_pad may have changed
2012                          * because a different table was loaded.
2013                          */
2014                         bioset_free(md->bs);
2015                         md->bs = p->bs;
2016                         p->bs = NULL;
2017                 } else if (dm_table_get_type(t) == DM_TYPE_REQUEST_BASED) {
2018                         /*
2019                          * There's no need to reload with request-based dm
2020                          * because the size of front_pad doesn't change.
2021                          * Note for future: If you are to reload bioset,
2022                          * prep-ed requests in the queue may refer
2023                          * to bio from the old bioset, so you must walk
2024                          * through the queue to unprep.
2025                          */
2026                 }
2027                 goto out;
2028         }
2029
2030         BUG_ON(!p || md->io_pool || md->bs);
2031
2032         md->io_pool = p->io_pool;
2033         p->io_pool = NULL;
2034         md->bs = p->bs;
2035         p->bs = NULL;
2036
2037 out:
2038         /* mempool bind completed, now no need any mempools in the table */
2039         dm_table_free_md_mempools(t);
2040 }
2041
2042 /*
2043  * Bind a table to the device.
2044  */
2045 static void event_callback(void *context)
2046 {
2047         unsigned long flags;
2048         LIST_HEAD(uevents);
2049         struct mapped_device *md = (struct mapped_device *) context;
2050
2051         spin_lock_irqsave(&md->uevent_lock, flags);
2052         list_splice_init(&md->uevent_list, &uevents);
2053         spin_unlock_irqrestore(&md->uevent_lock, flags);
2054
2055         dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
2056
2057         atomic_inc(&md->event_nr);
2058         wake_up(&md->eventq);
2059 }
2060
2061 /*
2062  * Protected by md->suspend_lock obtained by dm_swap_table().
2063  */
2064 static void __set_size(struct mapped_device *md, sector_t size)
2065 {
2066         set_capacity(md->disk, size);
2067
2068         i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
2069 }
2070
2071 /*
2072  * Return 1 if the queue has a compulsory merge_bvec_fn function.
2073  *
2074  * If this function returns 0, then the device is either a non-dm
2075  * device without a merge_bvec_fn, or it is a dm device that is
2076  * able to split any bios it receives that are too big.
2077  */
2078 int dm_queue_merge_is_compulsory(struct request_queue *q)
2079 {
2080         struct mapped_device *dev_md;
2081
2082         if (!q->merge_bvec_fn)
2083                 return 0;
2084
2085         if (q->make_request_fn == dm_request) {
2086                 dev_md = q->queuedata;
2087                 if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
2088                         return 0;
2089         }
2090
2091         return 1;
2092 }
2093
2094 static int dm_device_merge_is_compulsory(struct dm_target *ti,
2095                                          struct dm_dev *dev, sector_t start,
2096                                          sector_t len, void *data)
2097 {
2098         struct block_device *bdev = dev->bdev;
2099         struct request_queue *q = bdev_get_queue(bdev);
2100
2101         return dm_queue_merge_is_compulsory(q);
2102 }
2103
2104 /*
2105  * Return 1 if it is acceptable to ignore merge_bvec_fn based
2106  * on the properties of the underlying devices.
2107  */
2108 static int dm_table_merge_is_optional(struct dm_table *table)
2109 {
2110         unsigned i = 0;
2111         struct dm_target *ti;
2112
2113         while (i < dm_table_get_num_targets(table)) {
2114                 ti = dm_table_get_target(table, i++);
2115
2116                 if (ti->type->iterate_devices &&
2117                     ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
2118                         return 0;
2119         }
2120
2121         return 1;
2122 }
2123
2124 /*
2125  * Returns old map, which caller must destroy.
2126  */
2127 static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2128                                struct queue_limits *limits)
2129 {
2130         struct dm_table *old_map;
2131         struct request_queue *q = md->queue;
2132         sector_t size;
2133         int merge_is_optional;
2134
2135         size = dm_table_get_size(t);
2136
2137         /*
2138          * Wipe any geometry if the size of the table changed.
2139          */
2140         if (size != dm_get_size(md))
2141                 memset(&md->geometry, 0, sizeof(md->geometry));
2142
2143         __set_size(md, size);
2144
2145         dm_table_event_callback(t, event_callback, md);
2146
2147         /*
2148          * The queue hasn't been stopped yet, if the old table type wasn't
2149          * for request-based during suspension.  So stop it to prevent
2150          * I/O mapping before resume.
2151          * This must be done before setting the queue restrictions,
2152          * because request-based dm may be run just after the setting.
2153          */
2154         if (dm_table_request_based(t) && !blk_queue_stopped(q))
2155                 stop_queue(q);
2156
2157         __bind_mempools(md, t);
2158
2159         merge_is_optional = dm_table_merge_is_optional(t);
2160
2161         old_map = md->map;
2162         rcu_assign_pointer(md->map, t);
2163         md->immutable_target_type = dm_table_get_immutable_target_type(t);
2164
2165         dm_table_set_restrictions(t, q, limits);
2166         if (merge_is_optional)
2167                 set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2168         else
2169                 clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2170         dm_sync_table(md);
2171
2172         return old_map;
2173 }
2174
2175 /*
2176  * Returns unbound table for the caller to free.
2177  */
2178 static struct dm_table *__unbind(struct mapped_device *md)
2179 {
2180         struct dm_table *map = md->map;
2181
2182         if (!map)
2183                 return NULL;
2184
2185         dm_table_event_callback(map, NULL, NULL);
2186         rcu_assign_pointer(md->map, NULL);
2187         dm_sync_table(md);
2188
2189         return map;
2190 }
2191
2192 /*
2193  * Constructor for a new device.
2194  */
2195 int dm_create(int minor, struct mapped_device **result)
2196 {
2197         struct mapped_device *md;
2198
2199         md = alloc_dev(minor);
2200         if (!md)
2201                 return -ENXIO;
2202
2203         dm_sysfs_init(md);
2204
2205         *result = md;
2206         return 0;
2207 }
2208
2209 /*
2210  * Functions to manage md->type.
2211  * All are required to hold md->type_lock.
2212  */
2213 void dm_lock_md_type(struct mapped_device *md)
2214 {
2215         mutex_lock(&md->type_lock);
2216 }
2217
2218 void dm_unlock_md_type(struct mapped_device *md)
2219 {
2220         mutex_unlock(&md->type_lock);
2221 }
2222
2223 void dm_set_md_type(struct mapped_device *md, unsigned type)
2224 {
2225         BUG_ON(!mutex_is_locked(&md->type_lock));
2226         md->type = type;
2227 }
2228
2229 unsigned dm_get_md_type(struct mapped_device *md)
2230 {
2231         BUG_ON(!mutex_is_locked(&md->type_lock));
2232         return md->type;
2233 }
2234
2235 struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2236 {
2237         return md->immutable_target_type;
2238 }
2239
2240 /*
2241  * The queue_limits are only valid as long as you have a reference
2242  * count on 'md'.
2243  */
2244 struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2245 {
2246         BUG_ON(!atomic_read(&md->holders));
2247         return &md->queue->limits;
2248 }
2249 EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2250
2251 /*
2252  * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2253  */
2254 static int dm_init_request_based_queue(struct mapped_device *md)
2255 {
2256         struct request_queue *q = NULL;
2257
2258         if (md->queue->elevator)
2259                 return 1;
2260
2261         /* Fully initialize the queue */
2262         q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2263         if (!q)
2264                 return 0;
2265
2266         md->queue = q;
2267         dm_init_md_queue(md);
2268         blk_queue_softirq_done(md->queue, dm_softirq_done);
2269         blk_queue_prep_rq(md->queue, dm_prep_fn);
2270         blk_queue_lld_busy(md->queue, dm_lld_busy);
2271
2272         elv_register_queue(md->queue);
2273
2274         return 1;
2275 }
2276
2277 /*
2278  * Setup the DM device's queue based on md's type
2279  */
2280 int dm_setup_md_queue(struct mapped_device *md)
2281 {
2282         if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
2283             !dm_init_request_based_queue(md)) {
2284                 DMWARN("Cannot initialize queue for request-based mapped device");
2285                 return -EINVAL;
2286         }
2287
2288         return 0;
2289 }
2290
2291 static struct mapped_device *dm_find_md(dev_t dev)
2292 {
2293         struct mapped_device *md;
2294         unsigned minor = MINOR(dev);
2295
2296         if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2297                 return NULL;
2298
2299         spin_lock(&_minor_lock);
2300
2301         md = idr_find(&_minor_idr, minor);
2302         if (md && (md == MINOR_ALLOCED ||
2303                    (MINOR(disk_devt(dm_disk(md))) != minor) ||
2304                    dm_deleting_md(md) ||
2305                    test_bit(DMF_FREEING, &md->flags))) {
2306                 md = NULL;
2307                 goto out;
2308         }
2309
2310 out:
2311         spin_unlock(&_minor_lock);
2312
2313         return md;
2314 }
2315
2316 struct mapped_device *dm_get_md(dev_t dev)
2317 {
2318         struct mapped_device *md = dm_find_md(dev);
2319
2320         if (md)
2321                 dm_get(md);
2322
2323         return md;
2324 }
2325 EXPORT_SYMBOL_GPL(dm_get_md);
2326
2327 void *dm_get_mdptr(struct mapped_device *md)
2328 {
2329         return md->interface_ptr;
2330 }
2331
2332 void dm_set_mdptr(struct mapped_device *md, void *ptr)
2333 {
2334         md->interface_ptr = ptr;
2335 }
2336
2337 void dm_get(struct mapped_device *md)
2338 {
2339         atomic_inc(&md->holders);
2340         BUG_ON(test_bit(DMF_FREEING, &md->flags));
2341 }
2342
2343 const char *dm_device_name(struct mapped_device *md)
2344 {
2345         return md->name;
2346 }
2347 EXPORT_SYMBOL_GPL(dm_device_name);
2348
2349 static void __dm_destroy(struct mapped_device *md, bool wait)
2350 {
2351         struct dm_table *map;
2352         int srcu_idx;
2353
2354         might_sleep();
2355
2356         spin_lock(&_minor_lock);
2357         map = dm_get_live_table(md, &srcu_idx);
2358         idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2359         set_bit(DMF_FREEING, &md->flags);
2360         spin_unlock(&_minor_lock);
2361
2362         if (!dm_suspended_md(md)) {
2363                 dm_table_presuspend_targets(map);
2364                 dm_table_postsuspend_targets(map);
2365         }
2366
2367         /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2368         dm_put_live_table(md, srcu_idx);
2369
2370         /*
2371          * Rare, but there may be I/O requests still going to complete,
2372          * for example.  Wait for all references to disappear.
2373          * No one should increment the reference count of the mapped_device,
2374          * after the mapped_device state becomes DMF_FREEING.
2375          */
2376         if (wait)
2377                 while (atomic_read(&md->holders))
2378                         msleep(1);
2379         else if (atomic_read(&md->holders))
2380                 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2381                        dm_device_name(md), atomic_read(&md->holders));
2382
2383         dm_sysfs_exit(md);
2384         dm_table_destroy(__unbind(md));
2385         free_dev(md);
2386 }
2387
2388 void dm_destroy(struct mapped_device *md)
2389 {
2390         __dm_destroy(md, true);
2391 }
2392
2393 void dm_destroy_immediate(struct mapped_device *md)
2394 {
2395         __dm_destroy(md, false);
2396 }
2397
2398 void dm_put(struct mapped_device *md)
2399 {
2400         atomic_dec(&md->holders);
2401 }
2402 EXPORT_SYMBOL_GPL(dm_put);
2403
2404 static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
2405 {
2406         int r = 0;
2407         DECLARE_WAITQUEUE(wait, current);
2408
2409         add_wait_queue(&md->wait, &wait);
2410
2411         while (1) {
2412                 set_current_state(interruptible);
2413
2414                 if (!md_in_flight(md))
2415                         break;
2416
2417                 if (interruptible == TASK_INTERRUPTIBLE &&
2418                     signal_pending(current)) {
2419                         r = -EINTR;
2420                         break;
2421                 }
2422
2423                 io_schedule();
2424         }
2425         set_current_state(TASK_RUNNING);
2426
2427         remove_wait_queue(&md->wait, &wait);
2428
2429         return r;
2430 }
2431
2432 /*
2433  * Process the deferred bios
2434  */
2435 static void dm_wq_work(struct work_struct *work)
2436 {
2437         struct mapped_device *md = container_of(work, struct mapped_device,
2438                                                 work);
2439         struct bio *c;
2440         int srcu_idx;
2441         struct dm_table *map;
2442
2443         map = dm_get_live_table(md, &srcu_idx);
2444
2445         while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2446                 spin_lock_irq(&md->deferred_lock);
2447                 c = bio_list_pop(&md->deferred);
2448                 spin_unlock_irq(&md->deferred_lock);
2449
2450                 if (!c)
2451                         break;
2452
2453                 if (dm_request_based(md))
2454                         generic_make_request(c);
2455                 else
2456                         __split_and_process_bio(md, map, c);
2457         }
2458
2459         dm_put_live_table(md, srcu_idx);
2460 }
2461
2462 static void dm_queue_flush(struct mapped_device *md)
2463 {
2464         clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2465         smp_mb__after_clear_bit();
2466         queue_work(md->wq, &md->work);
2467 }
2468
2469 /*
2470  * Swap in a new table, returning the old one for the caller to destroy.
2471  */
2472 struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
2473 {
2474         struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2475         struct queue_limits limits;
2476         int r;
2477
2478         mutex_lock(&md->suspend_lock);
2479
2480         /* device must be suspended */
2481         if (!dm_suspended_md(md))
2482                 goto out;
2483
2484         /*
2485          * If the new table has no data devices, retain the existing limits.
2486          * This helps multipath with queue_if_no_path if all paths disappear,
2487          * then new I/O is queued based on these limits, and then some paths
2488          * reappear.
2489          */
2490         if (dm_table_has_no_data_devices(table)) {
2491                 live_map = dm_get_live_table_fast(md);
2492                 if (live_map)
2493                         limits = md->queue->limits;
2494                 dm_put_live_table_fast(md);
2495         }
2496
2497         if (!live_map) {
2498                 r = dm_calculate_queue_limits(table, &limits);
2499                 if (r) {
2500                         map = ERR_PTR(r);
2501                         goto out;
2502                 }
2503         }
2504
2505         map = __bind(md, table, &limits);
2506
2507 out:
2508         mutex_unlock(&md->suspend_lock);
2509         return map;
2510 }
2511
2512 /*
2513  * Functions to lock and unlock any filesystem running on the
2514  * device.
2515  */
2516 static int lock_fs(struct mapped_device *md)
2517 {
2518         int r;
2519
2520         WARN_ON(md->frozen_sb);
2521
2522         md->frozen_sb = freeze_bdev(md->bdev);
2523         if (IS_ERR(md->frozen_sb)) {
2524                 r = PTR_ERR(md->frozen_sb);
2525                 md->frozen_sb = NULL;
2526                 return r;
2527         }
2528
2529         set_bit(DMF_FROZEN, &md->flags);
2530
2531         return 0;
2532 }
2533
2534 static void unlock_fs(struct mapped_device *md)
2535 {
2536         if (!test_bit(DMF_FROZEN, &md->flags))
2537                 return;
2538
2539         thaw_bdev(md->bdev, md->frozen_sb);
2540         md->frozen_sb = NULL;
2541         clear_bit(DMF_FROZEN, &md->flags);
2542 }
2543
2544 /*
2545  * We need to be able to change a mapping table under a mounted
2546  * filesystem.  For example we might want to move some data in
2547  * the background.  Before the table can be swapped with
2548  * dm_bind_table, dm_suspend must be called to flush any in
2549  * flight bios and ensure that any further io gets deferred.
2550  */
2551 /*
2552  * Suspend mechanism in request-based dm.
2553  *
2554  * 1. Flush all I/Os by lock_fs() if needed.
2555  * 2. Stop dispatching any I/O by stopping the request_queue.
2556  * 3. Wait for all in-flight I/Os to be completed or requeued.
2557  *
2558  * To abort suspend, start the request_queue.
2559  */
2560 int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2561 {
2562         struct dm_table *map = NULL;
2563         int r = 0;
2564         int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
2565         int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
2566
2567         mutex_lock(&md->suspend_lock);
2568
2569         if (dm_suspended_md(md)) {
2570                 r = -EINVAL;
2571                 goto out_unlock;
2572         }
2573
2574         map = md->map;
2575
2576         /*
2577          * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2578          * This flag is cleared before dm_suspend returns.
2579          */
2580         if (noflush)
2581                 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2582
2583         /* This does not get reverted if there's an error later. */
2584         dm_table_presuspend_targets(map);
2585
2586         /*
2587          * Flush I/O to the device.
2588          * Any I/O submitted after lock_fs() may not be flushed.
2589          * noflush takes precedence over do_lockfs.
2590          * (lock_fs() flushes I/Os and waits for them to complete.)
2591          */
2592         if (!noflush && do_lockfs) {
2593                 r = lock_fs(md);
2594                 if (r)
2595                         goto out_unlock;
2596         }
2597
2598         /*
2599          * Here we must make sure that no processes are submitting requests
2600          * to target drivers i.e. no one may be executing
2601          * __split_and_process_bio. This is called from dm_request and
2602          * dm_wq_work.
2603          *
2604          * To get all processes out of __split_and_process_bio in dm_request,
2605          * we take the write lock. To prevent any process from reentering
2606          * __split_and_process_bio from dm_request and quiesce the thread
2607          * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2608          * flush_workqueue(md->wq).
2609          */
2610         set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2611         synchronize_srcu(&md->io_barrier);
2612
2613         /*
2614          * Stop md->queue before flushing md->wq in case request-based
2615          * dm defers requests to md->wq from md->queue.
2616          */
2617         if (dm_request_based(md))
2618                 stop_queue(md->queue);
2619
2620         flush_workqueue(md->wq);
2621
2622         /*
2623          * At this point no more requests are entering target request routines.
2624          * We call dm_wait_for_completion to wait for all existing requests
2625          * to finish.
2626          */
2627         r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
2628
2629         if (noflush)
2630                 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2631         synchronize_srcu(&md->io_barrier);
2632
2633         /* were we interrupted ? */
2634         if (r < 0) {
2635                 dm_queue_flush(md);
2636
2637                 if (dm_request_based(md))
2638                         start_queue(md->queue);
2639
2640                 unlock_fs(md);
2641                 goto out_unlock; /* pushback list is already flushed, so skip flush */
2642         }
2643
2644         /*
2645          * If dm_wait_for_completion returned 0, the device is completely
2646          * quiescent now. There is no request-processing activity. All new
2647          * requests are being added to md->deferred list.
2648          */
2649
2650         set_bit(DMF_SUSPENDED, &md->flags);
2651
2652         dm_table_postsuspend_targets(map);
2653
2654 out_unlock:
2655         mutex_unlock(&md->suspend_lock);
2656         return r;
2657 }
2658
2659 int dm_resume(struct mapped_device *md)
2660 {
2661         int r = -EINVAL;
2662         struct dm_table *map = NULL;
2663
2664         mutex_lock(&md->suspend_lock);
2665         if (!dm_suspended_md(md))
2666                 goto out;
2667
2668         map = md->map;
2669         if (!map || !dm_table_get_size(map))
2670                 goto out;
2671
2672         r = dm_table_resume_targets(map);
2673         if (r)
2674                 goto out;
2675
2676         dm_queue_flush(md);
2677
2678         /*
2679          * Flushing deferred I/Os must be done after targets are resumed
2680          * so that mapping of targets can work correctly.
2681          * Request-based dm is queueing the deferred I/Os in its request_queue.
2682          */
2683         if (dm_request_based(md))
2684                 start_queue(md->queue);
2685
2686         unlock_fs(md);
2687
2688         clear_bit(DMF_SUSPENDED, &md->flags);
2689
2690         r = 0;
2691 out:
2692         mutex_unlock(&md->suspend_lock);
2693
2694         return r;
2695 }
2696
2697 /*
2698  * Internal suspend/resume works like userspace-driven suspend. It waits
2699  * until all bios finish and prevents issuing new bios to the target drivers.
2700  * It may be used only from the kernel.
2701  *
2702  * Internal suspend holds md->suspend_lock, which prevents interaction with
2703  * userspace-driven suspend.
2704  */
2705
2706 void dm_internal_suspend(struct mapped_device *md)
2707 {
2708         mutex_lock(&md->suspend_lock);
2709         if (dm_suspended_md(md))
2710                 return;
2711
2712         set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2713         synchronize_srcu(&md->io_barrier);
2714         flush_workqueue(md->wq);
2715         dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2716 }
2717
2718 void dm_internal_resume(struct mapped_device *md)
2719 {
2720         if (dm_suspended_md(md))
2721                 goto done;
2722
2723         dm_queue_flush(md);
2724
2725 done:
2726         mutex_unlock(&md->suspend_lock);
2727 }
2728
2729 /*-----------------------------------------------------------------
2730  * Event notification.
2731  *---------------------------------------------------------------*/
2732 int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
2733                        unsigned cookie)
2734 {
2735         char udev_cookie[DM_COOKIE_LENGTH];
2736         char *envp[] = { udev_cookie, NULL };
2737
2738         if (!cookie)
2739                 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
2740         else {
2741                 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2742                          DM_COOKIE_ENV_VAR_NAME, cookie);
2743                 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2744                                           action, envp);
2745         }
2746 }
2747
2748 uint32_t dm_next_uevent_seq(struct mapped_device *md)
2749 {
2750         return atomic_add_return(1, &md->uevent_seq);
2751 }
2752
2753 uint32_t dm_get_event_nr(struct mapped_device *md)
2754 {
2755         return atomic_read(&md->event_nr);
2756 }
2757
2758 int dm_wait_event(struct mapped_device *md, int event_nr)
2759 {
2760         return wait_event_interruptible(md->eventq,
2761                         (event_nr != atomic_read(&md->event_nr)));
2762 }
2763
2764 void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2765 {
2766         unsigned long flags;
2767
2768         spin_lock_irqsave(&md->uevent_lock, flags);
2769         list_add(elist, &md->uevent_list);
2770         spin_unlock_irqrestore(&md->uevent_lock, flags);
2771 }
2772
2773 /*
2774  * The gendisk is only valid as long as you have a reference
2775  * count on 'md'.
2776  */
2777 struct gendisk *dm_disk(struct mapped_device *md)
2778 {
2779         return md->disk;
2780 }
2781
2782 struct kobject *dm_kobject(struct mapped_device *md)
2783 {
2784         return &md->kobj_holder.kobj;
2785 }
2786
2787 struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2788 {
2789         struct mapped_device *md;
2790
2791         md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
2792
2793         if (test_bit(DMF_FREEING, &md->flags) ||
2794             dm_deleting_md(md))
2795                 return NULL;
2796
2797         dm_get(md);
2798         return md;
2799 }
2800
2801 int dm_suspended_md(struct mapped_device *md)
2802 {
2803         return test_bit(DMF_SUSPENDED, &md->flags);
2804 }
2805
2806 int dm_test_deferred_remove_flag(struct mapped_device *md)
2807 {
2808         return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
2809 }
2810
2811 int dm_suspended(struct dm_target *ti)
2812 {
2813         return dm_suspended_md(dm_table_get_md(ti->table));
2814 }
2815 EXPORT_SYMBOL_GPL(dm_suspended);
2816
2817 int dm_noflush_suspending(struct dm_target *ti)
2818 {
2819         return __noflush_suspending(dm_table_get_md(ti->table));
2820 }
2821 EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2822
2823 struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity, unsigned per_bio_data_size)
2824 {
2825         struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
2826         struct kmem_cache *cachep;
2827         unsigned int pool_size;
2828         unsigned int front_pad;
2829
2830         if (!pools)
2831                 return NULL;
2832
2833         if (type == DM_TYPE_BIO_BASED) {
2834                 cachep = _io_cache;
2835                 pool_size = dm_get_reserved_bio_based_ios();
2836                 front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
2837         } else if (type == DM_TYPE_REQUEST_BASED) {
2838                 cachep = _rq_tio_cache;
2839                 pool_size = dm_get_reserved_rq_based_ios();
2840                 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
2841                 /* per_bio_data_size is not used. See __bind_mempools(). */
2842                 WARN_ON(per_bio_data_size != 0);
2843         } else
2844                 goto out;
2845
2846         pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
2847         if (!pools->io_pool)
2848                 goto out;
2849
2850         pools->bs = bioset_create(pool_size, front_pad);
2851         if (!pools->bs)
2852                 goto out;
2853
2854         if (integrity && bioset_integrity_create(pools->bs, pool_size))
2855                 goto out;
2856
2857         return pools;
2858
2859 out:
2860         dm_free_md_mempools(pools);
2861
2862         return NULL;
2863 }
2864
2865 void dm_free_md_mempools(struct dm_md_mempools *pools)
2866 {
2867         if (!pools)
2868                 return;
2869
2870         if (pools->io_pool)
2871                 mempool_destroy(pools->io_pool);
2872
2873         if (pools->bs)
2874                 bioset_free(pools->bs);
2875
2876         kfree(pools);
2877 }
2878
2879 static const struct block_device_operations dm_blk_dops = {
2880         .open = dm_blk_open,
2881         .release = dm_blk_close,
2882         .ioctl = dm_blk_ioctl,
2883         .getgeo = dm_blk_getgeo,
2884         .owner = THIS_MODULE
2885 };
2886
2887 EXPORT_SYMBOL(dm_get_mapinfo);
2888
2889 /*
2890  * module hooks
2891  */
2892 module_init(dm_init);
2893 module_exit(dm_exit);
2894
2895 module_param(major, uint, 0);
2896 MODULE_PARM_DESC(major, "The major number of the device mapper");
2897
2898 module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
2899 MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
2900
2901 module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
2902 MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
2903
2904 MODULE_DESCRIPTION(DM_NAME " driver");
2905 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2906 MODULE_LICENSE("GPL");