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