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