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