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