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