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