Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2001, 2002 Sistina Software (UK) Limited. |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 3 | * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is released under the GPL. |
| 6 | */ |
| 7 | |
| 8 | #include "dm.h" |
Mike Anderson | 51e5b2b | 2007-10-19 22:48:00 +0100 | [diff] [blame] | 9 | #include "dm-uevent.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/module.h> |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 13 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/moduleparam.h> |
| 15 | #include <linux/blkpg.h> |
| 16 | #include <linux/bio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/mempool.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/idr.h> |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 20 | #include <linux/hdreg.h> |
Kiyoshi Ueda | 3f77316d | 2010-08-12 04:13:56 +0100 | [diff] [blame] | 21 | #include <linux/delay.h> |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 22 | #include <linux/wait.h> |
Keith Busch | 2eb6e1e | 2014-10-17 17:46:36 -0600 | [diff] [blame] | 23 | #include <linux/kthread.h> |
Mike Snitzer | 0ce6579 | 2015-02-26 00:50:28 -0500 | [diff] [blame] | 24 | #include <linux/ktime.h> |
Mike Snitzer | de3ec86 | 2015-02-24 21:58:21 -0500 | [diff] [blame] | 25 | #include <linux/elevator.h> /* for rq_end_sector() */ |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 26 | #include <linux/blk-mq.h> |
Christoph Hellwig | 71cdb69 | 2015-10-15 14:10:51 +0200 | [diff] [blame] | 27 | #include <linux/pr.h> |
Li Zefan | 5578213 | 2009-06-09 13:43:05 +0800 | [diff] [blame] | 28 | |
| 29 | #include <trace/events/block.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 31 | #define DM_MSG_PREFIX "core" |
| 32 | |
Namhyung Kim | 71a1673 | 2011-10-31 20:18:54 +0000 | [diff] [blame] | 33 | #ifdef CONFIG_PRINTK |
| 34 | /* |
| 35 | * ratelimit state to be used in DMXXX_LIMIT(). |
| 36 | */ |
| 37 | DEFINE_RATELIMIT_STATE(dm_ratelimit_state, |
| 38 | DEFAULT_RATELIMIT_INTERVAL, |
| 39 | DEFAULT_RATELIMIT_BURST); |
| 40 | EXPORT_SYMBOL(dm_ratelimit_state); |
| 41 | #endif |
| 42 | |
Milan Broz | 60935eb | 2009-06-22 10:12:30 +0100 | [diff] [blame] | 43 | /* |
| 44 | * Cookies are numeric values sent with CHANGE and REMOVE |
| 45 | * uevents while resuming, removing or renaming the device. |
| 46 | */ |
| 47 | #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE" |
| 48 | #define DM_COOKIE_LENGTH 24 |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | static const char *_name = DM_NAME; |
| 51 | |
| 52 | static unsigned int major = 0; |
| 53 | static unsigned int _major = 0; |
| 54 | |
Alasdair G Kergon | d15b774 | 2011-08-02 12:32:01 +0100 | [diff] [blame] | 55 | static DEFINE_IDR(_minor_idr); |
| 56 | |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 57 | static DEFINE_SPINLOCK(_minor_lock); |
Mikulas Patocka | 2c140a2 | 2013-11-01 18:27:41 -0400 | [diff] [blame] | 58 | |
| 59 | static void do_deferred_remove(struct work_struct *w); |
| 60 | |
| 61 | static DECLARE_WORK(deferred_remove_work, do_deferred_remove); |
| 62 | |
Mikulas Patocka | acfe0ad | 2014-06-14 13:44:31 -0400 | [diff] [blame] | 63 | static struct workqueue_struct *deferred_remove_workqueue; |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | /* |
Kiyoshi Ueda | 8fbf26a | 2009-01-06 03:05:06 +0000 | [diff] [blame] | 66 | * For bio-based dm. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | * One of these is allocated per bio. |
| 68 | */ |
| 69 | struct dm_io { |
| 70 | struct mapped_device *md; |
| 71 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | atomic_t io_count; |
Richard Kennedy | 6ae2fa6 | 2008-07-21 12:00:28 +0100 | [diff] [blame] | 73 | struct bio *bio; |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 74 | unsigned long start_time; |
Kiyoshi Ueda | f88fb98 | 2009-10-16 23:18:15 +0100 | [diff] [blame] | 75 | spinlock_t endio_lock; |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 76 | struct dm_stats_aux stats_aux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | /* |
Kiyoshi Ueda | 8fbf26a | 2009-01-06 03:05:06 +0000 | [diff] [blame] | 80 | * For request-based dm. |
| 81 | * One of these is allocated per request. |
| 82 | */ |
| 83 | struct dm_rq_target_io { |
| 84 | struct mapped_device *md; |
| 85 | struct dm_target *ti; |
Mike Snitzer | 1ae49ea | 2014-12-05 17:11:05 -0500 | [diff] [blame] | 86 | struct request *orig, *clone; |
Keith Busch | 2eb6e1e | 2014-10-17 17:46:36 -0600 | [diff] [blame] | 87 | struct kthread_work work; |
Kiyoshi Ueda | 8fbf26a | 2009-01-06 03:05:06 +0000 | [diff] [blame] | 88 | int error; |
| 89 | union map_info info; |
Mikulas Patocka | e262f34 | 2015-06-09 17:22:49 -0400 | [diff] [blame] | 90 | struct dm_stats_aux stats_aux; |
| 91 | unsigned long duration_jiffies; |
| 92 | unsigned n_sectors; |
Kiyoshi Ueda | 8fbf26a | 2009-01-06 03:05:06 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | /* |
Kent Overstreet | 9481874 | 2012-09-07 13:44:01 -0700 | [diff] [blame] | 96 | * For request-based dm - the bio clones we allocate are embedded in these |
| 97 | * structs. |
| 98 | * |
| 99 | * We allocate these with bio_alloc_bioset, using the front_pad parameter when |
| 100 | * the bioset is created - this means the bio has to come at the end of the |
| 101 | * struct. |
Kiyoshi Ueda | 8fbf26a | 2009-01-06 03:05:06 +0000 | [diff] [blame] | 102 | */ |
| 103 | struct dm_rq_clone_bio_info { |
| 104 | struct bio *orig; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 105 | struct dm_rq_target_io *tio; |
Kent Overstreet | 9481874 | 2012-09-07 13:44:01 -0700 | [diff] [blame] | 106 | struct bio clone; |
Kiyoshi Ueda | 8fbf26a | 2009-01-06 03:05:06 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 109 | union map_info *dm_get_rq_mapinfo(struct request *rq) |
| 110 | { |
| 111 | if (rq && rq->end_io_data) |
| 112 | return &((struct dm_rq_target_io *)rq->end_io_data)->info; |
| 113 | return NULL; |
| 114 | } |
| 115 | EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo); |
| 116 | |
Jeff Mahoney | ba61fdd | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 117 | #define MINOR_ALLOCED ((void *)-1) |
| 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | /* |
| 120 | * Bits for the md->flags field. |
| 121 | */ |
Alasdair G Kergon | 1eb787e | 2009-04-09 00:27:14 +0100 | [diff] [blame] | 122 | #define DMF_BLOCK_IO_FOR_SUSPEND 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | #define DMF_SUSPENDED 1 |
Alasdair G Kergon | aa8d7c2 | 2006-01-06 00:20:06 -0800 | [diff] [blame] | 124 | #define DMF_FROZEN 2 |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 125 | #define DMF_FREEING 3 |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 126 | #define DMF_DELETING 4 |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 127 | #define DMF_NOFLUSH_SUSPENDING 5 |
Kent Overstreet | 8ae1266 | 2015-04-27 23:48:34 -0700 | [diff] [blame] | 128 | #define DMF_DEFERRED_REMOVE 6 |
| 129 | #define DMF_SUSPENDED_INTERNALLY 7 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Milan Broz | 304f3f6 | 2008-02-08 02:11:17 +0000 | [diff] [blame] | 131 | /* |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 132 | * A dummy definition to make RCU happy. |
| 133 | * struct dm_table should never be dereferenced in this file. |
| 134 | */ |
| 135 | struct dm_table { |
| 136 | int undefined__; |
| 137 | }; |
| 138 | |
| 139 | /* |
Milan Broz | 304f3f6 | 2008-02-08 02:11:17 +0000 | [diff] [blame] | 140 | * Work processed by per-device workqueue. |
| 141 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | struct mapped_device { |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 143 | struct srcu_struct io_barrier; |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 144 | struct mutex suspend_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | atomic_t holders; |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 146 | atomic_t open_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
Mikulas Patocka | 2a7faeb | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 148 | /* |
| 149 | * The current mapping. |
| 150 | * Use dm_get_live_table{_fast} or take suspend_lock for |
| 151 | * dereference. |
| 152 | */ |
Pranith Kumar | 6fa9952 | 2014-10-28 15:09:57 -0700 | [diff] [blame] | 153 | struct dm_table __rcu *map; |
Mikulas Patocka | 2a7faeb | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 154 | |
Benjamin Marzinski | 86f1152 | 2014-08-13 13:53:43 -0500 | [diff] [blame] | 155 | struct list_head table_devices; |
| 156 | struct mutex table_devices_lock; |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | unsigned long flags; |
| 159 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 160 | struct request_queue *queue; |
Mike Snitzer | a5664da | 2010-08-12 04:14:01 +0100 | [diff] [blame] | 161 | unsigned type; |
Mike Snitzer | 4a0b4dd | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 162 | /* Protect queue and type against concurrent access. */ |
Mike Snitzer | a5664da | 2010-08-12 04:14:01 +0100 | [diff] [blame] | 163 | struct mutex type_lock; |
| 164 | |
Alasdair G Kergon | 36a0456 | 2011-10-31 20:19:04 +0000 | [diff] [blame] | 165 | struct target_type *immutable_target_type; |
| 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | struct gendisk *disk; |
Mike Anderson | 7e51f25 | 2006-03-27 01:17:52 -0800 | [diff] [blame] | 168 | char name[16]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
| 170 | void *interface_ptr; |
| 171 | |
| 172 | /* |
| 173 | * A list of ios that arrived while we were suspended. |
| 174 | */ |
Nikanth Karthikesan | 316d315 | 2009-10-06 20:16:55 +0200 | [diff] [blame] | 175 | atomic_t pending[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | wait_queue_head_t wait; |
Mikulas Patocka | 53d5914 | 2009-04-02 19:55:37 +0100 | [diff] [blame] | 177 | struct work_struct work; |
Kiyoshi Ueda | 7485936 | 2006-12-08 02:41:02 -0800 | [diff] [blame] | 178 | struct bio_list deferred; |
Mikulas Patocka | 022c261 | 2009-04-02 19:55:39 +0100 | [diff] [blame] | 179 | spinlock_t deferred_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
| 181 | /* |
Tejun Heo | 29e4013 | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 182 | * Processing queue (flush) |
Milan Broz | 304f3f6 | 2008-02-08 02:11:17 +0000 | [diff] [blame] | 183 | */ |
| 184 | struct workqueue_struct *wq; |
| 185 | |
| 186 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | * io objects are allocated from here. |
| 188 | */ |
| 189 | mempool_t *io_pool; |
Mike Snitzer | 1ae49ea | 2014-12-05 17:11:05 -0500 | [diff] [blame] | 190 | mempool_t *rq_pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 192 | struct bio_set *bs; |
| 193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | /* |
| 195 | * Event handling. |
| 196 | */ |
| 197 | atomic_t event_nr; |
| 198 | wait_queue_head_t eventq; |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 199 | atomic_t uevent_seq; |
| 200 | struct list_head uevent_list; |
| 201 | spinlock_t uevent_lock; /* Protect access to uevent_list */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
| 203 | /* |
| 204 | * freeze/thaw support require holding onto a super block |
| 205 | */ |
| 206 | struct super_block *frozen_sb; |
Mikulas Patocka | db8fef4 | 2009-06-22 10:12:15 +0100 | [diff] [blame] | 207 | struct block_device *bdev; |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 208 | |
| 209 | /* forced geometry settings */ |
| 210 | struct hd_geometry geometry; |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 211 | |
Mikulas Patocka | 2995fa7 | 2014-01-13 19:37:54 -0500 | [diff] [blame] | 212 | /* kobject and completion */ |
| 213 | struct dm_kobject_holder kobj_holder; |
Mikulas Patocka | be35f48 | 2014-01-06 23:01:22 -0500 | [diff] [blame] | 214 | |
Tejun Heo | d87f4c1 | 2010-09-03 11:56:19 +0200 | [diff] [blame] | 215 | /* zero-length flush that will be cloned and submitted to targets */ |
| 216 | struct bio flush_bio; |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 217 | |
Mikulas Patocka | 96b26c8 | 2015-01-08 18:52:26 -0500 | [diff] [blame] | 218 | /* the number of internal suspends */ |
| 219 | unsigned internal_suspend_count; |
| 220 | |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 221 | struct dm_stats stats; |
Keith Busch | 2eb6e1e | 2014-10-17 17:46:36 -0600 | [diff] [blame] | 222 | |
| 223 | struct kthread_worker kworker; |
| 224 | struct task_struct *kworker_task; |
Mike Snitzer | de3ec86 | 2015-02-24 21:58:21 -0500 | [diff] [blame] | 225 | |
| 226 | /* for request-based merge heuristic in dm_request_fn() */ |
Mike Snitzer | 0ce6579 | 2015-02-26 00:50:28 -0500 | [diff] [blame] | 227 | unsigned seq_rq_merge_deadline_usecs; |
Mike Snitzer | de3ec86 | 2015-02-24 21:58:21 -0500 | [diff] [blame] | 228 | int last_rq_rw; |
Mike Snitzer | 0ce6579 | 2015-02-26 00:50:28 -0500 | [diff] [blame] | 229 | sector_t last_rq_pos; |
| 230 | ktime_t last_rq_start_time; |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 231 | |
| 232 | /* for blk-mq request-based DM support */ |
| 233 | struct blk_mq_tag_set tag_set; |
Mike Snitzer | 17e149b | 2015-03-11 15:01:09 -0400 | [diff] [blame] | 234 | bool use_blk_mq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | }; |
| 236 | |
Mike Snitzer | 17e149b | 2015-03-11 15:01:09 -0400 | [diff] [blame] | 237 | #ifdef CONFIG_DM_MQ_DEFAULT |
| 238 | static bool use_blk_mq = true; |
| 239 | #else |
| 240 | static bool use_blk_mq = false; |
| 241 | #endif |
| 242 | |
| 243 | bool dm_use_blk_mq(struct mapped_device *md) |
| 244 | { |
| 245 | return md->use_blk_mq; |
| 246 | } |
| 247 | |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 248 | /* |
| 249 | * For mempools pre-allocation at the table loading time. |
| 250 | */ |
| 251 | struct dm_md_mempools { |
| 252 | mempool_t *io_pool; |
Mike Snitzer | 1ae49ea | 2014-12-05 17:11:05 -0500 | [diff] [blame] | 253 | mempool_t *rq_pool; |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 254 | struct bio_set *bs; |
| 255 | }; |
| 256 | |
Benjamin Marzinski | 86f1152 | 2014-08-13 13:53:43 -0500 | [diff] [blame] | 257 | struct table_device { |
| 258 | struct list_head list; |
| 259 | atomic_t count; |
| 260 | struct dm_dev dm_dev; |
| 261 | }; |
| 262 | |
Mike Snitzer | 6cfa585 | 2013-09-12 18:06:11 -0400 | [diff] [blame] | 263 | #define RESERVED_BIO_BASED_IOS 16 |
| 264 | #define RESERVED_REQUEST_BASED_IOS 256 |
Mike Snitzer | f479082 | 2013-09-12 18:06:12 -0400 | [diff] [blame] | 265 | #define RESERVED_MAX_IOS 1024 |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 266 | static struct kmem_cache *_io_cache; |
Kiyoshi Ueda | 8fbf26a | 2009-01-06 03:05:06 +0000 | [diff] [blame] | 267 | static struct kmem_cache *_rq_tio_cache; |
Mike Snitzer | 1ae49ea | 2014-12-05 17:11:05 -0500 | [diff] [blame] | 268 | static struct kmem_cache *_rq_cache; |
Kent Overstreet | 9481874 | 2012-09-07 13:44:01 -0700 | [diff] [blame] | 269 | |
Mike Snitzer | f479082 | 2013-09-12 18:06:12 -0400 | [diff] [blame] | 270 | /* |
Mike Snitzer | e860313 | 2013-09-12 18:06:12 -0400 | [diff] [blame] | 271 | * Bio-based DM's mempools' reserved IOs set by the user. |
| 272 | */ |
| 273 | static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS; |
| 274 | |
| 275 | /* |
Mike Snitzer | f479082 | 2013-09-12 18:06:12 -0400 | [diff] [blame] | 276 | * Request-based DM's mempools' reserved IOs set by the user. |
| 277 | */ |
| 278 | static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS; |
| 279 | |
Mike Snitzer | 09c2d53 | 2015-02-27 22:25:26 -0500 | [diff] [blame] | 280 | static unsigned __dm_get_module_param(unsigned *module_param, |
Mike Snitzer | f479082 | 2013-09-12 18:06:12 -0400 | [diff] [blame] | 281 | unsigned def, unsigned max) |
| 282 | { |
Mike Snitzer | 09c2d53 | 2015-02-27 22:25:26 -0500 | [diff] [blame] | 283 | unsigned param = ACCESS_ONCE(*module_param); |
| 284 | unsigned modified_param = 0; |
Mike Snitzer | f479082 | 2013-09-12 18:06:12 -0400 | [diff] [blame] | 285 | |
Mike Snitzer | 09c2d53 | 2015-02-27 22:25:26 -0500 | [diff] [blame] | 286 | if (!param) |
| 287 | modified_param = def; |
| 288 | else if (param > max) |
| 289 | modified_param = max; |
Mike Snitzer | f479082 | 2013-09-12 18:06:12 -0400 | [diff] [blame] | 290 | |
Mike Snitzer | 09c2d53 | 2015-02-27 22:25:26 -0500 | [diff] [blame] | 291 | if (modified_param) { |
| 292 | (void)cmpxchg(module_param, param, modified_param); |
| 293 | param = modified_param; |
Mike Snitzer | f479082 | 2013-09-12 18:06:12 -0400 | [diff] [blame] | 294 | } |
| 295 | |
Mike Snitzer | 09c2d53 | 2015-02-27 22:25:26 -0500 | [diff] [blame] | 296 | return param; |
Mike Snitzer | f479082 | 2013-09-12 18:06:12 -0400 | [diff] [blame] | 297 | } |
| 298 | |
Mike Snitzer | e860313 | 2013-09-12 18:06:12 -0400 | [diff] [blame] | 299 | unsigned dm_get_reserved_bio_based_ios(void) |
| 300 | { |
Mike Snitzer | 09c2d53 | 2015-02-27 22:25:26 -0500 | [diff] [blame] | 301 | return __dm_get_module_param(&reserved_bio_based_ios, |
Mike Snitzer | e860313 | 2013-09-12 18:06:12 -0400 | [diff] [blame] | 302 | RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS); |
| 303 | } |
| 304 | EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios); |
| 305 | |
Mike Snitzer | f479082 | 2013-09-12 18:06:12 -0400 | [diff] [blame] | 306 | unsigned dm_get_reserved_rq_based_ios(void) |
| 307 | { |
Mike Snitzer | 09c2d53 | 2015-02-27 22:25:26 -0500 | [diff] [blame] | 308 | return __dm_get_module_param(&reserved_rq_based_ios, |
Mike Snitzer | f479082 | 2013-09-12 18:06:12 -0400 | [diff] [blame] | 309 | RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS); |
| 310 | } |
| 311 | EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios); |
| 312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | static int __init local_init(void) |
| 314 | { |
Kiyoshi Ueda | 51157b4 | 2008-10-21 17:45:08 +0100 | [diff] [blame] | 315 | int r = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | /* allocate a slab for the dm_ios */ |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 318 | _io_cache = KMEM_CACHE(dm_io, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | if (!_io_cache) |
Kiyoshi Ueda | 51157b4 | 2008-10-21 17:45:08 +0100 | [diff] [blame] | 320 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
Kiyoshi Ueda | 8fbf26a | 2009-01-06 03:05:06 +0000 | [diff] [blame] | 322 | _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0); |
| 323 | if (!_rq_tio_cache) |
Mikulas Patocka | dba1416 | 2012-10-12 21:02:15 +0100 | [diff] [blame] | 324 | goto out_free_io_cache; |
Kiyoshi Ueda | 8fbf26a | 2009-01-06 03:05:06 +0000 | [diff] [blame] | 325 | |
Mike Snitzer | 1ae49ea | 2014-12-05 17:11:05 -0500 | [diff] [blame] | 326 | _rq_cache = kmem_cache_create("dm_clone_request", sizeof(struct request), |
| 327 | __alignof__(struct request), 0, NULL); |
| 328 | if (!_rq_cache) |
| 329 | goto out_free_rq_tio_cache; |
| 330 | |
Mike Anderson | 51e5b2b | 2007-10-19 22:48:00 +0100 | [diff] [blame] | 331 | r = dm_uevent_init(); |
Kiyoshi Ueda | 51157b4 | 2008-10-21 17:45:08 +0100 | [diff] [blame] | 332 | if (r) |
Mike Snitzer | 1ae49ea | 2014-12-05 17:11:05 -0500 | [diff] [blame] | 333 | goto out_free_rq_cache; |
Mike Anderson | 51e5b2b | 2007-10-19 22:48:00 +0100 | [diff] [blame] | 334 | |
Mikulas Patocka | acfe0ad | 2014-06-14 13:44:31 -0400 | [diff] [blame] | 335 | deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1); |
| 336 | if (!deferred_remove_workqueue) { |
| 337 | r = -ENOMEM; |
| 338 | goto out_uevent_exit; |
| 339 | } |
| 340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | _major = major; |
| 342 | r = register_blkdev(_major, _name); |
Kiyoshi Ueda | 51157b4 | 2008-10-21 17:45:08 +0100 | [diff] [blame] | 343 | if (r < 0) |
Mikulas Patocka | acfe0ad | 2014-06-14 13:44:31 -0400 | [diff] [blame] | 344 | goto out_free_workqueue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
| 346 | if (!_major) |
| 347 | _major = r; |
| 348 | |
| 349 | return 0; |
Kiyoshi Ueda | 51157b4 | 2008-10-21 17:45:08 +0100 | [diff] [blame] | 350 | |
Mikulas Patocka | acfe0ad | 2014-06-14 13:44:31 -0400 | [diff] [blame] | 351 | out_free_workqueue: |
| 352 | destroy_workqueue(deferred_remove_workqueue); |
Kiyoshi Ueda | 51157b4 | 2008-10-21 17:45:08 +0100 | [diff] [blame] | 353 | out_uevent_exit: |
| 354 | dm_uevent_exit(); |
Mike Snitzer | 1ae49ea | 2014-12-05 17:11:05 -0500 | [diff] [blame] | 355 | out_free_rq_cache: |
| 356 | kmem_cache_destroy(_rq_cache); |
Kiyoshi Ueda | 8fbf26a | 2009-01-06 03:05:06 +0000 | [diff] [blame] | 357 | out_free_rq_tio_cache: |
| 358 | kmem_cache_destroy(_rq_tio_cache); |
Kiyoshi Ueda | 51157b4 | 2008-10-21 17:45:08 +0100 | [diff] [blame] | 359 | out_free_io_cache: |
| 360 | kmem_cache_destroy(_io_cache); |
| 361 | |
| 362 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static void local_exit(void) |
| 366 | { |
Mikulas Patocka | 2c140a2 | 2013-11-01 18:27:41 -0400 | [diff] [blame] | 367 | flush_scheduled_work(); |
Mikulas Patocka | acfe0ad | 2014-06-14 13:44:31 -0400 | [diff] [blame] | 368 | destroy_workqueue(deferred_remove_workqueue); |
Mikulas Patocka | 2c140a2 | 2013-11-01 18:27:41 -0400 | [diff] [blame] | 369 | |
Mike Snitzer | 1ae49ea | 2014-12-05 17:11:05 -0500 | [diff] [blame] | 370 | kmem_cache_destroy(_rq_cache); |
Kiyoshi Ueda | 8fbf26a | 2009-01-06 03:05:06 +0000 | [diff] [blame] | 371 | kmem_cache_destroy(_rq_tio_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | kmem_cache_destroy(_io_cache); |
Akinobu Mita | 00d5940 | 2007-07-17 04:03:46 -0700 | [diff] [blame] | 373 | unregister_blkdev(_major, _name); |
Mike Anderson | 51e5b2b | 2007-10-19 22:48:00 +0100 | [diff] [blame] | 374 | dm_uevent_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
| 376 | _major = 0; |
| 377 | |
| 378 | DMINFO("cleaned up"); |
| 379 | } |
| 380 | |
Alasdair G Kergon | b9249e5 | 2008-02-08 02:09:51 +0000 | [diff] [blame] | 381 | static int (*_inits[])(void) __initdata = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | local_init, |
| 383 | dm_target_init, |
| 384 | dm_linear_init, |
| 385 | dm_stripe_init, |
Mikulas Patocka | 952b355 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 386 | dm_io_init, |
Mikulas Patocka | 945fa4d | 2008-04-24 21:43:49 +0100 | [diff] [blame] | 387 | dm_kcopyd_init, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | dm_interface_init, |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 389 | dm_statistics_init, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | }; |
| 391 | |
Alasdair G Kergon | b9249e5 | 2008-02-08 02:09:51 +0000 | [diff] [blame] | 392 | static void (*_exits[])(void) = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | local_exit, |
| 394 | dm_target_exit, |
| 395 | dm_linear_exit, |
| 396 | dm_stripe_exit, |
Mikulas Patocka | 952b355 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 397 | dm_io_exit, |
Mikulas Patocka | 945fa4d | 2008-04-24 21:43:49 +0100 | [diff] [blame] | 398 | dm_kcopyd_exit, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | dm_interface_exit, |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 400 | dm_statistics_exit, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | }; |
| 402 | |
| 403 | static int __init dm_init(void) |
| 404 | { |
| 405 | const int count = ARRAY_SIZE(_inits); |
| 406 | |
| 407 | int r, i; |
| 408 | |
| 409 | for (i = 0; i < count; i++) { |
| 410 | r = _inits[i](); |
| 411 | if (r) |
| 412 | goto bad; |
| 413 | } |
| 414 | |
| 415 | return 0; |
| 416 | |
| 417 | bad: |
| 418 | while (i--) |
| 419 | _exits[i](); |
| 420 | |
| 421 | return r; |
| 422 | } |
| 423 | |
| 424 | static void __exit dm_exit(void) |
| 425 | { |
| 426 | int i = ARRAY_SIZE(_exits); |
| 427 | |
| 428 | while (i--) |
| 429 | _exits[i](); |
Alasdair G Kergon | d15b774 | 2011-08-02 12:32:01 +0100 | [diff] [blame] | 430 | |
| 431 | /* |
| 432 | * Should be empty by this point. |
| 433 | */ |
Alasdair G Kergon | d15b774 | 2011-08-02 12:32:01 +0100 | [diff] [blame] | 434 | idr_destroy(&_minor_idr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /* |
| 438 | * Block device functions |
| 439 | */ |
Mike Anderson | 432a212 | 2009-12-10 23:52:20 +0000 | [diff] [blame] | 440 | int dm_deleting_md(struct mapped_device *md) |
| 441 | { |
| 442 | return test_bit(DMF_DELETING, &md->flags); |
| 443 | } |
| 444 | |
Al Viro | fe5f9f2 | 2008-03-02 10:29:31 -0500 | [diff] [blame] | 445 | static int dm_blk_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | { |
| 447 | struct mapped_device *md; |
| 448 | |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 449 | spin_lock(&_minor_lock); |
| 450 | |
Al Viro | fe5f9f2 | 2008-03-02 10:29:31 -0500 | [diff] [blame] | 451 | md = bdev->bd_disk->private_data; |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 452 | if (!md) |
| 453 | goto out; |
| 454 | |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 455 | if (test_bit(DMF_FREEING, &md->flags) || |
Mike Anderson | 432a212 | 2009-12-10 23:52:20 +0000 | [diff] [blame] | 456 | dm_deleting_md(md)) { |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 457 | md = NULL; |
| 458 | goto out; |
| 459 | } |
| 460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | dm_get(md); |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 462 | atomic_inc(&md->open_count); |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 463 | out: |
| 464 | spin_unlock(&_minor_lock); |
| 465 | |
| 466 | return md ? 0 : -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 469 | static void dm_blk_close(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | { |
Mike Snitzer | 63a4f06 | 2015-03-23 17:01:43 -0400 | [diff] [blame] | 471 | struct mapped_device *md; |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 472 | |
Milan Broz | 4a1aeb9 | 2011-01-13 19:59:48 +0000 | [diff] [blame] | 473 | spin_lock(&_minor_lock); |
| 474 | |
Mike Snitzer | 63a4f06 | 2015-03-23 17:01:43 -0400 | [diff] [blame] | 475 | md = disk->private_data; |
| 476 | if (WARN_ON(!md)) |
| 477 | goto out; |
| 478 | |
Mikulas Patocka | 2c140a2 | 2013-11-01 18:27:41 -0400 | [diff] [blame] | 479 | if (atomic_dec_and_test(&md->open_count) && |
| 480 | (test_bit(DMF_DEFERRED_REMOVE, &md->flags))) |
Mikulas Patocka | acfe0ad | 2014-06-14 13:44:31 -0400 | [diff] [blame] | 481 | queue_work(deferred_remove_workqueue, &deferred_remove_work); |
Mikulas Patocka | 2c140a2 | 2013-11-01 18:27:41 -0400 | [diff] [blame] | 482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | dm_put(md); |
Mike Snitzer | 63a4f06 | 2015-03-23 17:01:43 -0400 | [diff] [blame] | 484 | out: |
Milan Broz | 4a1aeb9 | 2011-01-13 19:59:48 +0000 | [diff] [blame] | 485 | spin_unlock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 488 | int dm_open_count(struct mapped_device *md) |
| 489 | { |
| 490 | return atomic_read(&md->open_count); |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * Guarantees nothing is using the device before it's deleted. |
| 495 | */ |
Mikulas Patocka | 2c140a2 | 2013-11-01 18:27:41 -0400 | [diff] [blame] | 496 | int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred) |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 497 | { |
| 498 | int r = 0; |
| 499 | |
| 500 | spin_lock(&_minor_lock); |
| 501 | |
Mikulas Patocka | 2c140a2 | 2013-11-01 18:27:41 -0400 | [diff] [blame] | 502 | if (dm_open_count(md)) { |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 503 | r = -EBUSY; |
Mikulas Patocka | 2c140a2 | 2013-11-01 18:27:41 -0400 | [diff] [blame] | 504 | if (mark_deferred) |
| 505 | set_bit(DMF_DEFERRED_REMOVE, &md->flags); |
| 506 | } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags)) |
| 507 | r = -EEXIST; |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 508 | else |
| 509 | set_bit(DMF_DELETING, &md->flags); |
| 510 | |
| 511 | spin_unlock(&_minor_lock); |
| 512 | |
| 513 | return r; |
| 514 | } |
| 515 | |
Mikulas Patocka | 2c140a2 | 2013-11-01 18:27:41 -0400 | [diff] [blame] | 516 | int dm_cancel_deferred_remove(struct mapped_device *md) |
| 517 | { |
| 518 | int r = 0; |
| 519 | |
| 520 | spin_lock(&_minor_lock); |
| 521 | |
| 522 | if (test_bit(DMF_DELETING, &md->flags)) |
| 523 | r = -EBUSY; |
| 524 | else |
| 525 | clear_bit(DMF_DEFERRED_REMOVE, &md->flags); |
| 526 | |
| 527 | spin_unlock(&_minor_lock); |
| 528 | |
| 529 | return r; |
| 530 | } |
| 531 | |
| 532 | static void do_deferred_remove(struct work_struct *w) |
| 533 | { |
| 534 | dm_deferred_remove(); |
| 535 | } |
| 536 | |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 537 | sector_t dm_get_size(struct mapped_device *md) |
| 538 | { |
| 539 | return get_capacity(md->disk); |
| 540 | } |
| 541 | |
Mike Snitzer | 9974fa2 | 2014-02-28 15:33:43 +0100 | [diff] [blame] | 542 | struct request_queue *dm_get_md_queue(struct mapped_device *md) |
| 543 | { |
| 544 | return md->queue; |
| 545 | } |
| 546 | |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 547 | struct dm_stats *dm_get_stats(struct mapped_device *md) |
| 548 | { |
| 549 | return &md->stats; |
| 550 | } |
| 551 | |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 552 | static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 553 | { |
| 554 | struct mapped_device *md = bdev->bd_disk->private_data; |
| 555 | |
| 556 | return dm_get_geometry(md, geo); |
| 557 | } |
| 558 | |
Christoph Hellwig | e56f81e | 2015-10-15 14:10:50 +0200 | [diff] [blame] | 559 | static int dm_get_live_table_for_ioctl(struct mapped_device *md, |
| 560 | struct dm_target **tgt, struct block_device **bdev, |
| 561 | fmode_t *mode, int *srcu_idx) |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 562 | { |
Hannes Reinecke | 6c182cd | 2013-07-10 23:41:15 +0100 | [diff] [blame] | 563 | struct dm_table *map; |
Christoph Hellwig | e56f81e | 2015-10-15 14:10:50 +0200 | [diff] [blame] | 564 | int r; |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 565 | |
Hannes Reinecke | 6c182cd | 2013-07-10 23:41:15 +0100 | [diff] [blame] | 566 | retry: |
Christoph Hellwig | e56f81e | 2015-10-15 14:10:50 +0200 | [diff] [blame] | 567 | r = -ENOTTY; |
| 568 | map = dm_get_live_table(md, srcu_idx); |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 569 | if (!map || !dm_table_get_size(map)) |
| 570 | goto out; |
| 571 | |
| 572 | /* We only support devices that have a single target */ |
| 573 | if (dm_table_get_num_targets(map) != 1) |
| 574 | goto out; |
| 575 | |
Christoph Hellwig | e56f81e | 2015-10-15 14:10:50 +0200 | [diff] [blame] | 576 | *tgt = dm_table_get_target(map, 0); |
| 577 | |
| 578 | if (!(*tgt)->type->prepare_ioctl) |
Mike Snitzer | 4d341d8 | 2014-11-16 14:21:47 -0500 | [diff] [blame] | 579 | goto out; |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 580 | |
Kiyoshi Ueda | 4f186f8 | 2009-12-10 23:52:26 +0000 | [diff] [blame] | 581 | if (dm_suspended_md(md)) { |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 582 | r = -EAGAIN; |
| 583 | goto out; |
| 584 | } |
| 585 | |
Christoph Hellwig | e56f81e | 2015-10-15 14:10:50 +0200 | [diff] [blame] | 586 | r = (*tgt)->type->prepare_ioctl(*tgt, bdev, mode); |
| 587 | if (r < 0) |
| 588 | goto out; |
| 589 | |
| 590 | return r; |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 591 | |
| 592 | out: |
Christoph Hellwig | e56f81e | 2015-10-15 14:10:50 +0200 | [diff] [blame] | 593 | dm_put_live_table(md, *srcu_idx); |
Junichi Nomura | 5bbbfdf | 2015-11-17 09:39:26 +0000 | [diff] [blame] | 594 | if (r == -ENOTCONN && !fatal_signal_pending(current)) { |
Hannes Reinecke | 6c182cd | 2013-07-10 23:41:15 +0100 | [diff] [blame] | 595 | msleep(10); |
| 596 | goto retry; |
| 597 | } |
Christoph Hellwig | e56f81e | 2015-10-15 14:10:50 +0200 | [diff] [blame] | 598 | return r; |
| 599 | } |
Hannes Reinecke | 6c182cd | 2013-07-10 23:41:15 +0100 | [diff] [blame] | 600 | |
Christoph Hellwig | e56f81e | 2015-10-15 14:10:50 +0200 | [diff] [blame] | 601 | static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode, |
| 602 | unsigned int cmd, unsigned long arg) |
| 603 | { |
| 604 | struct mapped_device *md = bdev->bd_disk->private_data; |
| 605 | struct dm_target *tgt; |
Mike Snitzer | 647a20d | 2015-11-17 14:11:34 -0500 | [diff] [blame] | 606 | struct block_device *tgt_bdev = NULL; |
Christoph Hellwig | e56f81e | 2015-10-15 14:10:50 +0200 | [diff] [blame] | 607 | int srcu_idx, r; |
| 608 | |
Mike Snitzer | 647a20d | 2015-11-17 14:11:34 -0500 | [diff] [blame] | 609 | r = dm_get_live_table_for_ioctl(md, &tgt, &tgt_bdev, &mode, &srcu_idx); |
Christoph Hellwig | e56f81e | 2015-10-15 14:10:50 +0200 | [diff] [blame] | 610 | if (r < 0) |
| 611 | return r; |
| 612 | |
| 613 | if (r > 0) { |
| 614 | /* |
| 615 | * Target determined this ioctl is being issued against |
| 616 | * a logical partition of the parent bdev; so extra |
| 617 | * validation is needed. |
| 618 | */ |
| 619 | r = scsi_verify_blk_ioctl(NULL, cmd); |
| 620 | if (r) |
| 621 | goto out; |
| 622 | } |
| 623 | |
Mike Snitzer | 647a20d | 2015-11-17 14:11:34 -0500 | [diff] [blame] | 624 | r = __blkdev_driver_ioctl(tgt_bdev, mode, cmd, arg); |
Christoph Hellwig | e56f81e | 2015-10-15 14:10:50 +0200 | [diff] [blame] | 625 | out: |
| 626 | dm_put_live_table(md, srcu_idx); |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 627 | return r; |
| 628 | } |
| 629 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 630 | static struct dm_io *alloc_io(struct mapped_device *md) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | { |
| 632 | return mempool_alloc(md->io_pool, GFP_NOIO); |
| 633 | } |
| 634 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 635 | static void free_io(struct mapped_device *md, struct dm_io *io) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | { |
| 637 | mempool_free(io, md->io_pool); |
| 638 | } |
| 639 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 640 | static void free_tio(struct mapped_device *md, struct dm_target_io *tio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | { |
Mikulas Patocka | dba1416 | 2012-10-12 21:02:15 +0100 | [diff] [blame] | 642 | bio_put(&tio->clone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Kiyoshi Ueda | 0888564 | 2009-12-10 23:52:15 +0000 | [diff] [blame] | 645 | static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md, |
| 646 | gfp_t gfp_mask) |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 647 | { |
Jun'ichi Nomura | 5f01520 | 2013-03-01 22:45:48 +0000 | [diff] [blame] | 648 | return mempool_alloc(md->io_pool, gfp_mask); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | static void free_rq_tio(struct dm_rq_target_io *tio) |
| 652 | { |
Jun'ichi Nomura | 5f01520 | 2013-03-01 22:45:48 +0000 | [diff] [blame] | 653 | mempool_free(tio, tio->md->io_pool); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 654 | } |
| 655 | |
Mike Snitzer | 1ae49ea | 2014-12-05 17:11:05 -0500 | [diff] [blame] | 656 | static struct request *alloc_clone_request(struct mapped_device *md, |
| 657 | gfp_t gfp_mask) |
| 658 | { |
| 659 | return mempool_alloc(md->rq_pool, gfp_mask); |
| 660 | } |
| 661 | |
| 662 | static void free_clone_request(struct mapped_device *md, struct request *rq) |
| 663 | { |
| 664 | mempool_free(rq, md->rq_pool); |
| 665 | } |
| 666 | |
Kiyoshi Ueda | 90abb8c | 2009-12-10 23:52:13 +0000 | [diff] [blame] | 667 | static int md_in_flight(struct mapped_device *md) |
| 668 | { |
| 669 | return atomic_read(&md->pending[READ]) + |
| 670 | atomic_read(&md->pending[WRITE]); |
| 671 | } |
| 672 | |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 673 | static void start_io_acct(struct dm_io *io) |
| 674 | { |
| 675 | struct mapped_device *md = io->md; |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 676 | struct bio *bio = io->bio; |
Tejun Heo | c995905 | 2008-08-25 19:47:21 +0900 | [diff] [blame] | 677 | int cpu; |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 678 | int rw = bio_data_dir(bio); |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 679 | |
| 680 | io->start_time = jiffies; |
| 681 | |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 682 | cpu = part_stat_lock(); |
| 683 | part_round_stats(cpu, &dm_disk(md)->part0); |
| 684 | part_stat_unlock(); |
Shaohua Li | 1e9bb88 | 2011-03-22 08:35:35 +0100 | [diff] [blame] | 685 | atomic_set(&dm_disk(md)->part0.in_flight[rw], |
| 686 | atomic_inc_return(&md->pending[rw])); |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 687 | |
| 688 | if (unlikely(dm_stats_used(&md->stats))) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 689 | dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector, |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 690 | bio_sectors(bio), false, 0, &io->stats_aux); |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 691 | } |
| 692 | |
Mikulas Patocka | d221d2e | 2008-11-13 23:39:10 +0000 | [diff] [blame] | 693 | static void end_io_acct(struct dm_io *io) |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 694 | { |
| 695 | struct mapped_device *md = io->md; |
| 696 | struct bio *bio = io->bio; |
| 697 | unsigned long duration = jiffies - io->start_time; |
Gu Zheng | 18c0b22 | 2014-11-24 11:05:26 +0800 | [diff] [blame] | 698 | int pending; |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 699 | int rw = bio_data_dir(bio); |
| 700 | |
Gu Zheng | 18c0b22 | 2014-11-24 11:05:26 +0800 | [diff] [blame] | 701 | generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time); |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 702 | |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 703 | if (unlikely(dm_stats_used(&md->stats))) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 704 | dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector, |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 705 | bio_sectors(bio), true, duration, &io->stats_aux); |
| 706 | |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 707 | /* |
| 708 | * After this is decremented the bio must not be touched if it is |
Tejun Heo | d87f4c1 | 2010-09-03 11:56:19 +0200 | [diff] [blame] | 709 | * a flush. |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 710 | */ |
Shaohua Li | 1e9bb88 | 2011-03-22 08:35:35 +0100 | [diff] [blame] | 711 | pending = atomic_dec_return(&md->pending[rw]); |
| 712 | atomic_set(&dm_disk(md)->part0.in_flight[rw], pending); |
Nikanth Karthikesan | 316d315 | 2009-10-06 20:16:55 +0200 | [diff] [blame] | 713 | pending += atomic_read(&md->pending[rw^0x1]); |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 714 | |
Mikulas Patocka | d221d2e | 2008-11-13 23:39:10 +0000 | [diff] [blame] | 715 | /* nudge anyone waiting on suspend queue */ |
| 716 | if (!pending) |
| 717 | wake_up(&md->wait); |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 718 | } |
| 719 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | /* |
| 721 | * Add the bio to the list of deferred io. |
| 722 | */ |
Mikulas Patocka | 92c6390 | 2009-04-09 00:27:15 +0100 | [diff] [blame] | 723 | static void queue_io(struct mapped_device *md, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | { |
Kiyoshi Ueda | 05447420 | 2010-09-08 18:07:01 +0200 | [diff] [blame] | 725 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
Kiyoshi Ueda | 05447420 | 2010-09-08 18:07:01 +0200 | [diff] [blame] | 727 | spin_lock_irqsave(&md->deferred_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | bio_list_add(&md->deferred, bio); |
Kiyoshi Ueda | 05447420 | 2010-09-08 18:07:01 +0200 | [diff] [blame] | 729 | spin_unlock_irqrestore(&md->deferred_lock, flags); |
Tejun Heo | 6a8736d | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 730 | queue_work(md->wq, &md->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | /* |
| 734 | * Everyone (including functions in this file), should use this |
| 735 | * function to access the md->map field, and make sure they call |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 736 | * dm_put_live_table() when finished. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | */ |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 738 | struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | { |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 740 | *srcu_idx = srcu_read_lock(&md->io_barrier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 742 | return srcu_dereference(md->map, &md->io_barrier); |
| 743 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 745 | void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier) |
| 746 | { |
| 747 | srcu_read_unlock(&md->io_barrier, srcu_idx); |
| 748 | } |
| 749 | |
| 750 | void dm_sync_table(struct mapped_device *md) |
| 751 | { |
| 752 | synchronize_srcu(&md->io_barrier); |
| 753 | synchronize_rcu_expedited(); |
| 754 | } |
| 755 | |
| 756 | /* |
| 757 | * A fast alternative to dm_get_live_table/dm_put_live_table. |
| 758 | * The caller must not block between these two functions. |
| 759 | */ |
| 760 | static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU) |
| 761 | { |
| 762 | rcu_read_lock(); |
| 763 | return rcu_dereference(md->map); |
| 764 | } |
| 765 | |
| 766 | static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU) |
| 767 | { |
| 768 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 771 | /* |
Benjamin Marzinski | 86f1152 | 2014-08-13 13:53:43 -0500 | [diff] [blame] | 772 | * Open a table device so we can use it as a map destination. |
| 773 | */ |
| 774 | static int open_table_device(struct table_device *td, dev_t dev, |
| 775 | struct mapped_device *md) |
| 776 | { |
| 777 | static char *_claim_ptr = "I belong to device-mapper"; |
| 778 | struct block_device *bdev; |
| 779 | |
| 780 | int r; |
| 781 | |
| 782 | BUG_ON(td->dm_dev.bdev); |
| 783 | |
| 784 | bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr); |
| 785 | if (IS_ERR(bdev)) |
| 786 | return PTR_ERR(bdev); |
| 787 | |
| 788 | r = bd_link_disk_holder(bdev, dm_disk(md)); |
| 789 | if (r) { |
| 790 | blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL); |
| 791 | return r; |
| 792 | } |
| 793 | |
| 794 | td->dm_dev.bdev = bdev; |
| 795 | return 0; |
| 796 | } |
| 797 | |
| 798 | /* |
| 799 | * Close a table device that we've been using. |
| 800 | */ |
| 801 | static void close_table_device(struct table_device *td, struct mapped_device *md) |
| 802 | { |
| 803 | if (!td->dm_dev.bdev) |
| 804 | return; |
| 805 | |
| 806 | bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md)); |
| 807 | blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL); |
| 808 | td->dm_dev.bdev = NULL; |
| 809 | } |
| 810 | |
| 811 | static struct table_device *find_table_device(struct list_head *l, dev_t dev, |
| 812 | fmode_t mode) { |
| 813 | struct table_device *td; |
| 814 | |
| 815 | list_for_each_entry(td, l, list) |
| 816 | if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode) |
| 817 | return td; |
| 818 | |
| 819 | return NULL; |
| 820 | } |
| 821 | |
| 822 | int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode, |
| 823 | struct dm_dev **result) { |
| 824 | int r; |
| 825 | struct table_device *td; |
| 826 | |
| 827 | mutex_lock(&md->table_devices_lock); |
| 828 | td = find_table_device(&md->table_devices, dev, mode); |
| 829 | if (!td) { |
| 830 | td = kmalloc(sizeof(*td), GFP_KERNEL); |
| 831 | if (!td) { |
| 832 | mutex_unlock(&md->table_devices_lock); |
| 833 | return -ENOMEM; |
| 834 | } |
| 835 | |
| 836 | td->dm_dev.mode = mode; |
| 837 | td->dm_dev.bdev = NULL; |
| 838 | |
| 839 | if ((r = open_table_device(td, dev, md))) { |
| 840 | mutex_unlock(&md->table_devices_lock); |
| 841 | kfree(td); |
| 842 | return r; |
| 843 | } |
| 844 | |
| 845 | format_dev_t(td->dm_dev.name, dev); |
| 846 | |
| 847 | atomic_set(&td->count, 0); |
| 848 | list_add(&td->list, &md->table_devices); |
| 849 | } |
| 850 | atomic_inc(&td->count); |
| 851 | mutex_unlock(&md->table_devices_lock); |
| 852 | |
| 853 | *result = &td->dm_dev; |
| 854 | return 0; |
| 855 | } |
| 856 | EXPORT_SYMBOL_GPL(dm_get_table_device); |
| 857 | |
| 858 | void dm_put_table_device(struct mapped_device *md, struct dm_dev *d) |
| 859 | { |
| 860 | struct table_device *td = container_of(d, struct table_device, dm_dev); |
| 861 | |
| 862 | mutex_lock(&md->table_devices_lock); |
| 863 | if (atomic_dec_and_test(&td->count)) { |
| 864 | close_table_device(td, md); |
| 865 | list_del(&td->list); |
| 866 | kfree(td); |
| 867 | } |
| 868 | mutex_unlock(&md->table_devices_lock); |
| 869 | } |
| 870 | EXPORT_SYMBOL(dm_put_table_device); |
| 871 | |
| 872 | static void free_table_devices(struct list_head *devices) |
| 873 | { |
| 874 | struct list_head *tmp, *next; |
| 875 | |
| 876 | list_for_each_safe(tmp, next, devices) { |
| 877 | struct table_device *td = list_entry(tmp, struct table_device, list); |
| 878 | |
| 879 | DMWARN("dm_destroy: %s still exists with %d references", |
| 880 | td->dm_dev.name, atomic_read(&td->count)); |
| 881 | kfree(td); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | /* |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 886 | * Get the geometry associated with a dm device |
| 887 | */ |
| 888 | int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) |
| 889 | { |
| 890 | *geo = md->geometry; |
| 891 | |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | /* |
| 896 | * Set the geometry of a device. |
| 897 | */ |
| 898 | int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) |
| 899 | { |
| 900 | sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; |
| 901 | |
| 902 | if (geo->start > sz) { |
| 903 | DMWARN("Start sector is beyond the geometry limits."); |
| 904 | return -EINVAL; |
| 905 | } |
| 906 | |
| 907 | md->geometry = *geo; |
| 908 | |
| 909 | return 0; |
| 910 | } |
| 911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | /*----------------------------------------------------------------- |
| 913 | * CRUD START: |
| 914 | * A more elegant soln is in the works that uses the queue |
| 915 | * merge fn, unfortunately there are a couple of changes to |
| 916 | * the block layer that I want to make for this. So in the |
| 917 | * interests of getting something for people to use I give |
| 918 | * you this clearly demarcated crap. |
| 919 | *---------------------------------------------------------------*/ |
| 920 | |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 921 | static int __noflush_suspending(struct mapped_device *md) |
| 922 | { |
| 923 | return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); |
| 924 | } |
| 925 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | /* |
| 927 | * Decrements the number of outstanding ios that a bio has been |
| 928 | * cloned into, completing the original io if necc. |
| 929 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 930 | static void dec_pending(struct dm_io *io, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | { |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 932 | unsigned long flags; |
Milan Broz | b35f8ca | 2009-03-16 17:44:36 +0000 | [diff] [blame] | 933 | int io_error; |
| 934 | struct bio *bio; |
| 935 | struct mapped_device *md = io->md; |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 936 | |
| 937 | /* Push-back supersedes any I/O errors */ |
Kiyoshi Ueda | f88fb98 | 2009-10-16 23:18:15 +0100 | [diff] [blame] | 938 | if (unlikely(error)) { |
| 939 | spin_lock_irqsave(&io->endio_lock, flags); |
| 940 | if (!(io->error > 0 && __noflush_suspending(md))) |
| 941 | io->error = error; |
| 942 | spin_unlock_irqrestore(&io->endio_lock, flags); |
| 943 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | |
| 945 | if (atomic_dec_and_test(&io->io_count)) { |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 946 | if (io->error == DM_ENDIO_REQUEUE) { |
| 947 | /* |
| 948 | * Target requested pushing back the I/O. |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 949 | */ |
Mikulas Patocka | 022c261 | 2009-04-02 19:55:39 +0100 | [diff] [blame] | 950 | spin_lock_irqsave(&md->deferred_lock, flags); |
Tejun Heo | 6a8736d | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 951 | if (__noflush_suspending(md)) |
| 952 | bio_list_add_head(&md->deferred, io->bio); |
| 953 | else |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 954 | /* noflush suspend was interrupted. */ |
| 955 | io->error = -EIO; |
Mikulas Patocka | 022c261 | 2009-04-02 19:55:39 +0100 | [diff] [blame] | 956 | spin_unlock_irqrestore(&md->deferred_lock, flags); |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 957 | } |
| 958 | |
Milan Broz | b35f8ca | 2009-03-16 17:44:36 +0000 | [diff] [blame] | 959 | io_error = io->error; |
| 960 | bio = io->bio; |
Tejun Heo | 6a8736d | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 961 | end_io_acct(io); |
| 962 | free_io(md, io); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 963 | |
Tejun Heo | 6a8736d | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 964 | if (io_error == DM_ENDIO_REQUEUE) |
| 965 | return; |
| 966 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 967 | if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) { |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 968 | /* |
Tejun Heo | 6a8736d | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 969 | * Preflush done for flush with data, reissue |
| 970 | * without REQ_FLUSH. |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 971 | */ |
Tejun Heo | 6a8736d | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 972 | bio->bi_rw &= ~REQ_FLUSH; |
| 973 | queue_io(md, bio); |
Mikulas Patocka | af7e466 | 2009-04-09 00:27:16 +0100 | [diff] [blame] | 974 | } else { |
Mike Snitzer | b372d36 | 2010-09-08 18:07:01 +0200 | [diff] [blame] | 975 | /* done with normal IO or empty flush */ |
Linus Torvalds | 0a82a8d | 2013-04-18 09:00:26 -0700 | [diff] [blame] | 976 | trace_block_bio_complete(md->queue, bio, io_error); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 977 | bio->bi_error = io_error; |
| 978 | bio_endio(bio); |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 979 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | } |
| 981 | } |
| 982 | |
Mike Snitzer | 7eee4ae | 2014-06-02 15:50:06 -0400 | [diff] [blame] | 983 | static void disable_write_same(struct mapped_device *md) |
| 984 | { |
| 985 | struct queue_limits *limits = dm_get_queue_limits(md); |
| 986 | |
| 987 | /* device doesn't really support WRITE SAME, disable it */ |
| 988 | limits->max_write_same_sectors = 0; |
| 989 | } |
| 990 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 991 | static void clone_endio(struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 993 | int error = bio->bi_error; |
zhendong chen | 5164bec | 2014-12-17 14:37:04 +0800 | [diff] [blame] | 994 | int r = error; |
Mikulas Patocka | bfc6d41 | 2014-03-04 18:24:49 -0500 | [diff] [blame] | 995 | struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); |
Milan Broz | b35f8ca | 2009-03-16 17:44:36 +0000 | [diff] [blame] | 996 | struct dm_io *io = tio->io; |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 997 | struct mapped_device *md = tio->io->md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | dm_endio_fn endio = tio->ti->type->end_io; |
| 999 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | if (endio) { |
Mikulas Patocka | 7de3ee5 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 1001 | r = endio(tio->ti, bio, error); |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 1002 | if (r < 0 || r == DM_ENDIO_REQUEUE) |
| 1003 | /* |
| 1004 | * error and requeue request are handled |
| 1005 | * in dec_pending(). |
| 1006 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | error = r; |
Kiyoshi Ueda | 45cbcd7 | 2006-12-08 02:41:05 -0800 | [diff] [blame] | 1008 | else if (r == DM_ENDIO_INCOMPLETE) |
| 1009 | /* The target will handle the io */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1010 | return; |
Kiyoshi Ueda | 45cbcd7 | 2006-12-08 02:41:05 -0800 | [diff] [blame] | 1011 | else if (r) { |
| 1012 | DMWARN("unimplemented target endio return value: %d", r); |
| 1013 | BUG(); |
| 1014 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
Mike Snitzer | 7eee4ae | 2014-06-02 15:50:06 -0400 | [diff] [blame] | 1017 | if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) && |
| 1018 | !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors)) |
| 1019 | disable_write_same(md); |
| 1020 | |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 1021 | free_tio(md, tio); |
Milan Broz | b35f8ca | 2009-03-16 17:44:36 +0000 | [diff] [blame] | 1022 | dec_pending(io, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
Mike Snitzer | 78d8e58 | 2015-06-26 10:01:13 -0400 | [diff] [blame] | 1025 | /* |
| 1026 | * Partial completion handling for request-based dm |
| 1027 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1028 | static void end_clone_bio(struct bio *clone) |
Mike Snitzer | 78d8e58 | 2015-06-26 10:01:13 -0400 | [diff] [blame] | 1029 | { |
| 1030 | struct dm_rq_clone_bio_info *info = |
| 1031 | container_of(clone, struct dm_rq_clone_bio_info, clone); |
| 1032 | struct dm_rq_target_io *tio = info->tio; |
| 1033 | struct bio *bio = info->orig; |
| 1034 | unsigned int nr_bytes = info->orig->bi_iter.bi_size; |
Junichi Nomura | 50887bd | 2015-10-06 04:19:54 +0000 | [diff] [blame] | 1035 | int error = clone->bi_error; |
Mike Snitzer | 78d8e58 | 2015-06-26 10:01:13 -0400 | [diff] [blame] | 1036 | |
| 1037 | bio_put(clone); |
| 1038 | |
| 1039 | if (tio->error) |
| 1040 | /* |
| 1041 | * An error has already been detected on the request. |
| 1042 | * Once error occurred, just let clone->end_io() handle |
| 1043 | * the remainder. |
| 1044 | */ |
| 1045 | return; |
Junichi Nomura | 50887bd | 2015-10-06 04:19:54 +0000 | [diff] [blame] | 1046 | else if (error) { |
Mike Snitzer | 78d8e58 | 2015-06-26 10:01:13 -0400 | [diff] [blame] | 1047 | /* |
| 1048 | * Don't notice the error to the upper layer yet. |
| 1049 | * The error handling decision is made by the target driver, |
| 1050 | * when the request is completed. |
| 1051 | */ |
Junichi Nomura | 50887bd | 2015-10-06 04:19:54 +0000 | [diff] [blame] | 1052 | tio->error = error; |
Mike Snitzer | 78d8e58 | 2015-06-26 10:01:13 -0400 | [diff] [blame] | 1053 | return; |
| 1054 | } |
| 1055 | |
| 1056 | /* |
| 1057 | * I/O for the bio successfully completed. |
| 1058 | * Notice the data completion to the upper layer. |
| 1059 | */ |
| 1060 | |
| 1061 | /* |
| 1062 | * bios are processed from the head of the list. |
| 1063 | * So the completing bio should always be rq->bio. |
| 1064 | * If it's not, something wrong is happening. |
| 1065 | */ |
| 1066 | if (tio->orig->bio != bio) |
| 1067 | DMERR("bio completion is going in the middle of the request"); |
| 1068 | |
| 1069 | /* |
| 1070 | * Update the original request. |
| 1071 | * Do not use blk_end_request() here, because it may complete |
| 1072 | * the original request before the clone, and break the ordering. |
| 1073 | */ |
| 1074 | blk_update_request(tio->orig, 0, nr_bytes); |
| 1075 | } |
| 1076 | |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1077 | static struct dm_rq_target_io *tio_from_request(struct request *rq) |
| 1078 | { |
| 1079 | return (rq->q->mq_ops ? blk_mq_rq_to_pdu(rq) : rq->special); |
| 1080 | } |
| 1081 | |
Mikulas Patocka | e262f34 | 2015-06-09 17:22:49 -0400 | [diff] [blame] | 1082 | static void rq_end_stats(struct mapped_device *md, struct request *orig) |
| 1083 | { |
| 1084 | if (unlikely(dm_stats_used(&md->stats))) { |
| 1085 | struct dm_rq_target_io *tio = tio_from_request(orig); |
| 1086 | tio->duration_jiffies = jiffies - tio->duration_jiffies; |
| 1087 | dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig), |
| 1088 | tio->n_sectors, true, tio->duration_jiffies, |
| 1089 | &tio->stats_aux); |
| 1090 | } |
| 1091 | } |
| 1092 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1093 | /* |
| 1094 | * Don't touch any member of the md after calling this function because |
| 1095 | * the md may be freed in dm_put() at the end of this function. |
| 1096 | * Or do dm_get() before calling this function and dm_put() later. |
| 1097 | */ |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1098 | static void rq_completed(struct mapped_device *md, int rw, bool run_queue) |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1099 | { |
Kiyoshi Ueda | b4324fe | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 1100 | atomic_dec(&md->pending[rw]); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1101 | |
| 1102 | /* nudge anyone waiting on suspend queue */ |
Mike Snitzer | 621739b | 2015-07-08 16:08:24 -0400 | [diff] [blame] | 1103 | if (!md_in_flight(md)) |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1104 | wake_up(&md->wait); |
| 1105 | |
Jens Axboe | a8c32a5 | 2012-11-06 12:24:26 +0100 | [diff] [blame] | 1106 | /* |
| 1107 | * Run this off this callpath, as drivers could invoke end_io while |
| 1108 | * inside their request_fn (and holding the queue lock). Calling |
| 1109 | * back into ->request_fn() could deadlock attempting to grab the |
| 1110 | * queue lock again. |
| 1111 | */ |
Mike Snitzer | 9a0e609 | 2015-02-24 11:03:22 -0500 | [diff] [blame] | 1112 | if (run_queue) { |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1113 | if (md->queue->mq_ops) |
| 1114 | blk_mq_run_hw_queues(md->queue, true); |
Mike Snitzer | 621739b | 2015-07-08 16:08:24 -0400 | [diff] [blame] | 1115 | else |
Mike Snitzer | 9a0e609 | 2015-02-24 11:03:22 -0500 | [diff] [blame] | 1116 | blk_run_queue_async(md->queue); |
| 1117 | } |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1118 | |
| 1119 | /* |
| 1120 | * dm_put() must be at the end of this function. See the comment above |
| 1121 | */ |
| 1122 | dm_put(md); |
| 1123 | } |
| 1124 | |
Mike Snitzer | e5d8de3 | 2015-05-28 15:12:52 -0400 | [diff] [blame] | 1125 | static void free_rq_clone(struct request *clone) |
Kiyoshi Ueda | a77e28c | 2009-09-04 20:40:16 +0100 | [diff] [blame] | 1126 | { |
| 1127 | struct dm_rq_target_io *tio = clone->end_io_data; |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1128 | struct mapped_device *md = tio->md; |
Kiyoshi Ueda | a77e28c | 2009-09-04 20:40:16 +0100 | [diff] [blame] | 1129 | |
Mike Snitzer | 78d8e58 | 2015-06-26 10:01:13 -0400 | [diff] [blame] | 1130 | blk_rq_unprep_clone(clone); |
| 1131 | |
Mike Snitzer | aa6df8d | 2015-04-29 10:48:09 -0400 | [diff] [blame] | 1132 | if (md->type == DM_TYPE_MQ_REQUEST_BASED) |
| 1133 | /* stacked on blk-mq queue(s) */ |
Mike Snitzer | e5863d9 | 2014-12-17 21:08:12 -0500 | [diff] [blame] | 1134 | tio->ti->type->release_clone_rq(clone); |
Mike Snitzer | 0223334 | 2015-03-10 23:49:26 -0400 | [diff] [blame] | 1135 | else if (!md->queue->mq_ops) |
| 1136 | /* request_fn queue stacked on request_fn queue(s) */ |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1137 | free_clone_request(md, clone); |
Mike Snitzer | aa6df8d | 2015-04-29 10:48:09 -0400 | [diff] [blame] | 1138 | /* |
| 1139 | * NOTE: for the blk-mq queue stacked on request_fn queue(s) case: |
| 1140 | * no need to call free_clone_request() because we leverage blk-mq by |
| 1141 | * allocating the clone at the end of the blk-mq pdu (see: clone_rq) |
| 1142 | */ |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1143 | |
| 1144 | if (!md->queue->mq_ops) |
| 1145 | free_rq_tio(tio); |
Kiyoshi Ueda | a77e28c | 2009-09-04 20:40:16 +0100 | [diff] [blame] | 1146 | } |
| 1147 | |
Kiyoshi Ueda | 980691e | 2009-12-10 23:52:17 +0000 | [diff] [blame] | 1148 | /* |
| 1149 | * Complete the clone and the original request. |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1150 | * Must be called without clone's queue lock held, |
| 1151 | * see end_clone_request() for more details. |
Kiyoshi Ueda | 980691e | 2009-12-10 23:52:17 +0000 | [diff] [blame] | 1152 | */ |
| 1153 | static void dm_end_request(struct request *clone, int error) |
| 1154 | { |
| 1155 | int rw = rq_data_dir(clone); |
| 1156 | struct dm_rq_target_io *tio = clone->end_io_data; |
| 1157 | struct mapped_device *md = tio->md; |
| 1158 | struct request *rq = tio->orig; |
| 1159 | |
Tejun Heo | 29e4013 | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 1160 | if (rq->cmd_type == REQ_TYPE_BLOCK_PC) { |
Kiyoshi Ueda | 980691e | 2009-12-10 23:52:17 +0000 | [diff] [blame] | 1161 | rq->errors = clone->errors; |
| 1162 | rq->resid_len = clone->resid_len; |
| 1163 | |
| 1164 | if (rq->sense) |
| 1165 | /* |
| 1166 | * We are using the sense buffer of the original |
| 1167 | * request. |
| 1168 | * So setting the length of the sense data is enough. |
| 1169 | */ |
| 1170 | rq->sense_len = clone->sense_len; |
| 1171 | } |
| 1172 | |
Mike Snitzer | e5d8de3 | 2015-05-28 15:12:52 -0400 | [diff] [blame] | 1173 | free_rq_clone(clone); |
Mikulas Patocka | e262f34 | 2015-06-09 17:22:49 -0400 | [diff] [blame] | 1174 | rq_end_stats(md, rq); |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1175 | if (!rq->q->mq_ops) |
| 1176 | blk_end_request_all(rq, error); |
| 1177 | else |
| 1178 | blk_mq_end_request(rq, error); |
Tejun Heo | 29e4013 | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 1179 | rq_completed(md, rw, true); |
Kiyoshi Ueda | 980691e | 2009-12-10 23:52:17 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1182 | static void dm_unprep_request(struct request *rq) |
| 1183 | { |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1184 | struct dm_rq_target_io *tio = tio_from_request(rq); |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1185 | struct request *clone = tio->clone; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1186 | |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1187 | if (!rq->q->mq_ops) { |
| 1188 | rq->special = NULL; |
| 1189 | rq->cmd_flags &= ~REQ_DONTPREP; |
| 1190 | } |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1191 | |
Mike Snitzer | e5863d9 | 2014-12-17 21:08:12 -0500 | [diff] [blame] | 1192 | if (clone) |
Mike Snitzer | e5d8de3 | 2015-05-28 15:12:52 -0400 | [diff] [blame] | 1193 | free_rq_clone(clone); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | /* |
| 1197 | * Requeue the original request of a clone. |
| 1198 | */ |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1199 | static void old_requeue_request(struct request *rq) |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1200 | { |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1201 | struct request_queue *q = rq->q; |
| 1202 | unsigned long flags; |
| 1203 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1204 | spin_lock_irqsave(q->queue_lock, flags); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1205 | blk_requeue_request(q, rq); |
Junichi Nomura | 4ae9944 | 2015-05-26 08:25:54 +0000 | [diff] [blame] | 1206 | blk_run_queue_async(q); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1207 | spin_unlock_irqrestore(q->queue_lock, flags); |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1208 | } |
| 1209 | |
Mike Snitzer | 2d76fff | 2015-04-29 12:07:12 -0400 | [diff] [blame] | 1210 | static void dm_requeue_original_request(struct mapped_device *md, |
| 1211 | struct request *rq) |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1212 | { |
| 1213 | int rw = rq_data_dir(rq); |
| 1214 | |
| 1215 | dm_unprep_request(rq); |
| 1216 | |
Mikulas Patocka | e262f34 | 2015-06-09 17:22:49 -0400 | [diff] [blame] | 1217 | rq_end_stats(md, rq); |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1218 | if (!rq->q->mq_ops) |
| 1219 | old_requeue_request(rq); |
| 1220 | else { |
| 1221 | blk_mq_requeue_request(rq); |
| 1222 | blk_mq_kick_requeue_list(rq->q); |
| 1223 | } |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1224 | |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1225 | rq_completed(md, rw, false); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1226 | } |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1227 | |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1228 | static void old_stop_queue(struct request_queue *q) |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1229 | { |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1230 | unsigned long flags; |
| 1231 | |
| 1232 | if (blk_queue_stopped(q)) |
| 1233 | return; |
| 1234 | |
| 1235 | spin_lock_irqsave(q->queue_lock, flags); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1236 | blk_stop_queue(q); |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1237 | spin_unlock_irqrestore(q->queue_lock, flags); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | static void stop_queue(struct request_queue *q) |
| 1241 | { |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1242 | if (!q->mq_ops) |
| 1243 | old_stop_queue(q); |
| 1244 | else |
| 1245 | blk_mq_stop_hw_queues(q); |
| 1246 | } |
| 1247 | |
| 1248 | static void old_start_queue(struct request_queue *q) |
| 1249 | { |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1250 | unsigned long flags; |
| 1251 | |
| 1252 | spin_lock_irqsave(q->queue_lock, flags); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1253 | if (blk_queue_stopped(q)) |
| 1254 | blk_start_queue(q); |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1255 | spin_unlock_irqrestore(q->queue_lock, flags); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | static void start_queue(struct request_queue *q) |
| 1259 | { |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1260 | if (!q->mq_ops) |
| 1261 | old_start_queue(q); |
| 1262 | else |
| 1263 | blk_mq_start_stopped_hw_queues(q, true); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1264 | } |
| 1265 | |
Kiyoshi Ueda | 11a6824 | 2009-12-10 23:52:17 +0000 | [diff] [blame] | 1266 | static void dm_done(struct request *clone, int error, bool mapped) |
| 1267 | { |
| 1268 | int r = error; |
| 1269 | struct dm_rq_target_io *tio = clone->end_io_data; |
Mike Snitzer | ba1cbad | 2012-09-26 23:45:42 +0100 | [diff] [blame] | 1270 | dm_request_endio_fn rq_end_io = NULL; |
Kiyoshi Ueda | 11a6824 | 2009-12-10 23:52:17 +0000 | [diff] [blame] | 1271 | |
Mike Snitzer | ba1cbad | 2012-09-26 23:45:42 +0100 | [diff] [blame] | 1272 | if (tio->ti) { |
| 1273 | rq_end_io = tio->ti->type->rq_end_io; |
| 1274 | |
| 1275 | if (mapped && rq_end_io) |
| 1276 | r = rq_end_io(tio->ti, clone, error, &tio->info); |
| 1277 | } |
Kiyoshi Ueda | 11a6824 | 2009-12-10 23:52:17 +0000 | [diff] [blame] | 1278 | |
Mike Snitzer | 7eee4ae | 2014-06-02 15:50:06 -0400 | [diff] [blame] | 1279 | if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) && |
| 1280 | !clone->q->limits.max_write_same_sectors)) |
| 1281 | disable_write_same(tio->md); |
| 1282 | |
Kiyoshi Ueda | 11a6824 | 2009-12-10 23:52:17 +0000 | [diff] [blame] | 1283 | if (r <= 0) |
| 1284 | /* The target wants to complete the I/O */ |
| 1285 | dm_end_request(clone, r); |
| 1286 | else if (r == DM_ENDIO_INCOMPLETE) |
| 1287 | /* The target will handle the I/O */ |
| 1288 | return; |
| 1289 | else if (r == DM_ENDIO_REQUEUE) |
| 1290 | /* The target wants to requeue the I/O */ |
Mike Snitzer | 2d76fff | 2015-04-29 12:07:12 -0400 | [diff] [blame] | 1291 | dm_requeue_original_request(tio->md, tio->orig); |
Kiyoshi Ueda | 11a6824 | 2009-12-10 23:52:17 +0000 | [diff] [blame] | 1292 | else { |
| 1293 | DMWARN("unimplemented target endio return value: %d", r); |
| 1294 | BUG(); |
| 1295 | } |
| 1296 | } |
| 1297 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1298 | /* |
| 1299 | * Request completion handler for request-based dm |
| 1300 | */ |
| 1301 | static void dm_softirq_done(struct request *rq) |
| 1302 | { |
Kiyoshi Ueda | 11a6824 | 2009-12-10 23:52:17 +0000 | [diff] [blame] | 1303 | bool mapped = true; |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1304 | struct dm_rq_target_io *tio = tio_from_request(rq); |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1305 | struct request *clone = tio->clone; |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1306 | int rw; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1307 | |
Mike Snitzer | e5863d9 | 2014-12-17 21:08:12 -0500 | [diff] [blame] | 1308 | if (!clone) { |
Mikulas Patocka | e262f34 | 2015-06-09 17:22:49 -0400 | [diff] [blame] | 1309 | rq_end_stats(tio->md, rq); |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1310 | rw = rq_data_dir(rq); |
| 1311 | if (!rq->q->mq_ops) { |
| 1312 | blk_end_request_all(rq, tio->error); |
| 1313 | rq_completed(tio->md, rw, false); |
| 1314 | free_rq_tio(tio); |
| 1315 | } else { |
| 1316 | blk_mq_end_request(rq, tio->error); |
| 1317 | rq_completed(tio->md, rw, false); |
| 1318 | } |
Mike Snitzer | e5863d9 | 2014-12-17 21:08:12 -0500 | [diff] [blame] | 1319 | return; |
| 1320 | } |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1321 | |
Kiyoshi Ueda | 11a6824 | 2009-12-10 23:52:17 +0000 | [diff] [blame] | 1322 | if (rq->cmd_flags & REQ_FAILED) |
| 1323 | mapped = false; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1324 | |
Kiyoshi Ueda | 11a6824 | 2009-12-10 23:52:17 +0000 | [diff] [blame] | 1325 | dm_done(clone, tio->error, mapped); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | /* |
| 1329 | * Complete the clone and the original request with the error status |
| 1330 | * through softirq context. |
| 1331 | */ |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1332 | static void dm_complete_request(struct request *rq, int error) |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1333 | { |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1334 | struct dm_rq_target_io *tio = tio_from_request(rq); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1335 | |
| 1336 | tio->error = error; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1337 | blk_complete_request(rq); |
| 1338 | } |
| 1339 | |
| 1340 | /* |
| 1341 | * Complete the not-mapped clone and the original request with the error status |
| 1342 | * through softirq context. |
| 1343 | * Target's rq_end_io() function isn't called. |
Mike Snitzer | e5863d9 | 2014-12-17 21:08:12 -0500 | [diff] [blame] | 1344 | * This may be used when the target's map_rq() or clone_and_map_rq() functions fail. |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1345 | */ |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1346 | static void dm_kill_unmapped_request(struct request *rq, int error) |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1347 | { |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1348 | rq->cmd_flags |= REQ_FAILED; |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1349 | dm_complete_request(rq, error); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1350 | } |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1351 | |
| 1352 | /* |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1353 | * Called with the clone's queue lock held (for non-blk-mq) |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1354 | */ |
| 1355 | static void end_clone_request(struct request *clone, int error) |
| 1356 | { |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1357 | struct dm_rq_target_io *tio = clone->end_io_data; |
| 1358 | |
Mike Snitzer | e5863d9 | 2014-12-17 21:08:12 -0500 | [diff] [blame] | 1359 | if (!clone->q->mq_ops) { |
| 1360 | /* |
| 1361 | * For just cleaning up the information of the queue in which |
| 1362 | * the clone was dispatched. |
| 1363 | * The clone is *NOT* freed actually here because it is alloced |
| 1364 | * from dm own mempool (REQ_ALLOCED isn't set). |
| 1365 | */ |
| 1366 | __blk_put_request(clone->q, clone); |
| 1367 | } |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1368 | |
| 1369 | /* |
| 1370 | * Actual request completion is done in a softirq context which doesn't |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1371 | * hold the clone's queue lock. Otherwise, deadlock could occur because: |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1372 | * - another request may be submitted by the upper level driver |
| 1373 | * of the stacking during the completion |
| 1374 | * - the submission which requires queue lock may be done |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1375 | * against this clone's queue |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1376 | */ |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1377 | dm_complete_request(tio->orig, error); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1378 | } |
| 1379 | |
Mike Snitzer | 56a67df | 2010-08-12 04:14:10 +0100 | [diff] [blame] | 1380 | /* |
| 1381 | * Return maximum size of I/O possible at the supplied sector up to the current |
| 1382 | * target boundary. |
| 1383 | */ |
| 1384 | static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | { |
Mike Snitzer | 56a67df | 2010-08-12 04:14:10 +0100 | [diff] [blame] | 1386 | sector_t target_offset = dm_target_offset(ti, sector); |
| 1387 | |
| 1388 | return ti->len - target_offset; |
| 1389 | } |
| 1390 | |
| 1391 | static sector_t max_io_len(sector_t sector, struct dm_target *ti) |
| 1392 | { |
| 1393 | sector_t len = max_io_len_target_boundary(sector, ti); |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 1394 | sector_t offset, max_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | |
| 1396 | /* |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 1397 | * Does the target need to split even further? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | */ |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 1399 | if (ti->max_io_len) { |
| 1400 | offset = dm_target_offset(ti, sector); |
| 1401 | if (unlikely(ti->max_io_len & (ti->max_io_len - 1))) |
| 1402 | max_len = sector_div(offset, ti->max_io_len); |
| 1403 | else |
| 1404 | max_len = offset & (ti->max_io_len - 1); |
| 1405 | max_len = ti->max_io_len - max_len; |
| 1406 | |
| 1407 | if (len > max_len) |
| 1408 | len = max_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | return len; |
| 1412 | } |
| 1413 | |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 1414 | int dm_set_target_max_io_len(struct dm_target *ti, sector_t len) |
| 1415 | { |
| 1416 | if (len > UINT_MAX) { |
| 1417 | DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)", |
| 1418 | (unsigned long long)len, UINT_MAX); |
| 1419 | ti->error = "Maximum size of target IO is too large"; |
| 1420 | return -EINVAL; |
| 1421 | } |
| 1422 | |
| 1423 | ti->max_io_len = (uint32_t) len; |
| 1424 | |
| 1425 | return 0; |
| 1426 | } |
| 1427 | EXPORT_SYMBOL_GPL(dm_set_target_max_io_len); |
| 1428 | |
Mikulas Patocka | 1dd40c3 | 2014-03-14 18:41:24 -0400 | [diff] [blame] | 1429 | /* |
| 1430 | * A target may call dm_accept_partial_bio only from the map routine. It is |
| 1431 | * allowed for all bio types except REQ_FLUSH. |
| 1432 | * |
| 1433 | * dm_accept_partial_bio informs the dm that the target only wants to process |
| 1434 | * additional n_sectors sectors of the bio and the rest of the data should be |
| 1435 | * sent in a next bio. |
| 1436 | * |
| 1437 | * A diagram that explains the arithmetics: |
| 1438 | * +--------------------+---------------+-------+ |
| 1439 | * | 1 | 2 | 3 | |
| 1440 | * +--------------------+---------------+-------+ |
| 1441 | * |
| 1442 | * <-------------- *tio->len_ptr ---------------> |
| 1443 | * <------- bi_size -------> |
| 1444 | * <-- n_sectors --> |
| 1445 | * |
| 1446 | * Region 1 was already iterated over with bio_advance or similar function. |
| 1447 | * (it may be empty if the target doesn't use bio_advance) |
| 1448 | * Region 2 is the remaining bio size that the target wants to process. |
| 1449 | * (it may be empty if region 1 is non-empty, although there is no reason |
| 1450 | * to make it empty) |
| 1451 | * The target requires that region 3 is to be sent in the next bio. |
| 1452 | * |
| 1453 | * If the target wants to receive multiple copies of the bio (via num_*bios, etc), |
| 1454 | * the partially processed part (the sum of regions 1+2) must be the same for all |
| 1455 | * copies of the bio. |
| 1456 | */ |
| 1457 | void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors) |
| 1458 | { |
| 1459 | struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); |
| 1460 | unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT; |
| 1461 | BUG_ON(bio->bi_rw & REQ_FLUSH); |
| 1462 | BUG_ON(bi_size > *tio->len_ptr); |
| 1463 | BUG_ON(n_sectors > bi_size); |
| 1464 | *tio->len_ptr -= bi_size - n_sectors; |
| 1465 | bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT; |
| 1466 | } |
| 1467 | EXPORT_SYMBOL_GPL(dm_accept_partial_bio); |
| 1468 | |
Alasdair G Kergon | bd2a49b | 2013-03-01 22:45:46 +0000 | [diff] [blame] | 1469 | static void __map_bio(struct dm_target_io *tio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | { |
| 1471 | int r; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 1472 | sector_t sector; |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 1473 | struct mapped_device *md; |
Mikulas Patocka | dba1416 | 2012-10-12 21:02:15 +0100 | [diff] [blame] | 1474 | struct bio *clone = &tio->clone; |
Alasdair G Kergon | bd2a49b | 2013-03-01 22:45:46 +0000 | [diff] [blame] | 1475 | struct dm_target *ti = tio->ti; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | clone->bi_end_io = clone_endio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | |
| 1479 | /* |
| 1480 | * Map the clone. If r == 0 we don't need to do |
| 1481 | * anything, the target has assumed ownership of |
| 1482 | * this io. |
| 1483 | */ |
| 1484 | atomic_inc(&tio->io->io_count); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1485 | sector = clone->bi_iter.bi_sector; |
Mikulas Patocka | 7de3ee5 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 1486 | r = ti->type->map(ti, clone); |
Kiyoshi Ueda | 45cbcd7 | 2006-12-08 02:41:05 -0800 | [diff] [blame] | 1487 | if (r == DM_MAPIO_REMAPPED) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | /* the bio has been remapped so dispatch it */ |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 1489 | |
Mike Snitzer | d07335e | 2010-11-16 12:52:38 +0100 | [diff] [blame] | 1490 | trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone, |
| 1491 | tio->io->bio->bi_bdev->bd_dev, sector); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 1492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | generic_make_request(clone); |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 1494 | } else if (r < 0 || r == DM_MAPIO_REQUEUE) { |
| 1495 | /* error the io and bail out, or requeue it if needed */ |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 1496 | md = tio->io->md; |
| 1497 | dec_pending(tio->io, r); |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 1498 | free_tio(md, tio); |
Mikulas Patocka | ab37844 | 2015-07-01 17:30:36 -0400 | [diff] [blame] | 1499 | } else if (r != DM_MAPIO_SUBMITTED) { |
Kiyoshi Ueda | 45cbcd7 | 2006-12-08 02:41:05 -0800 | [diff] [blame] | 1500 | DMWARN("unimplemented target map return value: %d", r); |
| 1501 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | struct clone_info { |
| 1506 | struct mapped_device *md; |
| 1507 | struct dm_table *map; |
| 1508 | struct bio *bio; |
| 1509 | struct dm_io *io; |
| 1510 | sector_t sector; |
Mikulas Patocka | e0d6609 | 2014-03-14 18:40:39 -0400 | [diff] [blame] | 1511 | unsigned sector_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | }; |
| 1513 | |
Mikulas Patocka | e0d6609 | 2014-03-14 18:40:39 -0400 | [diff] [blame] | 1514 | static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len) |
Alasdair G Kergon | bd2a49b | 2013-03-01 22:45:46 +0000 | [diff] [blame] | 1515 | { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1516 | bio->bi_iter.bi_sector = sector; |
| 1517 | bio->bi_iter.bi_size = to_bytes(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | /* |
| 1521 | * Creates a bio that consists of range of complete bvecs. |
| 1522 | */ |
Mikulas Patocka | dba1416 | 2012-10-12 21:02:15 +0100 | [diff] [blame] | 1523 | static void clone_bio(struct dm_target_io *tio, struct bio *bio, |
Kent Overstreet | 1c3b13e | 2013-10-29 17:17:49 -0700 | [diff] [blame] | 1524 | sector_t sector, unsigned len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | { |
Mikulas Patocka | dba1416 | 2012-10-12 21:02:15 +0100 | [diff] [blame] | 1526 | struct bio *clone = &tio->clone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | |
Kent Overstreet | 1c3b13e | 2013-10-29 17:17:49 -0700 | [diff] [blame] | 1528 | __bio_clone_fast(clone, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | |
Kent Overstreet | 1c3b13e | 2013-10-29 17:17:49 -0700 | [diff] [blame] | 1530 | if (bio_integrity(bio)) |
| 1531 | bio_integrity_clone(clone, bio, GFP_NOIO); |
| 1532 | |
| 1533 | bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector)); |
| 1534 | clone->bi_iter.bi_size = to_bytes(len); |
| 1535 | |
| 1536 | if (bio_integrity(bio)) |
| 1537 | bio_integrity_trim(clone, 0, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | } |
| 1539 | |
Alasdair G Kergon | 9015df2 | 2009-06-22 10:12:21 +0100 | [diff] [blame] | 1540 | static struct dm_target_io *alloc_tio(struct clone_info *ci, |
Junichi Nomura | 9977827 | 2014-10-03 11:55:16 +0000 | [diff] [blame] | 1541 | struct dm_target *ti, |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1542 | unsigned target_bio_nr) |
Mikulas Patocka | f9ab94c | 2009-06-22 10:12:20 +0100 | [diff] [blame] | 1543 | { |
Mikulas Patocka | dba1416 | 2012-10-12 21:02:15 +0100 | [diff] [blame] | 1544 | struct dm_target_io *tio; |
| 1545 | struct bio *clone; |
| 1546 | |
Junichi Nomura | 9977827 | 2014-10-03 11:55:16 +0000 | [diff] [blame] | 1547 | clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs); |
Mikulas Patocka | dba1416 | 2012-10-12 21:02:15 +0100 | [diff] [blame] | 1548 | tio = container_of(clone, struct dm_target_io, clone); |
Mikulas Patocka | f9ab94c | 2009-06-22 10:12:20 +0100 | [diff] [blame] | 1549 | |
| 1550 | tio->io = ci->io; |
| 1551 | tio->ti = ti; |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1552 | tio->target_bio_nr = target_bio_nr; |
Alasdair G Kergon | 9015df2 | 2009-06-22 10:12:21 +0100 | [diff] [blame] | 1553 | |
| 1554 | return tio; |
| 1555 | } |
| 1556 | |
Alasdair G Kergon | 14fe594 | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1557 | static void __clone_and_map_simple_bio(struct clone_info *ci, |
| 1558 | struct dm_target *ti, |
Mikulas Patocka | 1dd40c3 | 2014-03-14 18:41:24 -0400 | [diff] [blame] | 1559 | unsigned target_bio_nr, unsigned *len) |
Alasdair G Kergon | 9015df2 | 2009-06-22 10:12:21 +0100 | [diff] [blame] | 1560 | { |
Junichi Nomura | 9977827 | 2014-10-03 11:55:16 +0000 | [diff] [blame] | 1561 | struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr); |
Mikulas Patocka | dba1416 | 2012-10-12 21:02:15 +0100 | [diff] [blame] | 1562 | struct bio *clone = &tio->clone; |
Alasdair G Kergon | 9015df2 | 2009-06-22 10:12:21 +0100 | [diff] [blame] | 1563 | |
Mikulas Patocka | 1dd40c3 | 2014-03-14 18:41:24 -0400 | [diff] [blame] | 1564 | tio->len_ptr = len; |
| 1565 | |
Junichi Nomura | 9977827 | 2014-10-03 11:55:16 +0000 | [diff] [blame] | 1566 | __bio_clone_fast(clone, ci->bio); |
Alasdair G Kergon | bd2a49b | 2013-03-01 22:45:46 +0000 | [diff] [blame] | 1567 | if (len) |
Mikulas Patocka | 1dd40c3 | 2014-03-14 18:41:24 -0400 | [diff] [blame] | 1568 | bio_setup_sector(clone, ci->sector, *len); |
Mikulas Patocka | f9ab94c | 2009-06-22 10:12:20 +0100 | [diff] [blame] | 1569 | |
Alasdair G Kergon | bd2a49b | 2013-03-01 22:45:46 +0000 | [diff] [blame] | 1570 | __map_bio(tio); |
Mikulas Patocka | f9ab94c | 2009-06-22 10:12:20 +0100 | [diff] [blame] | 1571 | } |
| 1572 | |
Alasdair G Kergon | 14fe594 | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1573 | static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti, |
Mikulas Patocka | 1dd40c3 | 2014-03-14 18:41:24 -0400 | [diff] [blame] | 1574 | unsigned num_bios, unsigned *len) |
Mike Snitzer | 06a426c | 2010-08-12 04:14:09 +0100 | [diff] [blame] | 1575 | { |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1576 | unsigned target_bio_nr; |
Mike Snitzer | 06a426c | 2010-08-12 04:14:09 +0100 | [diff] [blame] | 1577 | |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1578 | for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++) |
Alasdair G Kergon | 14fe594 | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1579 | __clone_and_map_simple_bio(ci, ti, target_bio_nr, len); |
Mike Snitzer | 06a426c | 2010-08-12 04:14:09 +0100 | [diff] [blame] | 1580 | } |
| 1581 | |
Alasdair G Kergon | 14fe594 | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1582 | static int __send_empty_flush(struct clone_info *ci) |
Mikulas Patocka | f9ab94c | 2009-06-22 10:12:20 +0100 | [diff] [blame] | 1583 | { |
Mike Snitzer | 06a426c | 2010-08-12 04:14:09 +0100 | [diff] [blame] | 1584 | unsigned target_nr = 0; |
Mikulas Patocka | f9ab94c | 2009-06-22 10:12:20 +0100 | [diff] [blame] | 1585 | struct dm_target *ti; |
| 1586 | |
Mike Snitzer | b372d36 | 2010-09-08 18:07:01 +0200 | [diff] [blame] | 1587 | BUG_ON(bio_has_data(ci->bio)); |
Mikulas Patocka | f9ab94c | 2009-06-22 10:12:20 +0100 | [diff] [blame] | 1588 | while ((ti = dm_table_get_target(ci->map, target_nr++))) |
Mikulas Patocka | 1dd40c3 | 2014-03-14 18:41:24 -0400 | [diff] [blame] | 1589 | __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL); |
Mikulas Patocka | f9ab94c | 2009-06-22 10:12:20 +0100 | [diff] [blame] | 1590 | |
Mikulas Patocka | f9ab94c | 2009-06-22 10:12:20 +0100 | [diff] [blame] | 1591 | return 0; |
| 1592 | } |
| 1593 | |
Alasdair G Kergon | e4c9381 | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1594 | static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti, |
Mikulas Patocka | 1dd40c3 | 2014-03-14 18:41:24 -0400 | [diff] [blame] | 1595 | sector_t sector, unsigned *len) |
Mike Snitzer | 5ae89a8 | 2010-08-12 04:14:08 +0100 | [diff] [blame] | 1596 | { |
Mikulas Patocka | dba1416 | 2012-10-12 21:02:15 +0100 | [diff] [blame] | 1597 | struct bio *bio = ci->bio; |
Mike Snitzer | 5ae89a8 | 2010-08-12 04:14:08 +0100 | [diff] [blame] | 1598 | struct dm_target_io *tio; |
Alasdair G Kergon | b0d8ed4 | 2013-03-01 22:45:49 +0000 | [diff] [blame] | 1599 | unsigned target_bio_nr; |
| 1600 | unsigned num_target_bios = 1; |
Mike Snitzer | 5ae89a8 | 2010-08-12 04:14:08 +0100 | [diff] [blame] | 1601 | |
Alasdair G Kergon | b0d8ed4 | 2013-03-01 22:45:49 +0000 | [diff] [blame] | 1602 | /* |
| 1603 | * Does the target want to receive duplicate copies of the bio? |
| 1604 | */ |
| 1605 | if (bio_data_dir(bio) == WRITE && ti->num_write_bios) |
| 1606 | num_target_bios = ti->num_write_bios(ti, bio); |
Alasdair G Kergon | e4c9381 | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1607 | |
Alasdair G Kergon | b0d8ed4 | 2013-03-01 22:45:49 +0000 | [diff] [blame] | 1608 | for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) { |
Junichi Nomura | 9977827 | 2014-10-03 11:55:16 +0000 | [diff] [blame] | 1609 | tio = alloc_tio(ci, ti, target_bio_nr); |
Mikulas Patocka | 1dd40c3 | 2014-03-14 18:41:24 -0400 | [diff] [blame] | 1610 | tio->len_ptr = len; |
| 1611 | clone_bio(tio, bio, sector, *len); |
Alasdair G Kergon | b0d8ed4 | 2013-03-01 22:45:49 +0000 | [diff] [blame] | 1612 | __map_bio(tio); |
| 1613 | } |
Mike Snitzer | 5ae89a8 | 2010-08-12 04:14:08 +0100 | [diff] [blame] | 1614 | } |
| 1615 | |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1616 | typedef unsigned (*get_num_bios_fn)(struct dm_target *ti); |
Mike Snitzer | 23508a9 | 2012-12-21 20:23:37 +0000 | [diff] [blame] | 1617 | |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1618 | static unsigned get_num_discard_bios(struct dm_target *ti) |
Mike Snitzer | 23508a9 | 2012-12-21 20:23:37 +0000 | [diff] [blame] | 1619 | { |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1620 | return ti->num_discard_bios; |
Mike Snitzer | 23508a9 | 2012-12-21 20:23:37 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1623 | static unsigned get_num_write_same_bios(struct dm_target *ti) |
Mike Snitzer | 23508a9 | 2012-12-21 20:23:37 +0000 | [diff] [blame] | 1624 | { |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1625 | return ti->num_write_same_bios; |
Mike Snitzer | 23508a9 | 2012-12-21 20:23:37 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | typedef bool (*is_split_required_fn)(struct dm_target *ti); |
| 1629 | |
| 1630 | static bool is_split_required_for_discard(struct dm_target *ti) |
| 1631 | { |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1632 | return ti->split_discard_bios; |
Mike Snitzer | 23508a9 | 2012-12-21 20:23:37 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
Alasdair G Kergon | 14fe594 | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1635 | static int __send_changing_extent_only(struct clone_info *ci, |
| 1636 | get_num_bios_fn get_num_bios, |
| 1637 | is_split_required_fn is_split_required) |
Mike Snitzer | 5ae89a8 | 2010-08-12 04:14:08 +0100 | [diff] [blame] | 1638 | { |
| 1639 | struct dm_target *ti; |
Mikulas Patocka | e0d6609 | 2014-03-14 18:40:39 -0400 | [diff] [blame] | 1640 | unsigned len; |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1641 | unsigned num_bios; |
Mike Snitzer | 5ae89a8 | 2010-08-12 04:14:08 +0100 | [diff] [blame] | 1642 | |
Mike Snitzer | a79245b | 2010-08-12 04:14:24 +0100 | [diff] [blame] | 1643 | do { |
| 1644 | ti = dm_table_find_target(ci->map, ci->sector); |
| 1645 | if (!dm_target_is_valid(ti)) |
| 1646 | return -EIO; |
Mike Snitzer | 5ae89a8 | 2010-08-12 04:14:08 +0100 | [diff] [blame] | 1647 | |
Mike Snitzer | 5ae89a8 | 2010-08-12 04:14:08 +0100 | [diff] [blame] | 1648 | /* |
Mike Snitzer | 23508a9 | 2012-12-21 20:23:37 +0000 | [diff] [blame] | 1649 | * Even though the device advertised support for this type of |
| 1650 | * request, that does not mean every target supports it, and |
Mike Snitzer | 936688d | 2011-08-02 12:32:01 +0100 | [diff] [blame] | 1651 | * reconfiguration might also have changed that since the |
Mike Snitzer | a79245b | 2010-08-12 04:14:24 +0100 | [diff] [blame] | 1652 | * check was performed. |
Mike Snitzer | 5ae89a8 | 2010-08-12 04:14:08 +0100 | [diff] [blame] | 1653 | */ |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1654 | num_bios = get_num_bios ? get_num_bios(ti) : 0; |
| 1655 | if (!num_bios) |
Mike Snitzer | a79245b | 2010-08-12 04:14:24 +0100 | [diff] [blame] | 1656 | return -EOPNOTSUPP; |
Mike Snitzer | 5ae89a8 | 2010-08-12 04:14:08 +0100 | [diff] [blame] | 1657 | |
Mike Snitzer | 23508a9 | 2012-12-21 20:23:37 +0000 | [diff] [blame] | 1658 | if (is_split_required && !is_split_required(ti)) |
Mikulas Patocka | e0d6609 | 2014-03-14 18:40:39 -0400 | [diff] [blame] | 1659 | len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti)); |
Mikulas Patocka | 7acf027 | 2012-07-27 15:08:03 +0100 | [diff] [blame] | 1660 | else |
Mikulas Patocka | e0d6609 | 2014-03-14 18:40:39 -0400 | [diff] [blame] | 1661 | len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti)); |
Mike Snitzer | 06a426c | 2010-08-12 04:14:09 +0100 | [diff] [blame] | 1662 | |
Mikulas Patocka | 1dd40c3 | 2014-03-14 18:41:24 -0400 | [diff] [blame] | 1663 | __send_duplicate_bios(ci, ti, num_bios, &len); |
Mike Snitzer | a79245b | 2010-08-12 04:14:24 +0100 | [diff] [blame] | 1664 | |
| 1665 | ci->sector += len; |
| 1666 | } while (ci->sector_count -= len); |
Mike Snitzer | 5ae89a8 | 2010-08-12 04:14:08 +0100 | [diff] [blame] | 1667 | |
| 1668 | return 0; |
| 1669 | } |
| 1670 | |
Alasdair G Kergon | 14fe594 | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1671 | static int __send_discard(struct clone_info *ci) |
Mike Snitzer | 23508a9 | 2012-12-21 20:23:37 +0000 | [diff] [blame] | 1672 | { |
Alasdair G Kergon | 14fe594 | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1673 | return __send_changing_extent_only(ci, get_num_discard_bios, |
| 1674 | is_split_required_for_discard); |
Mike Snitzer | 23508a9 | 2012-12-21 20:23:37 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Alasdair G Kergon | 14fe594 | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1677 | static int __send_write_same(struct clone_info *ci) |
Mike Snitzer | 23508a9 | 2012-12-21 20:23:37 +0000 | [diff] [blame] | 1678 | { |
Alasdair G Kergon | 14fe594 | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1679 | return __send_changing_extent_only(ci, get_num_write_same_bios, NULL); |
Mike Snitzer | 23508a9 | 2012-12-21 20:23:37 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
Alasdair G Kergon | e4c9381 | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1682 | /* |
Alasdair G Kergon | e4c9381 | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1683 | * Select the correct strategy for processing a non-flush bio. |
| 1684 | */ |
Alasdair G Kergon | 14fe594 | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1685 | static int __split_and_process_non_flush(struct clone_info *ci) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | { |
Mikulas Patocka | dba1416 | 2012-10-12 21:02:15 +0100 | [diff] [blame] | 1687 | struct bio *bio = ci->bio; |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 1688 | struct dm_target *ti; |
Kent Overstreet | 1c3b13e | 2013-10-29 17:17:49 -0700 | [diff] [blame] | 1689 | unsigned len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | |
Mike Snitzer | 5ae89a8 | 2010-08-12 04:14:08 +0100 | [diff] [blame] | 1691 | if (unlikely(bio->bi_rw & REQ_DISCARD)) |
Alasdair G Kergon | 14fe594 | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1692 | return __send_discard(ci); |
Mike Snitzer | 23508a9 | 2012-12-21 20:23:37 +0000 | [diff] [blame] | 1693 | else if (unlikely(bio->bi_rw & REQ_WRITE_SAME)) |
Alasdair G Kergon | 14fe594 | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1694 | return __send_write_same(ci); |
Mike Snitzer | 5ae89a8 | 2010-08-12 04:14:08 +0100 | [diff] [blame] | 1695 | |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 1696 | ti = dm_table_find_target(ci->map, ci->sector); |
| 1697 | if (!dm_target_is_valid(ti)) |
| 1698 | return -EIO; |
| 1699 | |
Kent Overstreet | 1c3b13e | 2013-10-29 17:17:49 -0700 | [diff] [blame] | 1700 | len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count); |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 1701 | |
Mikulas Patocka | 1dd40c3 | 2014-03-14 18:41:24 -0400 | [diff] [blame] | 1702 | __clone_and_map_data_bio(ci, ti, ci->sector, &len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | |
Kent Overstreet | 1c3b13e | 2013-10-29 17:17:49 -0700 | [diff] [blame] | 1704 | ci->sector += len; |
| 1705 | ci->sector_count -= len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | |
Kent Overstreet | 1c3b13e | 2013-10-29 17:17:49 -0700 | [diff] [blame] | 1707 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | /* |
Alasdair G Kergon | 14fe594 | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1711 | * Entry point to split a bio into clones and submit them to the targets. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | */ |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 1713 | static void __split_and_process_bio(struct mapped_device *md, |
| 1714 | struct dm_table *map, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | { |
| 1716 | struct clone_info ci; |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 1717 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 1719 | if (unlikely(!map)) { |
Tejun Heo | 6a8736d | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 1720 | bio_io_error(bio); |
Mikulas Patocka | f0b9a45 | 2009-04-02 19:55:38 +0100 | [diff] [blame] | 1721 | return; |
| 1722 | } |
Mikulas Patocka | 692d0eb | 2009-04-09 00:27:13 +0100 | [diff] [blame] | 1723 | |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 1724 | ci.map = map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | ci.md = md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | ci.io = alloc_io(md); |
| 1727 | ci.io->error = 0; |
| 1728 | atomic_set(&ci.io->io_count, 1); |
| 1729 | ci.io->bio = bio; |
| 1730 | ci.io->md = md; |
Kiyoshi Ueda | f88fb98 | 2009-10-16 23:18:15 +0100 | [diff] [blame] | 1731 | spin_lock_init(&ci.io->endio_lock); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1732 | ci.sector = bio->bi_iter.bi_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 1734 | start_io_acct(ci.io); |
Alasdair G Kergon | bd2a49b | 2013-03-01 22:45:46 +0000 | [diff] [blame] | 1735 | |
Mike Snitzer | b372d36 | 2010-09-08 18:07:01 +0200 | [diff] [blame] | 1736 | if (bio->bi_rw & REQ_FLUSH) { |
| 1737 | ci.bio = &ci.md->flush_bio; |
| 1738 | ci.sector_count = 0; |
Alasdair G Kergon | 14fe594 | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1739 | error = __send_empty_flush(&ci); |
Mike Snitzer | b372d36 | 2010-09-08 18:07:01 +0200 | [diff] [blame] | 1740 | /* dec_pending submits any data associated with flush */ |
| 1741 | } else { |
Tejun Heo | 6a8736d | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 1742 | ci.bio = bio; |
Tejun Heo | d87f4c1 | 2010-09-03 11:56:19 +0200 | [diff] [blame] | 1743 | ci.sector_count = bio_sectors(bio); |
Mike Snitzer | b372d36 | 2010-09-08 18:07:01 +0200 | [diff] [blame] | 1744 | while (ci.sector_count && !error) |
Alasdair G Kergon | 14fe594 | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1745 | error = __split_and_process_non_flush(&ci); |
Tejun Heo | d87f4c1 | 2010-09-03 11:56:19 +0200 | [diff] [blame] | 1746 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | |
| 1748 | /* drop the extra reference count */ |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 1749 | dec_pending(ci.io, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | } |
| 1751 | /*----------------------------------------------------------------- |
| 1752 | * CRUD END |
| 1753 | *---------------------------------------------------------------*/ |
| 1754 | |
| 1755 | /* |
| 1756 | * The request function that just remaps the bio built up by |
| 1757 | * dm_merge_bvec. |
| 1758 | */ |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 1759 | static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | { |
Kevin Corry | 12f03a4 | 2006-02-01 03:04:52 -0800 | [diff] [blame] | 1761 | int rw = bio_data_dir(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | struct mapped_device *md = q->queuedata; |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 1763 | int srcu_idx; |
| 1764 | struct dm_table *map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 1766 | map = dm_get_live_table(md, &srcu_idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | |
Gu Zheng | 18c0b22 | 2014-11-24 11:05:26 +0800 | [diff] [blame] | 1768 | generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0); |
Kevin Corry | 12f03a4 | 2006-02-01 03:04:52 -0800 | [diff] [blame] | 1769 | |
Tejun Heo | 6a8736d | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 1770 | /* if we're suspended, we have to queue this io for later */ |
| 1771 | if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) { |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 1772 | dm_put_live_table(md, srcu_idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | |
Tejun Heo | 6a8736d | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 1774 | if (bio_rw(bio) != READA) |
| 1775 | queue_io(md, bio); |
| 1776 | else |
Alasdair G Kergon | 54d9a1b | 2009-04-09 00:27:14 +0100 | [diff] [blame] | 1777 | bio_io_error(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 1778 | return BLK_QC_T_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | } |
| 1780 | |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 1781 | __split_and_process_bio(md, map, bio); |
| 1782 | dm_put_live_table(md, srcu_idx); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 1783 | return BLK_QC_T_NONE; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1784 | } |
| 1785 | |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 1786 | int dm_request_based(struct mapped_device *md) |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1787 | { |
| 1788 | return blk_queue_stackable(md->queue); |
| 1789 | } |
| 1790 | |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1791 | static void dm_dispatch_clone_request(struct request *clone, struct request *rq) |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1792 | { |
| 1793 | int r; |
| 1794 | |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1795 | if (blk_queue_io_stat(clone->q)) |
| 1796 | clone->cmd_flags |= REQ_IO_STAT; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1797 | |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1798 | clone->start_time = jiffies; |
| 1799 | r = blk_insert_cloned_request(clone->q, clone); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1800 | if (r) |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1801 | /* must complete clone in terms of original request */ |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1802 | dm_complete_request(rq, r); |
| 1803 | } |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1804 | |
Mike Snitzer | 78d8e58 | 2015-06-26 10:01:13 -0400 | [diff] [blame] | 1805 | static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig, |
| 1806 | void *data) |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1807 | { |
Mike Snitzer | 78d8e58 | 2015-06-26 10:01:13 -0400 | [diff] [blame] | 1808 | struct dm_rq_target_io *tio = data; |
| 1809 | struct dm_rq_clone_bio_info *info = |
| 1810 | container_of(bio, struct dm_rq_clone_bio_info, clone); |
| 1811 | |
| 1812 | info->orig = bio_orig; |
| 1813 | info->tio = tio; |
| 1814 | bio->bi_end_io = end_clone_bio; |
| 1815 | |
| 1816 | return 0; |
| 1817 | } |
| 1818 | |
| 1819 | static int setup_clone(struct request *clone, struct request *rq, |
| 1820 | struct dm_rq_target_io *tio, gfp_t gfp_mask) |
| 1821 | { |
| 1822 | int r; |
| 1823 | |
| 1824 | r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask, |
| 1825 | dm_rq_bio_constructor, tio); |
| 1826 | if (r) |
| 1827 | return r; |
| 1828 | |
| 1829 | clone->cmd = rq->cmd; |
| 1830 | clone->cmd_len = rq->cmd_len; |
| 1831 | clone->sense = rq->sense; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1832 | clone->end_io = end_clone_request; |
| 1833 | clone->end_io_data = tio; |
Mike Snitzer | 78d8e58 | 2015-06-26 10:01:13 -0400 | [diff] [blame] | 1834 | |
Mike Snitzer | 1ae49ea | 2014-12-05 17:11:05 -0500 | [diff] [blame] | 1835 | tio->clone = clone; |
Mike Snitzer | 78d8e58 | 2015-06-26 10:01:13 -0400 | [diff] [blame] | 1836 | |
| 1837 | return 0; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1838 | } |
| 1839 | |
Kiyoshi Ueda | 6facdaf | 2009-12-10 23:52:15 +0000 | [diff] [blame] | 1840 | static struct request *clone_rq(struct request *rq, struct mapped_device *md, |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1841 | struct dm_rq_target_io *tio, gfp_t gfp_mask) |
Kiyoshi Ueda | 6facdaf | 2009-12-10 23:52:15 +0000 | [diff] [blame] | 1842 | { |
Mike Snitzer | 0223334 | 2015-03-10 23:49:26 -0400 | [diff] [blame] | 1843 | /* |
| 1844 | * Do not allocate a clone if tio->clone was already set |
| 1845 | * (see: dm_mq_queue_rq). |
| 1846 | */ |
| 1847 | bool alloc_clone = !tio->clone; |
| 1848 | struct request *clone; |
Mike Snitzer | 1ae49ea | 2014-12-05 17:11:05 -0500 | [diff] [blame] | 1849 | |
Mike Snitzer | 0223334 | 2015-03-10 23:49:26 -0400 | [diff] [blame] | 1850 | if (alloc_clone) { |
| 1851 | clone = alloc_clone_request(md, gfp_mask); |
| 1852 | if (!clone) |
| 1853 | return NULL; |
| 1854 | } else |
| 1855 | clone = tio->clone; |
Mike Snitzer | 1ae49ea | 2014-12-05 17:11:05 -0500 | [diff] [blame] | 1856 | |
| 1857 | blk_rq_init(NULL, clone); |
Mike Snitzer | 78d8e58 | 2015-06-26 10:01:13 -0400 | [diff] [blame] | 1858 | if (setup_clone(clone, rq, tio, gfp_mask)) { |
| 1859 | /* -ENOMEM */ |
| 1860 | if (alloc_clone) |
| 1861 | free_clone_request(md, clone); |
| 1862 | return NULL; |
| 1863 | } |
Mike Snitzer | 1ae49ea | 2014-12-05 17:11:05 -0500 | [diff] [blame] | 1864 | |
| 1865 | return clone; |
| 1866 | } |
| 1867 | |
Keith Busch | 2eb6e1e | 2014-10-17 17:46:36 -0600 | [diff] [blame] | 1868 | static void map_tio_request(struct kthread_work *work); |
| 1869 | |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1870 | static void init_tio(struct dm_rq_target_io *tio, struct request *rq, |
| 1871 | struct mapped_device *md) |
| 1872 | { |
| 1873 | tio->md = md; |
| 1874 | tio->ti = NULL; |
| 1875 | tio->clone = NULL; |
| 1876 | tio->orig = rq; |
| 1877 | tio->error = 0; |
| 1878 | memset(&tio->info, 0, sizeof(tio->info)); |
Mike Snitzer | 0223334 | 2015-03-10 23:49:26 -0400 | [diff] [blame] | 1879 | if (md->kworker_task) |
| 1880 | init_kthread_work(&tio->work, map_tio_request); |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1881 | } |
| 1882 | |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1883 | static struct dm_rq_target_io *prep_tio(struct request *rq, |
| 1884 | struct mapped_device *md, gfp_t gfp_mask) |
Kiyoshi Ueda | 6facdaf | 2009-12-10 23:52:15 +0000 | [diff] [blame] | 1885 | { |
Kiyoshi Ueda | 6facdaf | 2009-12-10 23:52:15 +0000 | [diff] [blame] | 1886 | struct dm_rq_target_io *tio; |
Mike Snitzer | e5863d9 | 2014-12-17 21:08:12 -0500 | [diff] [blame] | 1887 | int srcu_idx; |
| 1888 | struct dm_table *table; |
Kiyoshi Ueda | 6facdaf | 2009-12-10 23:52:15 +0000 | [diff] [blame] | 1889 | |
| 1890 | tio = alloc_rq_tio(md, gfp_mask); |
| 1891 | if (!tio) |
| 1892 | return NULL; |
| 1893 | |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1894 | init_tio(tio, rq, md); |
Kiyoshi Ueda | 6facdaf | 2009-12-10 23:52:15 +0000 | [diff] [blame] | 1895 | |
Mike Snitzer | e5863d9 | 2014-12-17 21:08:12 -0500 | [diff] [blame] | 1896 | table = dm_get_live_table(md, &srcu_idx); |
| 1897 | if (!dm_table_mq_request_based(table)) { |
| 1898 | if (!clone_rq(rq, md, tio, gfp_mask)) { |
| 1899 | dm_put_live_table(md, srcu_idx); |
| 1900 | free_rq_tio(tio); |
| 1901 | return NULL; |
| 1902 | } |
Kiyoshi Ueda | 6facdaf | 2009-12-10 23:52:15 +0000 | [diff] [blame] | 1903 | } |
Mike Snitzer | e5863d9 | 2014-12-17 21:08:12 -0500 | [diff] [blame] | 1904 | dm_put_live_table(md, srcu_idx); |
Kiyoshi Ueda | 6facdaf | 2009-12-10 23:52:15 +0000 | [diff] [blame] | 1905 | |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1906 | return tio; |
Kiyoshi Ueda | 6facdaf | 2009-12-10 23:52:15 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1909 | /* |
| 1910 | * Called with the queue lock held. |
| 1911 | */ |
| 1912 | static int dm_prep_fn(struct request_queue *q, struct request *rq) |
| 1913 | { |
| 1914 | struct mapped_device *md = q->queuedata; |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1915 | struct dm_rq_target_io *tio; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1916 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1917 | if (unlikely(rq->special)) { |
| 1918 | DMWARN("Already has something in rq->special."); |
| 1919 | return BLKPREP_KILL; |
| 1920 | } |
| 1921 | |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1922 | tio = prep_tio(rq, md, GFP_ATOMIC); |
| 1923 | if (!tio) |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1924 | return BLKPREP_DEFER; |
| 1925 | |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1926 | rq->special = tio; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1927 | rq->cmd_flags |= REQ_DONTPREP; |
| 1928 | |
| 1929 | return BLKPREP_OK; |
| 1930 | } |
| 1931 | |
Kiyoshi Ueda | 9eef87d | 2010-02-16 18:43:01 +0000 | [diff] [blame] | 1932 | /* |
| 1933 | * Returns: |
Mike Snitzer | e5863d9 | 2014-12-17 21:08:12 -0500 | [diff] [blame] | 1934 | * 0 : the request has been processed |
| 1935 | * DM_MAPIO_REQUEUE : the original request needs to be requeued |
| 1936 | * < 0 : the request was completed due to failure |
Kiyoshi Ueda | 9eef87d | 2010-02-16 18:43:01 +0000 | [diff] [blame] | 1937 | */ |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1938 | static int map_request(struct dm_rq_target_io *tio, struct request *rq, |
Kiyoshi Ueda | 9eef87d | 2010-02-16 18:43:01 +0000 | [diff] [blame] | 1939 | struct mapped_device *md) |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1940 | { |
Mike Snitzer | e5863d9 | 2014-12-17 21:08:12 -0500 | [diff] [blame] | 1941 | int r; |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1942 | struct dm_target *ti = tio->ti; |
Mike Snitzer | e5863d9 | 2014-12-17 21:08:12 -0500 | [diff] [blame] | 1943 | struct request *clone = NULL; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1944 | |
Mike Snitzer | e5863d9 | 2014-12-17 21:08:12 -0500 | [diff] [blame] | 1945 | if (tio->clone) { |
| 1946 | clone = tio->clone; |
| 1947 | r = ti->type->map_rq(ti, clone, &tio->info); |
| 1948 | } else { |
| 1949 | r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone); |
| 1950 | if (r < 0) { |
| 1951 | /* The target wants to complete the I/O */ |
| 1952 | dm_kill_unmapped_request(rq, r); |
| 1953 | return r; |
| 1954 | } |
Junichi Nomura | 3a14075 | 2015-05-27 04:22:07 +0000 | [diff] [blame] | 1955 | if (r != DM_MAPIO_REMAPPED) |
| 1956 | return r; |
Mike Snitzer | 78d8e58 | 2015-06-26 10:01:13 -0400 | [diff] [blame] | 1957 | if (setup_clone(clone, rq, tio, GFP_ATOMIC)) { |
| 1958 | /* -ENOMEM */ |
| 1959 | ti->type->release_clone_rq(clone); |
| 1960 | return DM_MAPIO_REQUEUE; |
| 1961 | } |
Mike Snitzer | e5863d9 | 2014-12-17 21:08:12 -0500 | [diff] [blame] | 1962 | } |
| 1963 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1964 | switch (r) { |
| 1965 | case DM_MAPIO_SUBMITTED: |
| 1966 | /* The target has taken the I/O to submit by itself later */ |
| 1967 | break; |
| 1968 | case DM_MAPIO_REMAPPED: |
| 1969 | /* The target has remapped the I/O so dispatch it */ |
Jun'ichi Nomura | 6db4ccd | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 1970 | trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)), |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1971 | blk_rq_pos(rq)); |
| 1972 | dm_dispatch_clone_request(clone, rq); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1973 | break; |
| 1974 | case DM_MAPIO_REQUEUE: |
| 1975 | /* The target wants to requeue the I/O */ |
Mike Snitzer | 2d76fff | 2015-04-29 12:07:12 -0400 | [diff] [blame] | 1976 | dm_requeue_original_request(md, tio->orig); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1977 | break; |
| 1978 | default: |
| 1979 | if (r > 0) { |
| 1980 | DMWARN("unimplemented target map return value: %d", r); |
| 1981 | BUG(); |
| 1982 | } |
| 1983 | |
| 1984 | /* The target wants to complete the I/O */ |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 1985 | dm_kill_unmapped_request(rq, r); |
Mike Snitzer | e5863d9 | 2014-12-17 21:08:12 -0500 | [diff] [blame] | 1986 | return r; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1987 | } |
Kiyoshi Ueda | 9eef87d | 2010-02-16 18:43:01 +0000 | [diff] [blame] | 1988 | |
Mike Snitzer | e5863d9 | 2014-12-17 21:08:12 -0500 | [diff] [blame] | 1989 | return 0; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 1990 | } |
| 1991 | |
Keith Busch | 2eb6e1e | 2014-10-17 17:46:36 -0600 | [diff] [blame] | 1992 | static void map_tio_request(struct kthread_work *work) |
Mike Snitzer | ba1cbad | 2012-09-26 23:45:42 +0100 | [diff] [blame] | 1993 | { |
Keith Busch | 2eb6e1e | 2014-10-17 17:46:36 -0600 | [diff] [blame] | 1994 | struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work); |
Mike Snitzer | e5863d9 | 2014-12-17 21:08:12 -0500 | [diff] [blame] | 1995 | struct request *rq = tio->orig; |
| 1996 | struct mapped_device *md = tio->md; |
Mike Snitzer | ba1cbad | 2012-09-26 23:45:42 +0100 | [diff] [blame] | 1997 | |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 1998 | if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) |
Mike Snitzer | 2d76fff | 2015-04-29 12:07:12 -0400 | [diff] [blame] | 1999 | dm_requeue_original_request(md, rq); |
Keith Busch | 2eb6e1e | 2014-10-17 17:46:36 -0600 | [diff] [blame] | 2000 | } |
| 2001 | |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 2002 | static void dm_start_request(struct mapped_device *md, struct request *orig) |
Mike Snitzer | ba1cbad | 2012-09-26 23:45:42 +0100 | [diff] [blame] | 2003 | { |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2004 | if (!orig->q->mq_ops) |
| 2005 | blk_start_request(orig); |
| 2006 | else |
| 2007 | blk_mq_start_request(orig); |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 2008 | atomic_inc(&md->pending[rq_data_dir(orig)]); |
Mike Snitzer | ba1cbad | 2012-09-26 23:45:42 +0100 | [diff] [blame] | 2009 | |
Mike Snitzer | 0ce6579 | 2015-02-26 00:50:28 -0500 | [diff] [blame] | 2010 | if (md->seq_rq_merge_deadline_usecs) { |
| 2011 | md->last_rq_pos = rq_end_sector(orig); |
| 2012 | md->last_rq_rw = rq_data_dir(orig); |
| 2013 | md->last_rq_start_time = ktime_get(); |
| 2014 | } |
Mike Snitzer | de3ec86 | 2015-02-24 21:58:21 -0500 | [diff] [blame] | 2015 | |
Mikulas Patocka | e262f34 | 2015-06-09 17:22:49 -0400 | [diff] [blame] | 2016 | if (unlikely(dm_stats_used(&md->stats))) { |
| 2017 | struct dm_rq_target_io *tio = tio_from_request(orig); |
| 2018 | tio->duration_jiffies = jiffies; |
| 2019 | tio->n_sectors = blk_rq_sectors(orig); |
| 2020 | dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig), |
| 2021 | tio->n_sectors, false, 0, &tio->stats_aux); |
| 2022 | } |
| 2023 | |
Mike Snitzer | ba1cbad | 2012-09-26 23:45:42 +0100 | [diff] [blame] | 2024 | /* |
| 2025 | * Hold the md reference here for the in-flight I/O. |
| 2026 | * We can't rely on the reference count by device opener, |
| 2027 | * because the device may be closed during the request completion |
| 2028 | * when all bios are completed. |
| 2029 | * See the comment in rq_completed() too. |
| 2030 | */ |
| 2031 | dm_get(md); |
Mike Snitzer | ba1cbad | 2012-09-26 23:45:42 +0100 | [diff] [blame] | 2032 | } |
| 2033 | |
Mike Snitzer | 0ce6579 | 2015-02-26 00:50:28 -0500 | [diff] [blame] | 2034 | #define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000 |
| 2035 | |
| 2036 | ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf) |
| 2037 | { |
| 2038 | return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs); |
| 2039 | } |
| 2040 | |
| 2041 | ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md, |
| 2042 | const char *buf, size_t count) |
| 2043 | { |
| 2044 | unsigned deadline; |
| 2045 | |
Mike Snitzer | 17e149b | 2015-03-11 15:01:09 -0400 | [diff] [blame] | 2046 | if (!dm_request_based(md) || md->use_blk_mq) |
Mike Snitzer | 0ce6579 | 2015-02-26 00:50:28 -0500 | [diff] [blame] | 2047 | return count; |
| 2048 | |
| 2049 | if (kstrtouint(buf, 10, &deadline)) |
| 2050 | return -EINVAL; |
| 2051 | |
| 2052 | if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS) |
| 2053 | deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS; |
| 2054 | |
| 2055 | md->seq_rq_merge_deadline_usecs = deadline; |
| 2056 | |
| 2057 | return count; |
| 2058 | } |
| 2059 | |
| 2060 | static bool dm_request_peeked_before_merge_deadline(struct mapped_device *md) |
| 2061 | { |
| 2062 | ktime_t kt_deadline; |
| 2063 | |
| 2064 | if (!md->seq_rq_merge_deadline_usecs) |
| 2065 | return false; |
| 2066 | |
| 2067 | kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC); |
| 2068 | kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline); |
| 2069 | |
| 2070 | return !ktime_after(ktime_get(), kt_deadline); |
| 2071 | } |
| 2072 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 2073 | /* |
| 2074 | * q->request_fn for request-based dm. |
| 2075 | * Called with the queue lock held. |
| 2076 | */ |
| 2077 | static void dm_request_fn(struct request_queue *q) |
| 2078 | { |
| 2079 | struct mapped_device *md = q->queuedata; |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 2080 | int srcu_idx; |
| 2081 | struct dm_table *map = dm_get_live_table(md, &srcu_idx); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 2082 | struct dm_target *ti; |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 2083 | struct request *rq; |
Keith Busch | 2eb6e1e | 2014-10-17 17:46:36 -0600 | [diff] [blame] | 2084 | struct dm_rq_target_io *tio; |
Tejun Heo | 29e4013 | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 2085 | sector_t pos; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 2086 | |
| 2087 | /* |
Kiyoshi Ueda | b4324fe | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 2088 | * For suspend, check blk_queue_stopped() and increment |
| 2089 | * ->pending within a single queue_lock not to increment the |
| 2090 | * number of in-flight I/Os after the queue is stopped in |
| 2091 | * dm_suspend(). |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 2092 | */ |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 2093 | while (!blk_queue_stopped(q)) { |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 2094 | rq = blk_peek_request(q); |
| 2095 | if (!rq) |
Mike Snitzer | 9d1deb8 | 2015-02-24 20:49:18 -0500 | [diff] [blame] | 2096 | goto out; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 2097 | |
Tejun Heo | 29e4013 | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 2098 | /* always use block 0 to find the target for flushes for now */ |
| 2099 | pos = 0; |
| 2100 | if (!(rq->cmd_flags & REQ_FLUSH)) |
| 2101 | pos = blk_rq_pos(rq); |
Kiyoshi Ueda | d0bcb87 | 2009-12-10 23:52:18 +0000 | [diff] [blame] | 2102 | |
Tejun Heo | 29e4013 | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 2103 | ti = dm_table_find_target(map, pos); |
Mike Snitzer | ba1cbad | 2012-09-26 23:45:42 +0100 | [diff] [blame] | 2104 | if (!dm_target_is_valid(ti)) { |
| 2105 | /* |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 2106 | * Must perform setup, that rq_completed() requires, |
Mike Snitzer | ba1cbad | 2012-09-26 23:45:42 +0100 | [diff] [blame] | 2107 | * before calling dm_kill_unmapped_request |
| 2108 | */ |
| 2109 | DMERR_LIMIT("request attempted access beyond the end of device"); |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 2110 | dm_start_request(md, rq); |
| 2111 | dm_kill_unmapped_request(rq, -EIO); |
Mike Snitzer | ba1cbad | 2012-09-26 23:45:42 +0100 | [diff] [blame] | 2112 | continue; |
| 2113 | } |
Tejun Heo | 29e4013 | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 2114 | |
Mike Snitzer | 0ce6579 | 2015-02-26 00:50:28 -0500 | [diff] [blame] | 2115 | if (dm_request_peeked_before_merge_deadline(md) && |
| 2116 | md_in_flight(md) && rq->bio && rq->bio->bi_vcnt == 1 && |
Mike Snitzer | de3ec86 | 2015-02-24 21:58:21 -0500 | [diff] [blame] | 2117 | md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq)) |
| 2118 | goto delay_and_out; |
| 2119 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 2120 | if (ti->type->busy && ti->type->busy(ti)) |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 2121 | goto delay_and_out; |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 2122 | |
Keith Busch | 466d89a | 2014-10-17 17:46:37 -0600 | [diff] [blame] | 2123 | dm_start_request(md, rq); |
Kiyoshi Ueda | b4324fe | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 2124 | |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2125 | tio = tio_from_request(rq); |
Keith Busch | 2eb6e1e | 2014-10-17 17:46:36 -0600 | [diff] [blame] | 2126 | /* Establish tio->ti before queuing work (map_tio_request) */ |
| 2127 | tio->ti = ti; |
| 2128 | queue_kthread_work(&md->kworker, &tio->work); |
Kiyoshi Ueda | 052189a | 2011-01-13 20:00:00 +0000 | [diff] [blame] | 2129 | BUG_ON(!irqs_disabled()); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | goto out; |
| 2133 | |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 2134 | delay_and_out: |
Mike Snitzer | d548b34 | 2015-03-05 22:21:10 -0500 | [diff] [blame] | 2135 | blk_delay_queue(q, HZ / 100); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 2136 | out: |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 2137 | dm_put_live_table(md, srcu_idx); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 2138 | } |
| 2139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | static int dm_any_congested(void *congested_data, int bdi_bits) |
| 2141 | { |
Chandra Seetharaman | 8a57dfc | 2008-11-13 23:39:14 +0000 | [diff] [blame] | 2142 | int r = bdi_bits; |
| 2143 | struct mapped_device *md = congested_data; |
| 2144 | struct dm_table *map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | |
Alasdair G Kergon | 1eb787e | 2009-04-09 00:27:14 +0100 | [diff] [blame] | 2146 | if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 2147 | map = dm_get_live_table_fast(md); |
Chandra Seetharaman | 8a57dfc | 2008-11-13 23:39:14 +0000 | [diff] [blame] | 2148 | if (map) { |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 2149 | /* |
| 2150 | * Request-based dm cares about only own queue for |
| 2151 | * the query about congestion status of request_queue |
| 2152 | */ |
| 2153 | if (dm_request_based(md)) |
Tejun Heo | 4452226 | 2015-05-22 17:13:26 -0400 | [diff] [blame] | 2154 | r = md->queue->backing_dev_info.wb.state & |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 2155 | bdi_bits; |
| 2156 | else |
| 2157 | r = dm_table_any_congested(map, bdi_bits); |
Chandra Seetharaman | 8a57dfc | 2008-11-13 23:39:14 +0000 | [diff] [blame] | 2158 | } |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 2159 | dm_put_live_table_fast(md); |
Chandra Seetharaman | 8a57dfc | 2008-11-13 23:39:14 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | return r; |
| 2163 | } |
| 2164 | |
| 2165 | /*----------------------------------------------------------------- |
| 2166 | * An IDR is used to keep track of allocated minor numbers. |
| 2167 | *---------------------------------------------------------------*/ |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 2168 | static void free_minor(int minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | { |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 2170 | spin_lock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | idr_remove(&_minor_idr, minor); |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 2172 | spin_unlock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | /* |
| 2176 | * See if the device with a specific minor # is free. |
| 2177 | */ |
Frederik Deweerdt | cf13ab8 | 2008-04-24 22:10:59 +0100 | [diff] [blame] | 2178 | static int specific_minor(int minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | { |
Tejun Heo | c9d76be | 2013-02-27 17:04:26 -0800 | [diff] [blame] | 2180 | int r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | |
| 2182 | if (minor >= (1 << MINORBITS)) |
| 2183 | return -EINVAL; |
| 2184 | |
Tejun Heo | c9d76be | 2013-02-27 17:04:26 -0800 | [diff] [blame] | 2185 | idr_preload(GFP_KERNEL); |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 2186 | spin_lock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | |
Tejun Heo | c9d76be | 2013-02-27 17:04:26 -0800 | [diff] [blame] | 2188 | r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 2190 | spin_unlock(&_minor_lock); |
Tejun Heo | c9d76be | 2013-02-27 17:04:26 -0800 | [diff] [blame] | 2191 | idr_preload_end(); |
| 2192 | if (r < 0) |
| 2193 | return r == -ENOSPC ? -EBUSY : r; |
| 2194 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | } |
| 2196 | |
Frederik Deweerdt | cf13ab8 | 2008-04-24 22:10:59 +0100 | [diff] [blame] | 2197 | static int next_free_minor(int *minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | { |
Tejun Heo | c9d76be | 2013-02-27 17:04:26 -0800 | [diff] [blame] | 2199 | int r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | |
Tejun Heo | c9d76be | 2013-02-27 17:04:26 -0800 | [diff] [blame] | 2201 | idr_preload(GFP_KERNEL); |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 2202 | spin_lock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | |
Tejun Heo | c9d76be | 2013-02-27 17:04:26 -0800 | [diff] [blame] | 2204 | r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 2206 | spin_unlock(&_minor_lock); |
Tejun Heo | c9d76be | 2013-02-27 17:04:26 -0800 | [diff] [blame] | 2207 | idr_preload_end(); |
| 2208 | if (r < 0) |
| 2209 | return r; |
| 2210 | *minor = r; |
| 2211 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | } |
| 2213 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 2214 | static const struct block_device_operations dm_blk_dops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | |
Mikulas Patocka | 53d5914 | 2009-04-02 19:55:37 +0100 | [diff] [blame] | 2216 | static void dm_wq_work(struct work_struct *work); |
| 2217 | |
Mike Snitzer | 4a0b4dd | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 2218 | static void dm_init_md_queue(struct mapped_device *md) |
| 2219 | { |
| 2220 | /* |
| 2221 | * Request-based dm devices cannot be stacked on top of bio-based dm |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2222 | * devices. The type of this dm device may not have been decided yet. |
Mike Snitzer | 4a0b4dd | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 2223 | * The type is decided at the first table loading time. |
| 2224 | * To prevent problematic device stacking, clear the queue flag |
| 2225 | * for request stacking support until then. |
| 2226 | * |
| 2227 | * This queue is new, so no concurrency on the queue_flags. |
| 2228 | */ |
| 2229 | queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue); |
Mikulas Patocka | ad5f498 | 2015-10-27 19:06:55 -0400 | [diff] [blame] | 2230 | |
| 2231 | /* |
| 2232 | * Initialize data that will only be used by a non-blk-mq DM queue |
| 2233 | * - must do so here (in alloc_dev callchain) before queue is used |
| 2234 | */ |
| 2235 | md->queue->queuedata = md; |
| 2236 | md->queue->backing_dev_info.congested_data = md; |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2237 | } |
Mike Snitzer | 4a0b4dd | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 2238 | |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2239 | static void dm_init_old_md_queue(struct mapped_device *md) |
| 2240 | { |
Mike Snitzer | 17e149b | 2015-03-11 15:01:09 -0400 | [diff] [blame] | 2241 | md->use_blk_mq = false; |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2242 | dm_init_md_queue(md); |
| 2243 | |
| 2244 | /* |
| 2245 | * Initialize aspects of queue that aren't relevant for blk-mq |
| 2246 | */ |
Mike Snitzer | 4a0b4dd | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 2247 | md->queue->backing_dev_info.congested_fn = dm_any_congested; |
Mike Snitzer | 4a0b4dd | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 2248 | blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY); |
Mike Snitzer | 4a0b4dd | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 2249 | } |
| 2250 | |
Mike Snitzer | 0f20972 | 2015-04-28 11:50:29 -0400 | [diff] [blame] | 2251 | static void cleanup_mapped_device(struct mapped_device *md) |
| 2252 | { |
Mike Snitzer | 0f20972 | 2015-04-28 11:50:29 -0400 | [diff] [blame] | 2253 | if (md->wq) |
| 2254 | destroy_workqueue(md->wq); |
| 2255 | if (md->kworker_task) |
| 2256 | kthread_stop(md->kworker_task); |
Julia Lawall | 6f65985 | 2015-09-13 14:15:05 +0200 | [diff] [blame] | 2257 | mempool_destroy(md->io_pool); |
| 2258 | mempool_destroy(md->rq_pool); |
Mike Snitzer | 0f20972 | 2015-04-28 11:50:29 -0400 | [diff] [blame] | 2259 | if (md->bs) |
| 2260 | bioset_free(md->bs); |
| 2261 | |
Mikulas Patocka | b06075a | 2015-07-10 17:21:43 -0400 | [diff] [blame] | 2262 | cleanup_srcu_struct(&md->io_barrier); |
| 2263 | |
Mike Snitzer | 0f20972 | 2015-04-28 11:50:29 -0400 | [diff] [blame] | 2264 | if (md->disk) { |
| 2265 | spin_lock(&_minor_lock); |
| 2266 | md->disk->private_data = NULL; |
| 2267 | spin_unlock(&_minor_lock); |
Mike Snitzer | 0f20972 | 2015-04-28 11:50:29 -0400 | [diff] [blame] | 2268 | del_gendisk(md->disk); |
| 2269 | put_disk(md->disk); |
| 2270 | } |
| 2271 | |
| 2272 | if (md->queue) |
| 2273 | blk_cleanup_queue(md->queue); |
| 2274 | |
| 2275 | if (md->bdev) { |
| 2276 | bdput(md->bdev); |
| 2277 | md->bdev = NULL; |
| 2278 | } |
| 2279 | } |
| 2280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | /* |
| 2282 | * Allocate and initialise a blank device with a given minor. |
| 2283 | */ |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 2284 | static struct mapped_device *alloc_dev(int minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | { |
| 2286 | int r; |
Frederik Deweerdt | cf13ab8 | 2008-04-24 22:10:59 +0100 | [diff] [blame] | 2287 | struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL); |
Jeff Mahoney | ba61fdd | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 2288 | void *old_md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | |
| 2290 | if (!md) { |
| 2291 | DMWARN("unable to allocate device, out of memory."); |
| 2292 | return NULL; |
| 2293 | } |
| 2294 | |
Jeff Mahoney | 10da4f7 | 2006-06-26 00:27:25 -0700 | [diff] [blame] | 2295 | if (!try_module_get(THIS_MODULE)) |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 2296 | goto bad_module_get; |
Jeff Mahoney | 10da4f7 | 2006-06-26 00:27:25 -0700 | [diff] [blame] | 2297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2298 | /* get a minor number for the dev */ |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 2299 | if (minor == DM_ANY_MINOR) |
Frederik Deweerdt | cf13ab8 | 2008-04-24 22:10:59 +0100 | [diff] [blame] | 2300 | r = next_free_minor(&minor); |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 2301 | else |
Frederik Deweerdt | cf13ab8 | 2008-04-24 22:10:59 +0100 | [diff] [blame] | 2302 | r = specific_minor(minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | if (r < 0) |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 2304 | goto bad_minor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 2306 | r = init_srcu_struct(&md->io_barrier); |
| 2307 | if (r < 0) |
| 2308 | goto bad_io_barrier; |
| 2309 | |
Mike Snitzer | 17e149b | 2015-03-11 15:01:09 -0400 | [diff] [blame] | 2310 | md->use_blk_mq = use_blk_mq; |
Mike Snitzer | a5664da | 2010-08-12 04:14:01 +0100 | [diff] [blame] | 2311 | md->type = DM_TYPE_NONE; |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 2312 | mutex_init(&md->suspend_lock); |
Mike Snitzer | a5664da | 2010-08-12 04:14:01 +0100 | [diff] [blame] | 2313 | mutex_init(&md->type_lock); |
Benjamin Marzinski | 86f1152 | 2014-08-13 13:53:43 -0500 | [diff] [blame] | 2314 | mutex_init(&md->table_devices_lock); |
Mikulas Patocka | 022c261 | 2009-04-02 19:55:39 +0100 | [diff] [blame] | 2315 | spin_lock_init(&md->deferred_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2316 | atomic_set(&md->holders, 1); |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 2317 | atomic_set(&md->open_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | atomic_set(&md->event_nr, 0); |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 2319 | atomic_set(&md->uevent_seq, 0); |
| 2320 | INIT_LIST_HEAD(&md->uevent_list); |
Benjamin Marzinski | 86f1152 | 2014-08-13 13:53:43 -0500 | [diff] [blame] | 2321 | INIT_LIST_HEAD(&md->table_devices); |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 2322 | spin_lock_init(&md->uevent_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | |
Mike Snitzer | 4a0b4dd | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 2324 | md->queue = blk_alloc_queue(GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | if (!md->queue) |
Mike Snitzer | 0f20972 | 2015-04-28 11:50:29 -0400 | [diff] [blame] | 2326 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | |
Mike Snitzer | 4a0b4dd | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 2328 | dm_init_md_queue(md); |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 2329 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | md->disk = alloc_disk(1); |
| 2331 | if (!md->disk) |
Mike Snitzer | 0f20972 | 2015-04-28 11:50:29 -0400 | [diff] [blame] | 2332 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | |
Nikanth Karthikesan | 316d315 | 2009-10-06 20:16:55 +0200 | [diff] [blame] | 2334 | atomic_set(&md->pending[0], 0); |
| 2335 | atomic_set(&md->pending[1], 0); |
Jeff Mahoney | f0b0411 | 2006-06-26 00:27:25 -0700 | [diff] [blame] | 2336 | init_waitqueue_head(&md->wait); |
Mikulas Patocka | 53d5914 | 2009-04-02 19:55:37 +0100 | [diff] [blame] | 2337 | INIT_WORK(&md->work, dm_wq_work); |
Jeff Mahoney | f0b0411 | 2006-06-26 00:27:25 -0700 | [diff] [blame] | 2338 | init_waitqueue_head(&md->eventq); |
Mikulas Patocka | 2995fa7 | 2014-01-13 19:37:54 -0500 | [diff] [blame] | 2339 | init_completion(&md->kobj_holder.completion); |
Keith Busch | 2eb6e1e | 2014-10-17 17:46:36 -0600 | [diff] [blame] | 2340 | md->kworker_task = NULL; |
Jeff Mahoney | f0b0411 | 2006-06-26 00:27:25 -0700 | [diff] [blame] | 2341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | md->disk->major = _major; |
| 2343 | md->disk->first_minor = minor; |
| 2344 | md->disk->fops = &dm_blk_dops; |
| 2345 | md->disk->queue = md->queue; |
| 2346 | md->disk->private_data = md; |
| 2347 | sprintf(md->disk->disk_name, "dm-%d", minor); |
| 2348 | add_disk(md->disk); |
Mike Anderson | 7e51f25 | 2006-03-27 01:17:52 -0800 | [diff] [blame] | 2349 | format_dev_t(md->name, MKDEV(_major, minor)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | |
Tejun Heo | 670368a | 2013-07-30 08:40:21 -0400 | [diff] [blame] | 2351 | md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0); |
Milan Broz | 304f3f6 | 2008-02-08 02:11:17 +0000 | [diff] [blame] | 2352 | if (!md->wq) |
Mike Snitzer | 0f20972 | 2015-04-28 11:50:29 -0400 | [diff] [blame] | 2353 | goto bad; |
Milan Broz | 304f3f6 | 2008-02-08 02:11:17 +0000 | [diff] [blame] | 2354 | |
Mikulas Patocka | 32a926d | 2009-06-22 10:12:17 +0100 | [diff] [blame] | 2355 | md->bdev = bdget_disk(md->disk, 0); |
| 2356 | if (!md->bdev) |
Mike Snitzer | 0f20972 | 2015-04-28 11:50:29 -0400 | [diff] [blame] | 2357 | goto bad; |
Mikulas Patocka | 32a926d | 2009-06-22 10:12:17 +0100 | [diff] [blame] | 2358 | |
Tejun Heo | 6a8736d | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 2359 | bio_init(&md->flush_bio); |
| 2360 | md->flush_bio.bi_bdev = md->bdev; |
| 2361 | md->flush_bio.bi_rw = WRITE_FLUSH; |
| 2362 | |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 2363 | dm_stats_init(&md->stats); |
| 2364 | |
Jeff Mahoney | ba61fdd | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 2365 | /* Populate the mapping, nobody knows we exist yet */ |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 2366 | spin_lock(&_minor_lock); |
Jeff Mahoney | ba61fdd | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 2367 | old_md = idr_replace(&_minor_idr, md, minor); |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 2368 | spin_unlock(&_minor_lock); |
Jeff Mahoney | ba61fdd | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 2369 | |
| 2370 | BUG_ON(old_md != MINOR_ALLOCED); |
| 2371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | return md; |
| 2373 | |
Mike Snitzer | 0f20972 | 2015-04-28 11:50:29 -0400 | [diff] [blame] | 2374 | bad: |
| 2375 | cleanup_mapped_device(md); |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 2376 | bad_io_barrier: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | free_minor(minor); |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 2378 | bad_minor: |
Jeff Mahoney | 10da4f7 | 2006-06-26 00:27:25 -0700 | [diff] [blame] | 2379 | module_put(THIS_MODULE); |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 2380 | bad_module_get: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | kfree(md); |
| 2382 | return NULL; |
| 2383 | } |
| 2384 | |
Jun'ichi Nomura | ae9da83 | 2007-10-19 22:38:43 +0100 | [diff] [blame] | 2385 | static void unlock_fs(struct mapped_device *md); |
| 2386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | static void free_dev(struct mapped_device *md) |
| 2388 | { |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 2389 | int minor = MINOR(disk_devt(md->disk)); |
Jun'ichi Nomura | 63d94e4 | 2006-02-24 13:04:25 -0800 | [diff] [blame] | 2390 | |
Mikulas Patocka | 32a926d | 2009-06-22 10:12:17 +0100 | [diff] [blame] | 2391 | unlock_fs(md); |
Keith Busch | 2eb6e1e | 2014-10-17 17:46:36 -0600 | [diff] [blame] | 2392 | |
Mike Snitzer | 0f20972 | 2015-04-28 11:50:29 -0400 | [diff] [blame] | 2393 | cleanup_mapped_device(md); |
Mike Snitzer | 17e149b | 2015-03-11 15:01:09 -0400 | [diff] [blame] | 2394 | if (md->use_blk_mq) |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2395 | blk_mq_free_tag_set(&md->tag_set); |
Mike Snitzer | 0f20972 | 2015-04-28 11:50:29 -0400 | [diff] [blame] | 2396 | |
| 2397 | free_table_devices(&md->table_devices); |
| 2398 | dm_stats_cleanup(&md->stats); |
Mike Snitzer | 63a4f06 | 2015-03-23 17:01:43 -0400 | [diff] [blame] | 2399 | free_minor(minor); |
| 2400 | |
Jeff Mahoney | 10da4f7 | 2006-06-26 00:27:25 -0700 | [diff] [blame] | 2401 | module_put(THIS_MODULE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | kfree(md); |
| 2403 | } |
| 2404 | |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 2405 | static void __bind_mempools(struct mapped_device *md, struct dm_table *t) |
| 2406 | { |
Mikulas Patocka | c0820cf | 2012-12-21 20:23:38 +0000 | [diff] [blame] | 2407 | struct dm_md_mempools *p = dm_table_get_md_mempools(t); |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 2408 | |
Mike Snitzer | 4e6e36c | 2015-06-26 09:42:57 -0400 | [diff] [blame] | 2409 | if (md->bs) { |
| 2410 | /* The md already has necessary mempools. */ |
| 2411 | if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) { |
Jun'ichi Nomura | 16245bd | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 2412 | /* |
| 2413 | * Reload bioset because front_pad may have changed |
| 2414 | * because a different table was loaded. |
| 2415 | */ |
| 2416 | bioset_free(md->bs); |
| 2417 | md->bs = p->bs; |
| 2418 | p->bs = NULL; |
Jun'ichi Nomura | 16245bd | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 2419 | } |
Mike Snitzer | 4e6e36c | 2015-06-26 09:42:57 -0400 | [diff] [blame] | 2420 | /* |
| 2421 | * There's no need to reload with request-based dm |
| 2422 | * because the size of front_pad doesn't change. |
| 2423 | * Note for future: If you are to reload bioset, |
| 2424 | * prep-ed requests in the queue may refer |
| 2425 | * to bio from the old bioset, so you must walk |
| 2426 | * through the queue to unprep. |
| 2427 | */ |
| 2428 | goto out; |
Mikulas Patocka | c0820cf | 2012-12-21 20:23:38 +0000 | [diff] [blame] | 2429 | } |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 2430 | |
Mike Snitzer | cbc4e3c | 2015-04-27 16:37:50 -0400 | [diff] [blame] | 2431 | BUG_ON(!p || md->io_pool || md->rq_pool || md->bs); |
| 2432 | |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 2433 | md->io_pool = p->io_pool; |
| 2434 | p->io_pool = NULL; |
Mike Snitzer | 1ae49ea | 2014-12-05 17:11:05 -0500 | [diff] [blame] | 2435 | md->rq_pool = p->rq_pool; |
| 2436 | p->rq_pool = NULL; |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 2437 | md->bs = p->bs; |
| 2438 | p->bs = NULL; |
Mike Snitzer | 4e6e36c | 2015-06-26 09:42:57 -0400 | [diff] [blame] | 2439 | |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 2440 | out: |
Mike Snitzer | 0223334 | 2015-03-10 23:49:26 -0400 | [diff] [blame] | 2441 | /* mempool bind completed, no longer need any mempools in the table */ |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 2442 | dm_table_free_md_mempools(t); |
| 2443 | } |
| 2444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | /* |
| 2446 | * Bind a table to the device. |
| 2447 | */ |
| 2448 | static void event_callback(void *context) |
| 2449 | { |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 2450 | unsigned long flags; |
| 2451 | LIST_HEAD(uevents); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2452 | struct mapped_device *md = (struct mapped_device *) context; |
| 2453 | |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 2454 | spin_lock_irqsave(&md->uevent_lock, flags); |
| 2455 | list_splice_init(&md->uevent_list, &uevents); |
| 2456 | spin_unlock_irqrestore(&md->uevent_lock, flags); |
| 2457 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 2458 | dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 2459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | atomic_inc(&md->event_nr); |
| 2461 | wake_up(&md->eventq); |
| 2462 | } |
| 2463 | |
Mike Snitzer | c217649 | 2011-01-13 19:53:46 +0000 | [diff] [blame] | 2464 | /* |
| 2465 | * Protected by md->suspend_lock obtained by dm_swap_table(). |
| 2466 | */ |
Alasdair G Kergon | 4e90188be | 2005-07-28 21:15:59 -0700 | [diff] [blame] | 2467 | static void __set_size(struct mapped_device *md, sector_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | { |
Alasdair G Kergon | 4e90188be | 2005-07-28 21:15:59 -0700 | [diff] [blame] | 2469 | set_capacity(md->disk, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | |
Mikulas Patocka | db8fef4 | 2009-06-22 10:12:15 +0100 | [diff] [blame] | 2471 | i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | } |
| 2473 | |
Alasdair G Kergon | 042d2a9 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 2474 | /* |
| 2475 | * Returns old map, which caller must destroy. |
| 2476 | */ |
| 2477 | static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t, |
| 2478 | struct queue_limits *limits) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | { |
Alasdair G Kergon | 042d2a9 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 2480 | struct dm_table *old_map; |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 2481 | struct request_queue *q = md->queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | sector_t size; |
| 2483 | |
| 2484 | size = dm_table_get_size(t); |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 2485 | |
| 2486 | /* |
| 2487 | * Wipe any geometry if the size of the table changed. |
| 2488 | */ |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 2489 | if (size != dm_get_size(md)) |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 2490 | memset(&md->geometry, 0, sizeof(md->geometry)); |
| 2491 | |
Mikulas Patocka | 32a926d | 2009-06-22 10:12:17 +0100 | [diff] [blame] | 2492 | __set_size(md, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 2494 | dm_table_event_callback(t, event_callback, md); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 2495 | |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 2496 | /* |
| 2497 | * The queue hasn't been stopped yet, if the old table type wasn't |
| 2498 | * for request-based during suspension. So stop it to prevent |
| 2499 | * I/O mapping before resume. |
| 2500 | * This must be done before setting the queue restrictions, |
| 2501 | * because request-based dm may be run just after the setting. |
| 2502 | */ |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2503 | if (dm_table_request_based(t)) |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 2504 | stop_queue(q); |
| 2505 | |
| 2506 | __bind_mempools(md, t); |
| 2507 | |
Eric Dumazet | a12f5d4 | 2014-11-23 09:34:29 -0800 | [diff] [blame] | 2508 | old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 2509 | rcu_assign_pointer(md->map, t); |
Alasdair G Kergon | 36a0456 | 2011-10-31 20:19:04 +0000 | [diff] [blame] | 2510 | md->immutable_target_type = dm_table_get_immutable_target_type(t); |
| 2511 | |
Mike Snitzer | 754c5fc | 2009-06-22 10:12:34 +0100 | [diff] [blame] | 2512 | dm_table_set_restrictions(t, q, limits); |
Hannes Reinecke | 41abc4e | 2014-11-05 14:35:50 +0100 | [diff] [blame] | 2513 | if (old_map) |
| 2514 | dm_sync_table(md); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 2515 | |
Alasdair G Kergon | 042d2a9 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 2516 | return old_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | } |
| 2518 | |
Alasdair G Kergon | a794015 | 2009-12-10 23:52:23 +0000 | [diff] [blame] | 2519 | /* |
| 2520 | * Returns unbound table for the caller to free. |
| 2521 | */ |
| 2522 | static struct dm_table *__unbind(struct mapped_device *md) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2523 | { |
Eric Dumazet | a12f5d4 | 2014-11-23 09:34:29 -0800 | [diff] [blame] | 2524 | struct dm_table *map = rcu_dereference_protected(md->map, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2525 | |
| 2526 | if (!map) |
Alasdair G Kergon | a794015 | 2009-12-10 23:52:23 +0000 | [diff] [blame] | 2527 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | |
| 2529 | dm_table_event_callback(map, NULL, NULL); |
Monam Agarwal | 9cdb852 | 2014-03-23 23:58:27 +0530 | [diff] [blame] | 2530 | RCU_INIT_POINTER(md->map, NULL); |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 2531 | dm_sync_table(md); |
Alasdair G Kergon | a794015 | 2009-12-10 23:52:23 +0000 | [diff] [blame] | 2532 | |
| 2533 | return map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | /* |
| 2537 | * Constructor for a new device. |
| 2538 | */ |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 2539 | int dm_create(int minor, struct mapped_device **result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | { |
| 2541 | struct mapped_device *md; |
| 2542 | |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 2543 | md = alloc_dev(minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2544 | if (!md) |
| 2545 | return -ENXIO; |
| 2546 | |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 2547 | dm_sysfs_init(md); |
| 2548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | *result = md; |
| 2550 | return 0; |
| 2551 | } |
| 2552 | |
Mike Snitzer | a5664da | 2010-08-12 04:14:01 +0100 | [diff] [blame] | 2553 | /* |
| 2554 | * Functions to manage md->type. |
| 2555 | * All are required to hold md->type_lock. |
| 2556 | */ |
| 2557 | void dm_lock_md_type(struct mapped_device *md) |
| 2558 | { |
| 2559 | mutex_lock(&md->type_lock); |
| 2560 | } |
| 2561 | |
| 2562 | void dm_unlock_md_type(struct mapped_device *md) |
| 2563 | { |
| 2564 | mutex_unlock(&md->type_lock); |
| 2565 | } |
| 2566 | |
| 2567 | void dm_set_md_type(struct mapped_device *md, unsigned type) |
| 2568 | { |
Mike Snitzer | 00c4fc3 | 2013-08-27 18:57:03 -0400 | [diff] [blame] | 2569 | BUG_ON(!mutex_is_locked(&md->type_lock)); |
Mike Snitzer | a5664da | 2010-08-12 04:14:01 +0100 | [diff] [blame] | 2570 | md->type = type; |
| 2571 | } |
| 2572 | |
| 2573 | unsigned dm_get_md_type(struct mapped_device *md) |
| 2574 | { |
Mike Snitzer | 00c4fc3 | 2013-08-27 18:57:03 -0400 | [diff] [blame] | 2575 | BUG_ON(!mutex_is_locked(&md->type_lock)); |
Mike Snitzer | a5664da | 2010-08-12 04:14:01 +0100 | [diff] [blame] | 2576 | return md->type; |
| 2577 | } |
| 2578 | |
Alasdair G Kergon | 36a0456 | 2011-10-31 20:19:04 +0000 | [diff] [blame] | 2579 | struct target_type *dm_get_immutable_target_type(struct mapped_device *md) |
| 2580 | { |
| 2581 | return md->immutable_target_type; |
| 2582 | } |
| 2583 | |
Mike Snitzer | 4a0b4dd | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 2584 | /* |
Mike Snitzer | f84cb8a | 2013-09-19 12:13:58 -0400 | [diff] [blame] | 2585 | * The queue_limits are only valid as long as you have a reference |
| 2586 | * count on 'md'. |
| 2587 | */ |
| 2588 | struct queue_limits *dm_get_queue_limits(struct mapped_device *md) |
| 2589 | { |
| 2590 | BUG_ON(!atomic_read(&md->holders)); |
| 2591 | return &md->queue->limits; |
| 2592 | } |
| 2593 | EXPORT_SYMBOL_GPL(dm_get_queue_limits); |
| 2594 | |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2595 | static void init_rq_based_worker_thread(struct mapped_device *md) |
| 2596 | { |
| 2597 | /* Initialize the request-based DM worker thread */ |
| 2598 | init_kthread_worker(&md->kworker); |
| 2599 | md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker, |
| 2600 | "kdmwork-%s", dm_device_name(md)); |
| 2601 | } |
| 2602 | |
Mike Snitzer | f84cb8a | 2013-09-19 12:13:58 -0400 | [diff] [blame] | 2603 | /* |
Mike Snitzer | 4a0b4dd | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 2604 | * Fully initialize a request-based queue (->elevator, ->request_fn, etc). |
| 2605 | */ |
| 2606 | static int dm_init_request_based_queue(struct mapped_device *md) |
| 2607 | { |
| 2608 | struct request_queue *q = NULL; |
| 2609 | |
Mike Snitzer | 4a0b4dd | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 2610 | /* Fully initialize the queue */ |
| 2611 | q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL); |
| 2612 | if (!q) |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2613 | return -EINVAL; |
Mike Snitzer | 4a0b4dd | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 2614 | |
Mike Snitzer | 0ce6579 | 2015-02-26 00:50:28 -0500 | [diff] [blame] | 2615 | /* disable dm_request_fn's merge heuristic by default */ |
| 2616 | md->seq_rq_merge_deadline_usecs = 0; |
| 2617 | |
Mike Snitzer | 4a0b4dd | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 2618 | md->queue = q; |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2619 | dm_init_old_md_queue(md); |
Mike Snitzer | 4a0b4dd | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 2620 | blk_queue_softirq_done(md->queue, dm_softirq_done); |
| 2621 | blk_queue_prep_rq(md->queue, dm_prep_fn); |
Mike Snitzer | 4a0b4dd | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 2622 | |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2623 | init_rq_based_worker_thread(md); |
Keith Busch | 2eb6e1e | 2014-10-17 17:46:36 -0600 | [diff] [blame] | 2624 | |
Mike Snitzer | 4a0b4dd | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 2625 | elv_register_queue(md->queue); |
| 2626 | |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2627 | return 0; |
| 2628 | } |
| 2629 | |
| 2630 | static int dm_mq_init_request(void *data, struct request *rq, |
| 2631 | unsigned int hctx_idx, unsigned int request_idx, |
| 2632 | unsigned int numa_node) |
| 2633 | { |
| 2634 | struct mapped_device *md = data; |
| 2635 | struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq); |
| 2636 | |
| 2637 | /* |
| 2638 | * Must initialize md member of tio, otherwise it won't |
| 2639 | * be available in dm_mq_queue_rq. |
| 2640 | */ |
| 2641 | tio->md = md; |
| 2642 | |
| 2643 | return 0; |
| 2644 | } |
| 2645 | |
| 2646 | static int dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 2647 | const struct blk_mq_queue_data *bd) |
| 2648 | { |
| 2649 | struct request *rq = bd->rq; |
| 2650 | struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq); |
| 2651 | struct mapped_device *md = tio->md; |
| 2652 | int srcu_idx; |
| 2653 | struct dm_table *map = dm_get_live_table(md, &srcu_idx); |
| 2654 | struct dm_target *ti; |
| 2655 | sector_t pos; |
| 2656 | |
| 2657 | /* always use block 0 to find the target for flushes for now */ |
| 2658 | pos = 0; |
| 2659 | if (!(rq->cmd_flags & REQ_FLUSH)) |
| 2660 | pos = blk_rq_pos(rq); |
| 2661 | |
| 2662 | ti = dm_table_find_target(map, pos); |
| 2663 | if (!dm_target_is_valid(ti)) { |
| 2664 | dm_put_live_table(md, srcu_idx); |
| 2665 | DMERR_LIMIT("request attempted access beyond the end of device"); |
| 2666 | /* |
| 2667 | * Must perform setup, that rq_completed() requires, |
| 2668 | * before returning BLK_MQ_RQ_QUEUE_ERROR |
| 2669 | */ |
| 2670 | dm_start_request(md, rq); |
| 2671 | return BLK_MQ_RQ_QUEUE_ERROR; |
| 2672 | } |
| 2673 | dm_put_live_table(md, srcu_idx); |
| 2674 | |
| 2675 | if (ti->type->busy && ti->type->busy(ti)) |
| 2676 | return BLK_MQ_RQ_QUEUE_BUSY; |
| 2677 | |
| 2678 | dm_start_request(md, rq); |
| 2679 | |
| 2680 | /* Init tio using md established in .init_request */ |
| 2681 | init_tio(tio, rq, md); |
| 2682 | |
Mike Snitzer | 0223334 | 2015-03-10 23:49:26 -0400 | [diff] [blame] | 2683 | /* |
| 2684 | * Establish tio->ti before queuing work (map_tio_request) |
| 2685 | * or making direct call to map_request(). |
| 2686 | */ |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2687 | tio->ti = ti; |
Mike Snitzer | 0223334 | 2015-03-10 23:49:26 -0400 | [diff] [blame] | 2688 | |
| 2689 | /* Clone the request if underlying devices aren't blk-mq */ |
| 2690 | if (dm_table_get_type(map) == DM_TYPE_REQUEST_BASED) { |
| 2691 | /* clone request is allocated at the end of the pdu */ |
| 2692 | tio->clone = (void *)blk_mq_rq_to_pdu(rq) + sizeof(struct dm_rq_target_io); |
Mike Snitzer | 45714fb | 2015-05-27 15:25:27 -0400 | [diff] [blame] | 2693 | (void) clone_rq(rq, md, tio, GFP_ATOMIC); |
Mike Snitzer | 0223334 | 2015-03-10 23:49:26 -0400 | [diff] [blame] | 2694 | queue_kthread_work(&md->kworker, &tio->work); |
| 2695 | } else { |
| 2696 | /* Direct call is fine since .queue_rq allows allocations */ |
Mike Snitzer | 45714fb | 2015-05-27 15:25:27 -0400 | [diff] [blame] | 2697 | if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) { |
| 2698 | /* Undo dm_start_request() before requeuing */ |
Mikulas Patocka | e262f34 | 2015-06-09 17:22:49 -0400 | [diff] [blame] | 2699 | rq_end_stats(md, rq); |
Mike Snitzer | 45714fb | 2015-05-27 15:25:27 -0400 | [diff] [blame] | 2700 | rq_completed(md, rq_data_dir(rq), false); |
| 2701 | return BLK_MQ_RQ_QUEUE_BUSY; |
| 2702 | } |
Mike Snitzer | 0223334 | 2015-03-10 23:49:26 -0400 | [diff] [blame] | 2703 | } |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2704 | |
| 2705 | return BLK_MQ_RQ_QUEUE_OK; |
| 2706 | } |
| 2707 | |
| 2708 | static struct blk_mq_ops dm_mq_ops = { |
| 2709 | .queue_rq = dm_mq_queue_rq, |
| 2710 | .map_queue = blk_mq_map_queue, |
| 2711 | .complete = dm_softirq_done, |
| 2712 | .init_request = dm_mq_init_request, |
| 2713 | }; |
| 2714 | |
| 2715 | static int dm_init_request_based_blk_mq_queue(struct mapped_device *md) |
| 2716 | { |
Mike Snitzer | 0223334 | 2015-03-10 23:49:26 -0400 | [diff] [blame] | 2717 | unsigned md_type = dm_get_md_type(md); |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2718 | struct request_queue *q; |
| 2719 | int err; |
| 2720 | |
| 2721 | memset(&md->tag_set, 0, sizeof(md->tag_set)); |
| 2722 | md->tag_set.ops = &dm_mq_ops; |
| 2723 | md->tag_set.queue_depth = BLKDEV_MAX_RQ; |
| 2724 | md->tag_set.numa_node = NUMA_NO_NODE; |
| 2725 | md->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE; |
| 2726 | md->tag_set.nr_hw_queues = 1; |
Mike Snitzer | 0223334 | 2015-03-10 23:49:26 -0400 | [diff] [blame] | 2727 | if (md_type == DM_TYPE_REQUEST_BASED) { |
| 2728 | /* make the memory for non-blk-mq clone part of the pdu */ |
| 2729 | md->tag_set.cmd_size = sizeof(struct dm_rq_target_io) + sizeof(struct request); |
| 2730 | } else |
| 2731 | md->tag_set.cmd_size = sizeof(struct dm_rq_target_io); |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2732 | md->tag_set.driver_data = md; |
| 2733 | |
| 2734 | err = blk_mq_alloc_tag_set(&md->tag_set); |
| 2735 | if (err) |
| 2736 | return err; |
| 2737 | |
| 2738 | q = blk_mq_init_allocated_queue(&md->tag_set, md->queue); |
| 2739 | if (IS_ERR(q)) { |
| 2740 | err = PTR_ERR(q); |
| 2741 | goto out_tag_set; |
| 2742 | } |
| 2743 | md->queue = q; |
| 2744 | dm_init_md_queue(md); |
| 2745 | |
| 2746 | /* backfill 'mq' sysfs registration normally done in blk_register_queue */ |
| 2747 | blk_mq_register_disk(md->disk); |
| 2748 | |
Mike Snitzer | 0223334 | 2015-03-10 23:49:26 -0400 | [diff] [blame] | 2749 | if (md_type == DM_TYPE_REQUEST_BASED) |
| 2750 | init_rq_based_worker_thread(md); |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2751 | |
| 2752 | return 0; |
| 2753 | |
| 2754 | out_tag_set: |
| 2755 | blk_mq_free_tag_set(&md->tag_set); |
| 2756 | return err; |
Mike Snitzer | 4a0b4dd | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 2757 | } |
| 2758 | |
Mike Snitzer | 4e6e36c | 2015-06-26 09:42:57 -0400 | [diff] [blame] | 2759 | static unsigned filter_md_type(unsigned type, struct mapped_device *md) |
| 2760 | { |
| 2761 | if (type == DM_TYPE_BIO_BASED) |
| 2762 | return type; |
| 2763 | |
| 2764 | return !md->use_blk_mq ? DM_TYPE_REQUEST_BASED : DM_TYPE_MQ_REQUEST_BASED; |
| 2765 | } |
| 2766 | |
Mike Snitzer | 4a0b4dd | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 2767 | /* |
| 2768 | * Setup the DM device's queue based on md's type |
| 2769 | */ |
| 2770 | int dm_setup_md_queue(struct mapped_device *md) |
| 2771 | { |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2772 | int r; |
Mike Snitzer | 17e149b | 2015-03-11 15:01:09 -0400 | [diff] [blame] | 2773 | unsigned md_type = filter_md_type(dm_get_md_type(md), md); |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2774 | |
| 2775 | switch (md_type) { |
| 2776 | case DM_TYPE_REQUEST_BASED: |
| 2777 | r = dm_init_request_based_queue(md); |
| 2778 | if (r) { |
Mike Snitzer | ff36ab3 | 2015-02-23 17:56:37 -0500 | [diff] [blame] | 2779 | DMWARN("Cannot initialize queue for request-based mapped device"); |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2780 | return r; |
Mike Snitzer | ff36ab3 | 2015-02-23 17:56:37 -0500 | [diff] [blame] | 2781 | } |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2782 | break; |
| 2783 | case DM_TYPE_MQ_REQUEST_BASED: |
| 2784 | r = dm_init_request_based_blk_mq_queue(md); |
| 2785 | if (r) { |
| 2786 | DMWARN("Cannot initialize queue for request-based blk-mq mapped device"); |
| 2787 | return r; |
| 2788 | } |
| 2789 | break; |
| 2790 | case DM_TYPE_BIO_BASED: |
| 2791 | dm_init_old_md_queue(md); |
Mike Snitzer | ff36ab3 | 2015-02-23 17:56:37 -0500 | [diff] [blame] | 2792 | blk_queue_make_request(md->queue, dm_make_request); |
Mikulas Patocka | dbba42d | 2015-10-21 16:34:20 -0400 | [diff] [blame] | 2793 | /* |
| 2794 | * DM handles splitting bios as needed. Free the bio_split bioset |
| 2795 | * since it won't be used (saves 1 process per bio-based DM device). |
| 2796 | */ |
| 2797 | bioset_free(md->queue->bio_split); |
| 2798 | md->queue->bio_split = NULL; |
Mike Snitzer | bfebd1c | 2015-03-08 00:51:47 -0500 | [diff] [blame] | 2799 | break; |
Mike Snitzer | 4a0b4dd | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 2800 | } |
| 2801 | |
| 2802 | return 0; |
| 2803 | } |
| 2804 | |
Mikulas Patocka | 2bec1f4 | 2015-02-17 14:30:53 -0500 | [diff] [blame] | 2805 | struct mapped_device *dm_get_md(dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | { |
| 2807 | struct mapped_device *md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2808 | unsigned minor = MINOR(dev); |
| 2809 | |
| 2810 | if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) |
| 2811 | return NULL; |
| 2812 | |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 2813 | spin_lock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2814 | |
| 2815 | md = idr_find(&_minor_idr, minor); |
Mikulas Patocka | 2bec1f4 | 2015-02-17 14:30:53 -0500 | [diff] [blame] | 2816 | if (md) { |
| 2817 | if ((md == MINOR_ALLOCED || |
| 2818 | (MINOR(disk_devt(dm_disk(md))) != minor) || |
| 2819 | dm_deleting_md(md) || |
| 2820 | test_bit(DMF_FREEING, &md->flags))) { |
| 2821 | md = NULL; |
| 2822 | goto out; |
| 2823 | } |
| 2824 | dm_get(md); |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 2825 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2826 | |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 2827 | out: |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 2828 | spin_unlock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | |
David Teigland | 637842c | 2006-01-06 00:20:00 -0800 | [diff] [blame] | 2830 | return md; |
| 2831 | } |
Alasdair G Kergon | 3cf2e4b | 2011-10-31 20:19:06 +0000 | [diff] [blame] | 2832 | EXPORT_SYMBOL_GPL(dm_get_md); |
David Teigland | d229a95 | 2006-01-06 00:20:01 -0800 | [diff] [blame] | 2833 | |
Alasdair G Kergon | 9ade92a | 2006-03-27 01:17:53 -0800 | [diff] [blame] | 2834 | void *dm_get_mdptr(struct mapped_device *md) |
David Teigland | 637842c | 2006-01-06 00:20:00 -0800 | [diff] [blame] | 2835 | { |
Alasdair G Kergon | 9ade92a | 2006-03-27 01:17:53 -0800 | [diff] [blame] | 2836 | return md->interface_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2837 | } |
| 2838 | |
| 2839 | void dm_set_mdptr(struct mapped_device *md, void *ptr) |
| 2840 | { |
| 2841 | md->interface_ptr = ptr; |
| 2842 | } |
| 2843 | |
| 2844 | void dm_get(struct mapped_device *md) |
| 2845 | { |
| 2846 | atomic_inc(&md->holders); |
Kiyoshi Ueda | 3f77316d | 2010-08-12 04:13:56 +0100 | [diff] [blame] | 2847 | BUG_ON(test_bit(DMF_FREEING, &md->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | } |
| 2849 | |
Mikulas Patocka | 09ee96b | 2015-02-26 11:41:28 -0500 | [diff] [blame] | 2850 | int dm_hold(struct mapped_device *md) |
| 2851 | { |
| 2852 | spin_lock(&_minor_lock); |
| 2853 | if (test_bit(DMF_FREEING, &md->flags)) { |
| 2854 | spin_unlock(&_minor_lock); |
| 2855 | return -EBUSY; |
| 2856 | } |
| 2857 | dm_get(md); |
| 2858 | spin_unlock(&_minor_lock); |
| 2859 | return 0; |
| 2860 | } |
| 2861 | EXPORT_SYMBOL_GPL(dm_hold); |
| 2862 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 2863 | const char *dm_device_name(struct mapped_device *md) |
| 2864 | { |
| 2865 | return md->name; |
| 2866 | } |
| 2867 | EXPORT_SYMBOL_GPL(dm_device_name); |
| 2868 | |
Kiyoshi Ueda | 3f77316d | 2010-08-12 04:13:56 +0100 | [diff] [blame] | 2869 | static void __dm_destroy(struct mapped_device *md, bool wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2870 | { |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 2871 | struct dm_table *map; |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 2872 | int srcu_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2873 | |
Kiyoshi Ueda | 3f77316d | 2010-08-12 04:13:56 +0100 | [diff] [blame] | 2874 | might_sleep(); |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 2875 | |
Mike Snitzer | 63a4f06 | 2015-03-23 17:01:43 -0400 | [diff] [blame] | 2876 | spin_lock(&_minor_lock); |
Kiyoshi Ueda | 3f77316d | 2010-08-12 04:13:56 +0100 | [diff] [blame] | 2877 | idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md)))); |
| 2878 | set_bit(DMF_FREEING, &md->flags); |
| 2879 | spin_unlock(&_minor_lock); |
| 2880 | |
Mike Snitzer | 0223334 | 2015-03-10 23:49:26 -0400 | [diff] [blame] | 2881 | if (dm_request_based(md) && md->kworker_task) |
Keith Busch | 2eb6e1e | 2014-10-17 17:46:36 -0600 | [diff] [blame] | 2882 | flush_kthread_worker(&md->kworker); |
| 2883 | |
Mikulas Patocka | ab7c7bb | 2015-02-27 14:04:27 -0500 | [diff] [blame] | 2884 | /* |
| 2885 | * Take suspend_lock so that presuspend and postsuspend methods |
| 2886 | * do not race with internal suspend. |
| 2887 | */ |
| 2888 | mutex_lock(&md->suspend_lock); |
Junichi Nomura | 2a708cf | 2015-10-01 08:31:51 +0000 | [diff] [blame] | 2889 | map = dm_get_live_table(md, &srcu_idx); |
Kiyoshi Ueda | 3f77316d | 2010-08-12 04:13:56 +0100 | [diff] [blame] | 2890 | if (!dm_suspended_md(md)) { |
| 2891 | dm_table_presuspend_targets(map); |
| 2892 | dm_table_postsuspend_targets(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2893 | } |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 2894 | /* dm_put_live_table must be before msleep, otherwise deadlock is possible */ |
| 2895 | dm_put_live_table(md, srcu_idx); |
Junichi Nomura | 2a708cf | 2015-10-01 08:31:51 +0000 | [diff] [blame] | 2896 | mutex_unlock(&md->suspend_lock); |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 2897 | |
Kiyoshi Ueda | 3f77316d | 2010-08-12 04:13:56 +0100 | [diff] [blame] | 2898 | /* |
| 2899 | * Rare, but there may be I/O requests still going to complete, |
| 2900 | * for example. Wait for all references to disappear. |
| 2901 | * No one should increment the reference count of the mapped_device, |
| 2902 | * after the mapped_device state becomes DMF_FREEING. |
| 2903 | */ |
| 2904 | if (wait) |
| 2905 | while (atomic_read(&md->holders)) |
| 2906 | msleep(1); |
| 2907 | else if (atomic_read(&md->holders)) |
| 2908 | DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)", |
| 2909 | dm_device_name(md), atomic_read(&md->holders)); |
| 2910 | |
| 2911 | dm_sysfs_exit(md); |
Kiyoshi Ueda | 3f77316d | 2010-08-12 04:13:56 +0100 | [diff] [blame] | 2912 | dm_table_destroy(__unbind(md)); |
| 2913 | free_dev(md); |
| 2914 | } |
| 2915 | |
| 2916 | void dm_destroy(struct mapped_device *md) |
| 2917 | { |
| 2918 | __dm_destroy(md, true); |
| 2919 | } |
| 2920 | |
| 2921 | void dm_destroy_immediate(struct mapped_device *md) |
| 2922 | { |
| 2923 | __dm_destroy(md, false); |
| 2924 | } |
| 2925 | |
| 2926 | void dm_put(struct mapped_device *md) |
| 2927 | { |
| 2928 | atomic_dec(&md->holders); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2929 | } |
Edward Goggin | 79eb885 | 2007-05-09 02:32:56 -0700 | [diff] [blame] | 2930 | EXPORT_SYMBOL_GPL(dm_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | |
Mikulas Patocka | 401600d | 2009-04-02 19:55:38 +0100 | [diff] [blame] | 2932 | static int dm_wait_for_completion(struct mapped_device *md, int interruptible) |
Milan Broz | 46125c1 | 2008-02-08 02:10:30 +0000 | [diff] [blame] | 2933 | { |
| 2934 | int r = 0; |
Mikulas Patocka | b44ebeb | 2009-04-02 19:55:39 +0100 | [diff] [blame] | 2935 | DECLARE_WAITQUEUE(wait, current); |
| 2936 | |
Mikulas Patocka | b44ebeb | 2009-04-02 19:55:39 +0100 | [diff] [blame] | 2937 | add_wait_queue(&md->wait, &wait); |
Milan Broz | 46125c1 | 2008-02-08 02:10:30 +0000 | [diff] [blame] | 2938 | |
| 2939 | while (1) { |
Mikulas Patocka | 401600d | 2009-04-02 19:55:38 +0100 | [diff] [blame] | 2940 | set_current_state(interruptible); |
Milan Broz | 46125c1 | 2008-02-08 02:10:30 +0000 | [diff] [blame] | 2941 | |
Kiyoshi Ueda | b4324fe | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 2942 | if (!md_in_flight(md)) |
Milan Broz | 46125c1 | 2008-02-08 02:10:30 +0000 | [diff] [blame] | 2943 | break; |
| 2944 | |
Mikulas Patocka | 401600d | 2009-04-02 19:55:38 +0100 | [diff] [blame] | 2945 | if (interruptible == TASK_INTERRUPTIBLE && |
| 2946 | signal_pending(current)) { |
Milan Broz | 46125c1 | 2008-02-08 02:10:30 +0000 | [diff] [blame] | 2947 | r = -EINTR; |
| 2948 | break; |
| 2949 | } |
| 2950 | |
| 2951 | io_schedule(); |
| 2952 | } |
| 2953 | set_current_state(TASK_RUNNING); |
| 2954 | |
Mikulas Patocka | b44ebeb | 2009-04-02 19:55:39 +0100 | [diff] [blame] | 2955 | remove_wait_queue(&md->wait, &wait); |
| 2956 | |
Milan Broz | 46125c1 | 2008-02-08 02:10:30 +0000 | [diff] [blame] | 2957 | return r; |
| 2958 | } |
| 2959 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2960 | /* |
| 2961 | * Process the deferred bios |
| 2962 | */ |
Mikulas Patocka | ef20858 | 2009-04-02 19:55:38 +0100 | [diff] [blame] | 2963 | static void dm_wq_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2964 | { |
Mikulas Patocka | ef20858 | 2009-04-02 19:55:38 +0100 | [diff] [blame] | 2965 | struct mapped_device *md = container_of(work, struct mapped_device, |
| 2966 | work); |
Milan Broz | 6d6f10d | 2008-02-08 02:10:22 +0000 | [diff] [blame] | 2967 | struct bio *c; |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 2968 | int srcu_idx; |
| 2969 | struct dm_table *map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2970 | |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 2971 | map = dm_get_live_table(md, &srcu_idx); |
Mikulas Patocka | ef20858 | 2009-04-02 19:55:38 +0100 | [diff] [blame] | 2972 | |
Mikulas Patocka | 3b00b20 | 2009-04-09 00:27:15 +0100 | [diff] [blame] | 2973 | while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { |
Alasdair G Kergon | df12ee9 | 2009-04-09 00:27:13 +0100 | [diff] [blame] | 2974 | spin_lock_irq(&md->deferred_lock); |
| 2975 | c = bio_list_pop(&md->deferred); |
| 2976 | spin_unlock_irq(&md->deferred_lock); |
Mikulas Patocka | 022c261 | 2009-04-02 19:55:39 +0100 | [diff] [blame] | 2977 | |
Tejun Heo | 6a8736d | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 2978 | if (!c) |
Alasdair G Kergon | df12ee9 | 2009-04-09 00:27:13 +0100 | [diff] [blame] | 2979 | break; |
Alasdair G Kergon | df12ee9 | 2009-04-09 00:27:13 +0100 | [diff] [blame] | 2980 | |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 2981 | if (dm_request_based(md)) |
| 2982 | generic_make_request(c); |
Tejun Heo | 6a8736d | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 2983 | else |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 2984 | __split_and_process_bio(md, map, c); |
Mikulas Patocka | 022c261 | 2009-04-02 19:55:39 +0100 | [diff] [blame] | 2985 | } |
Milan Broz | 73d410c | 2008-02-08 02:10:25 +0000 | [diff] [blame] | 2986 | |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 2987 | dm_put_live_table(md, srcu_idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2988 | } |
| 2989 | |
Mikulas Patocka | 9a1fb46 | 2009-04-02 19:55:36 +0100 | [diff] [blame] | 2990 | static void dm_queue_flush(struct mapped_device *md) |
Milan Broz | 304f3f6 | 2008-02-08 02:11:17 +0000 | [diff] [blame] | 2991 | { |
Mikulas Patocka | 3b00b20 | 2009-04-09 00:27:15 +0100 | [diff] [blame] | 2992 | clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 2993 | smp_mb__after_atomic(); |
Mikulas Patocka | 53d5914 | 2009-04-02 19:55:37 +0100 | [diff] [blame] | 2994 | queue_work(md->wq, &md->work); |
Milan Broz | 304f3f6 | 2008-02-08 02:11:17 +0000 | [diff] [blame] | 2995 | } |
| 2996 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2997 | /* |
Alasdair G Kergon | 042d2a9 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 2998 | * Swap in a new table, returning the old one for the caller to destroy. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | */ |
Alasdair G Kergon | 042d2a9 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 3000 | struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | { |
Mike Christie | 87eb5b2 | 2013-03-01 22:45:48 +0000 | [diff] [blame] | 3002 | struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL); |
Mike Snitzer | 754c5fc | 2009-06-22 10:12:34 +0100 | [diff] [blame] | 3003 | struct queue_limits limits; |
Alasdair G Kergon | 042d2a9 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 3004 | int r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3005 | |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 3006 | mutex_lock(&md->suspend_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3007 | |
| 3008 | /* device must be suspended */ |
Kiyoshi Ueda | 4f186f8 | 2009-12-10 23:52:26 +0000 | [diff] [blame] | 3009 | if (!dm_suspended_md(md)) |
Alasdair G Kergon | 93c534a | 2005-07-12 15:53:05 -0700 | [diff] [blame] | 3010 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3011 | |
Mike Snitzer | 3ae7065 | 2012-09-26 23:45:45 +0100 | [diff] [blame] | 3012 | /* |
| 3013 | * If the new table has no data devices, retain the existing limits. |
| 3014 | * This helps multipath with queue_if_no_path if all paths disappear, |
| 3015 | * then new I/O is queued based on these limits, and then some paths |
| 3016 | * reappear. |
| 3017 | */ |
| 3018 | if (dm_table_has_no_data_devices(table)) { |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 3019 | live_map = dm_get_live_table_fast(md); |
Mike Snitzer | 3ae7065 | 2012-09-26 23:45:45 +0100 | [diff] [blame] | 3020 | if (live_map) |
| 3021 | limits = md->queue->limits; |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 3022 | dm_put_live_table_fast(md); |
Mike Snitzer | 3ae7065 | 2012-09-26 23:45:45 +0100 | [diff] [blame] | 3023 | } |
| 3024 | |
Mike Christie | 87eb5b2 | 2013-03-01 22:45:48 +0000 | [diff] [blame] | 3025 | if (!live_map) { |
| 3026 | r = dm_calculate_queue_limits(table, &limits); |
| 3027 | if (r) { |
| 3028 | map = ERR_PTR(r); |
| 3029 | goto out; |
| 3030 | } |
Alasdair G Kergon | 042d2a9 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 3031 | } |
Mike Snitzer | 754c5fc | 2009-06-22 10:12:34 +0100 | [diff] [blame] | 3032 | |
Alasdair G Kergon | 042d2a9 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 3033 | map = __bind(md, table, &limits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3034 | |
Alasdair G Kergon | 93c534a | 2005-07-12 15:53:05 -0700 | [diff] [blame] | 3035 | out: |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 3036 | mutex_unlock(&md->suspend_lock); |
Alasdair G Kergon | 042d2a9 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 3037 | return map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3038 | } |
| 3039 | |
| 3040 | /* |
| 3041 | * Functions to lock and unlock any filesystem running on the |
| 3042 | * device. |
| 3043 | */ |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 3044 | static int lock_fs(struct mapped_device *md) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3045 | { |
Alasdair G Kergon | e39e2e9 | 2006-01-06 00:20:05 -0800 | [diff] [blame] | 3046 | int r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3047 | |
| 3048 | WARN_ON(md->frozen_sb); |
Alasdair G Kergon | dfbe03f | 2005-05-05 16:16:04 -0700 | [diff] [blame] | 3049 | |
Mikulas Patocka | db8fef4 | 2009-06-22 10:12:15 +0100 | [diff] [blame] | 3050 | md->frozen_sb = freeze_bdev(md->bdev); |
Alasdair G Kergon | dfbe03f | 2005-05-05 16:16:04 -0700 | [diff] [blame] | 3051 | if (IS_ERR(md->frozen_sb)) { |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 3052 | r = PTR_ERR(md->frozen_sb); |
Alasdair G Kergon | e39e2e9 | 2006-01-06 00:20:05 -0800 | [diff] [blame] | 3053 | md->frozen_sb = NULL; |
| 3054 | return r; |
Alasdair G Kergon | dfbe03f | 2005-05-05 16:16:04 -0700 | [diff] [blame] | 3055 | } |
| 3056 | |
Alasdair G Kergon | aa8d7c2 | 2006-01-06 00:20:06 -0800 | [diff] [blame] | 3057 | set_bit(DMF_FROZEN, &md->flags); |
| 3058 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | return 0; |
| 3060 | } |
| 3061 | |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 3062 | static void unlock_fs(struct mapped_device *md) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3063 | { |
Alasdair G Kergon | aa8d7c2 | 2006-01-06 00:20:06 -0800 | [diff] [blame] | 3064 | if (!test_bit(DMF_FROZEN, &md->flags)) |
| 3065 | return; |
| 3066 | |
Mikulas Patocka | db8fef4 | 2009-06-22 10:12:15 +0100 | [diff] [blame] | 3067 | thaw_bdev(md->bdev, md->frozen_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3068 | md->frozen_sb = NULL; |
Alasdair G Kergon | aa8d7c2 | 2006-01-06 00:20:06 -0800 | [diff] [blame] | 3069 | clear_bit(DMF_FROZEN, &md->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3070 | } |
| 3071 | |
| 3072 | /* |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 3073 | * If __dm_suspend returns 0, the device is completely quiescent |
| 3074 | * now. There is no request-processing activity. All new requests |
| 3075 | * are being added to md->deferred list. |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 3076 | * |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 3077 | * Caller must hold md->suspend_lock |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 3078 | */ |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 3079 | static int __dm_suspend(struct mapped_device *md, struct dm_table *map, |
| 3080 | unsigned suspend_flags, int interruptible) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3081 | { |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 3082 | bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG; |
| 3083 | bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG; |
| 3084 | int r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 3086 | /* |
| 3087 | * DMF_NOFLUSH_SUSPENDING must be set before presuspend. |
| 3088 | * This flag is cleared before dm_suspend returns. |
| 3089 | */ |
| 3090 | if (noflush) |
| 3091 | set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); |
| 3092 | |
Mike Snitzer | d67ee21 | 2014-10-28 20:13:31 -0400 | [diff] [blame] | 3093 | /* |
| 3094 | * This gets reverted if there's an error later and the targets |
| 3095 | * provide the .presuspend_undo hook. |
| 3096 | */ |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 3097 | dm_table_presuspend_targets(map); |
| 3098 | |
Mikulas Patocka | 32a926d | 2009-06-22 10:12:17 +0100 | [diff] [blame] | 3099 | /* |
Kiyoshi Ueda | 9f518b2 | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 3100 | * Flush I/O to the device. |
| 3101 | * Any I/O submitted after lock_fs() may not be flushed. |
| 3102 | * noflush takes precedence over do_lockfs. |
| 3103 | * (lock_fs() flushes I/Os and waits for them to complete.) |
Mikulas Patocka | 32a926d | 2009-06-22 10:12:17 +0100 | [diff] [blame] | 3104 | */ |
| 3105 | if (!noflush && do_lockfs) { |
| 3106 | r = lock_fs(md); |
Mike Snitzer | d67ee21 | 2014-10-28 20:13:31 -0400 | [diff] [blame] | 3107 | if (r) { |
| 3108 | dm_table_presuspend_undo_targets(map); |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 3109 | return r; |
Mike Snitzer | d67ee21 | 2014-10-28 20:13:31 -0400 | [diff] [blame] | 3110 | } |
Alasdair G Kergon | aa8d7c2 | 2006-01-06 00:20:06 -0800 | [diff] [blame] | 3111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | |
| 3113 | /* |
Mikulas Patocka | 3b00b20 | 2009-04-09 00:27:15 +0100 | [diff] [blame] | 3114 | * Here we must make sure that no processes are submitting requests |
| 3115 | * to target drivers i.e. no one may be executing |
| 3116 | * __split_and_process_bio. This is called from dm_request and |
| 3117 | * dm_wq_work. |
| 3118 | * |
| 3119 | * To get all processes out of __split_and_process_bio in dm_request, |
| 3120 | * we take the write lock. To prevent any process from reentering |
Tejun Heo | 6a8736d | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 3121 | * __split_and_process_bio from dm_request and quiesce the thread |
| 3122 | * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call |
| 3123 | * flush_workqueue(md->wq). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3124 | */ |
Alasdair G Kergon | 1eb787e | 2009-04-09 00:27:14 +0100 | [diff] [blame] | 3125 | set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); |
Hannes Reinecke | 41abc4e | 2014-11-05 14:35:50 +0100 | [diff] [blame] | 3126 | if (map) |
| 3127 | synchronize_srcu(&md->io_barrier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3128 | |
Kiyoshi Ueda | d0bcb87 | 2009-12-10 23:52:18 +0000 | [diff] [blame] | 3129 | /* |
Tejun Heo | 29e4013 | 2010-09-08 18:07:00 +0200 | [diff] [blame] | 3130 | * Stop md->queue before flushing md->wq in case request-based |
| 3131 | * dm defers requests to md->wq from md->queue. |
Kiyoshi Ueda | d0bcb87 | 2009-12-10 23:52:18 +0000 | [diff] [blame] | 3132 | */ |
Keith Busch | 2eb6e1e | 2014-10-17 17:46:36 -0600 | [diff] [blame] | 3133 | if (dm_request_based(md)) { |
Kiyoshi Ueda | 9f518b2 | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 3134 | stop_queue(md->queue); |
Mike Snitzer | 0223334 | 2015-03-10 23:49:26 -0400 | [diff] [blame] | 3135 | if (md->kworker_task) |
| 3136 | flush_kthread_worker(&md->kworker); |
Keith Busch | 2eb6e1e | 2014-10-17 17:46:36 -0600 | [diff] [blame] | 3137 | } |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 3138 | |
Kiyoshi Ueda | d0bcb87 | 2009-12-10 23:52:18 +0000 | [diff] [blame] | 3139 | flush_workqueue(md->wq); |
| 3140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3141 | /* |
Mikulas Patocka | 3b00b20 | 2009-04-09 00:27:15 +0100 | [diff] [blame] | 3142 | * At this point no more requests are entering target request routines. |
| 3143 | * We call dm_wait_for_completion to wait for all existing requests |
| 3144 | * to finish. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3145 | */ |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 3146 | r = dm_wait_for_completion(md, interruptible); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | |
Milan Broz | 6d6f10d | 2008-02-08 02:10:22 +0000 | [diff] [blame] | 3148 | if (noflush) |
Mikulas Patocka | 022c261 | 2009-04-02 19:55:39 +0100 | [diff] [blame] | 3149 | clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); |
Hannes Reinecke | 41abc4e | 2014-11-05 14:35:50 +0100 | [diff] [blame] | 3150 | if (map) |
| 3151 | synchronize_srcu(&md->io_barrier); |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 3152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | /* were we interrupted ? */ |
Milan Broz | 46125c1 | 2008-02-08 02:10:30 +0000 | [diff] [blame] | 3154 | if (r < 0) { |
Mikulas Patocka | 9a1fb46 | 2009-04-02 19:55:36 +0100 | [diff] [blame] | 3155 | dm_queue_flush(md); |
Milan Broz | 73d410c | 2008-02-08 02:10:25 +0000 | [diff] [blame] | 3156 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 3157 | if (dm_request_based(md)) |
Kiyoshi Ueda | 9f518b2 | 2009-12-10 23:52:16 +0000 | [diff] [blame] | 3158 | start_queue(md->queue); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 3159 | |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 3160 | unlock_fs(md); |
Mike Snitzer | d67ee21 | 2014-10-28 20:13:31 -0400 | [diff] [blame] | 3161 | dm_table_presuspend_undo_targets(map); |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 3162 | /* pushback list is already flushed, so skip flush */ |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 3163 | } |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 3164 | |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 3165 | return r; |
| 3166 | } |
| 3167 | |
| 3168 | /* |
| 3169 | * We need to be able to change a mapping table under a mounted |
| 3170 | * filesystem. For example we might want to move some data in |
| 3171 | * the background. Before the table can be swapped with |
| 3172 | * dm_bind_table, dm_suspend must be called to flush any in |
| 3173 | * flight bios and ensure that any further io gets deferred. |
| 3174 | */ |
| 3175 | /* |
| 3176 | * Suspend mechanism in request-based dm. |
| 3177 | * |
| 3178 | * 1. Flush all I/Os by lock_fs() if needed. |
| 3179 | * 2. Stop dispatching any I/O by stopping the request_queue. |
| 3180 | * 3. Wait for all in-flight I/Os to be completed or requeued. |
| 3181 | * |
| 3182 | * To abort suspend, start the request_queue. |
| 3183 | */ |
| 3184 | int dm_suspend(struct mapped_device *md, unsigned suspend_flags) |
| 3185 | { |
| 3186 | struct dm_table *map = NULL; |
| 3187 | int r = 0; |
| 3188 | |
| 3189 | retry: |
| 3190 | mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); |
| 3191 | |
| 3192 | if (dm_suspended_md(md)) { |
| 3193 | r = -EINVAL; |
| 3194 | goto out_unlock; |
| 3195 | } |
| 3196 | |
| 3197 | if (dm_suspended_internally_md(md)) { |
| 3198 | /* already internally suspended, wait for internal resume */ |
| 3199 | mutex_unlock(&md->suspend_lock); |
| 3200 | r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); |
| 3201 | if (r) |
| 3202 | return r; |
| 3203 | goto retry; |
| 3204 | } |
| 3205 | |
Eric Dumazet | a12f5d4 | 2014-11-23 09:34:29 -0800 | [diff] [blame] | 3206 | map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 3207 | |
| 3208 | r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE); |
| 3209 | if (r) |
| 3210 | goto out_unlock; |
Mikulas Patocka | 3b00b20 | 2009-04-09 00:27:15 +0100 | [diff] [blame] | 3211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3212 | set_bit(DMF_SUSPENDED, &md->flags); |
| 3213 | |
Kiyoshi Ueda | 4d4471c | 2009-12-10 23:52:26 +0000 | [diff] [blame] | 3214 | dm_table_postsuspend_targets(map); |
| 3215 | |
Alasdair G Kergon | d287483 | 2006-11-08 17:44:43 -0800 | [diff] [blame] | 3216 | out_unlock: |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 3217 | mutex_unlock(&md->suspend_lock); |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 3218 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3219 | } |
| 3220 | |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 3221 | static int __dm_resume(struct mapped_device *md, struct dm_table *map) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3222 | { |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 3223 | if (map) { |
| 3224 | int r = dm_table_resume_targets(map); |
| 3225 | if (r) |
| 3226 | return r; |
| 3227 | } |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 3228 | |
Mikulas Patocka | 9a1fb46 | 2009-04-02 19:55:36 +0100 | [diff] [blame] | 3229 | dm_queue_flush(md); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 3230 | |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 3231 | /* |
| 3232 | * Flushing deferred I/Os must be done after targets are resumed |
| 3233 | * so that mapping of targets can work correctly. |
| 3234 | * Request-based dm is queueing the deferred I/Os in its request_queue. |
| 3235 | */ |
| 3236 | if (dm_request_based(md)) |
| 3237 | start_queue(md->queue); |
| 3238 | |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 3239 | unlock_fs(md); |
| 3240 | |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 3241 | return 0; |
| 3242 | } |
| 3243 | |
| 3244 | int dm_resume(struct mapped_device *md) |
| 3245 | { |
| 3246 | int r = -EINVAL; |
| 3247 | struct dm_table *map = NULL; |
| 3248 | |
| 3249 | retry: |
| 3250 | mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); |
| 3251 | |
| 3252 | if (!dm_suspended_md(md)) |
| 3253 | goto out; |
| 3254 | |
| 3255 | if (dm_suspended_internally_md(md)) { |
| 3256 | /* already internally suspended, wait for internal resume */ |
| 3257 | mutex_unlock(&md->suspend_lock); |
| 3258 | r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); |
| 3259 | if (r) |
| 3260 | return r; |
| 3261 | goto retry; |
| 3262 | } |
| 3263 | |
Eric Dumazet | a12f5d4 | 2014-11-23 09:34:29 -0800 | [diff] [blame] | 3264 | map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 3265 | if (!map || !dm_table_get_size(map)) |
| 3266 | goto out; |
| 3267 | |
| 3268 | r = __dm_resume(md, map); |
| 3269 | if (r) |
| 3270 | goto out; |
| 3271 | |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 3272 | clear_bit(DMF_SUSPENDED, &md->flags); |
| 3273 | |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 3274 | r = 0; |
| 3275 | out: |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 3276 | mutex_unlock(&md->suspend_lock); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 3277 | |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 3278 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3279 | } |
| 3280 | |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 3281 | /* |
| 3282 | * Internal suspend/resume works like userspace-driven suspend. It waits |
| 3283 | * until all bios finish and prevents issuing new bios to the target drivers. |
| 3284 | * It may be used only from the kernel. |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 3285 | */ |
| 3286 | |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 3287 | static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags) |
| 3288 | { |
| 3289 | struct dm_table *map = NULL; |
| 3290 | |
Mikulas Patocka | 96b26c8 | 2015-01-08 18:52:26 -0500 | [diff] [blame] | 3291 | if (md->internal_suspend_count++) |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 3292 | return; /* nested internal suspend */ |
| 3293 | |
| 3294 | if (dm_suspended_md(md)) { |
| 3295 | set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); |
| 3296 | return; /* nest suspend */ |
| 3297 | } |
| 3298 | |
Eric Dumazet | a12f5d4 | 2014-11-23 09:34:29 -0800 | [diff] [blame] | 3299 | map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 3300 | |
| 3301 | /* |
| 3302 | * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is |
| 3303 | * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend |
| 3304 | * would require changing .presuspend to return an error -- avoid this |
| 3305 | * until there is a need for more elaborate variants of internal suspend. |
| 3306 | */ |
| 3307 | (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE); |
| 3308 | |
| 3309 | set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); |
| 3310 | |
| 3311 | dm_table_postsuspend_targets(map); |
| 3312 | } |
| 3313 | |
| 3314 | static void __dm_internal_resume(struct mapped_device *md) |
| 3315 | { |
Mikulas Patocka | 96b26c8 | 2015-01-08 18:52:26 -0500 | [diff] [blame] | 3316 | BUG_ON(!md->internal_suspend_count); |
| 3317 | |
| 3318 | if (--md->internal_suspend_count) |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 3319 | return; /* resume from nested internal suspend */ |
| 3320 | |
| 3321 | if (dm_suspended_md(md)) |
| 3322 | goto done; /* resume from nested suspend */ |
| 3323 | |
| 3324 | /* |
| 3325 | * NOTE: existing callers don't need to call dm_table_resume_targets |
| 3326 | * (which may fail -- so best to avoid it for now by passing NULL map) |
| 3327 | */ |
| 3328 | (void) __dm_resume(md, NULL); |
| 3329 | |
| 3330 | done: |
| 3331 | clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); |
| 3332 | smp_mb__after_atomic(); |
| 3333 | wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY); |
| 3334 | } |
| 3335 | |
| 3336 | void dm_internal_suspend_noflush(struct mapped_device *md) |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 3337 | { |
| 3338 | mutex_lock(&md->suspend_lock); |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 3339 | __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG); |
| 3340 | mutex_unlock(&md->suspend_lock); |
| 3341 | } |
| 3342 | EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush); |
| 3343 | |
| 3344 | void dm_internal_resume(struct mapped_device *md) |
| 3345 | { |
| 3346 | mutex_lock(&md->suspend_lock); |
| 3347 | __dm_internal_resume(md); |
| 3348 | mutex_unlock(&md->suspend_lock); |
| 3349 | } |
| 3350 | EXPORT_SYMBOL_GPL(dm_internal_resume); |
| 3351 | |
| 3352 | /* |
| 3353 | * Fast variants of internal suspend/resume hold md->suspend_lock, |
| 3354 | * which prevents interaction with userspace-driven suspend. |
| 3355 | */ |
| 3356 | |
| 3357 | void dm_internal_suspend_fast(struct mapped_device *md) |
| 3358 | { |
| 3359 | mutex_lock(&md->suspend_lock); |
| 3360 | if (dm_suspended_md(md) || dm_suspended_internally_md(md)) |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 3361 | return; |
| 3362 | |
| 3363 | set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); |
| 3364 | synchronize_srcu(&md->io_barrier); |
| 3365 | flush_workqueue(md->wq); |
| 3366 | dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); |
| 3367 | } |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 3368 | EXPORT_SYMBOL_GPL(dm_internal_suspend_fast); |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 3369 | |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 3370 | void dm_internal_resume_fast(struct mapped_device *md) |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 3371 | { |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 3372 | if (dm_suspended_md(md) || dm_suspended_internally_md(md)) |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 3373 | goto done; |
| 3374 | |
| 3375 | dm_queue_flush(md); |
| 3376 | |
| 3377 | done: |
| 3378 | mutex_unlock(&md->suspend_lock); |
| 3379 | } |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 3380 | EXPORT_SYMBOL_GPL(dm_internal_resume_fast); |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 3381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3382 | /*----------------------------------------------------------------- |
| 3383 | * Event notification. |
| 3384 | *---------------------------------------------------------------*/ |
Peter Rajnoha | 3abf85b | 2010-03-06 02:32:31 +0000 | [diff] [blame] | 3385 | int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action, |
Milan Broz | 60935eb | 2009-06-22 10:12:30 +0100 | [diff] [blame] | 3386 | unsigned cookie) |
Alasdair G Kergon | 69267a3 | 2007-12-13 14:15:57 +0000 | [diff] [blame] | 3387 | { |
Milan Broz | 60935eb | 2009-06-22 10:12:30 +0100 | [diff] [blame] | 3388 | char udev_cookie[DM_COOKIE_LENGTH]; |
| 3389 | char *envp[] = { udev_cookie, NULL }; |
| 3390 | |
| 3391 | if (!cookie) |
Peter Rajnoha | 3abf85b | 2010-03-06 02:32:31 +0000 | [diff] [blame] | 3392 | return kobject_uevent(&disk_to_dev(md->disk)->kobj, action); |
Milan Broz | 60935eb | 2009-06-22 10:12:30 +0100 | [diff] [blame] | 3393 | else { |
| 3394 | snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u", |
| 3395 | DM_COOKIE_ENV_VAR_NAME, cookie); |
Peter Rajnoha | 3abf85b | 2010-03-06 02:32:31 +0000 | [diff] [blame] | 3396 | return kobject_uevent_env(&disk_to_dev(md->disk)->kobj, |
| 3397 | action, envp); |
Milan Broz | 60935eb | 2009-06-22 10:12:30 +0100 | [diff] [blame] | 3398 | } |
Alasdair G Kergon | 69267a3 | 2007-12-13 14:15:57 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 3401 | uint32_t dm_next_uevent_seq(struct mapped_device *md) |
| 3402 | { |
| 3403 | return atomic_add_return(1, &md->uevent_seq); |
| 3404 | } |
| 3405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3406 | uint32_t dm_get_event_nr(struct mapped_device *md) |
| 3407 | { |
| 3408 | return atomic_read(&md->event_nr); |
| 3409 | } |
| 3410 | |
| 3411 | int dm_wait_event(struct mapped_device *md, int event_nr) |
| 3412 | { |
| 3413 | return wait_event_interruptible(md->eventq, |
| 3414 | (event_nr != atomic_read(&md->event_nr))); |
| 3415 | } |
| 3416 | |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 3417 | void dm_uevent_add(struct mapped_device *md, struct list_head *elist) |
| 3418 | { |
| 3419 | unsigned long flags; |
| 3420 | |
| 3421 | spin_lock_irqsave(&md->uevent_lock, flags); |
| 3422 | list_add(elist, &md->uevent_list); |
| 3423 | spin_unlock_irqrestore(&md->uevent_lock, flags); |
| 3424 | } |
| 3425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3426 | /* |
| 3427 | * The gendisk is only valid as long as you have a reference |
| 3428 | * count on 'md'. |
| 3429 | */ |
| 3430 | struct gendisk *dm_disk(struct mapped_device *md) |
| 3431 | { |
| 3432 | return md->disk; |
| 3433 | } |
Sami Tolvanen | 65ff5b7 | 2015-03-18 15:52:14 +0000 | [diff] [blame] | 3434 | EXPORT_SYMBOL_GPL(dm_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3435 | |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 3436 | struct kobject *dm_kobject(struct mapped_device *md) |
| 3437 | { |
Mikulas Patocka | 2995fa7 | 2014-01-13 19:37:54 -0500 | [diff] [blame] | 3438 | return &md->kobj_holder.kobj; |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 3439 | } |
| 3440 | |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 3441 | struct mapped_device *dm_get_from_kobject(struct kobject *kobj) |
| 3442 | { |
| 3443 | struct mapped_device *md; |
| 3444 | |
Mikulas Patocka | 2995fa7 | 2014-01-13 19:37:54 -0500 | [diff] [blame] | 3445 | md = container_of(kobj, struct mapped_device, kobj_holder.kobj); |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 3446 | |
Milan Broz | 4d89b7b | 2009-06-22 10:12:11 +0100 | [diff] [blame] | 3447 | if (test_bit(DMF_FREEING, &md->flags) || |
Mike Anderson | 432a212 | 2009-12-10 23:52:20 +0000 | [diff] [blame] | 3448 | dm_deleting_md(md)) |
Milan Broz | 4d89b7b | 2009-06-22 10:12:11 +0100 | [diff] [blame] | 3449 | return NULL; |
| 3450 | |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 3451 | dm_get(md); |
| 3452 | return md; |
| 3453 | } |
| 3454 | |
Kiyoshi Ueda | 4f186f8 | 2009-12-10 23:52:26 +0000 | [diff] [blame] | 3455 | int dm_suspended_md(struct mapped_device *md) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3456 | { |
| 3457 | return test_bit(DMF_SUSPENDED, &md->flags); |
| 3458 | } |
| 3459 | |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 3460 | int dm_suspended_internally_md(struct mapped_device *md) |
| 3461 | { |
| 3462 | return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); |
| 3463 | } |
| 3464 | |
Mikulas Patocka | 2c140a2 | 2013-11-01 18:27:41 -0400 | [diff] [blame] | 3465 | int dm_test_deferred_remove_flag(struct mapped_device *md) |
| 3466 | { |
| 3467 | return test_bit(DMF_DEFERRED_REMOVE, &md->flags); |
| 3468 | } |
| 3469 | |
Kiyoshi Ueda | 64dbce5 | 2009-12-10 23:52:27 +0000 | [diff] [blame] | 3470 | int dm_suspended(struct dm_target *ti) |
| 3471 | { |
Kiyoshi Ueda | ecdb2e2 | 2010-03-06 02:29:52 +0000 | [diff] [blame] | 3472 | return dm_suspended_md(dm_table_get_md(ti->table)); |
Kiyoshi Ueda | 64dbce5 | 2009-12-10 23:52:27 +0000 | [diff] [blame] | 3473 | } |
| 3474 | EXPORT_SYMBOL_GPL(dm_suspended); |
| 3475 | |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 3476 | int dm_noflush_suspending(struct dm_target *ti) |
| 3477 | { |
Kiyoshi Ueda | ecdb2e2 | 2010-03-06 02:29:52 +0000 | [diff] [blame] | 3478 | return __noflush_suspending(dm_table_get_md(ti->table)); |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 3479 | } |
| 3480 | EXPORT_SYMBOL_GPL(dm_noflush_suspending); |
| 3481 | |
Mike Snitzer | 78d8e58 | 2015-06-26 10:01:13 -0400 | [diff] [blame] | 3482 | struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, unsigned type, |
| 3483 | unsigned integrity, unsigned per_bio_data_size) |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 3484 | { |
Mike Snitzer | 78d8e58 | 2015-06-26 10:01:13 -0400 | [diff] [blame] | 3485 | struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL); |
| 3486 | struct kmem_cache *cachep = NULL; |
| 3487 | unsigned int pool_size = 0; |
Jun'ichi Nomura | 5f01520 | 2013-03-01 22:45:48 +0000 | [diff] [blame] | 3488 | unsigned int front_pad; |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 3489 | |
| 3490 | if (!pools) |
Mike Snitzer | 4e6e36c | 2015-06-26 09:42:57 -0400 | [diff] [blame] | 3491 | return NULL; |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 3492 | |
Mike Snitzer | 78d8e58 | 2015-06-26 10:01:13 -0400 | [diff] [blame] | 3493 | type = filter_md_type(type, md); |
Mike Snitzer | 17e149b | 2015-03-11 15:01:09 -0400 | [diff] [blame] | 3494 | |
Mike Snitzer | 78d8e58 | 2015-06-26 10:01:13 -0400 | [diff] [blame] | 3495 | switch (type) { |
| 3496 | case DM_TYPE_BIO_BASED: |
| 3497 | cachep = _io_cache; |
| 3498 | pool_size = dm_get_reserved_bio_based_ios(); |
| 3499 | front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone); |
| 3500 | break; |
| 3501 | case DM_TYPE_REQUEST_BASED: |
| 3502 | cachep = _rq_tio_cache; |
| 3503 | pool_size = dm_get_reserved_rq_based_ios(); |
| 3504 | pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache); |
| 3505 | if (!pools->rq_pool) |
| 3506 | goto out; |
| 3507 | /* fall through to setup remaining rq-based pools */ |
| 3508 | case DM_TYPE_MQ_REQUEST_BASED: |
| 3509 | if (!pool_size) |
| 3510 | pool_size = dm_get_reserved_rq_based_ios(); |
| 3511 | front_pad = offsetof(struct dm_rq_clone_bio_info, clone); |
| 3512 | /* per_bio_data_size is not used. See __bind_mempools(). */ |
| 3513 | WARN_ON(per_bio_data_size != 0); |
| 3514 | break; |
| 3515 | default: |
| 3516 | BUG(); |
| 3517 | } |
| 3518 | |
| 3519 | if (cachep) { |
| 3520 | pools->io_pool = mempool_create_slab_pool(pool_size, cachep); |
| 3521 | if (!pools->io_pool) |
| 3522 | goto out; |
| 3523 | } |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 3524 | |
Junichi Nomura | 3d8aab2 | 2014-10-03 11:55:26 +0000 | [diff] [blame] | 3525 | pools->bs = bioset_create_nobvec(pool_size, front_pad); |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 3526 | if (!pools->bs) |
Jun'ichi Nomura | 5f01520 | 2013-03-01 22:45:48 +0000 | [diff] [blame] | 3527 | goto out; |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 3528 | |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 3529 | if (integrity && bioset_integrity_create(pools->bs, pool_size)) |
Jun'ichi Nomura | 5f01520 | 2013-03-01 22:45:48 +0000 | [diff] [blame] | 3530 | goto out; |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 3531 | |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 3532 | return pools; |
Mike Snitzer | 78d8e58 | 2015-06-26 10:01:13 -0400 | [diff] [blame] | 3533 | |
Jun'ichi Nomura | 5f01520 | 2013-03-01 22:45:48 +0000 | [diff] [blame] | 3534 | out: |
| 3535 | dm_free_md_mempools(pools); |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 3536 | |
Mike Snitzer | 4e6e36c | 2015-06-26 09:42:57 -0400 | [diff] [blame] | 3537 | return NULL; |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 3538 | } |
| 3539 | |
| 3540 | void dm_free_md_mempools(struct dm_md_mempools *pools) |
| 3541 | { |
| 3542 | if (!pools) |
| 3543 | return; |
| 3544 | |
Julia Lawall | 6f65985 | 2015-09-13 14:15:05 +0200 | [diff] [blame] | 3545 | mempool_destroy(pools->io_pool); |
| 3546 | mempool_destroy(pools->rq_pool); |
Mike Snitzer | 1ae49ea | 2014-12-05 17:11:05 -0500 | [diff] [blame] | 3547 | |
Kiyoshi Ueda | e6ee8c0 | 2009-06-22 10:12:36 +0100 | [diff] [blame] | 3548 | if (pools->bs) |
| 3549 | bioset_free(pools->bs); |
| 3550 | |
| 3551 | kfree(pools); |
| 3552 | } |
| 3553 | |
Christoph Hellwig | 71cdb69 | 2015-10-15 14:10:51 +0200 | [diff] [blame] | 3554 | static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key, |
| 3555 | u32 flags) |
| 3556 | { |
| 3557 | struct mapped_device *md = bdev->bd_disk->private_data; |
| 3558 | const struct pr_ops *ops; |
| 3559 | struct dm_target *tgt; |
| 3560 | fmode_t mode; |
| 3561 | int srcu_idx, r; |
| 3562 | |
| 3563 | r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx); |
| 3564 | if (r < 0) |
| 3565 | return r; |
| 3566 | |
| 3567 | ops = bdev->bd_disk->fops->pr_ops; |
| 3568 | if (ops && ops->pr_register) |
| 3569 | r = ops->pr_register(bdev, old_key, new_key, flags); |
| 3570 | else |
| 3571 | r = -EOPNOTSUPP; |
| 3572 | |
| 3573 | dm_put_live_table(md, srcu_idx); |
| 3574 | return r; |
| 3575 | } |
| 3576 | |
| 3577 | static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type, |
| 3578 | u32 flags) |
| 3579 | { |
| 3580 | struct mapped_device *md = bdev->bd_disk->private_data; |
| 3581 | const struct pr_ops *ops; |
| 3582 | struct dm_target *tgt; |
| 3583 | fmode_t mode; |
| 3584 | int srcu_idx, r; |
| 3585 | |
| 3586 | r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx); |
| 3587 | if (r < 0) |
| 3588 | return r; |
| 3589 | |
| 3590 | ops = bdev->bd_disk->fops->pr_ops; |
| 3591 | if (ops && ops->pr_reserve) |
| 3592 | r = ops->pr_reserve(bdev, key, type, flags); |
| 3593 | else |
| 3594 | r = -EOPNOTSUPP; |
| 3595 | |
| 3596 | dm_put_live_table(md, srcu_idx); |
| 3597 | return r; |
| 3598 | } |
| 3599 | |
| 3600 | static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type) |
| 3601 | { |
| 3602 | struct mapped_device *md = bdev->bd_disk->private_data; |
| 3603 | const struct pr_ops *ops; |
| 3604 | struct dm_target *tgt; |
| 3605 | fmode_t mode; |
| 3606 | int srcu_idx, r; |
| 3607 | |
| 3608 | r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx); |
| 3609 | if (r < 0) |
| 3610 | return r; |
| 3611 | |
| 3612 | ops = bdev->bd_disk->fops->pr_ops; |
| 3613 | if (ops && ops->pr_release) |
| 3614 | r = ops->pr_release(bdev, key, type); |
| 3615 | else |
| 3616 | r = -EOPNOTSUPP; |
| 3617 | |
| 3618 | dm_put_live_table(md, srcu_idx); |
| 3619 | return r; |
| 3620 | } |
| 3621 | |
| 3622 | static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key, |
| 3623 | enum pr_type type, bool abort) |
| 3624 | { |
| 3625 | struct mapped_device *md = bdev->bd_disk->private_data; |
| 3626 | const struct pr_ops *ops; |
| 3627 | struct dm_target *tgt; |
| 3628 | fmode_t mode; |
| 3629 | int srcu_idx, r; |
| 3630 | |
| 3631 | r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx); |
| 3632 | if (r < 0) |
| 3633 | return r; |
| 3634 | |
| 3635 | ops = bdev->bd_disk->fops->pr_ops; |
| 3636 | if (ops && ops->pr_preempt) |
| 3637 | r = ops->pr_preempt(bdev, old_key, new_key, type, abort); |
| 3638 | else |
| 3639 | r = -EOPNOTSUPP; |
| 3640 | |
| 3641 | dm_put_live_table(md, srcu_idx); |
| 3642 | return r; |
| 3643 | } |
| 3644 | |
| 3645 | static int dm_pr_clear(struct block_device *bdev, u64 key) |
| 3646 | { |
| 3647 | struct mapped_device *md = bdev->bd_disk->private_data; |
| 3648 | const struct pr_ops *ops; |
| 3649 | struct dm_target *tgt; |
| 3650 | fmode_t mode; |
| 3651 | int srcu_idx, r; |
| 3652 | |
| 3653 | r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx); |
| 3654 | if (r < 0) |
| 3655 | return r; |
| 3656 | |
| 3657 | ops = bdev->bd_disk->fops->pr_ops; |
| 3658 | if (ops && ops->pr_clear) |
| 3659 | r = ops->pr_clear(bdev, key); |
| 3660 | else |
| 3661 | r = -EOPNOTSUPP; |
| 3662 | |
| 3663 | dm_put_live_table(md, srcu_idx); |
| 3664 | return r; |
| 3665 | } |
| 3666 | |
| 3667 | static const struct pr_ops dm_pr_ops = { |
| 3668 | .pr_register = dm_pr_register, |
| 3669 | .pr_reserve = dm_pr_reserve, |
| 3670 | .pr_release = dm_pr_release, |
| 3671 | .pr_preempt = dm_pr_preempt, |
| 3672 | .pr_clear = dm_pr_clear, |
| 3673 | }; |
| 3674 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 3675 | static const struct block_device_operations dm_blk_dops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3676 | .open = dm_blk_open, |
| 3677 | .release = dm_blk_close, |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 3678 | .ioctl = dm_blk_ioctl, |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 3679 | .getgeo = dm_blk_getgeo, |
Christoph Hellwig | 71cdb69 | 2015-10-15 14:10:51 +0200 | [diff] [blame] | 3680 | .pr_ops = &dm_pr_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3681 | .owner = THIS_MODULE |
| 3682 | }; |
| 3683 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3684 | /* |
| 3685 | * module hooks |
| 3686 | */ |
| 3687 | module_init(dm_init); |
| 3688 | module_exit(dm_exit); |
| 3689 | |
| 3690 | module_param(major, uint, 0); |
| 3691 | MODULE_PARM_DESC(major, "The major number of the device mapper"); |
Mike Snitzer | f479082 | 2013-09-12 18:06:12 -0400 | [diff] [blame] | 3692 | |
Mike Snitzer | e860313 | 2013-09-12 18:06:12 -0400 | [diff] [blame] | 3693 | module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR); |
| 3694 | MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools"); |
| 3695 | |
Mike Snitzer | f479082 | 2013-09-12 18:06:12 -0400 | [diff] [blame] | 3696 | module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR); |
| 3697 | MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools"); |
| 3698 | |
Mike Snitzer | 17e149b | 2015-03-11 15:01:09 -0400 | [diff] [blame] | 3699 | module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR); |
| 3700 | MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices"); |
| 3701 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3702 | MODULE_DESCRIPTION(DM_NAME " driver"); |
| 3703 | MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); |
| 3704 | MODULE_LICENSE("GPL"); |