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