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