11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Copyright (C) 2001, 2002 Sistina Software (UK) Limited. 3784aae73SMilan Broz * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This file is released under the GPL. 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds #include "dm.h" 91da177e4SLinus Torvalds #include "dm-bio-list.h" 1051e5b2bdSMike Anderson #include "dm-uevent.h" 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #include <linux/init.h> 131da177e4SLinus Torvalds #include <linux/module.h> 1448c9c27bSArjan van de Ven #include <linux/mutex.h> 151da177e4SLinus Torvalds #include <linux/moduleparam.h> 161da177e4SLinus Torvalds #include <linux/blkpg.h> 171da177e4SLinus Torvalds #include <linux/bio.h> 181da177e4SLinus Torvalds #include <linux/buffer_head.h> 191da177e4SLinus Torvalds #include <linux/mempool.h> 201da177e4SLinus Torvalds #include <linux/slab.h> 211da177e4SLinus Torvalds #include <linux/idr.h> 223ac51e74SDarrick J. Wong #include <linux/hdreg.h> 232056a782SJens Axboe #include <linux/blktrace_api.h> 245f3ea37cSArnaldo Carvalho de Melo #include <trace/block.h> 251da177e4SLinus Torvalds 2672d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core" 2772d94861SAlasdair G Kergon 281da177e4SLinus Torvalds static const char *_name = DM_NAME; 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds static unsigned int major = 0; 311da177e4SLinus Torvalds static unsigned int _major = 0; 321da177e4SLinus Torvalds 33f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock); 341da177e4SLinus Torvalds /* 358fbf26adSKiyoshi Ueda * For bio-based dm. 361da177e4SLinus Torvalds * One of these is allocated per bio. 371da177e4SLinus Torvalds */ 381da177e4SLinus Torvalds struct dm_io { 391da177e4SLinus Torvalds struct mapped_device *md; 401da177e4SLinus Torvalds int error; 411da177e4SLinus Torvalds atomic_t io_count; 426ae2fa67SRichard Kennedy struct bio *bio; 433eaf840eSJun'ichi "Nick" Nomura unsigned long start_time; 441da177e4SLinus Torvalds }; 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds /* 478fbf26adSKiyoshi Ueda * For bio-based dm. 481da177e4SLinus Torvalds * One of these is allocated per target within a bio. Hopefully 491da177e4SLinus Torvalds * this will be simplified out one day. 501da177e4SLinus Torvalds */ 51028867acSAlasdair G Kergon struct dm_target_io { 521da177e4SLinus Torvalds struct dm_io *io; 531da177e4SLinus Torvalds struct dm_target *ti; 541da177e4SLinus Torvalds union map_info info; 551da177e4SLinus Torvalds }; 561da177e4SLinus Torvalds 570bfc2455SIngo Molnar DEFINE_TRACE(block_bio_complete); 580bfc2455SIngo Molnar 598fbf26adSKiyoshi Ueda /* 608fbf26adSKiyoshi Ueda * For request-based dm. 618fbf26adSKiyoshi Ueda * One of these is allocated per request. 628fbf26adSKiyoshi Ueda */ 638fbf26adSKiyoshi Ueda struct dm_rq_target_io { 648fbf26adSKiyoshi Ueda struct mapped_device *md; 658fbf26adSKiyoshi Ueda struct dm_target *ti; 668fbf26adSKiyoshi Ueda struct request *orig, clone; 678fbf26adSKiyoshi Ueda int error; 688fbf26adSKiyoshi Ueda union map_info info; 698fbf26adSKiyoshi Ueda }; 708fbf26adSKiyoshi Ueda 718fbf26adSKiyoshi Ueda /* 728fbf26adSKiyoshi Ueda * For request-based dm. 738fbf26adSKiyoshi Ueda * One of these is allocated per bio. 748fbf26adSKiyoshi Ueda */ 758fbf26adSKiyoshi Ueda struct dm_rq_clone_bio_info { 768fbf26adSKiyoshi Ueda struct bio *orig; 778fbf26adSKiyoshi Ueda struct request *rq; 788fbf26adSKiyoshi Ueda }; 798fbf26adSKiyoshi Ueda 801da177e4SLinus Torvalds union map_info *dm_get_mapinfo(struct bio *bio) 811da177e4SLinus Torvalds { 821da177e4SLinus Torvalds if (bio && bio->bi_private) 83028867acSAlasdair G Kergon return &((struct dm_target_io *)bio->bi_private)->info; 841da177e4SLinus Torvalds return NULL; 851da177e4SLinus Torvalds } 861da177e4SLinus Torvalds 87ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1) 88ba61fdd1SJeff Mahoney 891da177e4SLinus Torvalds /* 901da177e4SLinus Torvalds * Bits for the md->flags field. 911da177e4SLinus Torvalds */ 921da177e4SLinus Torvalds #define DMF_BLOCK_IO 0 931da177e4SLinus Torvalds #define DMF_SUSPENDED 1 94aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2 95fba9f90eSJeff Mahoney #define DMF_FREEING 3 965c6bd75dSAlasdair G Kergon #define DMF_DELETING 4 972e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5 981da177e4SLinus Torvalds 99304f3f6aSMilan Broz /* 100304f3f6aSMilan Broz * Work processed by per-device workqueue. 101304f3f6aSMilan Broz */ 102304f3f6aSMilan Broz struct dm_wq_req { 103304f3f6aSMilan Broz enum { 104304f3f6aSMilan Broz DM_WQ_FLUSH_DEFERRED, 105304f3f6aSMilan Broz } type; 106304f3f6aSMilan Broz struct work_struct work; 107304f3f6aSMilan Broz struct mapped_device *md; 108304f3f6aSMilan Broz void *context; 109304f3f6aSMilan Broz }; 110304f3f6aSMilan Broz 1111da177e4SLinus Torvalds struct mapped_device { 1122ca3310eSAlasdair G Kergon struct rw_semaphore io_lock; 113e61290a4SDaniel Walker struct mutex suspend_lock; 1142e93ccc1SKiyoshi Ueda spinlock_t pushback_lock; 1151da177e4SLinus Torvalds rwlock_t map_lock; 1161da177e4SLinus Torvalds atomic_t holders; 1175c6bd75dSAlasdair G Kergon atomic_t open_count; 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds unsigned long flags; 1201da177e4SLinus Torvalds 121165125e1SJens Axboe struct request_queue *queue; 1221da177e4SLinus Torvalds struct gendisk *disk; 1237e51f257SMike Anderson char name[16]; 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds void *interface_ptr; 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds /* 1281da177e4SLinus Torvalds * A list of ios that arrived while we were suspended. 1291da177e4SLinus Torvalds */ 1301da177e4SLinus Torvalds atomic_t pending; 1311da177e4SLinus Torvalds wait_queue_head_t wait; 1321da177e4SLinus Torvalds struct bio_list deferred; 1332e93ccc1SKiyoshi Ueda struct bio_list pushback; 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds /* 136304f3f6aSMilan Broz * Processing queue (flush/barriers) 137304f3f6aSMilan Broz */ 138304f3f6aSMilan Broz struct workqueue_struct *wq; 139304f3f6aSMilan Broz 140304f3f6aSMilan Broz /* 1411da177e4SLinus Torvalds * The current mapping. 1421da177e4SLinus Torvalds */ 1431da177e4SLinus Torvalds struct dm_table *map; 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds /* 1461da177e4SLinus Torvalds * io objects are allocated from here. 1471da177e4SLinus Torvalds */ 1481da177e4SLinus Torvalds mempool_t *io_pool; 1491da177e4SLinus Torvalds mempool_t *tio_pool; 1501da177e4SLinus Torvalds 1519faf400fSStefan Bader struct bio_set *bs; 1529faf400fSStefan Bader 1531da177e4SLinus Torvalds /* 1541da177e4SLinus Torvalds * Event handling. 1551da177e4SLinus Torvalds */ 1561da177e4SLinus Torvalds atomic_t event_nr; 1571da177e4SLinus Torvalds wait_queue_head_t eventq; 1587a8c3d3bSMike Anderson atomic_t uevent_seq; 1597a8c3d3bSMike Anderson struct list_head uevent_list; 1607a8c3d3bSMike Anderson spinlock_t uevent_lock; /* Protect access to uevent_list */ 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds /* 1631da177e4SLinus Torvalds * freeze/thaw support require holding onto a super block 1641da177e4SLinus Torvalds */ 1651da177e4SLinus Torvalds struct super_block *frozen_sb; 166e39e2e95SAlasdair G Kergon struct block_device *suspended_bdev; 1673ac51e74SDarrick J. Wong 1683ac51e74SDarrick J. Wong /* forced geometry settings */ 1693ac51e74SDarrick J. Wong struct hd_geometry geometry; 170784aae73SMilan Broz 171784aae73SMilan Broz /* sysfs handle */ 172784aae73SMilan Broz struct kobject kobj; 1731da177e4SLinus Torvalds }; 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds #define MIN_IOS 256 176e18b890bSChristoph Lameter static struct kmem_cache *_io_cache; 177e18b890bSChristoph Lameter static struct kmem_cache *_tio_cache; 1788fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache; 1798fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_bio_info_cache; 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds static int __init local_init(void) 1821da177e4SLinus Torvalds { 18351157b4aSKiyoshi Ueda int r = -ENOMEM; 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds /* allocate a slab for the dm_ios */ 186028867acSAlasdair G Kergon _io_cache = KMEM_CACHE(dm_io, 0); 1871da177e4SLinus Torvalds if (!_io_cache) 18851157b4aSKiyoshi Ueda return r; 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds /* allocate a slab for the target ios */ 191028867acSAlasdair G Kergon _tio_cache = KMEM_CACHE(dm_target_io, 0); 19251157b4aSKiyoshi Ueda if (!_tio_cache) 19351157b4aSKiyoshi Ueda goto out_free_io_cache; 1941da177e4SLinus Torvalds 1958fbf26adSKiyoshi Ueda _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0); 1968fbf26adSKiyoshi Ueda if (!_rq_tio_cache) 1978fbf26adSKiyoshi Ueda goto out_free_tio_cache; 1988fbf26adSKiyoshi Ueda 1998fbf26adSKiyoshi Ueda _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0); 2008fbf26adSKiyoshi Ueda if (!_rq_bio_info_cache) 2018fbf26adSKiyoshi Ueda goto out_free_rq_tio_cache; 2028fbf26adSKiyoshi Ueda 20351e5b2bdSMike Anderson r = dm_uevent_init(); 20451157b4aSKiyoshi Ueda if (r) 2058fbf26adSKiyoshi Ueda goto out_free_rq_bio_info_cache; 20651e5b2bdSMike Anderson 2071da177e4SLinus Torvalds _major = major; 2081da177e4SLinus Torvalds r = register_blkdev(_major, _name); 20951157b4aSKiyoshi Ueda if (r < 0) 21051157b4aSKiyoshi Ueda goto out_uevent_exit; 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds if (!_major) 2131da177e4SLinus Torvalds _major = r; 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds return 0; 21651157b4aSKiyoshi Ueda 21751157b4aSKiyoshi Ueda out_uevent_exit: 21851157b4aSKiyoshi Ueda dm_uevent_exit(); 2198fbf26adSKiyoshi Ueda out_free_rq_bio_info_cache: 2208fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_bio_info_cache); 2218fbf26adSKiyoshi Ueda out_free_rq_tio_cache: 2228fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_tio_cache); 22351157b4aSKiyoshi Ueda out_free_tio_cache: 22451157b4aSKiyoshi Ueda kmem_cache_destroy(_tio_cache); 22551157b4aSKiyoshi Ueda out_free_io_cache: 22651157b4aSKiyoshi Ueda kmem_cache_destroy(_io_cache); 22751157b4aSKiyoshi Ueda 22851157b4aSKiyoshi Ueda return r; 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds static void local_exit(void) 2321da177e4SLinus Torvalds { 2338fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_bio_info_cache); 2348fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_tio_cache); 2351da177e4SLinus Torvalds kmem_cache_destroy(_tio_cache); 2361da177e4SLinus Torvalds kmem_cache_destroy(_io_cache); 23700d59405SAkinobu Mita unregister_blkdev(_major, _name); 23851e5b2bdSMike Anderson dm_uevent_exit(); 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds _major = 0; 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds DMINFO("cleaned up"); 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds 245b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = { 2461da177e4SLinus Torvalds local_init, 2471da177e4SLinus Torvalds dm_target_init, 2481da177e4SLinus Torvalds dm_linear_init, 2491da177e4SLinus Torvalds dm_stripe_init, 250945fa4d2SMikulas Patocka dm_kcopyd_init, 2511da177e4SLinus Torvalds dm_interface_init, 2521da177e4SLinus Torvalds }; 2531da177e4SLinus Torvalds 254b9249e55SAlasdair G Kergon static void (*_exits[])(void) = { 2551da177e4SLinus Torvalds local_exit, 2561da177e4SLinus Torvalds dm_target_exit, 2571da177e4SLinus Torvalds dm_linear_exit, 2581da177e4SLinus Torvalds dm_stripe_exit, 259945fa4d2SMikulas Patocka dm_kcopyd_exit, 2601da177e4SLinus Torvalds dm_interface_exit, 2611da177e4SLinus Torvalds }; 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds static int __init dm_init(void) 2641da177e4SLinus Torvalds { 2651da177e4SLinus Torvalds const int count = ARRAY_SIZE(_inits); 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds int r, i; 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds for (i = 0; i < count; i++) { 2701da177e4SLinus Torvalds r = _inits[i](); 2711da177e4SLinus Torvalds if (r) 2721da177e4SLinus Torvalds goto bad; 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds return 0; 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds bad: 2781da177e4SLinus Torvalds while (i--) 2791da177e4SLinus Torvalds _exits[i](); 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds return r; 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds static void __exit dm_exit(void) 2851da177e4SLinus Torvalds { 2861da177e4SLinus Torvalds int i = ARRAY_SIZE(_exits); 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds while (i--) 2891da177e4SLinus Torvalds _exits[i](); 2901da177e4SLinus Torvalds } 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds /* 2931da177e4SLinus Torvalds * Block device functions 2941da177e4SLinus Torvalds */ 295fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode) 2961da177e4SLinus Torvalds { 2971da177e4SLinus Torvalds struct mapped_device *md; 2981da177e4SLinus Torvalds 299fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 300fba9f90eSJeff Mahoney 301fe5f9f2cSAl Viro md = bdev->bd_disk->private_data; 302fba9f90eSJeff Mahoney if (!md) 303fba9f90eSJeff Mahoney goto out; 304fba9f90eSJeff Mahoney 3055c6bd75dSAlasdair G Kergon if (test_bit(DMF_FREEING, &md->flags) || 3065c6bd75dSAlasdair G Kergon test_bit(DMF_DELETING, &md->flags)) { 307fba9f90eSJeff Mahoney md = NULL; 308fba9f90eSJeff Mahoney goto out; 309fba9f90eSJeff Mahoney } 310fba9f90eSJeff Mahoney 3111da177e4SLinus Torvalds dm_get(md); 3125c6bd75dSAlasdair G Kergon atomic_inc(&md->open_count); 313fba9f90eSJeff Mahoney 314fba9f90eSJeff Mahoney out: 315fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 316fba9f90eSJeff Mahoney 317fba9f90eSJeff Mahoney return md ? 0 : -ENXIO; 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds 320fe5f9f2cSAl Viro static int dm_blk_close(struct gendisk *disk, fmode_t mode) 3211da177e4SLinus Torvalds { 322fe5f9f2cSAl Viro struct mapped_device *md = disk->private_data; 3235c6bd75dSAlasdair G Kergon atomic_dec(&md->open_count); 3241da177e4SLinus Torvalds dm_put(md); 3251da177e4SLinus Torvalds return 0; 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds 3285c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md) 3295c6bd75dSAlasdair G Kergon { 3305c6bd75dSAlasdair G Kergon return atomic_read(&md->open_count); 3315c6bd75dSAlasdair G Kergon } 3325c6bd75dSAlasdair G Kergon 3335c6bd75dSAlasdair G Kergon /* 3345c6bd75dSAlasdair G Kergon * Guarantees nothing is using the device before it's deleted. 3355c6bd75dSAlasdair G Kergon */ 3365c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md) 3375c6bd75dSAlasdair G Kergon { 3385c6bd75dSAlasdair G Kergon int r = 0; 3395c6bd75dSAlasdair G Kergon 3405c6bd75dSAlasdair G Kergon spin_lock(&_minor_lock); 3415c6bd75dSAlasdair G Kergon 3425c6bd75dSAlasdair G Kergon if (dm_open_count(md)) 3435c6bd75dSAlasdair G Kergon r = -EBUSY; 3445c6bd75dSAlasdair G Kergon else 3455c6bd75dSAlasdair G Kergon set_bit(DMF_DELETING, &md->flags); 3465c6bd75dSAlasdair G Kergon 3475c6bd75dSAlasdair G Kergon spin_unlock(&_minor_lock); 3485c6bd75dSAlasdair G Kergon 3495c6bd75dSAlasdair G Kergon return r; 3505c6bd75dSAlasdair G Kergon } 3515c6bd75dSAlasdair G Kergon 3523ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 3533ac51e74SDarrick J. Wong { 3543ac51e74SDarrick J. Wong struct mapped_device *md = bdev->bd_disk->private_data; 3553ac51e74SDarrick J. Wong 3563ac51e74SDarrick J. Wong return dm_get_geometry(md, geo); 3573ac51e74SDarrick J. Wong } 3583ac51e74SDarrick J. Wong 359fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode, 360aa129a22SMilan Broz unsigned int cmd, unsigned long arg) 361aa129a22SMilan Broz { 362fe5f9f2cSAl Viro struct mapped_device *md = bdev->bd_disk->private_data; 363fe5f9f2cSAl Viro struct dm_table *map = dm_get_table(md); 364aa129a22SMilan Broz struct dm_target *tgt; 365aa129a22SMilan Broz int r = -ENOTTY; 366aa129a22SMilan Broz 367aa129a22SMilan Broz if (!map || !dm_table_get_size(map)) 368aa129a22SMilan Broz goto out; 369aa129a22SMilan Broz 370aa129a22SMilan Broz /* We only support devices that have a single target */ 371aa129a22SMilan Broz if (dm_table_get_num_targets(map) != 1) 372aa129a22SMilan Broz goto out; 373aa129a22SMilan Broz 374aa129a22SMilan Broz tgt = dm_table_get_target(map, 0); 375aa129a22SMilan Broz 376aa129a22SMilan Broz if (dm_suspended(md)) { 377aa129a22SMilan Broz r = -EAGAIN; 378aa129a22SMilan Broz goto out; 379aa129a22SMilan Broz } 380aa129a22SMilan Broz 381aa129a22SMilan Broz if (tgt->type->ioctl) 382647b3d00SAl Viro r = tgt->type->ioctl(tgt, cmd, arg); 383aa129a22SMilan Broz 384aa129a22SMilan Broz out: 385aa129a22SMilan Broz dm_table_put(map); 386aa129a22SMilan Broz 387aa129a22SMilan Broz return r; 388aa129a22SMilan Broz } 389aa129a22SMilan Broz 390028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md) 3911da177e4SLinus Torvalds { 3921da177e4SLinus Torvalds return mempool_alloc(md->io_pool, GFP_NOIO); 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds 395028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io) 3961da177e4SLinus Torvalds { 3971da177e4SLinus Torvalds mempool_free(io, md->io_pool); 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds 400028867acSAlasdair G Kergon static struct dm_target_io *alloc_tio(struct mapped_device *md) 4011da177e4SLinus Torvalds { 4021da177e4SLinus Torvalds return mempool_alloc(md->tio_pool, GFP_NOIO); 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds 405028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio) 4061da177e4SLinus Torvalds { 4071da177e4SLinus Torvalds mempool_free(tio, md->tio_pool); 4081da177e4SLinus Torvalds } 4091da177e4SLinus Torvalds 4103eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io) 4113eaf840eSJun'ichi "Nick" Nomura { 4123eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 413c9959059STejun Heo int cpu; 4143eaf840eSJun'ichi "Nick" Nomura 4153eaf840eSJun'ichi "Nick" Nomura io->start_time = jiffies; 4163eaf840eSJun'ichi "Nick" Nomura 417074a7acaSTejun Heo cpu = part_stat_lock(); 418074a7acaSTejun Heo part_round_stats(cpu, &dm_disk(md)->part0); 419074a7acaSTejun Heo part_stat_unlock(); 420074a7acaSTejun Heo dm_disk(md)->part0.in_flight = atomic_inc_return(&md->pending); 4213eaf840eSJun'ichi "Nick" Nomura } 4223eaf840eSJun'ichi "Nick" Nomura 423d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io) 4243eaf840eSJun'ichi "Nick" Nomura { 4253eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 4263eaf840eSJun'ichi "Nick" Nomura struct bio *bio = io->bio; 4273eaf840eSJun'ichi "Nick" Nomura unsigned long duration = jiffies - io->start_time; 428c9959059STejun Heo int pending, cpu; 4293eaf840eSJun'ichi "Nick" Nomura int rw = bio_data_dir(bio); 4303eaf840eSJun'ichi "Nick" Nomura 431074a7acaSTejun Heo cpu = part_stat_lock(); 432074a7acaSTejun Heo part_round_stats(cpu, &dm_disk(md)->part0); 433074a7acaSTejun Heo part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration); 434074a7acaSTejun Heo part_stat_unlock(); 4353eaf840eSJun'ichi "Nick" Nomura 436074a7acaSTejun Heo dm_disk(md)->part0.in_flight = pending = 437074a7acaSTejun Heo atomic_dec_return(&md->pending); 4383eaf840eSJun'ichi "Nick" Nomura 439d221d2e7SMikulas Patocka /* nudge anyone waiting on suspend queue */ 440d221d2e7SMikulas Patocka if (!pending) 441d221d2e7SMikulas Patocka wake_up(&md->wait); 4423eaf840eSJun'ichi "Nick" Nomura } 4433eaf840eSJun'ichi "Nick" Nomura 4441da177e4SLinus Torvalds /* 4451da177e4SLinus Torvalds * Add the bio to the list of deferred io. 4461da177e4SLinus Torvalds */ 4471da177e4SLinus Torvalds static int queue_io(struct mapped_device *md, struct bio *bio) 4481da177e4SLinus Torvalds { 4492ca3310eSAlasdair G Kergon down_write(&md->io_lock); 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds if (!test_bit(DMF_BLOCK_IO, &md->flags)) { 4522ca3310eSAlasdair G Kergon up_write(&md->io_lock); 4531da177e4SLinus Torvalds return 1; 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds bio_list_add(&md->deferred, bio); 4571da177e4SLinus Torvalds 4582ca3310eSAlasdair G Kergon up_write(&md->io_lock); 4591da177e4SLinus Torvalds return 0; /* deferred successfully */ 4601da177e4SLinus Torvalds } 4611da177e4SLinus Torvalds 4621da177e4SLinus Torvalds /* 4631da177e4SLinus Torvalds * Everyone (including functions in this file), should use this 4641da177e4SLinus Torvalds * function to access the md->map field, and make sure they call 4651da177e4SLinus Torvalds * dm_table_put() when finished. 4661da177e4SLinus Torvalds */ 4671da177e4SLinus Torvalds struct dm_table *dm_get_table(struct mapped_device *md) 4681da177e4SLinus Torvalds { 4691da177e4SLinus Torvalds struct dm_table *t; 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds read_lock(&md->map_lock); 4721da177e4SLinus Torvalds t = md->map; 4731da177e4SLinus Torvalds if (t) 4741da177e4SLinus Torvalds dm_table_get(t); 4751da177e4SLinus Torvalds read_unlock(&md->map_lock); 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds return t; 4781da177e4SLinus Torvalds } 4791da177e4SLinus Torvalds 4803ac51e74SDarrick J. Wong /* 4813ac51e74SDarrick J. Wong * Get the geometry associated with a dm device 4823ac51e74SDarrick J. Wong */ 4833ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) 4843ac51e74SDarrick J. Wong { 4853ac51e74SDarrick J. Wong *geo = md->geometry; 4863ac51e74SDarrick J. Wong 4873ac51e74SDarrick J. Wong return 0; 4883ac51e74SDarrick J. Wong } 4893ac51e74SDarrick J. Wong 4903ac51e74SDarrick J. Wong /* 4913ac51e74SDarrick J. Wong * Set the geometry of a device. 4923ac51e74SDarrick J. Wong */ 4933ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) 4943ac51e74SDarrick J. Wong { 4953ac51e74SDarrick J. Wong sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; 4963ac51e74SDarrick J. Wong 4973ac51e74SDarrick J. Wong if (geo->start > sz) { 4983ac51e74SDarrick J. Wong DMWARN("Start sector is beyond the geometry limits."); 4993ac51e74SDarrick J. Wong return -EINVAL; 5003ac51e74SDarrick J. Wong } 5013ac51e74SDarrick J. Wong 5023ac51e74SDarrick J. Wong md->geometry = *geo; 5033ac51e74SDarrick J. Wong 5043ac51e74SDarrick J. Wong return 0; 5053ac51e74SDarrick J. Wong } 5063ac51e74SDarrick J. Wong 5071da177e4SLinus Torvalds /*----------------------------------------------------------------- 5081da177e4SLinus Torvalds * CRUD START: 5091da177e4SLinus Torvalds * A more elegant soln is in the works that uses the queue 5101da177e4SLinus Torvalds * merge fn, unfortunately there are a couple of changes to 5111da177e4SLinus Torvalds * the block layer that I want to make for this. So in the 5121da177e4SLinus Torvalds * interests of getting something for people to use I give 5131da177e4SLinus Torvalds * you this clearly demarcated crap. 5141da177e4SLinus Torvalds *---------------------------------------------------------------*/ 5151da177e4SLinus Torvalds 5162e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md) 5172e93ccc1SKiyoshi Ueda { 5182e93ccc1SKiyoshi Ueda return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 5192e93ccc1SKiyoshi Ueda } 5202e93ccc1SKiyoshi Ueda 5211da177e4SLinus Torvalds /* 5221da177e4SLinus Torvalds * Decrements the number of outstanding ios that a bio has been 5231da177e4SLinus Torvalds * cloned into, completing the original io if necc. 5241da177e4SLinus Torvalds */ 525858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error) 5261da177e4SLinus Torvalds { 5272e93ccc1SKiyoshi Ueda unsigned long flags; 528*b35f8caaSMilan Broz int io_error; 529*b35f8caaSMilan Broz struct bio *bio; 530*b35f8caaSMilan Broz struct mapped_device *md = io->md; 5312e93ccc1SKiyoshi Ueda 5322e93ccc1SKiyoshi Ueda /* Push-back supersedes any I/O errors */ 533*b35f8caaSMilan Broz if (error && !(io->error > 0 && __noflush_suspending(md))) 5341da177e4SLinus Torvalds io->error = error; 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds if (atomic_dec_and_test(&io->io_count)) { 5372e93ccc1SKiyoshi Ueda if (io->error == DM_ENDIO_REQUEUE) { 5382e93ccc1SKiyoshi Ueda /* 5392e93ccc1SKiyoshi Ueda * Target requested pushing back the I/O. 5402e93ccc1SKiyoshi Ueda * This must be handled before the sleeper on 5412e93ccc1SKiyoshi Ueda * suspend queue merges the pushback list. 5422e93ccc1SKiyoshi Ueda */ 543*b35f8caaSMilan Broz spin_lock_irqsave(&md->pushback_lock, flags); 544*b35f8caaSMilan Broz if (__noflush_suspending(md)) 545*b35f8caaSMilan Broz bio_list_add(&md->pushback, io->bio); 5462e93ccc1SKiyoshi Ueda else 5472e93ccc1SKiyoshi Ueda /* noflush suspend was interrupted. */ 5482e93ccc1SKiyoshi Ueda io->error = -EIO; 549*b35f8caaSMilan Broz spin_unlock_irqrestore(&md->pushback_lock, flags); 5502e93ccc1SKiyoshi Ueda } 5512e93ccc1SKiyoshi Ueda 552d221d2e7SMikulas Patocka end_io_acct(io); 5531da177e4SLinus Torvalds 554*b35f8caaSMilan Broz io_error = io->error; 555*b35f8caaSMilan Broz bio = io->bio; 5562056a782SJens Axboe 557*b35f8caaSMilan Broz free_io(md, io); 558*b35f8caaSMilan Broz 559*b35f8caaSMilan Broz if (io_error != DM_ENDIO_REQUEUE) { 560*b35f8caaSMilan Broz trace_block_bio_complete(md->queue, bio); 561*b35f8caaSMilan Broz 562*b35f8caaSMilan Broz bio_endio(bio, io_error); 5632e93ccc1SKiyoshi Ueda } 5641da177e4SLinus Torvalds } 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds 5676712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error) 5681da177e4SLinus Torvalds { 5691da177e4SLinus Torvalds int r = 0; 570028867acSAlasdair G Kergon struct dm_target_io *tio = bio->bi_private; 571*b35f8caaSMilan Broz struct dm_io *io = tio->io; 5729faf400fSStefan Bader struct mapped_device *md = tio->io->md; 5731da177e4SLinus Torvalds dm_endio_fn endio = tio->ti->type->end_io; 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds if (!bio_flagged(bio, BIO_UPTODATE) && !error) 5761da177e4SLinus Torvalds error = -EIO; 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds if (endio) { 5791da177e4SLinus Torvalds r = endio(tio->ti, bio, error, &tio->info); 5802e93ccc1SKiyoshi Ueda if (r < 0 || r == DM_ENDIO_REQUEUE) 5812e93ccc1SKiyoshi Ueda /* 5822e93ccc1SKiyoshi Ueda * error and requeue request are handled 5832e93ccc1SKiyoshi Ueda * in dec_pending(). 5842e93ccc1SKiyoshi Ueda */ 5851da177e4SLinus Torvalds error = r; 58645cbcd79SKiyoshi Ueda else if (r == DM_ENDIO_INCOMPLETE) 58745cbcd79SKiyoshi Ueda /* The target will handle the io */ 5886712ecf8SNeilBrown return; 58945cbcd79SKiyoshi Ueda else if (r) { 59045cbcd79SKiyoshi Ueda DMWARN("unimplemented target endio return value: %d", r); 59145cbcd79SKiyoshi Ueda BUG(); 59245cbcd79SKiyoshi Ueda } 5931da177e4SLinus Torvalds } 5941da177e4SLinus Torvalds 5959faf400fSStefan Bader /* 5969faf400fSStefan Bader * Store md for cleanup instead of tio which is about to get freed. 5979faf400fSStefan Bader */ 5989faf400fSStefan Bader bio->bi_private = md->bs; 5999faf400fSStefan Bader 6009faf400fSStefan Bader free_tio(md, tio); 601*b35f8caaSMilan Broz bio_put(bio); 602*b35f8caaSMilan Broz dec_pending(io, error); 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds static sector_t max_io_len(struct mapped_device *md, 6061da177e4SLinus Torvalds sector_t sector, struct dm_target *ti) 6071da177e4SLinus Torvalds { 6081da177e4SLinus Torvalds sector_t offset = sector - ti->begin; 6091da177e4SLinus Torvalds sector_t len = ti->len - offset; 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds /* 6121da177e4SLinus Torvalds * Does the target need to split even further ? 6131da177e4SLinus Torvalds */ 6141da177e4SLinus Torvalds if (ti->split_io) { 6151da177e4SLinus Torvalds sector_t boundary; 6161da177e4SLinus Torvalds boundary = ((offset + ti->split_io) & ~(ti->split_io - 1)) 6171da177e4SLinus Torvalds - offset; 6181da177e4SLinus Torvalds if (len > boundary) 6191da177e4SLinus Torvalds len = boundary; 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds return len; 6231da177e4SLinus Torvalds } 6241da177e4SLinus Torvalds 6251da177e4SLinus Torvalds static void __map_bio(struct dm_target *ti, struct bio *clone, 626028867acSAlasdair G Kergon struct dm_target_io *tio) 6271da177e4SLinus Torvalds { 6281da177e4SLinus Torvalds int r; 6292056a782SJens Axboe sector_t sector; 6309faf400fSStefan Bader struct mapped_device *md; 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds /* 6331da177e4SLinus Torvalds * Sanity checks. 6341da177e4SLinus Torvalds */ 6351da177e4SLinus Torvalds BUG_ON(!clone->bi_size); 6361da177e4SLinus Torvalds 6371da177e4SLinus Torvalds clone->bi_end_io = clone_endio; 6381da177e4SLinus Torvalds clone->bi_private = tio; 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds /* 6411da177e4SLinus Torvalds * Map the clone. If r == 0 we don't need to do 6421da177e4SLinus Torvalds * anything, the target has assumed ownership of 6431da177e4SLinus Torvalds * this io. 6441da177e4SLinus Torvalds */ 6451da177e4SLinus Torvalds atomic_inc(&tio->io->io_count); 6462056a782SJens Axboe sector = clone->bi_sector; 6471da177e4SLinus Torvalds r = ti->type->map(ti, clone, &tio->info); 64845cbcd79SKiyoshi Ueda if (r == DM_MAPIO_REMAPPED) { 6491da177e4SLinus Torvalds /* the bio has been remapped so dispatch it */ 6502056a782SJens Axboe 6515f3ea37cSArnaldo Carvalho de Melo trace_block_remap(bdev_get_queue(clone->bi_bdev), clone, 652c7149d6bSAlan D. Brunelle tio->io->bio->bi_bdev->bd_dev, 653c7149d6bSAlan D. Brunelle clone->bi_sector, sector); 6542056a782SJens Axboe 6551da177e4SLinus Torvalds generic_make_request(clone); 6562e93ccc1SKiyoshi Ueda } else if (r < 0 || r == DM_MAPIO_REQUEUE) { 6572e93ccc1SKiyoshi Ueda /* error the io and bail out, or requeue it if needed */ 6589faf400fSStefan Bader md = tio->io->md; 6599faf400fSStefan Bader dec_pending(tio->io, r); 6609faf400fSStefan Bader /* 6619faf400fSStefan Bader * Store bio_set for cleanup. 6629faf400fSStefan Bader */ 6639faf400fSStefan Bader clone->bi_private = md->bs; 6641da177e4SLinus Torvalds bio_put(clone); 6659faf400fSStefan Bader free_tio(md, tio); 66645cbcd79SKiyoshi Ueda } else if (r) { 66745cbcd79SKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 66845cbcd79SKiyoshi Ueda BUG(); 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds } 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds struct clone_info { 6731da177e4SLinus Torvalds struct mapped_device *md; 6741da177e4SLinus Torvalds struct dm_table *map; 6751da177e4SLinus Torvalds struct bio *bio; 6761da177e4SLinus Torvalds struct dm_io *io; 6771da177e4SLinus Torvalds sector_t sector; 6781da177e4SLinus Torvalds sector_t sector_count; 6791da177e4SLinus Torvalds unsigned short idx; 6801da177e4SLinus Torvalds }; 6811da177e4SLinus Torvalds 6823676347aSPeter Osterlund static void dm_bio_destructor(struct bio *bio) 6833676347aSPeter Osterlund { 6849faf400fSStefan Bader struct bio_set *bs = bio->bi_private; 6859faf400fSStefan Bader 6869faf400fSStefan Bader bio_free(bio, bs); 6873676347aSPeter Osterlund } 6883676347aSPeter Osterlund 6891da177e4SLinus Torvalds /* 6901da177e4SLinus Torvalds * Creates a little bio that is just does part of a bvec. 6911da177e4SLinus Torvalds */ 6921da177e4SLinus Torvalds static struct bio *split_bvec(struct bio *bio, sector_t sector, 6931da177e4SLinus Torvalds unsigned short idx, unsigned int offset, 6949faf400fSStefan Bader unsigned int len, struct bio_set *bs) 6951da177e4SLinus Torvalds { 6961da177e4SLinus Torvalds struct bio *clone; 6971da177e4SLinus Torvalds struct bio_vec *bv = bio->bi_io_vec + idx; 6981da177e4SLinus Torvalds 6999faf400fSStefan Bader clone = bio_alloc_bioset(GFP_NOIO, 1, bs); 7003676347aSPeter Osterlund clone->bi_destructor = dm_bio_destructor; 7011da177e4SLinus Torvalds *clone->bi_io_vec = *bv; 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds clone->bi_sector = sector; 7041da177e4SLinus Torvalds clone->bi_bdev = bio->bi_bdev; 7051da177e4SLinus Torvalds clone->bi_rw = bio->bi_rw; 7061da177e4SLinus Torvalds clone->bi_vcnt = 1; 7071da177e4SLinus Torvalds clone->bi_size = to_bytes(len); 7081da177e4SLinus Torvalds clone->bi_io_vec->bv_offset = offset; 7091da177e4SLinus Torvalds clone->bi_io_vec->bv_len = clone->bi_size; 710f3e1d26eSMartin K. Petersen clone->bi_flags |= 1 << BIO_CLONED; 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds return clone; 7131da177e4SLinus Torvalds } 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds /* 7161da177e4SLinus Torvalds * Creates a bio that consists of range of complete bvecs. 7171da177e4SLinus Torvalds */ 7181da177e4SLinus Torvalds static struct bio *clone_bio(struct bio *bio, sector_t sector, 7191da177e4SLinus Torvalds unsigned short idx, unsigned short bv_count, 7209faf400fSStefan Bader unsigned int len, struct bio_set *bs) 7211da177e4SLinus Torvalds { 7221da177e4SLinus Torvalds struct bio *clone; 7231da177e4SLinus Torvalds 7249faf400fSStefan Bader clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs); 7259faf400fSStefan Bader __bio_clone(clone, bio); 7269faf400fSStefan Bader clone->bi_destructor = dm_bio_destructor; 7271da177e4SLinus Torvalds clone->bi_sector = sector; 7281da177e4SLinus Torvalds clone->bi_idx = idx; 7291da177e4SLinus Torvalds clone->bi_vcnt = idx + bv_count; 7301da177e4SLinus Torvalds clone->bi_size = to_bytes(len); 7311da177e4SLinus Torvalds clone->bi_flags &= ~(1 << BIO_SEG_VALID); 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds return clone; 7341da177e4SLinus Torvalds } 7351da177e4SLinus Torvalds 736512875bdSJun'ichi Nomura static int __clone_and_map(struct clone_info *ci) 7371da177e4SLinus Torvalds { 7381da177e4SLinus Torvalds struct bio *clone, *bio = ci->bio; 739512875bdSJun'ichi Nomura struct dm_target *ti; 740512875bdSJun'ichi Nomura sector_t len = 0, max; 741028867acSAlasdair G Kergon struct dm_target_io *tio; 7421da177e4SLinus Torvalds 743512875bdSJun'ichi Nomura ti = dm_table_find_target(ci->map, ci->sector); 744512875bdSJun'ichi Nomura if (!dm_target_is_valid(ti)) 745512875bdSJun'ichi Nomura return -EIO; 746512875bdSJun'ichi Nomura 747512875bdSJun'ichi Nomura max = max_io_len(ci->md, ci->sector, ti); 748512875bdSJun'ichi Nomura 7491da177e4SLinus Torvalds /* 7501da177e4SLinus Torvalds * Allocate a target io object. 7511da177e4SLinus Torvalds */ 7521da177e4SLinus Torvalds tio = alloc_tio(ci->md); 7531da177e4SLinus Torvalds tio->io = ci->io; 7541da177e4SLinus Torvalds tio->ti = ti; 7551da177e4SLinus Torvalds memset(&tio->info, 0, sizeof(tio->info)); 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds if (ci->sector_count <= max) { 7581da177e4SLinus Torvalds /* 7591da177e4SLinus Torvalds * Optimise for the simple case where we can do all of 7601da177e4SLinus Torvalds * the remaining io with a single clone. 7611da177e4SLinus Torvalds */ 7621da177e4SLinus Torvalds clone = clone_bio(bio, ci->sector, ci->idx, 7639faf400fSStefan Bader bio->bi_vcnt - ci->idx, ci->sector_count, 7649faf400fSStefan Bader ci->md->bs); 7651da177e4SLinus Torvalds __map_bio(ti, clone, tio); 7661da177e4SLinus Torvalds ci->sector_count = 0; 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) { 7691da177e4SLinus Torvalds /* 7701da177e4SLinus Torvalds * There are some bvecs that don't span targets. 7711da177e4SLinus Torvalds * Do as many of these as possible. 7721da177e4SLinus Torvalds */ 7731da177e4SLinus Torvalds int i; 7741da177e4SLinus Torvalds sector_t remaining = max; 7751da177e4SLinus Torvalds sector_t bv_len; 7761da177e4SLinus Torvalds 7771da177e4SLinus Torvalds for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) { 7781da177e4SLinus Torvalds bv_len = to_sector(bio->bi_io_vec[i].bv_len); 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds if (bv_len > remaining) 7811da177e4SLinus Torvalds break; 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds remaining -= bv_len; 7841da177e4SLinus Torvalds len += bv_len; 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds 7879faf400fSStefan Bader clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len, 7889faf400fSStefan Bader ci->md->bs); 7891da177e4SLinus Torvalds __map_bio(ti, clone, tio); 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds ci->sector += len; 7921da177e4SLinus Torvalds ci->sector_count -= len; 7931da177e4SLinus Torvalds ci->idx = i; 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds } else { 7961da177e4SLinus Torvalds /* 797d2044a94SAlasdair G Kergon * Handle a bvec that must be split between two or more targets. 7981da177e4SLinus Torvalds */ 7991da177e4SLinus Torvalds struct bio_vec *bv = bio->bi_io_vec + ci->idx; 800d2044a94SAlasdair G Kergon sector_t remaining = to_sector(bv->bv_len); 801d2044a94SAlasdair G Kergon unsigned int offset = 0; 8021da177e4SLinus Torvalds 803d2044a94SAlasdair G Kergon do { 804d2044a94SAlasdair G Kergon if (offset) { 8051da177e4SLinus Torvalds ti = dm_table_find_target(ci->map, ci->sector); 806512875bdSJun'ichi Nomura if (!dm_target_is_valid(ti)) 807512875bdSJun'ichi Nomura return -EIO; 808512875bdSJun'ichi Nomura 809d2044a94SAlasdair G Kergon max = max_io_len(ci->md, ci->sector, ti); 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds tio = alloc_tio(ci->md); 8121da177e4SLinus Torvalds tio->io = ci->io; 8131da177e4SLinus Torvalds tio->ti = ti; 8141da177e4SLinus Torvalds memset(&tio->info, 0, sizeof(tio->info)); 815d2044a94SAlasdair G Kergon } 816d2044a94SAlasdair G Kergon 817d2044a94SAlasdair G Kergon len = min(remaining, max); 818d2044a94SAlasdair G Kergon 819d2044a94SAlasdair G Kergon clone = split_bvec(bio, ci->sector, ci->idx, 8209faf400fSStefan Bader bv->bv_offset + offset, len, 8219faf400fSStefan Bader ci->md->bs); 822d2044a94SAlasdair G Kergon 8231da177e4SLinus Torvalds __map_bio(ti, clone, tio); 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds ci->sector += len; 8261da177e4SLinus Torvalds ci->sector_count -= len; 827d2044a94SAlasdair G Kergon offset += to_bytes(len); 828d2044a94SAlasdair G Kergon } while (remaining -= len); 829d2044a94SAlasdair G Kergon 8301da177e4SLinus Torvalds ci->idx++; 8311da177e4SLinus Torvalds } 832512875bdSJun'ichi Nomura 833512875bdSJun'ichi Nomura return 0; 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds /* 8371da177e4SLinus Torvalds * Split the bio into several clones. 8381da177e4SLinus Torvalds */ 8399e4e5f87SMilan Broz static int __split_bio(struct mapped_device *md, struct bio *bio) 8401da177e4SLinus Torvalds { 8411da177e4SLinus Torvalds struct clone_info ci; 842512875bdSJun'ichi Nomura int error = 0; 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds ci.map = dm_get_table(md); 8459e4e5f87SMilan Broz if (unlikely(!ci.map)) 8469e4e5f87SMilan Broz return -EIO; 847ab4c1424SAndi Kleen if (unlikely(bio_barrier(bio) && !dm_table_barrier_ok(ci.map))) { 848ab4c1424SAndi Kleen dm_table_put(ci.map); 849ab4c1424SAndi Kleen bio_endio(bio, -EOPNOTSUPP); 850ab4c1424SAndi Kleen return 0; 851ab4c1424SAndi Kleen } 8521da177e4SLinus Torvalds ci.md = md; 8531da177e4SLinus Torvalds ci.bio = bio; 8541da177e4SLinus Torvalds ci.io = alloc_io(md); 8551da177e4SLinus Torvalds ci.io->error = 0; 8561da177e4SLinus Torvalds atomic_set(&ci.io->io_count, 1); 8571da177e4SLinus Torvalds ci.io->bio = bio; 8581da177e4SLinus Torvalds ci.io->md = md; 8591da177e4SLinus Torvalds ci.sector = bio->bi_sector; 8601da177e4SLinus Torvalds ci.sector_count = bio_sectors(bio); 8611da177e4SLinus Torvalds ci.idx = bio->bi_idx; 8621da177e4SLinus Torvalds 8633eaf840eSJun'ichi "Nick" Nomura start_io_acct(ci.io); 864512875bdSJun'ichi Nomura while (ci.sector_count && !error) 865512875bdSJun'ichi Nomura error = __clone_and_map(&ci); 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds /* drop the extra reference count */ 868512875bdSJun'ichi Nomura dec_pending(ci.io, error); 8691da177e4SLinus Torvalds dm_table_put(ci.map); 8709e4e5f87SMilan Broz 8719e4e5f87SMilan Broz return 0; 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds /*----------------------------------------------------------------- 8741da177e4SLinus Torvalds * CRUD END 8751da177e4SLinus Torvalds *---------------------------------------------------------------*/ 8761da177e4SLinus Torvalds 877f6fccb12SMilan Broz static int dm_merge_bvec(struct request_queue *q, 878f6fccb12SMilan Broz struct bvec_merge_data *bvm, 879f6fccb12SMilan Broz struct bio_vec *biovec) 880f6fccb12SMilan Broz { 881f6fccb12SMilan Broz struct mapped_device *md = q->queuedata; 882f6fccb12SMilan Broz struct dm_table *map = dm_get_table(md); 883f6fccb12SMilan Broz struct dm_target *ti; 884f6fccb12SMilan Broz sector_t max_sectors; 8855037108aSMikulas Patocka int max_size = 0; 886f6fccb12SMilan Broz 887f6fccb12SMilan Broz if (unlikely(!map)) 8885037108aSMikulas Patocka goto out; 889f6fccb12SMilan Broz 890f6fccb12SMilan Broz ti = dm_table_find_target(map, bvm->bi_sector); 891b01cd5acSMikulas Patocka if (!dm_target_is_valid(ti)) 892b01cd5acSMikulas Patocka goto out_table; 893f6fccb12SMilan Broz 894f6fccb12SMilan Broz /* 895f6fccb12SMilan Broz * Find maximum amount of I/O that won't need splitting 896f6fccb12SMilan Broz */ 897f6fccb12SMilan Broz max_sectors = min(max_io_len(md, bvm->bi_sector, ti), 898f6fccb12SMilan Broz (sector_t) BIO_MAX_SECTORS); 899f6fccb12SMilan Broz max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size; 900f6fccb12SMilan Broz if (max_size < 0) 901f6fccb12SMilan Broz max_size = 0; 902f6fccb12SMilan Broz 903f6fccb12SMilan Broz /* 904f6fccb12SMilan Broz * merge_bvec_fn() returns number of bytes 905f6fccb12SMilan Broz * it can accept at this offset 906f6fccb12SMilan Broz * max is precomputed maximal io size 907f6fccb12SMilan Broz */ 908f6fccb12SMilan Broz if (max_size && ti->type->merge) 909f6fccb12SMilan Broz max_size = ti->type->merge(ti, bvm, biovec, max_size); 910f6fccb12SMilan Broz 911b01cd5acSMikulas Patocka out_table: 9125037108aSMikulas Patocka dm_table_put(map); 9135037108aSMikulas Patocka 9145037108aSMikulas Patocka out: 915f6fccb12SMilan Broz /* 916f6fccb12SMilan Broz * Always allow an entire first page 917f6fccb12SMilan Broz */ 918f6fccb12SMilan Broz if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT)) 919f6fccb12SMilan Broz max_size = biovec->bv_len; 920f6fccb12SMilan Broz 921f6fccb12SMilan Broz return max_size; 922f6fccb12SMilan Broz } 923f6fccb12SMilan Broz 9241da177e4SLinus Torvalds /* 9251da177e4SLinus Torvalds * The request function that just remaps the bio built up by 9261da177e4SLinus Torvalds * dm_merge_bvec. 9271da177e4SLinus Torvalds */ 928165125e1SJens Axboe static int dm_request(struct request_queue *q, struct bio *bio) 9291da177e4SLinus Torvalds { 9309e4e5f87SMilan Broz int r = -EIO; 93112f03a49SKevin Corry int rw = bio_data_dir(bio); 9321da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 933c9959059STejun Heo int cpu; 9341da177e4SLinus Torvalds 9352ca3310eSAlasdair G Kergon down_read(&md->io_lock); 9361da177e4SLinus Torvalds 937074a7acaSTejun Heo cpu = part_stat_lock(); 938074a7acaSTejun Heo part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]); 939074a7acaSTejun Heo part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio)); 940074a7acaSTejun Heo part_stat_unlock(); 94112f03a49SKevin Corry 9421da177e4SLinus Torvalds /* 9431da177e4SLinus Torvalds * If we're suspended we have to queue 9441da177e4SLinus Torvalds * this io for later. 9451da177e4SLinus Torvalds */ 9461da177e4SLinus Torvalds while (test_bit(DMF_BLOCK_IO, &md->flags)) { 9472ca3310eSAlasdair G Kergon up_read(&md->io_lock); 9481da177e4SLinus Torvalds 9499e4e5f87SMilan Broz if (bio_rw(bio) != READA) 9501da177e4SLinus Torvalds r = queue_io(md, bio); 9511da177e4SLinus Torvalds 9529e4e5f87SMilan Broz if (r <= 0) 9539e4e5f87SMilan Broz goto out_req; 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds /* 9561da177e4SLinus Torvalds * We're in a while loop, because someone could suspend 9571da177e4SLinus Torvalds * before we get to the following read lock. 9581da177e4SLinus Torvalds */ 9592ca3310eSAlasdair G Kergon down_read(&md->io_lock); 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds 9629e4e5f87SMilan Broz r = __split_bio(md, bio); 9632ca3310eSAlasdair G Kergon up_read(&md->io_lock); 9649e4e5f87SMilan Broz 9659e4e5f87SMilan Broz out_req: 9669e4e5f87SMilan Broz if (r < 0) 9679e4e5f87SMilan Broz bio_io_error(bio); 9689e4e5f87SMilan Broz 9691da177e4SLinus Torvalds return 0; 9701da177e4SLinus Torvalds } 9711da177e4SLinus Torvalds 972165125e1SJens Axboe static void dm_unplug_all(struct request_queue *q) 9731da177e4SLinus Torvalds { 9741da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 9751da177e4SLinus Torvalds struct dm_table *map = dm_get_table(md); 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds if (map) { 9781da177e4SLinus Torvalds dm_table_unplug_all(map); 9791da177e4SLinus Torvalds dm_table_put(map); 9801da177e4SLinus Torvalds } 9811da177e4SLinus Torvalds } 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits) 9841da177e4SLinus Torvalds { 9858a57dfc6SChandra Seetharaman int r = bdi_bits; 9868a57dfc6SChandra Seetharaman struct mapped_device *md = congested_data; 9878a57dfc6SChandra Seetharaman struct dm_table *map; 9881da177e4SLinus Torvalds 9898a57dfc6SChandra Seetharaman if (!test_bit(DMF_BLOCK_IO, &md->flags)) { 9908a57dfc6SChandra Seetharaman map = dm_get_table(md); 9918a57dfc6SChandra Seetharaman if (map) { 9921da177e4SLinus Torvalds r = dm_table_any_congested(map, bdi_bits); 9931da177e4SLinus Torvalds dm_table_put(map); 9948a57dfc6SChandra Seetharaman } 9958a57dfc6SChandra Seetharaman } 9968a57dfc6SChandra Seetharaman 9971da177e4SLinus Torvalds return r; 9981da177e4SLinus Torvalds } 9991da177e4SLinus Torvalds 10001da177e4SLinus Torvalds /*----------------------------------------------------------------- 10011da177e4SLinus Torvalds * An IDR is used to keep track of allocated minor numbers. 10021da177e4SLinus Torvalds *---------------------------------------------------------------*/ 10031da177e4SLinus Torvalds static DEFINE_IDR(_minor_idr); 10041da177e4SLinus Torvalds 10052b06cfffSAlasdair G Kergon static void free_minor(int minor) 10061da177e4SLinus Torvalds { 1007f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 10081da177e4SLinus Torvalds idr_remove(&_minor_idr, minor); 1009f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 10101da177e4SLinus Torvalds } 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds /* 10131da177e4SLinus Torvalds * See if the device with a specific minor # is free. 10141da177e4SLinus Torvalds */ 1015cf13ab8eSFrederik Deweerdt static int specific_minor(int minor) 10161da177e4SLinus Torvalds { 10171da177e4SLinus Torvalds int r, m; 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds if (minor >= (1 << MINORBITS)) 10201da177e4SLinus Torvalds return -EINVAL; 10211da177e4SLinus Torvalds 102262f75c2fSJeff Mahoney r = idr_pre_get(&_minor_idr, GFP_KERNEL); 102362f75c2fSJeff Mahoney if (!r) 102462f75c2fSJeff Mahoney return -ENOMEM; 102562f75c2fSJeff Mahoney 1026f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds if (idr_find(&_minor_idr, minor)) { 10291da177e4SLinus Torvalds r = -EBUSY; 10301da177e4SLinus Torvalds goto out; 10311da177e4SLinus Torvalds } 10321da177e4SLinus Torvalds 1033ba61fdd1SJeff Mahoney r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m); 103462f75c2fSJeff Mahoney if (r) 10351da177e4SLinus Torvalds goto out; 10361da177e4SLinus Torvalds 10371da177e4SLinus Torvalds if (m != minor) { 10381da177e4SLinus Torvalds idr_remove(&_minor_idr, m); 10391da177e4SLinus Torvalds r = -EBUSY; 10401da177e4SLinus Torvalds goto out; 10411da177e4SLinus Torvalds } 10421da177e4SLinus Torvalds 10431da177e4SLinus Torvalds out: 1044f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 10451da177e4SLinus Torvalds return r; 10461da177e4SLinus Torvalds } 10471da177e4SLinus Torvalds 1048cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor) 10491da177e4SLinus Torvalds { 10502b06cfffSAlasdair G Kergon int r, m; 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds r = idr_pre_get(&_minor_idr, GFP_KERNEL); 105362f75c2fSJeff Mahoney if (!r) 105462f75c2fSJeff Mahoney return -ENOMEM; 105562f75c2fSJeff Mahoney 1056f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 10571da177e4SLinus Torvalds 1058ba61fdd1SJeff Mahoney r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m); 1059cf13ab8eSFrederik Deweerdt if (r) 10601da177e4SLinus Torvalds goto out; 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds if (m >= (1 << MINORBITS)) { 10631da177e4SLinus Torvalds idr_remove(&_minor_idr, m); 10641da177e4SLinus Torvalds r = -ENOSPC; 10651da177e4SLinus Torvalds goto out; 10661da177e4SLinus Torvalds } 10671da177e4SLinus Torvalds 10681da177e4SLinus Torvalds *minor = m; 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds out: 1071f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 10721da177e4SLinus Torvalds return r; 10731da177e4SLinus Torvalds } 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops; 10761da177e4SLinus Torvalds 10771da177e4SLinus Torvalds /* 10781da177e4SLinus Torvalds * Allocate and initialise a blank device with a given minor. 10791da177e4SLinus Torvalds */ 10802b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor) 10811da177e4SLinus Torvalds { 10821da177e4SLinus Torvalds int r; 1083cf13ab8eSFrederik Deweerdt struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL); 1084ba61fdd1SJeff Mahoney void *old_md; 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds if (!md) { 10871da177e4SLinus Torvalds DMWARN("unable to allocate device, out of memory."); 10881da177e4SLinus Torvalds return NULL; 10891da177e4SLinus Torvalds } 10901da177e4SLinus Torvalds 109110da4f79SJeff Mahoney if (!try_module_get(THIS_MODULE)) 10926ed7ade8SMilan Broz goto bad_module_get; 109310da4f79SJeff Mahoney 10941da177e4SLinus Torvalds /* get a minor number for the dev */ 10952b06cfffSAlasdair G Kergon if (minor == DM_ANY_MINOR) 1096cf13ab8eSFrederik Deweerdt r = next_free_minor(&minor); 10972b06cfffSAlasdair G Kergon else 1098cf13ab8eSFrederik Deweerdt r = specific_minor(minor); 10991da177e4SLinus Torvalds if (r < 0) 11006ed7ade8SMilan Broz goto bad_minor; 11011da177e4SLinus Torvalds 11022ca3310eSAlasdair G Kergon init_rwsem(&md->io_lock); 1103e61290a4SDaniel Walker mutex_init(&md->suspend_lock); 11042e93ccc1SKiyoshi Ueda spin_lock_init(&md->pushback_lock); 11051da177e4SLinus Torvalds rwlock_init(&md->map_lock); 11061da177e4SLinus Torvalds atomic_set(&md->holders, 1); 11075c6bd75dSAlasdair G Kergon atomic_set(&md->open_count, 0); 11081da177e4SLinus Torvalds atomic_set(&md->event_nr, 0); 11097a8c3d3bSMike Anderson atomic_set(&md->uevent_seq, 0); 11107a8c3d3bSMike Anderson INIT_LIST_HEAD(&md->uevent_list); 11117a8c3d3bSMike Anderson spin_lock_init(&md->uevent_lock); 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds md->queue = blk_alloc_queue(GFP_KERNEL); 11141da177e4SLinus Torvalds if (!md->queue) 11156ed7ade8SMilan Broz goto bad_queue; 11161da177e4SLinus Torvalds 11171da177e4SLinus Torvalds md->queue->queuedata = md; 11181da177e4SLinus Torvalds md->queue->backing_dev_info.congested_fn = dm_any_congested; 11191da177e4SLinus Torvalds md->queue->backing_dev_info.congested_data = md; 11201da177e4SLinus Torvalds blk_queue_make_request(md->queue, dm_request); 1121daef265fSJens Axboe blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY); 11221da177e4SLinus Torvalds md->queue->unplug_fn = dm_unplug_all; 1123f6fccb12SMilan Broz blk_queue_merge_bvec(md->queue, dm_merge_bvec); 11241da177e4SLinus Torvalds 112593d2341cSMatthew Dobson md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache); 11261da177e4SLinus Torvalds if (!md->io_pool) 11276ed7ade8SMilan Broz goto bad_io_pool; 11281da177e4SLinus Torvalds 112993d2341cSMatthew Dobson md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache); 11301da177e4SLinus Torvalds if (!md->tio_pool) 11316ed7ade8SMilan Broz goto bad_tio_pool; 11321da177e4SLinus Torvalds 1133bb799ca0SJens Axboe md->bs = bioset_create(16, 0); 11349faf400fSStefan Bader if (!md->bs) 11359faf400fSStefan Bader goto bad_no_bioset; 11369faf400fSStefan Bader 11371da177e4SLinus Torvalds md->disk = alloc_disk(1); 11381da177e4SLinus Torvalds if (!md->disk) 11396ed7ade8SMilan Broz goto bad_disk; 11401da177e4SLinus Torvalds 1141f0b04115SJeff Mahoney atomic_set(&md->pending, 0); 1142f0b04115SJeff Mahoney init_waitqueue_head(&md->wait); 1143f0b04115SJeff Mahoney init_waitqueue_head(&md->eventq); 1144f0b04115SJeff Mahoney 11451da177e4SLinus Torvalds md->disk->major = _major; 11461da177e4SLinus Torvalds md->disk->first_minor = minor; 11471da177e4SLinus Torvalds md->disk->fops = &dm_blk_dops; 11481da177e4SLinus Torvalds md->disk->queue = md->queue; 11491da177e4SLinus Torvalds md->disk->private_data = md; 11501da177e4SLinus Torvalds sprintf(md->disk->disk_name, "dm-%d", minor); 11511da177e4SLinus Torvalds add_disk(md->disk); 11527e51f257SMike Anderson format_dev_t(md->name, MKDEV(_major, minor)); 11531da177e4SLinus Torvalds 1154304f3f6aSMilan Broz md->wq = create_singlethread_workqueue("kdmflush"); 1155304f3f6aSMilan Broz if (!md->wq) 1156304f3f6aSMilan Broz goto bad_thread; 1157304f3f6aSMilan Broz 1158ba61fdd1SJeff Mahoney /* Populate the mapping, nobody knows we exist yet */ 1159f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 1160ba61fdd1SJeff Mahoney old_md = idr_replace(&_minor_idr, md, minor); 1161f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1162ba61fdd1SJeff Mahoney 1163ba61fdd1SJeff Mahoney BUG_ON(old_md != MINOR_ALLOCED); 1164ba61fdd1SJeff Mahoney 11651da177e4SLinus Torvalds return md; 11661da177e4SLinus Torvalds 1167304f3f6aSMilan Broz bad_thread: 1168304f3f6aSMilan Broz put_disk(md->disk); 11696ed7ade8SMilan Broz bad_disk: 11709faf400fSStefan Bader bioset_free(md->bs); 11719faf400fSStefan Bader bad_no_bioset: 11721da177e4SLinus Torvalds mempool_destroy(md->tio_pool); 11736ed7ade8SMilan Broz bad_tio_pool: 11741da177e4SLinus Torvalds mempool_destroy(md->io_pool); 11756ed7ade8SMilan Broz bad_io_pool: 11761312f40eSAl Viro blk_cleanup_queue(md->queue); 11776ed7ade8SMilan Broz bad_queue: 11781da177e4SLinus Torvalds free_minor(minor); 11796ed7ade8SMilan Broz bad_minor: 118010da4f79SJeff Mahoney module_put(THIS_MODULE); 11816ed7ade8SMilan Broz bad_module_get: 11821da177e4SLinus Torvalds kfree(md); 11831da177e4SLinus Torvalds return NULL; 11841da177e4SLinus Torvalds } 11851da177e4SLinus Torvalds 1186ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md); 1187ae9da83fSJun'ichi Nomura 11881da177e4SLinus Torvalds static void free_dev(struct mapped_device *md) 11891da177e4SLinus Torvalds { 1190f331c029STejun Heo int minor = MINOR(disk_devt(md->disk)); 119163d94e48SJun'ichi Nomura 1192d9dde59bSJun'ichi Nomura if (md->suspended_bdev) { 1193ae9da83fSJun'ichi Nomura unlock_fs(md); 1194d9dde59bSJun'ichi Nomura bdput(md->suspended_bdev); 1195d9dde59bSJun'ichi Nomura } 1196304f3f6aSMilan Broz destroy_workqueue(md->wq); 11971da177e4SLinus Torvalds mempool_destroy(md->tio_pool); 11981da177e4SLinus Torvalds mempool_destroy(md->io_pool); 11999faf400fSStefan Bader bioset_free(md->bs); 12001da177e4SLinus Torvalds del_gendisk(md->disk); 120163d94e48SJun'ichi Nomura free_minor(minor); 1202fba9f90eSJeff Mahoney 1203fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 1204fba9f90eSJeff Mahoney md->disk->private_data = NULL; 1205fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 1206fba9f90eSJeff Mahoney 12071da177e4SLinus Torvalds put_disk(md->disk); 12081312f40eSAl Viro blk_cleanup_queue(md->queue); 120910da4f79SJeff Mahoney module_put(THIS_MODULE); 12101da177e4SLinus Torvalds kfree(md); 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds 12131da177e4SLinus Torvalds /* 12141da177e4SLinus Torvalds * Bind a table to the device. 12151da177e4SLinus Torvalds */ 12161da177e4SLinus Torvalds static void event_callback(void *context) 12171da177e4SLinus Torvalds { 12187a8c3d3bSMike Anderson unsigned long flags; 12197a8c3d3bSMike Anderson LIST_HEAD(uevents); 12201da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) context; 12211da177e4SLinus Torvalds 12227a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 12237a8c3d3bSMike Anderson list_splice_init(&md->uevent_list, &uevents); 12247a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 12257a8c3d3bSMike Anderson 1226ed9e1982STejun Heo dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); 12277a8c3d3bSMike Anderson 12281da177e4SLinus Torvalds atomic_inc(&md->event_nr); 12291da177e4SLinus Torvalds wake_up(&md->eventq); 12301da177e4SLinus Torvalds } 12311da177e4SLinus Torvalds 12324e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size) 12331da177e4SLinus Torvalds { 12344e90188bSAlasdair G Kergon set_capacity(md->disk, size); 12351da177e4SLinus Torvalds 12361b1dcc1bSJes Sorensen mutex_lock(&md->suspended_bdev->bd_inode->i_mutex); 1237e39e2e95SAlasdair G Kergon i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT); 12381b1dcc1bSJes Sorensen mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex); 12391da177e4SLinus Torvalds } 12401da177e4SLinus Torvalds 12411da177e4SLinus Torvalds static int __bind(struct mapped_device *md, struct dm_table *t) 12421da177e4SLinus Torvalds { 1243165125e1SJens Axboe struct request_queue *q = md->queue; 12441da177e4SLinus Torvalds sector_t size; 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds size = dm_table_get_size(t); 12473ac51e74SDarrick J. Wong 12483ac51e74SDarrick J. Wong /* 12493ac51e74SDarrick J. Wong * Wipe any geometry if the size of the table changed. 12503ac51e74SDarrick J. Wong */ 12513ac51e74SDarrick J. Wong if (size != get_capacity(md->disk)) 12523ac51e74SDarrick J. Wong memset(&md->geometry, 0, sizeof(md->geometry)); 12533ac51e74SDarrick J. Wong 1254bfa152faSJun'ichi Nomura if (md->suspended_bdev) 12554e90188bSAlasdair G Kergon __set_size(md, size); 12561da177e4SLinus Torvalds 1257d5816876SMikulas Patocka if (!size) { 1258d5816876SMikulas Patocka dm_table_destroy(t); 1259d5816876SMikulas Patocka return 0; 1260d5816876SMikulas Patocka } 1261d5816876SMikulas Patocka 1262cf222b37SAlasdair G Kergon dm_table_event_callback(t, event_callback, md); 12632ca3310eSAlasdair G Kergon 12642ca3310eSAlasdair G Kergon write_lock(&md->map_lock); 12652ca3310eSAlasdair G Kergon md->map = t; 12661da177e4SLinus Torvalds dm_table_set_restrictions(t, q); 12672ca3310eSAlasdair G Kergon write_unlock(&md->map_lock); 12682ca3310eSAlasdair G Kergon 12691da177e4SLinus Torvalds return 0; 12701da177e4SLinus Torvalds } 12711da177e4SLinus Torvalds 12721da177e4SLinus Torvalds static void __unbind(struct mapped_device *md) 12731da177e4SLinus Torvalds { 12741da177e4SLinus Torvalds struct dm_table *map = md->map; 12751da177e4SLinus Torvalds 12761da177e4SLinus Torvalds if (!map) 12771da177e4SLinus Torvalds return; 12781da177e4SLinus Torvalds 12791da177e4SLinus Torvalds dm_table_event_callback(map, NULL, NULL); 12801da177e4SLinus Torvalds write_lock(&md->map_lock); 12811da177e4SLinus Torvalds md->map = NULL; 12821da177e4SLinus Torvalds write_unlock(&md->map_lock); 1283d5816876SMikulas Patocka dm_table_destroy(map); 12841da177e4SLinus Torvalds } 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds /* 12871da177e4SLinus Torvalds * Constructor for a new device. 12881da177e4SLinus Torvalds */ 12892b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result) 12901da177e4SLinus Torvalds { 12911da177e4SLinus Torvalds struct mapped_device *md; 12921da177e4SLinus Torvalds 12932b06cfffSAlasdair G Kergon md = alloc_dev(minor); 12941da177e4SLinus Torvalds if (!md) 12951da177e4SLinus Torvalds return -ENXIO; 12961da177e4SLinus Torvalds 1297784aae73SMilan Broz dm_sysfs_init(md); 1298784aae73SMilan Broz 12991da177e4SLinus Torvalds *result = md; 13001da177e4SLinus Torvalds return 0; 13011da177e4SLinus Torvalds } 13021da177e4SLinus Torvalds 1303637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev) 13041da177e4SLinus Torvalds { 13051da177e4SLinus Torvalds struct mapped_device *md; 13061da177e4SLinus Torvalds unsigned minor = MINOR(dev); 13071da177e4SLinus Torvalds 13081da177e4SLinus Torvalds if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) 13091da177e4SLinus Torvalds return NULL; 13101da177e4SLinus Torvalds 1311f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 13121da177e4SLinus Torvalds 13131da177e4SLinus Torvalds md = idr_find(&_minor_idr, minor); 1314fba9f90eSJeff Mahoney if (md && (md == MINOR_ALLOCED || 1315f331c029STejun Heo (MINOR(disk_devt(dm_disk(md))) != minor) || 1316fba9f90eSJeff Mahoney test_bit(DMF_FREEING, &md->flags))) { 1317637842cfSDavid Teigland md = NULL; 1318fba9f90eSJeff Mahoney goto out; 1319fba9f90eSJeff Mahoney } 13201da177e4SLinus Torvalds 1321fba9f90eSJeff Mahoney out: 1322f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 13231da177e4SLinus Torvalds 1324637842cfSDavid Teigland return md; 1325637842cfSDavid Teigland } 1326637842cfSDavid Teigland 1327d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev) 1328d229a958SDavid Teigland { 1329d229a958SDavid Teigland struct mapped_device *md = dm_find_md(dev); 1330d229a958SDavid Teigland 1331d229a958SDavid Teigland if (md) 1332d229a958SDavid Teigland dm_get(md); 1333d229a958SDavid Teigland 1334d229a958SDavid Teigland return md; 1335d229a958SDavid Teigland } 1336d229a958SDavid Teigland 13379ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md) 1338637842cfSDavid Teigland { 13399ade92a9SAlasdair G Kergon return md->interface_ptr; 13401da177e4SLinus Torvalds } 13411da177e4SLinus Torvalds 13421da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr) 13431da177e4SLinus Torvalds { 13441da177e4SLinus Torvalds md->interface_ptr = ptr; 13451da177e4SLinus Torvalds } 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds void dm_get(struct mapped_device *md) 13481da177e4SLinus Torvalds { 13491da177e4SLinus Torvalds atomic_inc(&md->holders); 13501da177e4SLinus Torvalds } 13511da177e4SLinus Torvalds 135272d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md) 135372d94861SAlasdair G Kergon { 135472d94861SAlasdair G Kergon return md->name; 135572d94861SAlasdair G Kergon } 135672d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name); 135772d94861SAlasdair G Kergon 13581da177e4SLinus Torvalds void dm_put(struct mapped_device *md) 13591da177e4SLinus Torvalds { 13601134e5aeSMike Anderson struct dm_table *map; 13611da177e4SLinus Torvalds 1362fba9f90eSJeff Mahoney BUG_ON(test_bit(DMF_FREEING, &md->flags)); 1363fba9f90eSJeff Mahoney 1364f32c10b0SJeff Mahoney if (atomic_dec_and_lock(&md->holders, &_minor_lock)) { 13651134e5aeSMike Anderson map = dm_get_table(md); 1366f331c029STejun Heo idr_replace(&_minor_idr, MINOR_ALLOCED, 1367f331c029STejun Heo MINOR(disk_devt(dm_disk(md)))); 1368fba9f90eSJeff Mahoney set_bit(DMF_FREEING, &md->flags); 1369f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1370cf222b37SAlasdair G Kergon if (!dm_suspended(md)) { 13711da177e4SLinus Torvalds dm_table_presuspend_targets(map); 13721da177e4SLinus Torvalds dm_table_postsuspend_targets(map); 13731da177e4SLinus Torvalds } 1374784aae73SMilan Broz dm_sysfs_exit(md); 13751134e5aeSMike Anderson dm_table_put(map); 1376a1b51e98SMikulas Patocka __unbind(md); 13771da177e4SLinus Torvalds free_dev(md); 13781da177e4SLinus Torvalds } 13791da177e4SLinus Torvalds } 138079eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put); 13811da177e4SLinus Torvalds 138246125c1cSMilan Broz static int dm_wait_for_completion(struct mapped_device *md) 138346125c1cSMilan Broz { 138446125c1cSMilan Broz int r = 0; 138546125c1cSMilan Broz 138646125c1cSMilan Broz while (1) { 138746125c1cSMilan Broz set_current_state(TASK_INTERRUPTIBLE); 138846125c1cSMilan Broz 138946125c1cSMilan Broz smp_mb(); 139046125c1cSMilan Broz if (!atomic_read(&md->pending)) 139146125c1cSMilan Broz break; 139246125c1cSMilan Broz 139346125c1cSMilan Broz if (signal_pending(current)) { 139446125c1cSMilan Broz r = -EINTR; 139546125c1cSMilan Broz break; 139646125c1cSMilan Broz } 139746125c1cSMilan Broz 139846125c1cSMilan Broz io_schedule(); 139946125c1cSMilan Broz } 140046125c1cSMilan Broz set_current_state(TASK_RUNNING); 140146125c1cSMilan Broz 140246125c1cSMilan Broz return r; 140346125c1cSMilan Broz } 140446125c1cSMilan Broz 14051da177e4SLinus Torvalds /* 14061da177e4SLinus Torvalds * Process the deferred bios 14071da177e4SLinus Torvalds */ 14086d6f10dfSMilan Broz static void __flush_deferred_io(struct mapped_device *md) 14091da177e4SLinus Torvalds { 14106d6f10dfSMilan Broz struct bio *c; 14111da177e4SLinus Torvalds 14126d6f10dfSMilan Broz while ((c = bio_list_pop(&md->deferred))) { 14139e4e5f87SMilan Broz if (__split_bio(md, c)) 14149e4e5f87SMilan Broz bio_io_error(c); 14151da177e4SLinus Torvalds } 141673d410c0SMilan Broz 141773d410c0SMilan Broz clear_bit(DMF_BLOCK_IO, &md->flags); 14181da177e4SLinus Torvalds } 14191da177e4SLinus Torvalds 14206d6f10dfSMilan Broz static void __merge_pushback_list(struct mapped_device *md) 14216d6f10dfSMilan Broz { 14226d6f10dfSMilan Broz unsigned long flags; 14236d6f10dfSMilan Broz 14246d6f10dfSMilan Broz spin_lock_irqsave(&md->pushback_lock, flags); 14256d6f10dfSMilan Broz clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 14266d6f10dfSMilan Broz bio_list_merge_head(&md->deferred, &md->pushback); 14276d6f10dfSMilan Broz bio_list_init(&md->pushback); 14286d6f10dfSMilan Broz spin_unlock_irqrestore(&md->pushback_lock, flags); 14296d6f10dfSMilan Broz } 14306d6f10dfSMilan Broz 1431304f3f6aSMilan Broz static void dm_wq_work(struct work_struct *work) 1432304f3f6aSMilan Broz { 1433304f3f6aSMilan Broz struct dm_wq_req *req = container_of(work, struct dm_wq_req, work); 1434304f3f6aSMilan Broz struct mapped_device *md = req->md; 1435304f3f6aSMilan Broz 1436304f3f6aSMilan Broz down_write(&md->io_lock); 1437304f3f6aSMilan Broz switch (req->type) { 1438304f3f6aSMilan Broz case DM_WQ_FLUSH_DEFERRED: 1439304f3f6aSMilan Broz __flush_deferred_io(md); 1440304f3f6aSMilan Broz break; 1441304f3f6aSMilan Broz default: 1442304f3f6aSMilan Broz DMERR("dm_wq_work: unrecognised work type %d", req->type); 1443304f3f6aSMilan Broz BUG(); 1444304f3f6aSMilan Broz } 1445304f3f6aSMilan Broz up_write(&md->io_lock); 1446304f3f6aSMilan Broz } 1447304f3f6aSMilan Broz 1448304f3f6aSMilan Broz static void dm_wq_queue(struct mapped_device *md, int type, void *context, 1449304f3f6aSMilan Broz struct dm_wq_req *req) 1450304f3f6aSMilan Broz { 1451304f3f6aSMilan Broz req->type = type; 1452304f3f6aSMilan Broz req->md = md; 1453304f3f6aSMilan Broz req->context = context; 1454304f3f6aSMilan Broz INIT_WORK(&req->work, dm_wq_work); 1455304f3f6aSMilan Broz queue_work(md->wq, &req->work); 1456304f3f6aSMilan Broz } 1457304f3f6aSMilan Broz 1458304f3f6aSMilan Broz static void dm_queue_flush(struct mapped_device *md, int type, void *context) 1459304f3f6aSMilan Broz { 1460304f3f6aSMilan Broz struct dm_wq_req req; 1461304f3f6aSMilan Broz 1462304f3f6aSMilan Broz dm_wq_queue(md, type, context, &req); 1463304f3f6aSMilan Broz flush_workqueue(md->wq); 1464304f3f6aSMilan Broz } 1465304f3f6aSMilan Broz 14661da177e4SLinus Torvalds /* 14671da177e4SLinus Torvalds * Swap in a new table (destroying old one). 14681da177e4SLinus Torvalds */ 14691da177e4SLinus Torvalds int dm_swap_table(struct mapped_device *md, struct dm_table *table) 14701da177e4SLinus Torvalds { 147193c534aeSAlasdair G Kergon int r = -EINVAL; 14721da177e4SLinus Torvalds 1473e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 14741da177e4SLinus Torvalds 14751da177e4SLinus Torvalds /* device must be suspended */ 1476cf222b37SAlasdair G Kergon if (!dm_suspended(md)) 147793c534aeSAlasdair G Kergon goto out; 14781da177e4SLinus Torvalds 1479bfa152faSJun'ichi Nomura /* without bdev, the device size cannot be changed */ 1480bfa152faSJun'ichi Nomura if (!md->suspended_bdev) 1481bfa152faSJun'ichi Nomura if (get_capacity(md->disk) != dm_table_get_size(table)) 1482bfa152faSJun'ichi Nomura goto out; 1483bfa152faSJun'ichi Nomura 14841da177e4SLinus Torvalds __unbind(md); 14851da177e4SLinus Torvalds r = __bind(md, table); 14861da177e4SLinus Torvalds 148793c534aeSAlasdair G Kergon out: 1488e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 148993c534aeSAlasdair G Kergon return r; 14901da177e4SLinus Torvalds } 14911da177e4SLinus Torvalds 14921da177e4SLinus Torvalds /* 14931da177e4SLinus Torvalds * Functions to lock and unlock any filesystem running on the 14941da177e4SLinus Torvalds * device. 14951da177e4SLinus Torvalds */ 14962ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md) 14971da177e4SLinus Torvalds { 1498e39e2e95SAlasdair G Kergon int r; 14991da177e4SLinus Torvalds 15001da177e4SLinus Torvalds WARN_ON(md->frozen_sb); 1501dfbe03f6SAlasdair G Kergon 1502e39e2e95SAlasdair G Kergon md->frozen_sb = freeze_bdev(md->suspended_bdev); 1503dfbe03f6SAlasdair G Kergon if (IS_ERR(md->frozen_sb)) { 1504cf222b37SAlasdair G Kergon r = PTR_ERR(md->frozen_sb); 1505e39e2e95SAlasdair G Kergon md->frozen_sb = NULL; 1506e39e2e95SAlasdair G Kergon return r; 1507dfbe03f6SAlasdair G Kergon } 1508dfbe03f6SAlasdair G Kergon 1509aa8d7c2fSAlasdair G Kergon set_bit(DMF_FROZEN, &md->flags); 1510aa8d7c2fSAlasdair G Kergon 15111da177e4SLinus Torvalds /* don't bdput right now, we don't want the bdev 1512e39e2e95SAlasdair G Kergon * to go away while it is locked. 15131da177e4SLinus Torvalds */ 15141da177e4SLinus Torvalds return 0; 15151da177e4SLinus Torvalds } 15161da177e4SLinus Torvalds 15172ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md) 15181da177e4SLinus Torvalds { 1519aa8d7c2fSAlasdair G Kergon if (!test_bit(DMF_FROZEN, &md->flags)) 1520aa8d7c2fSAlasdair G Kergon return; 1521aa8d7c2fSAlasdair G Kergon 1522e39e2e95SAlasdair G Kergon thaw_bdev(md->suspended_bdev, md->frozen_sb); 15231da177e4SLinus Torvalds md->frozen_sb = NULL; 1524aa8d7c2fSAlasdair G Kergon clear_bit(DMF_FROZEN, &md->flags); 15251da177e4SLinus Torvalds } 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds /* 15281da177e4SLinus Torvalds * We need to be able to change a mapping table under a mounted 15291da177e4SLinus Torvalds * filesystem. For example we might want to move some data in 15301da177e4SLinus Torvalds * the background. Before the table can be swapped with 15311da177e4SLinus Torvalds * dm_bind_table, dm_suspend must be called to flush any in 15321da177e4SLinus Torvalds * flight bios and ensure that any further io gets deferred. 15331da177e4SLinus Torvalds */ 1534a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags) 15351da177e4SLinus Torvalds { 15362ca3310eSAlasdair G Kergon struct dm_table *map = NULL; 15371da177e4SLinus Torvalds DECLARE_WAITQUEUE(wait, current); 153846125c1cSMilan Broz int r = 0; 1539a3d77d35SKiyoshi Ueda int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0; 15402e93ccc1SKiyoshi Ueda int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0; 15411da177e4SLinus Torvalds 1542e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 15432ca3310eSAlasdair G Kergon 154473d410c0SMilan Broz if (dm_suspended(md)) { 154573d410c0SMilan Broz r = -EINVAL; 1546d287483dSAlasdair G Kergon goto out_unlock; 154773d410c0SMilan Broz } 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds map = dm_get_table(md); 1550cf222b37SAlasdair G Kergon 15512e93ccc1SKiyoshi Ueda /* 15522e93ccc1SKiyoshi Ueda * DMF_NOFLUSH_SUSPENDING must be set before presuspend. 15532e93ccc1SKiyoshi Ueda * This flag is cleared before dm_suspend returns. 15542e93ccc1SKiyoshi Ueda */ 15552e93ccc1SKiyoshi Ueda if (noflush) 15562e93ccc1SKiyoshi Ueda set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 15572e93ccc1SKiyoshi Ueda 1558436d4108SAlasdair G Kergon /* This does not get reverted if there's an error later. */ 15591da177e4SLinus Torvalds dm_table_presuspend_targets(map); 15601da177e4SLinus Torvalds 1561bfa152faSJun'ichi Nomura /* bdget() can stall if the pending I/Os are not flushed */ 1562bfa152faSJun'ichi Nomura if (!noflush) { 1563e39e2e95SAlasdair G Kergon md->suspended_bdev = bdget_disk(md->disk, 0); 1564e39e2e95SAlasdair G Kergon if (!md->suspended_bdev) { 1565e39e2e95SAlasdair G Kergon DMWARN("bdget failed in dm_suspend"); 1566e39e2e95SAlasdair G Kergon r = -ENOMEM; 1567f431d966SKiyoshi Ueda goto out; 1568e39e2e95SAlasdair G Kergon } 1569e39e2e95SAlasdair G Kergon 15702e93ccc1SKiyoshi Ueda /* 15716d6f10dfSMilan Broz * Flush I/O to the device. noflush supersedes do_lockfs, 15726d6f10dfSMilan Broz * because lock_fs() needs to flush I/Os. 15732e93ccc1SKiyoshi Ueda */ 15746d6f10dfSMilan Broz if (do_lockfs) { 15752ca3310eSAlasdair G Kergon r = lock_fs(md); 15762ca3310eSAlasdair G Kergon if (r) 15772ca3310eSAlasdair G Kergon goto out; 1578aa8d7c2fSAlasdair G Kergon } 15796d6f10dfSMilan Broz } 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds /* 1582354e0071SAlasdair G Kergon * First we set the BLOCK_IO flag so no more ios will be mapped. 15831da177e4SLinus Torvalds */ 15842ca3310eSAlasdair G Kergon down_write(&md->io_lock); 15852ca3310eSAlasdair G Kergon set_bit(DMF_BLOCK_IO, &md->flags); 15861da177e4SLinus Torvalds 15871da177e4SLinus Torvalds add_wait_queue(&md->wait, &wait); 15882ca3310eSAlasdair G Kergon up_write(&md->io_lock); 15891da177e4SLinus Torvalds 15901da177e4SLinus Torvalds /* unplug */ 15912ca3310eSAlasdair G Kergon if (map) 15921da177e4SLinus Torvalds dm_table_unplug_all(map); 15931da177e4SLinus Torvalds 15941da177e4SLinus Torvalds /* 159546125c1cSMilan Broz * Wait for the already-mapped ios to complete. 15961da177e4SLinus Torvalds */ 159746125c1cSMilan Broz r = dm_wait_for_completion(md); 15981da177e4SLinus Torvalds 15992ca3310eSAlasdair G Kergon down_write(&md->io_lock); 16001da177e4SLinus Torvalds remove_wait_queue(&md->wait, &wait); 16011da177e4SLinus Torvalds 16026d6f10dfSMilan Broz if (noflush) 16036d6f10dfSMilan Broz __merge_pushback_list(md); 160494d6351eSMilan Broz up_write(&md->io_lock); 16052e93ccc1SKiyoshi Ueda 16061da177e4SLinus Torvalds /* were we interrupted ? */ 160746125c1cSMilan Broz if (r < 0) { 1608304f3f6aSMilan Broz dm_queue_flush(md, DM_WQ_FLUSH_DEFERRED, NULL); 160973d410c0SMilan Broz 16102ca3310eSAlasdair G Kergon unlock_fs(md); 16112e93ccc1SKiyoshi Ueda goto out; /* pushback list is already flushed, so skip flush */ 16122ca3310eSAlasdair G Kergon } 16132ca3310eSAlasdair G Kergon 16142ca3310eSAlasdair G Kergon dm_table_postsuspend_targets(map); 16151da177e4SLinus Torvalds 16161da177e4SLinus Torvalds set_bit(DMF_SUSPENDED, &md->flags); 16171da177e4SLinus Torvalds 16182ca3310eSAlasdair G Kergon out: 1619e39e2e95SAlasdair G Kergon if (r && md->suspended_bdev) { 1620e39e2e95SAlasdair G Kergon bdput(md->suspended_bdev); 1621e39e2e95SAlasdair G Kergon md->suspended_bdev = NULL; 1622e39e2e95SAlasdair G Kergon } 1623e39e2e95SAlasdair G Kergon 16241da177e4SLinus Torvalds dm_table_put(map); 1625d287483dSAlasdair G Kergon 1626d287483dSAlasdair G Kergon out_unlock: 1627e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 1628cf222b37SAlasdair G Kergon return r; 16291da177e4SLinus Torvalds } 16301da177e4SLinus Torvalds 16311da177e4SLinus Torvalds int dm_resume(struct mapped_device *md) 16321da177e4SLinus Torvalds { 1633cf222b37SAlasdair G Kergon int r = -EINVAL; 1634cf222b37SAlasdair G Kergon struct dm_table *map = NULL; 16351da177e4SLinus Torvalds 1636e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 16372ca3310eSAlasdair G Kergon if (!dm_suspended(md)) 1638cf222b37SAlasdair G Kergon goto out; 1639cf222b37SAlasdair G Kergon 1640cf222b37SAlasdair G Kergon map = dm_get_table(md); 16412ca3310eSAlasdair G Kergon if (!map || !dm_table_get_size(map)) 1642cf222b37SAlasdair G Kergon goto out; 16431da177e4SLinus Torvalds 16448757b776SMilan Broz r = dm_table_resume_targets(map); 16458757b776SMilan Broz if (r) 16468757b776SMilan Broz goto out; 16472ca3310eSAlasdair G Kergon 1648304f3f6aSMilan Broz dm_queue_flush(md, DM_WQ_FLUSH_DEFERRED, NULL); 16492ca3310eSAlasdair G Kergon 16502ca3310eSAlasdair G Kergon unlock_fs(md); 16512ca3310eSAlasdair G Kergon 1652bfa152faSJun'ichi Nomura if (md->suspended_bdev) { 1653e39e2e95SAlasdair G Kergon bdput(md->suspended_bdev); 1654e39e2e95SAlasdair G Kergon md->suspended_bdev = NULL; 1655bfa152faSJun'ichi Nomura } 1656e39e2e95SAlasdair G Kergon 16572ca3310eSAlasdair G Kergon clear_bit(DMF_SUSPENDED, &md->flags); 16582ca3310eSAlasdair G Kergon 16591da177e4SLinus Torvalds dm_table_unplug_all(map); 16601da177e4SLinus Torvalds 166169267a30SAlasdair G Kergon dm_kobject_uevent(md); 16628560ed6fSHannes Reinecke 1663cf222b37SAlasdair G Kergon r = 0; 16642ca3310eSAlasdair G Kergon 1665cf222b37SAlasdair G Kergon out: 1666cf222b37SAlasdair G Kergon dm_table_put(map); 1667e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 16682ca3310eSAlasdair G Kergon 1669cf222b37SAlasdair G Kergon return r; 16701da177e4SLinus Torvalds } 16711da177e4SLinus Torvalds 16721da177e4SLinus Torvalds /*----------------------------------------------------------------- 16731da177e4SLinus Torvalds * Event notification. 16741da177e4SLinus Torvalds *---------------------------------------------------------------*/ 167569267a30SAlasdair G Kergon void dm_kobject_uevent(struct mapped_device *md) 167669267a30SAlasdair G Kergon { 1677ed9e1982STejun Heo kobject_uevent(&disk_to_dev(md->disk)->kobj, KOBJ_CHANGE); 167869267a30SAlasdair G Kergon } 167969267a30SAlasdair G Kergon 16807a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md) 16817a8c3d3bSMike Anderson { 16827a8c3d3bSMike Anderson return atomic_add_return(1, &md->uevent_seq); 16837a8c3d3bSMike Anderson } 16847a8c3d3bSMike Anderson 16851da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md) 16861da177e4SLinus Torvalds { 16871da177e4SLinus Torvalds return atomic_read(&md->event_nr); 16881da177e4SLinus Torvalds } 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr) 16911da177e4SLinus Torvalds { 16921da177e4SLinus Torvalds return wait_event_interruptible(md->eventq, 16931da177e4SLinus Torvalds (event_nr != atomic_read(&md->event_nr))); 16941da177e4SLinus Torvalds } 16951da177e4SLinus Torvalds 16967a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist) 16977a8c3d3bSMike Anderson { 16987a8c3d3bSMike Anderson unsigned long flags; 16997a8c3d3bSMike Anderson 17007a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 17017a8c3d3bSMike Anderson list_add(elist, &md->uevent_list); 17027a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 17037a8c3d3bSMike Anderson } 17047a8c3d3bSMike Anderson 17051da177e4SLinus Torvalds /* 17061da177e4SLinus Torvalds * The gendisk is only valid as long as you have a reference 17071da177e4SLinus Torvalds * count on 'md'. 17081da177e4SLinus Torvalds */ 17091da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md) 17101da177e4SLinus Torvalds { 17111da177e4SLinus Torvalds return md->disk; 17121da177e4SLinus Torvalds } 17131da177e4SLinus Torvalds 1714784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md) 1715784aae73SMilan Broz { 1716784aae73SMilan Broz return &md->kobj; 1717784aae73SMilan Broz } 1718784aae73SMilan Broz 1719784aae73SMilan Broz /* 1720784aae73SMilan Broz * struct mapped_device should not be exported outside of dm.c 1721784aae73SMilan Broz * so use this check to verify that kobj is part of md structure 1722784aae73SMilan Broz */ 1723784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj) 1724784aae73SMilan Broz { 1725784aae73SMilan Broz struct mapped_device *md; 1726784aae73SMilan Broz 1727784aae73SMilan Broz md = container_of(kobj, struct mapped_device, kobj); 1728784aae73SMilan Broz if (&md->kobj != kobj) 1729784aae73SMilan Broz return NULL; 1730784aae73SMilan Broz 1731784aae73SMilan Broz dm_get(md); 1732784aae73SMilan Broz return md; 1733784aae73SMilan Broz } 1734784aae73SMilan Broz 17351da177e4SLinus Torvalds int dm_suspended(struct mapped_device *md) 17361da177e4SLinus Torvalds { 17371da177e4SLinus Torvalds return test_bit(DMF_SUSPENDED, &md->flags); 17381da177e4SLinus Torvalds } 17391da177e4SLinus Torvalds 17402e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti) 17412e93ccc1SKiyoshi Ueda { 17422e93ccc1SKiyoshi Ueda struct mapped_device *md = dm_table_get_md(ti->table); 17432e93ccc1SKiyoshi Ueda int r = __noflush_suspending(md); 17442e93ccc1SKiyoshi Ueda 17452e93ccc1SKiyoshi Ueda dm_put(md); 17462e93ccc1SKiyoshi Ueda 17472e93ccc1SKiyoshi Ueda return r; 17482e93ccc1SKiyoshi Ueda } 17492e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending); 17502e93ccc1SKiyoshi Ueda 17511da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops = { 17521da177e4SLinus Torvalds .open = dm_blk_open, 17531da177e4SLinus Torvalds .release = dm_blk_close, 1754aa129a22SMilan Broz .ioctl = dm_blk_ioctl, 17553ac51e74SDarrick J. Wong .getgeo = dm_blk_getgeo, 17561da177e4SLinus Torvalds .owner = THIS_MODULE 17571da177e4SLinus Torvalds }; 17581da177e4SLinus Torvalds 17591da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo); 17601da177e4SLinus Torvalds 17611da177e4SLinus Torvalds /* 17621da177e4SLinus Torvalds * module hooks 17631da177e4SLinus Torvalds */ 17641da177e4SLinus Torvalds module_init(dm_init); 17651da177e4SLinus Torvalds module_exit(dm_exit); 17661da177e4SLinus Torvalds 17671da177e4SLinus Torvalds module_param(major, uint, 0); 17681da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper"); 17691da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver"); 17701da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 17711da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1772