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