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