12 #include <linux/module.h>
18 #include <linux/slab.h>
25 #define DM_MSG_PREFIX "core"
41 #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
42 #define DM_COOKIE_LENGTH 24
44 static const char *_name =
DM_NAME;
46 static unsigned int major = 0;
47 static unsigned int _major = 0;
105 if (bio && bio->bi_private)
112 if (rq && rq->end_io_data)
118 #define MINOR_ALLOCED ((void *)-1)
123 #define DMF_BLOCK_IO_FOR_SUSPEND 0
124 #define DMF_SUSPENDED 1
126 #define DMF_FREEING 3
127 #define DMF_DELETING 4
128 #define DMF_NOFLUSH_SUSPENDING 5
129 #define DMF_MERGE_IS_OPTIONAL 6
226 static int __init local_init(
void)
237 goto out_free_io_cache;
240 if (!_rq_bio_info_cache)
241 goto out_free_rq_tio_cache;
245 goto out_free_rq_bio_info_cache;
250 goto out_uevent_exit;
259 out_free_rq_bio_info_cache:
261 out_free_rq_tio_cache:
269 static void local_exit(
void)
302 static int __init dm_init(
void)
308 for (i = 0; i <
count; i++) {
323 static void __exit dm_exit(
void)
349 spin_lock(&_minor_lock);
351 md = bdev->
bd_disk->private_data;
365 spin_unlock(&_minor_lock);
367 return md ? 0 : -
ENXIO;
370 static int dm_blk_close(
struct gendisk *
disk,
fmode_t mode)
374 spin_lock(&_minor_lock);
379 spin_unlock(&_minor_lock);
396 spin_lock(&_minor_lock);
403 spin_unlock(&_minor_lock);
416 unsigned int cmd,
unsigned long arg)
437 if (tgt->
type->ioctl)
438 r = tgt->
type->ioctl(tgt, cmd, arg);
478 static void start_io_acct(
struct dm_io *
io)
482 int rw = bio_data_dir(io->
bio);
486 cpu = part_stat_lock();
493 static void end_io_acct(
struct dm_io *io)
496 struct bio *bio = io->
bio;
499 int rw = bio_data_dir(bio);
501 cpu = part_stat_lock();
503 part_stat_add(cpu, &
dm_disk(md)->part0,
ticks[rw], duration);
522 static void queue_io(
struct mapped_device *md,
struct bio *bio)
569 DMWARN(
"Start sector is beyond the geometry limits.");
596 static void dec_pending(
struct dm_io *io,
int error)
606 if (!(io->
error > 0 && __noflush_suspending(md)))
608 spin_unlock_irqrestore(&io->
endio_lock, flags);
617 if (__noflush_suspending(md))
625 io_error = io->
error;
633 if ((bio->bi_rw &
REQ_FLUSH) && bio->bi_size) {
642 trace_block_bio_complete(md->
queue, bio, io_error);
648 static void clone_endio(
struct bio *bio,
int error)
656 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
660 r = endio(tio->
ti, bio, error, &tio->
info);
671 DMWARN(
"unimplemented target endio return value: %d", r);
677 dec_pending(io, error);
683 static void end_clone_bio(
struct bio *clone,
int error)
687 struct bio *bio = info->
orig;
688 unsigned int nr_bytes = info->
orig->bi_size;
719 if (tio->
orig->bio != bio)
720 DMERR(
"bio completion is going in the middle of the request");
735 static void rq_completed(
struct mapped_device *md,
int rw,
int run_queue)
740 if (!md_in_flight(md))
758 static void free_rq_clone(
struct request *clone)
770 static void dm_end_request(
struct request *clone,
int error)
772 int rw = rq_data_dir(clone);
777 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
778 rq->errors = clone->errors;
779 rq->resid_len = clone->resid_len;
787 rq->sense_len = clone->sense_len;
790 free_rq_clone(clone);
792 rq_completed(md, rw,
true);
795 static void dm_unprep_request(
struct request *rq)
797 struct request *clone = rq->special;
802 free_rq_clone(clone);
810 int rw = rq_data_dir(clone);
817 dm_unprep_request(rq);
821 spin_unlock_irqrestore(q->queue_lock, flags);
823 rq_completed(md, rw, 0);
838 spin_unlock_irqrestore(q->queue_lock, flags);
843 if (blk_queue_stopped(q))
853 spin_unlock_irqrestore(q->queue_lock, flags);
856 static void dm_done(
struct request *clone,
int error,
bool mapped)
863 rq_end_io = tio->
ti->type->rq_end_io;
865 if (mapped && rq_end_io)
866 r = rq_end_io(tio->
ti, clone, error, &tio->
info);
871 dm_end_request(clone, r);
879 DMWARN(
"unimplemented target endio return value: %d", r);
887 static void dm_softirq_done(
struct request *rq)
890 struct request *clone = rq->completion_data;
896 dm_done(clone, tio->
error, mapped);
903 static void dm_complete_request(
struct request *clone,
int error)
909 rq->completion_data = clone;
925 dm_complete_request(clone, error);
932 static void end_clone_request(
struct request *clone,
int error)
950 dm_complete_request(clone, error);
961 return ti->
len - target_offset;
966 sector_t len = max_io_len_target_boundary(sector, ti);
990 DMERR(
"Specified maximum size of target IO (%llu) exceeds limit (%u)",
992 ti->
error =
"Maximum size of target IO is too large";
1007 struct bio *clone = &tio->
clone;
1009 clone->bi_end_io = clone_endio;
1010 clone->bi_private = tio;
1018 sector = clone->bi_sector;
1019 r = ti->
type->map(ti, clone, &tio->
info);
1023 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1024 tio->
io->bio->bi_bdev->bd_dev, sector);
1030 dec_pending(tio->
io, r);
1033 DMWARN(
"unimplemented target map return value: %d", r);
1051 static void split_bvec(
struct dm_target_io *tio,
struct bio *bio,
1052 sector_t sector,
unsigned short idx,
unsigned int offset,
1053 unsigned int len,
struct bio_set *bs)
1055 struct bio *clone = &tio->
clone;
1056 struct bio_vec *bv = bio->bi_io_vec +
idx;
1058 *clone->bi_io_vec = *bv;
1060 clone->bi_sector =
sector;
1061 clone->bi_bdev = bio->bi_bdev;
1062 clone->bi_rw = bio->bi_rw;
1064 clone->bi_size = to_bytes(len);
1065 clone->bi_io_vec->bv_offset =
offset;
1066 clone->bi_io_vec->bv_len = clone->bi_size;
1067 clone->bi_flags |= 1 << BIO_CLONED;
1069 if (bio_integrity(bio)) {
1079 static void clone_bio(
struct dm_target_io *tio,
struct bio *bio,
1081 unsigned short bv_count,
unsigned int len,
1084 struct bio *clone = &tio->
clone;
1087 clone->bi_sector =
sector;
1088 clone->bi_idx =
idx;
1089 clone->bi_vcnt = idx + bv_count;
1090 clone->bi_size = to_bytes(len);
1091 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
1093 if (bio_integrity(bio)) {
1096 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1122 struct bio *clone = &tio->
clone;
1134 clone->bi_sector = ci->
sector;
1135 clone->bi_size = to_bytes(len);
1142 unsigned num_requests,
sector_t len)
1144 unsigned request_nr;
1146 for (request_nr = 0; request_nr < num_requests; request_nr++)
1147 __issue_target_request(ci, ti, request_nr, len);
1150 static int __clone_and_map_empty_flush(
struct clone_info *ci)
1152 unsigned target_nr = 0;
1167 struct bio *bio = ci->
bio;
1170 tio = alloc_tio(ci, ti, bio->bi_max_vecs);
1171 clone_bio(tio, bio, ci->
sector, ci->
idx, bio->bi_vcnt - ci->
idx,
1177 static int __clone_and_map_discard(
struct clone_info *ci)
1209 static int __clone_and_map(
struct clone_info *ci)
1211 struct bio *bio = ci->
bio;
1217 return __clone_and_map_discard(ci);
1230 __clone_and_map_simple(ci, ti);
1232 }
else if (to_sector(bio->bi_io_vec[ci->
idx].bv_len) <=
max) {
1241 for (i = ci->
idx; remaining && (i < bio->bi_vcnt); i++) {
1242 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
1244 if (bv_len > remaining)
1247 remaining -= bv_len;
1251 tio = alloc_tio(ci, ti, bio->bi_max_vecs);
1252 clone_bio(tio, bio, ci->
sector, ci->
idx, i - ci->
idx, len,
1264 struct bio_vec *bv = bio->bi_io_vec + ci->
idx;
1265 sector_t remaining = to_sector(bv->bv_len);
1266 unsigned int offset = 0;
1277 len =
min(remaining,
max);
1279 tio = alloc_tio(ci, ti, 1);
1280 split_bvec(tio, bio, ci->
sector, ci->
idx,
1281 bv->bv_offset + offset, len, ci->
md->bs);
1287 offset += to_bytes(len);
1288 }
while (remaining -= len);
1299 static void __split_and_process_bio(
struct mapped_device *md,
struct bio *bio)
1311 ci.
io = alloc_io(md);
1317 ci.
sector = bio->bi_sector;
1318 ci.
idx = bio->bi_idx;
1320 start_io_acct(ci.
io);
1322 ci.
bio = &ci.
md->flush_bio;
1324 error = __clone_and_map_empty_flush(&ci);
1330 error = __clone_and_map(&ci);
1334 dec_pending(ci.
io, error);
1342 struct bvec_merge_data *bvm,
1343 struct bio_vec *biovec)
1363 max_size = (max_sectors <<
SECTOR_SHIFT) - bvm->bi_size;
1372 if (max_size && ti->
type->merge)
1373 max_size = ti->
type->merge(ti, bvm, biovec, max_size);
1381 else if (queue_max_hw_sectors(q) <=
PAGE_SIZE >> 9)
1392 if (max_size <= biovec->bv_len && !(bvm->bi_size >>
SECTOR_SHIFT))
1393 max_size = biovec->bv_len;
1402 static void _dm_request(
struct request_queue *q,
struct bio *bio)
1404 int rw = bio_data_dir(bio);
1410 cpu = part_stat_lock();
1411 part_stat_inc(cpu, &
dm_disk(md)->part0, ios[rw]);
1412 part_stat_add(cpu, &
dm_disk(md)->part0,
sectors[rw], bio_sectors(bio));
1419 if (bio_rw(bio) !=
READA)
1426 __split_and_process_bio(md, bio);
1433 return blk_queue_stackable(md->
queue);
1436 static void dm_request(
struct request_queue *q,
struct bio *bio)
1440 if (dm_request_based(md))
1443 _dm_request(q, bio);
1450 if (blk_queue_io_stat(rq->q))
1456 dm_complete_request(rq, r);
1460 static int dm_rq_bio_constructor(
struct bio *bio,
struct bio *bio_orig,
1467 info->
orig = bio_orig;
1469 bio->bi_end_io = end_clone_bio;
1470 bio->bi_private =
info;
1475 static int setup_clone(
struct request *clone,
struct request *rq,
1481 dm_rq_bio_constructor, tio);
1485 clone->cmd = rq->cmd;
1486 clone->cmd_len = rq->cmd_len;
1487 clone->sense = rq->sense;
1488 clone->buffer = rq->buffer;
1489 clone->end_io = end_clone_request;
1490 clone->end_io_data =
tio;
1501 tio = alloc_rq_tio(md, gfp_mask);
1511 clone = &tio->
clone;
1512 if (setup_clone(clone, rq, tio)) {
1530 DMWARN(
"Already has something in rq->special.");
1531 return BLKPREP_KILL;
1536 return BLKPREP_DEFER;
1538 rq->special = clone;
1552 int r, requeued = 0;
1556 r = ti->
type->map_rq(ti, clone, &tio->
info);
1563 trace_block_rq_remap(clone->q, clone, disk_devt(
dm_disk(md)),
1564 blk_rq_pos(tio->
orig));
1574 DMWARN(
"unimplemented target map return value: %d", r);
1591 clone = orig->special;
1624 while (!blk_queue_stopped(q)) {
1632 pos = blk_rq_pos(rq);
1640 DMERR_LIMIT(
"request attempted access beyond the end of device");
1641 clone = dm_start_request(md, rq);
1646 if (ti->
type->busy && ti->
type->busy(ti))
1649 clone = dm_start_request(md, rq);
1651 spin_unlock(q->queue_lock);
1652 if (map_request(ti, clone, md))
1656 spin_lock(q->queue_lock);
1663 spin_lock(q->queue_lock);
1693 static int dm_any_congested(
void *congested_data,
int bdi_bits)
1706 if (dm_request_based(md))
1707 r = md->
queue->backing_dev_info.state &
1722 static void free_minor(
int minor)
1724 spin_lock(&_minor_lock);
1726 spin_unlock(&_minor_lock);
1732 static int specific_minor(
int minor)
1743 spin_lock(&_minor_lock);
1745 if (
idr_find(&_minor_idr, minor)) {
1761 spin_unlock(&_minor_lock);
1765 static int next_free_minor(
int *minor)
1773 spin_lock(&_minor_lock);
1788 spin_unlock(&_minor_lock);
1792 static const struct block_device_operations dm_blk_dops;
1807 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->
queue);
1810 md->
queue->backing_dev_info.congested_fn = dm_any_congested;
1811 md->
queue->backing_dev_info.congested_data =
md;
1827 DMWARN(
"unable to allocate device, out of memory.");
1832 goto bad_module_get;
1836 r = next_free_minor(&minor);
1838 r = specific_minor(minor);
1859 dm_init_md_queue(md);
1872 md->
disk->first_minor = minor;
1873 md->
disk->fops = &dm_blk_dops;
1875 md->
disk->private_data =
md;
1894 spin_lock(&_minor_lock);
1896 spin_unlock(&_minor_lock);
1937 spin_lock(&_minor_lock);
1939 spin_unlock(&_minor_lock);
1973 static void event_callback(
void *
context)
1975 unsigned long flags;
1994 set_capacity(md->
disk, size);
2010 if (!q->merge_bvec_fn)
2013 if (q->make_request_fn == dm_request) {
2014 dev_md = q->queuedata;
2022 static int dm_device_merge_is_compulsory(
struct dm_target *ti,
2036 static int dm_table_merge_is_optional(
struct dm_table *
table)
2044 if (ti->
type->iterate_devices &&
2045 ti->
type->iterate_devices(ti, dm_device_merge_is_compulsory,
NULL))
2056 struct queue_limits *limits)
2061 unsigned long flags;
2062 int merge_is_optional;
2069 if (size != get_capacity(md->
disk))
2072 __set_size(md, size);
2086 __bind_mempools(md, t);
2088 merge_is_optional = dm_table_merge_is_optional(t);
2096 if (merge_is_optional)
2111 unsigned long flags;
2131 md = alloc_dev(minor);
2173 static int dm_init_request_based_queue(
struct mapped_device *md)
2177 if (md->
queue->elevator)
2186 dm_init_md_queue(md);
2202 !dm_init_request_based_queue(md)) {
2203 DMWARN(
"Cannot initialize queue for request-based mapped device");
2213 unsigned minor =
MINOR(dev);
2218 spin_lock(&_minor_lock);
2230 spin_unlock(&_minor_lock);
2274 spin_lock(&_minor_lock);
2278 spin_unlock(&_minor_lock);
2295 DMWARN(
"%s: Forcibly removing mapped_device still in use! (%d users)",
2306 __dm_destroy(md,
true);
2311 __dm_destroy(md,
false);
2320 static int dm_wait_for_completion(
struct mapped_device *md,
int interruptible)
2330 if (!md_in_flight(md))
2369 if (dm_request_based(md))
2372 __split_and_process_bio(md, c);
2393 struct queue_limits limits;
2411 limits = md->
queue->limits;
2421 map = __bind(md, table, &limits);
2508 if (!noflush && do_lockfs) {
2534 if (dm_request_based(md))
2535 stop_queue(md->
queue);
2555 if (dm_request_based(md))
2556 start_queue(md->
queue);
2604 if (dm_request_based(md))
2605 start_queue(md->
queue);
2626 char *envp[] = { udev_cookie,
NULL };
2656 unsigned long flags;
2686 if (&md->
kobj != kobj)
2723 mempool_create_slab_pool(
MIN_IOS, _io_cache) :
2724 mempool_create_slab_pool(
MIN_IOS, _rq_bio_info_cache);
2726 goto free_pools_and_out;
2732 goto free_io_pool_and_out;
2741 goto free_tio_pool_and_out;
2744 goto free_bioset_and_out;
2748 free_bioset_and_out:
2751 free_tio_pool_and_out:
2755 free_io_pool_and_out:
2781 static const struct block_device_operations dm_blk_dops = {
2782 .open = dm_blk_open,
2783 .release = dm_blk_close,
2784 .ioctl = dm_blk_ioctl,
2785 .getgeo = dm_blk_getgeo,