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