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