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