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 */ 1021da177e4SLinus Torvalds struct mapped_device { 1032ca3310eSAlasdair G Kergon struct rw_semaphore io_lock; 104e61290a4SDaniel Walker struct mutex suspend_lock; 1052e93ccc1SKiyoshi Ueda spinlock_t pushback_lock; 1061da177e4SLinus Torvalds rwlock_t map_lock; 1071da177e4SLinus Torvalds atomic_t holders; 1085c6bd75dSAlasdair G Kergon atomic_t open_count; 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds unsigned long flags; 1111da177e4SLinus Torvalds 112165125e1SJens Axboe struct request_queue *queue; 1131da177e4SLinus Torvalds struct gendisk *disk; 1147e51f257SMike Anderson char name[16]; 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds void *interface_ptr; 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds /* 1191da177e4SLinus Torvalds * A list of ios that arrived while we were suspended. 1201da177e4SLinus Torvalds */ 1211da177e4SLinus Torvalds atomic_t pending; 1221da177e4SLinus Torvalds wait_queue_head_t wait; 12353d5914fSMikulas Patocka struct work_struct work; 1241da177e4SLinus Torvalds struct bio_list deferred; 1252e93ccc1SKiyoshi Ueda struct bio_list pushback; 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds /* 128304f3f6aSMilan Broz * Processing queue (flush/barriers) 129304f3f6aSMilan Broz */ 130304f3f6aSMilan Broz struct workqueue_struct *wq; 131304f3f6aSMilan Broz 132304f3f6aSMilan Broz /* 1331da177e4SLinus Torvalds * The current mapping. 1341da177e4SLinus Torvalds */ 1351da177e4SLinus Torvalds struct dm_table *map; 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds /* 1381da177e4SLinus Torvalds * io objects are allocated from here. 1391da177e4SLinus Torvalds */ 1401da177e4SLinus Torvalds mempool_t *io_pool; 1411da177e4SLinus Torvalds mempool_t *tio_pool; 1421da177e4SLinus Torvalds 1439faf400fSStefan Bader struct bio_set *bs; 1449faf400fSStefan Bader 1451da177e4SLinus Torvalds /* 1461da177e4SLinus Torvalds * Event handling. 1471da177e4SLinus Torvalds */ 1481da177e4SLinus Torvalds atomic_t event_nr; 1491da177e4SLinus Torvalds wait_queue_head_t eventq; 1507a8c3d3bSMike Anderson atomic_t uevent_seq; 1517a8c3d3bSMike Anderson struct list_head uevent_list; 1527a8c3d3bSMike Anderson spinlock_t uevent_lock; /* Protect access to uevent_list */ 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds /* 1551da177e4SLinus Torvalds * freeze/thaw support require holding onto a super block 1561da177e4SLinus Torvalds */ 1571da177e4SLinus Torvalds struct super_block *frozen_sb; 158e39e2e95SAlasdair G Kergon struct block_device *suspended_bdev; 1593ac51e74SDarrick J. Wong 1603ac51e74SDarrick J. Wong /* forced geometry settings */ 1613ac51e74SDarrick J. Wong struct hd_geometry geometry; 162784aae73SMilan Broz 163784aae73SMilan Broz /* sysfs handle */ 164784aae73SMilan Broz struct kobject kobj; 1651da177e4SLinus Torvalds }; 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds #define MIN_IOS 256 168e18b890bSChristoph Lameter static struct kmem_cache *_io_cache; 169e18b890bSChristoph Lameter static struct kmem_cache *_tio_cache; 1708fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache; 1718fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_bio_info_cache; 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds static int __init local_init(void) 1741da177e4SLinus Torvalds { 17551157b4aSKiyoshi Ueda int r = -ENOMEM; 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds /* allocate a slab for the dm_ios */ 178028867acSAlasdair G Kergon _io_cache = KMEM_CACHE(dm_io, 0); 1791da177e4SLinus Torvalds if (!_io_cache) 18051157b4aSKiyoshi Ueda return r; 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds /* allocate a slab for the target ios */ 183028867acSAlasdair G Kergon _tio_cache = KMEM_CACHE(dm_target_io, 0); 18451157b4aSKiyoshi Ueda if (!_tio_cache) 18551157b4aSKiyoshi Ueda goto out_free_io_cache; 1861da177e4SLinus Torvalds 1878fbf26adSKiyoshi Ueda _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0); 1888fbf26adSKiyoshi Ueda if (!_rq_tio_cache) 1898fbf26adSKiyoshi Ueda goto out_free_tio_cache; 1908fbf26adSKiyoshi Ueda 1918fbf26adSKiyoshi Ueda _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0); 1928fbf26adSKiyoshi Ueda if (!_rq_bio_info_cache) 1938fbf26adSKiyoshi Ueda goto out_free_rq_tio_cache; 1948fbf26adSKiyoshi Ueda 19551e5b2bdSMike Anderson r = dm_uevent_init(); 19651157b4aSKiyoshi Ueda if (r) 1978fbf26adSKiyoshi Ueda goto out_free_rq_bio_info_cache; 19851e5b2bdSMike Anderson 1991da177e4SLinus Torvalds _major = major; 2001da177e4SLinus Torvalds r = register_blkdev(_major, _name); 20151157b4aSKiyoshi Ueda if (r < 0) 20251157b4aSKiyoshi Ueda goto out_uevent_exit; 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds if (!_major) 2051da177e4SLinus Torvalds _major = r; 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds return 0; 20851157b4aSKiyoshi Ueda 20951157b4aSKiyoshi Ueda out_uevent_exit: 21051157b4aSKiyoshi Ueda dm_uevent_exit(); 2118fbf26adSKiyoshi Ueda out_free_rq_bio_info_cache: 2128fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_bio_info_cache); 2138fbf26adSKiyoshi Ueda out_free_rq_tio_cache: 2148fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_tio_cache); 21551157b4aSKiyoshi Ueda out_free_tio_cache: 21651157b4aSKiyoshi Ueda kmem_cache_destroy(_tio_cache); 21751157b4aSKiyoshi Ueda out_free_io_cache: 21851157b4aSKiyoshi Ueda kmem_cache_destroy(_io_cache); 21951157b4aSKiyoshi Ueda 22051157b4aSKiyoshi Ueda return r; 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds static void local_exit(void) 2241da177e4SLinus Torvalds { 2258fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_bio_info_cache); 2268fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_tio_cache); 2271da177e4SLinus Torvalds kmem_cache_destroy(_tio_cache); 2281da177e4SLinus Torvalds kmem_cache_destroy(_io_cache); 22900d59405SAkinobu Mita unregister_blkdev(_major, _name); 23051e5b2bdSMike Anderson dm_uevent_exit(); 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds _major = 0; 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds DMINFO("cleaned up"); 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds 237b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = { 2381da177e4SLinus Torvalds local_init, 2391da177e4SLinus Torvalds dm_target_init, 2401da177e4SLinus Torvalds dm_linear_init, 2411da177e4SLinus Torvalds dm_stripe_init, 242945fa4d2SMikulas Patocka dm_kcopyd_init, 2431da177e4SLinus Torvalds dm_interface_init, 2441da177e4SLinus Torvalds }; 2451da177e4SLinus Torvalds 246b9249e55SAlasdair G Kergon static void (*_exits[])(void) = { 2471da177e4SLinus Torvalds local_exit, 2481da177e4SLinus Torvalds dm_target_exit, 2491da177e4SLinus Torvalds dm_linear_exit, 2501da177e4SLinus Torvalds dm_stripe_exit, 251945fa4d2SMikulas Patocka dm_kcopyd_exit, 2521da177e4SLinus Torvalds dm_interface_exit, 2531da177e4SLinus Torvalds }; 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds static int __init dm_init(void) 2561da177e4SLinus Torvalds { 2571da177e4SLinus Torvalds const int count = ARRAY_SIZE(_inits); 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds int r, i; 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds for (i = 0; i < count; i++) { 2621da177e4SLinus Torvalds r = _inits[i](); 2631da177e4SLinus Torvalds if (r) 2641da177e4SLinus Torvalds goto bad; 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds return 0; 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds bad: 2701da177e4SLinus Torvalds while (i--) 2711da177e4SLinus Torvalds _exits[i](); 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds return r; 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds static void __exit dm_exit(void) 2771da177e4SLinus Torvalds { 2781da177e4SLinus Torvalds int i = ARRAY_SIZE(_exits); 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds while (i--) 2811da177e4SLinus Torvalds _exits[i](); 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds /* 2851da177e4SLinus Torvalds * Block device functions 2861da177e4SLinus Torvalds */ 287fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode) 2881da177e4SLinus Torvalds { 2891da177e4SLinus Torvalds struct mapped_device *md; 2901da177e4SLinus Torvalds 291fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 292fba9f90eSJeff Mahoney 293fe5f9f2cSAl Viro md = bdev->bd_disk->private_data; 294fba9f90eSJeff Mahoney if (!md) 295fba9f90eSJeff Mahoney goto out; 296fba9f90eSJeff Mahoney 2975c6bd75dSAlasdair G Kergon if (test_bit(DMF_FREEING, &md->flags) || 2985c6bd75dSAlasdair G Kergon test_bit(DMF_DELETING, &md->flags)) { 299fba9f90eSJeff Mahoney md = NULL; 300fba9f90eSJeff Mahoney goto out; 301fba9f90eSJeff Mahoney } 302fba9f90eSJeff Mahoney 3031da177e4SLinus Torvalds dm_get(md); 3045c6bd75dSAlasdair G Kergon atomic_inc(&md->open_count); 305fba9f90eSJeff Mahoney 306fba9f90eSJeff Mahoney out: 307fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 308fba9f90eSJeff Mahoney 309fba9f90eSJeff Mahoney return md ? 0 : -ENXIO; 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds 312fe5f9f2cSAl Viro static int dm_blk_close(struct gendisk *disk, fmode_t mode) 3131da177e4SLinus Torvalds { 314fe5f9f2cSAl Viro struct mapped_device *md = disk->private_data; 3155c6bd75dSAlasdair G Kergon atomic_dec(&md->open_count); 3161da177e4SLinus Torvalds dm_put(md); 3171da177e4SLinus Torvalds return 0; 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds 3205c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md) 3215c6bd75dSAlasdair G Kergon { 3225c6bd75dSAlasdair G Kergon return atomic_read(&md->open_count); 3235c6bd75dSAlasdair G Kergon } 3245c6bd75dSAlasdair G Kergon 3255c6bd75dSAlasdair G Kergon /* 3265c6bd75dSAlasdair G Kergon * Guarantees nothing is using the device before it's deleted. 3275c6bd75dSAlasdair G Kergon */ 3285c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md) 3295c6bd75dSAlasdair G Kergon { 3305c6bd75dSAlasdair G Kergon int r = 0; 3315c6bd75dSAlasdair G Kergon 3325c6bd75dSAlasdair G Kergon spin_lock(&_minor_lock); 3335c6bd75dSAlasdair G Kergon 3345c6bd75dSAlasdair G Kergon if (dm_open_count(md)) 3355c6bd75dSAlasdair G Kergon r = -EBUSY; 3365c6bd75dSAlasdair G Kergon else 3375c6bd75dSAlasdair G Kergon set_bit(DMF_DELETING, &md->flags); 3385c6bd75dSAlasdair G Kergon 3395c6bd75dSAlasdair G Kergon spin_unlock(&_minor_lock); 3405c6bd75dSAlasdair G Kergon 3415c6bd75dSAlasdair G Kergon return r; 3425c6bd75dSAlasdair G Kergon } 3435c6bd75dSAlasdair G Kergon 3443ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 3453ac51e74SDarrick J. Wong { 3463ac51e74SDarrick J. Wong struct mapped_device *md = bdev->bd_disk->private_data; 3473ac51e74SDarrick J. Wong 3483ac51e74SDarrick J. Wong return dm_get_geometry(md, geo); 3493ac51e74SDarrick J. Wong } 3503ac51e74SDarrick J. Wong 351fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode, 352aa129a22SMilan Broz unsigned int cmd, unsigned long arg) 353aa129a22SMilan Broz { 354fe5f9f2cSAl Viro struct mapped_device *md = bdev->bd_disk->private_data; 355fe5f9f2cSAl Viro struct dm_table *map = dm_get_table(md); 356aa129a22SMilan Broz struct dm_target *tgt; 357aa129a22SMilan Broz int r = -ENOTTY; 358aa129a22SMilan Broz 359aa129a22SMilan Broz if (!map || !dm_table_get_size(map)) 360aa129a22SMilan Broz goto out; 361aa129a22SMilan Broz 362aa129a22SMilan Broz /* We only support devices that have a single target */ 363aa129a22SMilan Broz if (dm_table_get_num_targets(map) != 1) 364aa129a22SMilan Broz goto out; 365aa129a22SMilan Broz 366aa129a22SMilan Broz tgt = dm_table_get_target(map, 0); 367aa129a22SMilan Broz 368aa129a22SMilan Broz if (dm_suspended(md)) { 369aa129a22SMilan Broz r = -EAGAIN; 370aa129a22SMilan Broz goto out; 371aa129a22SMilan Broz } 372aa129a22SMilan Broz 373aa129a22SMilan Broz if (tgt->type->ioctl) 374647b3d00SAl Viro r = tgt->type->ioctl(tgt, cmd, arg); 375aa129a22SMilan Broz 376aa129a22SMilan Broz out: 377aa129a22SMilan Broz dm_table_put(map); 378aa129a22SMilan Broz 379aa129a22SMilan Broz return r; 380aa129a22SMilan Broz } 381aa129a22SMilan Broz 382028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md) 3831da177e4SLinus Torvalds { 3841da177e4SLinus Torvalds return mempool_alloc(md->io_pool, GFP_NOIO); 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds 387028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io) 3881da177e4SLinus Torvalds { 3891da177e4SLinus Torvalds mempool_free(io, md->io_pool); 3901da177e4SLinus Torvalds } 3911da177e4SLinus Torvalds 392028867acSAlasdair G Kergon static struct dm_target_io *alloc_tio(struct mapped_device *md) 3931da177e4SLinus Torvalds { 3941da177e4SLinus Torvalds return mempool_alloc(md->tio_pool, GFP_NOIO); 3951da177e4SLinus Torvalds } 3961da177e4SLinus Torvalds 397028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio) 3981da177e4SLinus Torvalds { 3991da177e4SLinus Torvalds mempool_free(tio, md->tio_pool); 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds 4023eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io) 4033eaf840eSJun'ichi "Nick" Nomura { 4043eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 405c9959059STejun Heo int cpu; 4063eaf840eSJun'ichi "Nick" Nomura 4073eaf840eSJun'ichi "Nick" Nomura io->start_time = jiffies; 4083eaf840eSJun'ichi "Nick" Nomura 409074a7acaSTejun Heo cpu = part_stat_lock(); 410074a7acaSTejun Heo part_round_stats(cpu, &dm_disk(md)->part0); 411074a7acaSTejun Heo part_stat_unlock(); 412074a7acaSTejun Heo dm_disk(md)->part0.in_flight = atomic_inc_return(&md->pending); 4133eaf840eSJun'ichi "Nick" Nomura } 4143eaf840eSJun'ichi "Nick" Nomura 415d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io) 4163eaf840eSJun'ichi "Nick" Nomura { 4173eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 4183eaf840eSJun'ichi "Nick" Nomura struct bio *bio = io->bio; 4193eaf840eSJun'ichi "Nick" Nomura unsigned long duration = jiffies - io->start_time; 420c9959059STejun Heo int pending, cpu; 4213eaf840eSJun'ichi "Nick" Nomura int rw = bio_data_dir(bio); 4223eaf840eSJun'ichi "Nick" Nomura 423074a7acaSTejun Heo cpu = part_stat_lock(); 424074a7acaSTejun Heo part_round_stats(cpu, &dm_disk(md)->part0); 425074a7acaSTejun Heo part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration); 426074a7acaSTejun Heo part_stat_unlock(); 4273eaf840eSJun'ichi "Nick" Nomura 428074a7acaSTejun Heo dm_disk(md)->part0.in_flight = pending = 429074a7acaSTejun Heo atomic_dec_return(&md->pending); 4303eaf840eSJun'ichi "Nick" Nomura 431d221d2e7SMikulas Patocka /* nudge anyone waiting on suspend queue */ 432d221d2e7SMikulas Patocka if (!pending) 433d221d2e7SMikulas Patocka wake_up(&md->wait); 4343eaf840eSJun'ichi "Nick" Nomura } 4353eaf840eSJun'ichi "Nick" Nomura 4361da177e4SLinus Torvalds /* 4371da177e4SLinus Torvalds * Add the bio to the list of deferred io. 4381da177e4SLinus Torvalds */ 4391da177e4SLinus Torvalds static int queue_io(struct mapped_device *md, struct bio *bio) 4401da177e4SLinus Torvalds { 4412ca3310eSAlasdair G Kergon down_write(&md->io_lock); 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds if (!test_bit(DMF_BLOCK_IO, &md->flags)) { 4442ca3310eSAlasdair G Kergon up_write(&md->io_lock); 4451da177e4SLinus Torvalds return 1; 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds bio_list_add(&md->deferred, bio); 4491da177e4SLinus Torvalds 4502ca3310eSAlasdair G Kergon up_write(&md->io_lock); 4511da177e4SLinus Torvalds return 0; /* deferred successfully */ 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds /* 4551da177e4SLinus Torvalds * Everyone (including functions in this file), should use this 4561da177e4SLinus Torvalds * function to access the md->map field, and make sure they call 4571da177e4SLinus Torvalds * dm_table_put() when finished. 4581da177e4SLinus Torvalds */ 4591da177e4SLinus Torvalds struct dm_table *dm_get_table(struct mapped_device *md) 4601da177e4SLinus Torvalds { 4611da177e4SLinus Torvalds struct dm_table *t; 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds read_lock(&md->map_lock); 4641da177e4SLinus Torvalds t = md->map; 4651da177e4SLinus Torvalds if (t) 4661da177e4SLinus Torvalds dm_table_get(t); 4671da177e4SLinus Torvalds read_unlock(&md->map_lock); 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds return t; 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds 4723ac51e74SDarrick J. Wong /* 4733ac51e74SDarrick J. Wong * Get the geometry associated with a dm device 4743ac51e74SDarrick J. Wong */ 4753ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) 4763ac51e74SDarrick J. Wong { 4773ac51e74SDarrick J. Wong *geo = md->geometry; 4783ac51e74SDarrick J. Wong 4793ac51e74SDarrick J. Wong return 0; 4803ac51e74SDarrick J. Wong } 4813ac51e74SDarrick J. Wong 4823ac51e74SDarrick J. Wong /* 4833ac51e74SDarrick J. Wong * Set the geometry of a device. 4843ac51e74SDarrick J. Wong */ 4853ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) 4863ac51e74SDarrick J. Wong { 4873ac51e74SDarrick J. Wong sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; 4883ac51e74SDarrick J. Wong 4893ac51e74SDarrick J. Wong if (geo->start > sz) { 4903ac51e74SDarrick J. Wong DMWARN("Start sector is beyond the geometry limits."); 4913ac51e74SDarrick J. Wong return -EINVAL; 4923ac51e74SDarrick J. Wong } 4933ac51e74SDarrick J. Wong 4943ac51e74SDarrick J. Wong md->geometry = *geo; 4953ac51e74SDarrick J. Wong 4963ac51e74SDarrick J. Wong return 0; 4973ac51e74SDarrick J. Wong } 4983ac51e74SDarrick J. Wong 4991da177e4SLinus Torvalds /*----------------------------------------------------------------- 5001da177e4SLinus Torvalds * CRUD START: 5011da177e4SLinus Torvalds * A more elegant soln is in the works that uses the queue 5021da177e4SLinus Torvalds * merge fn, unfortunately there are a couple of changes to 5031da177e4SLinus Torvalds * the block layer that I want to make for this. So in the 5041da177e4SLinus Torvalds * interests of getting something for people to use I give 5051da177e4SLinus Torvalds * you this clearly demarcated crap. 5061da177e4SLinus Torvalds *---------------------------------------------------------------*/ 5071da177e4SLinus Torvalds 5082e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md) 5092e93ccc1SKiyoshi Ueda { 5102e93ccc1SKiyoshi Ueda return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 5112e93ccc1SKiyoshi Ueda } 5122e93ccc1SKiyoshi Ueda 5131da177e4SLinus Torvalds /* 5141da177e4SLinus Torvalds * Decrements the number of outstanding ios that a bio has been 5151da177e4SLinus Torvalds * cloned into, completing the original io if necc. 5161da177e4SLinus Torvalds */ 517858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error) 5181da177e4SLinus Torvalds { 5192e93ccc1SKiyoshi Ueda unsigned long flags; 520b35f8caaSMilan Broz int io_error; 521b35f8caaSMilan Broz struct bio *bio; 522b35f8caaSMilan Broz struct mapped_device *md = io->md; 5232e93ccc1SKiyoshi Ueda 5242e93ccc1SKiyoshi Ueda /* Push-back supersedes any I/O errors */ 525b35f8caaSMilan Broz if (error && !(io->error > 0 && __noflush_suspending(md))) 5261da177e4SLinus Torvalds io->error = error; 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds if (atomic_dec_and_test(&io->io_count)) { 5292e93ccc1SKiyoshi Ueda if (io->error == DM_ENDIO_REQUEUE) { 5302e93ccc1SKiyoshi Ueda /* 5312e93ccc1SKiyoshi Ueda * Target requested pushing back the I/O. 5322e93ccc1SKiyoshi Ueda * This must be handled before the sleeper on 5332e93ccc1SKiyoshi Ueda * suspend queue merges the pushback list. 5342e93ccc1SKiyoshi Ueda */ 535b35f8caaSMilan Broz spin_lock_irqsave(&md->pushback_lock, flags); 536b35f8caaSMilan Broz if (__noflush_suspending(md)) 537b35f8caaSMilan Broz bio_list_add(&md->pushback, io->bio); 5382e93ccc1SKiyoshi Ueda else 5392e93ccc1SKiyoshi Ueda /* noflush suspend was interrupted. */ 5402e93ccc1SKiyoshi Ueda io->error = -EIO; 541b35f8caaSMilan Broz spin_unlock_irqrestore(&md->pushback_lock, flags); 5422e93ccc1SKiyoshi Ueda } 5432e93ccc1SKiyoshi Ueda 544d221d2e7SMikulas Patocka end_io_acct(io); 5451da177e4SLinus Torvalds 546b35f8caaSMilan Broz io_error = io->error; 547b35f8caaSMilan Broz bio = io->bio; 5482056a782SJens Axboe 549b35f8caaSMilan Broz free_io(md, io); 550b35f8caaSMilan Broz 551b35f8caaSMilan Broz if (io_error != DM_ENDIO_REQUEUE) { 552b35f8caaSMilan Broz trace_block_bio_complete(md->queue, bio); 553b35f8caaSMilan Broz 554b35f8caaSMilan Broz bio_endio(bio, io_error); 5552e93ccc1SKiyoshi Ueda } 5561da177e4SLinus Torvalds } 5571da177e4SLinus Torvalds } 5581da177e4SLinus Torvalds 5596712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error) 5601da177e4SLinus Torvalds { 5611da177e4SLinus Torvalds int r = 0; 562028867acSAlasdair G Kergon struct dm_target_io *tio = bio->bi_private; 563b35f8caaSMilan Broz struct dm_io *io = tio->io; 5649faf400fSStefan Bader struct mapped_device *md = tio->io->md; 5651da177e4SLinus Torvalds dm_endio_fn endio = tio->ti->type->end_io; 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds if (!bio_flagged(bio, BIO_UPTODATE) && !error) 5681da177e4SLinus Torvalds error = -EIO; 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds if (endio) { 5711da177e4SLinus Torvalds r = endio(tio->ti, bio, error, &tio->info); 5722e93ccc1SKiyoshi Ueda if (r < 0 || r == DM_ENDIO_REQUEUE) 5732e93ccc1SKiyoshi Ueda /* 5742e93ccc1SKiyoshi Ueda * error and requeue request are handled 5752e93ccc1SKiyoshi Ueda * in dec_pending(). 5762e93ccc1SKiyoshi Ueda */ 5771da177e4SLinus Torvalds error = r; 57845cbcd79SKiyoshi Ueda else if (r == DM_ENDIO_INCOMPLETE) 57945cbcd79SKiyoshi Ueda /* The target will handle the io */ 5806712ecf8SNeilBrown return; 58145cbcd79SKiyoshi Ueda else if (r) { 58245cbcd79SKiyoshi Ueda DMWARN("unimplemented target endio return value: %d", r); 58345cbcd79SKiyoshi Ueda BUG(); 58445cbcd79SKiyoshi Ueda } 5851da177e4SLinus Torvalds } 5861da177e4SLinus Torvalds 5879faf400fSStefan Bader /* 5889faf400fSStefan Bader * Store md for cleanup instead of tio which is about to get freed. 5899faf400fSStefan Bader */ 5909faf400fSStefan Bader bio->bi_private = md->bs; 5919faf400fSStefan Bader 5929faf400fSStefan Bader free_tio(md, tio); 593b35f8caaSMilan Broz bio_put(bio); 594b35f8caaSMilan Broz dec_pending(io, error); 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds static sector_t max_io_len(struct mapped_device *md, 5981da177e4SLinus Torvalds sector_t sector, struct dm_target *ti) 5991da177e4SLinus Torvalds { 6001da177e4SLinus Torvalds sector_t offset = sector - ti->begin; 6011da177e4SLinus Torvalds sector_t len = ti->len - offset; 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds /* 6041da177e4SLinus Torvalds * Does the target need to split even further ? 6051da177e4SLinus Torvalds */ 6061da177e4SLinus Torvalds if (ti->split_io) { 6071da177e4SLinus Torvalds sector_t boundary; 6081da177e4SLinus Torvalds boundary = ((offset + ti->split_io) & ~(ti->split_io - 1)) 6091da177e4SLinus Torvalds - offset; 6101da177e4SLinus Torvalds if (len > boundary) 6111da177e4SLinus Torvalds len = boundary; 6121da177e4SLinus Torvalds } 6131da177e4SLinus Torvalds 6141da177e4SLinus Torvalds return len; 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds static void __map_bio(struct dm_target *ti, struct bio *clone, 618028867acSAlasdair G Kergon struct dm_target_io *tio) 6191da177e4SLinus Torvalds { 6201da177e4SLinus Torvalds int r; 6212056a782SJens Axboe sector_t sector; 6229faf400fSStefan Bader struct mapped_device *md; 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds /* 6251da177e4SLinus Torvalds * Sanity checks. 6261da177e4SLinus Torvalds */ 6271da177e4SLinus Torvalds BUG_ON(!clone->bi_size); 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds clone->bi_end_io = clone_endio; 6301da177e4SLinus Torvalds clone->bi_private = tio; 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds /* 6331da177e4SLinus Torvalds * Map the clone. If r == 0 we don't need to do 6341da177e4SLinus Torvalds * anything, the target has assumed ownership of 6351da177e4SLinus Torvalds * this io. 6361da177e4SLinus Torvalds */ 6371da177e4SLinus Torvalds atomic_inc(&tio->io->io_count); 6382056a782SJens Axboe sector = clone->bi_sector; 6391da177e4SLinus Torvalds r = ti->type->map(ti, clone, &tio->info); 64045cbcd79SKiyoshi Ueda if (r == DM_MAPIO_REMAPPED) { 6411da177e4SLinus Torvalds /* the bio has been remapped so dispatch it */ 6422056a782SJens Axboe 6435f3ea37cSArnaldo Carvalho de Melo trace_block_remap(bdev_get_queue(clone->bi_bdev), clone, 644c7149d6bSAlan D. Brunelle tio->io->bio->bi_bdev->bd_dev, 645c7149d6bSAlan D. Brunelle clone->bi_sector, sector); 6462056a782SJens Axboe 6471da177e4SLinus Torvalds generic_make_request(clone); 6482e93ccc1SKiyoshi Ueda } else if (r < 0 || r == DM_MAPIO_REQUEUE) { 6492e93ccc1SKiyoshi Ueda /* error the io and bail out, or requeue it if needed */ 6509faf400fSStefan Bader md = tio->io->md; 6519faf400fSStefan Bader dec_pending(tio->io, r); 6529faf400fSStefan Bader /* 6539faf400fSStefan Bader * Store bio_set for cleanup. 6549faf400fSStefan Bader */ 6559faf400fSStefan Bader clone->bi_private = md->bs; 6561da177e4SLinus Torvalds bio_put(clone); 6579faf400fSStefan Bader free_tio(md, tio); 65845cbcd79SKiyoshi Ueda } else if (r) { 65945cbcd79SKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 66045cbcd79SKiyoshi Ueda BUG(); 6611da177e4SLinus Torvalds } 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds struct clone_info { 6651da177e4SLinus Torvalds struct mapped_device *md; 6661da177e4SLinus Torvalds struct dm_table *map; 6671da177e4SLinus Torvalds struct bio *bio; 6681da177e4SLinus Torvalds struct dm_io *io; 6691da177e4SLinus Torvalds sector_t sector; 6701da177e4SLinus Torvalds sector_t sector_count; 6711da177e4SLinus Torvalds unsigned short idx; 6721da177e4SLinus Torvalds }; 6731da177e4SLinus Torvalds 6743676347aSPeter Osterlund static void dm_bio_destructor(struct bio *bio) 6753676347aSPeter Osterlund { 6769faf400fSStefan Bader struct bio_set *bs = bio->bi_private; 6779faf400fSStefan Bader 6789faf400fSStefan Bader bio_free(bio, bs); 6793676347aSPeter Osterlund } 6803676347aSPeter Osterlund 6811da177e4SLinus Torvalds /* 6821da177e4SLinus Torvalds * Creates a little bio that is just does part of a bvec. 6831da177e4SLinus Torvalds */ 6841da177e4SLinus Torvalds static struct bio *split_bvec(struct bio *bio, sector_t sector, 6851da177e4SLinus Torvalds unsigned short idx, unsigned int offset, 6869faf400fSStefan Bader unsigned int len, struct bio_set *bs) 6871da177e4SLinus Torvalds { 6881da177e4SLinus Torvalds struct bio *clone; 6891da177e4SLinus Torvalds struct bio_vec *bv = bio->bi_io_vec + idx; 6901da177e4SLinus Torvalds 6919faf400fSStefan Bader clone = bio_alloc_bioset(GFP_NOIO, 1, bs); 6923676347aSPeter Osterlund clone->bi_destructor = dm_bio_destructor; 6931da177e4SLinus Torvalds *clone->bi_io_vec = *bv; 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds clone->bi_sector = sector; 6961da177e4SLinus Torvalds clone->bi_bdev = bio->bi_bdev; 6971da177e4SLinus Torvalds clone->bi_rw = bio->bi_rw; 6981da177e4SLinus Torvalds clone->bi_vcnt = 1; 6991da177e4SLinus Torvalds clone->bi_size = to_bytes(len); 7001da177e4SLinus Torvalds clone->bi_io_vec->bv_offset = offset; 7011da177e4SLinus Torvalds clone->bi_io_vec->bv_len = clone->bi_size; 702f3e1d26eSMartin K. Petersen clone->bi_flags |= 1 << BIO_CLONED; 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds return clone; 7051da177e4SLinus Torvalds } 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds /* 7081da177e4SLinus Torvalds * Creates a bio that consists of range of complete bvecs. 7091da177e4SLinus Torvalds */ 7101da177e4SLinus Torvalds static struct bio *clone_bio(struct bio *bio, sector_t sector, 7111da177e4SLinus Torvalds unsigned short idx, unsigned short bv_count, 7129faf400fSStefan Bader unsigned int len, struct bio_set *bs) 7131da177e4SLinus Torvalds { 7141da177e4SLinus Torvalds struct bio *clone; 7151da177e4SLinus Torvalds 7169faf400fSStefan Bader clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs); 7179faf400fSStefan Bader __bio_clone(clone, bio); 7189faf400fSStefan Bader clone->bi_destructor = dm_bio_destructor; 7191da177e4SLinus Torvalds clone->bi_sector = sector; 7201da177e4SLinus Torvalds clone->bi_idx = idx; 7211da177e4SLinus Torvalds clone->bi_vcnt = idx + bv_count; 7221da177e4SLinus Torvalds clone->bi_size = to_bytes(len); 7231da177e4SLinus Torvalds clone->bi_flags &= ~(1 << BIO_SEG_VALID); 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds return clone; 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds 728512875bdSJun'ichi Nomura static int __clone_and_map(struct clone_info *ci) 7291da177e4SLinus Torvalds { 7301da177e4SLinus Torvalds struct bio *clone, *bio = ci->bio; 731512875bdSJun'ichi Nomura struct dm_target *ti; 732512875bdSJun'ichi Nomura sector_t len = 0, max; 733028867acSAlasdair G Kergon struct dm_target_io *tio; 7341da177e4SLinus Torvalds 735512875bdSJun'ichi Nomura ti = dm_table_find_target(ci->map, ci->sector); 736512875bdSJun'ichi Nomura if (!dm_target_is_valid(ti)) 737512875bdSJun'ichi Nomura return -EIO; 738512875bdSJun'ichi Nomura 739512875bdSJun'ichi Nomura max = max_io_len(ci->md, ci->sector, ti); 740512875bdSJun'ichi Nomura 7411da177e4SLinus Torvalds /* 7421da177e4SLinus Torvalds * Allocate a target io object. 7431da177e4SLinus Torvalds */ 7441da177e4SLinus Torvalds tio = alloc_tio(ci->md); 7451da177e4SLinus Torvalds tio->io = ci->io; 7461da177e4SLinus Torvalds tio->ti = ti; 7471da177e4SLinus Torvalds memset(&tio->info, 0, sizeof(tio->info)); 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds if (ci->sector_count <= max) { 7501da177e4SLinus Torvalds /* 7511da177e4SLinus Torvalds * Optimise for the simple case where we can do all of 7521da177e4SLinus Torvalds * the remaining io with a single clone. 7531da177e4SLinus Torvalds */ 7541da177e4SLinus Torvalds clone = clone_bio(bio, ci->sector, ci->idx, 7559faf400fSStefan Bader bio->bi_vcnt - ci->idx, ci->sector_count, 7569faf400fSStefan Bader ci->md->bs); 7571da177e4SLinus Torvalds __map_bio(ti, clone, tio); 7581da177e4SLinus Torvalds ci->sector_count = 0; 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) { 7611da177e4SLinus Torvalds /* 7621da177e4SLinus Torvalds * There are some bvecs that don't span targets. 7631da177e4SLinus Torvalds * Do as many of these as possible. 7641da177e4SLinus Torvalds */ 7651da177e4SLinus Torvalds int i; 7661da177e4SLinus Torvalds sector_t remaining = max; 7671da177e4SLinus Torvalds sector_t bv_len; 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) { 7701da177e4SLinus Torvalds bv_len = to_sector(bio->bi_io_vec[i].bv_len); 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds if (bv_len > remaining) 7731da177e4SLinus Torvalds break; 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds remaining -= bv_len; 7761da177e4SLinus Torvalds len += bv_len; 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds 7799faf400fSStefan Bader clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len, 7809faf400fSStefan Bader ci->md->bs); 7811da177e4SLinus Torvalds __map_bio(ti, clone, tio); 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds ci->sector += len; 7841da177e4SLinus Torvalds ci->sector_count -= len; 7851da177e4SLinus Torvalds ci->idx = i; 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds } else { 7881da177e4SLinus Torvalds /* 789d2044a94SAlasdair G Kergon * Handle a bvec that must be split between two or more targets. 7901da177e4SLinus Torvalds */ 7911da177e4SLinus Torvalds struct bio_vec *bv = bio->bi_io_vec + ci->idx; 792d2044a94SAlasdair G Kergon sector_t remaining = to_sector(bv->bv_len); 793d2044a94SAlasdair G Kergon unsigned int offset = 0; 7941da177e4SLinus Torvalds 795d2044a94SAlasdair G Kergon do { 796d2044a94SAlasdair G Kergon if (offset) { 7971da177e4SLinus Torvalds ti = dm_table_find_target(ci->map, ci->sector); 798512875bdSJun'ichi Nomura if (!dm_target_is_valid(ti)) 799512875bdSJun'ichi Nomura return -EIO; 800512875bdSJun'ichi Nomura 801d2044a94SAlasdair G Kergon max = max_io_len(ci->md, ci->sector, ti); 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds tio = alloc_tio(ci->md); 8041da177e4SLinus Torvalds tio->io = ci->io; 8051da177e4SLinus Torvalds tio->ti = ti; 8061da177e4SLinus Torvalds memset(&tio->info, 0, sizeof(tio->info)); 807d2044a94SAlasdair G Kergon } 808d2044a94SAlasdair G Kergon 809d2044a94SAlasdair G Kergon len = min(remaining, max); 810d2044a94SAlasdair G Kergon 811d2044a94SAlasdair G Kergon clone = split_bvec(bio, ci->sector, ci->idx, 8129faf400fSStefan Bader bv->bv_offset + offset, len, 8139faf400fSStefan Bader ci->md->bs); 814d2044a94SAlasdair G Kergon 8151da177e4SLinus Torvalds __map_bio(ti, clone, tio); 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds ci->sector += len; 8181da177e4SLinus Torvalds ci->sector_count -= len; 819d2044a94SAlasdair G Kergon offset += to_bytes(len); 820d2044a94SAlasdair G Kergon } while (remaining -= len); 821d2044a94SAlasdair G Kergon 8221da177e4SLinus Torvalds ci->idx++; 8231da177e4SLinus Torvalds } 824512875bdSJun'ichi Nomura 825512875bdSJun'ichi Nomura return 0; 8261da177e4SLinus Torvalds } 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds /* 829*8a53c28dSMikulas Patocka * Split the bio into several clones and submit it to targets. 8301da177e4SLinus Torvalds */ 831*8a53c28dSMikulas Patocka static int __split_and_process_bio(struct mapped_device *md, struct bio *bio) 8321da177e4SLinus Torvalds { 8331da177e4SLinus Torvalds struct clone_info ci; 834512875bdSJun'ichi Nomura int error = 0; 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds ci.map = dm_get_table(md); 8379e4e5f87SMilan Broz if (unlikely(!ci.map)) 8389e4e5f87SMilan Broz return -EIO; 839ab4c1424SAndi Kleen if (unlikely(bio_barrier(bio) && !dm_table_barrier_ok(ci.map))) { 840ab4c1424SAndi Kleen dm_table_put(ci.map); 841ab4c1424SAndi Kleen bio_endio(bio, -EOPNOTSUPP); 842ab4c1424SAndi Kleen return 0; 843ab4c1424SAndi Kleen } 8441da177e4SLinus Torvalds ci.md = md; 8451da177e4SLinus Torvalds ci.bio = bio; 8461da177e4SLinus Torvalds ci.io = alloc_io(md); 8471da177e4SLinus Torvalds ci.io->error = 0; 8481da177e4SLinus Torvalds atomic_set(&ci.io->io_count, 1); 8491da177e4SLinus Torvalds ci.io->bio = bio; 8501da177e4SLinus Torvalds ci.io->md = md; 8511da177e4SLinus Torvalds ci.sector = bio->bi_sector; 8521da177e4SLinus Torvalds ci.sector_count = bio_sectors(bio); 8531da177e4SLinus Torvalds ci.idx = bio->bi_idx; 8541da177e4SLinus Torvalds 8553eaf840eSJun'ichi "Nick" Nomura start_io_acct(ci.io); 856512875bdSJun'ichi Nomura while (ci.sector_count && !error) 857512875bdSJun'ichi Nomura error = __clone_and_map(&ci); 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds /* drop the extra reference count */ 860512875bdSJun'ichi Nomura dec_pending(ci.io, error); 8611da177e4SLinus Torvalds dm_table_put(ci.map); 8629e4e5f87SMilan Broz 8639e4e5f87SMilan Broz return 0; 8641da177e4SLinus Torvalds } 8651da177e4SLinus Torvalds /*----------------------------------------------------------------- 8661da177e4SLinus Torvalds * CRUD END 8671da177e4SLinus Torvalds *---------------------------------------------------------------*/ 8681da177e4SLinus Torvalds 869f6fccb12SMilan Broz static int dm_merge_bvec(struct request_queue *q, 870f6fccb12SMilan Broz struct bvec_merge_data *bvm, 871f6fccb12SMilan Broz struct bio_vec *biovec) 872f6fccb12SMilan Broz { 873f6fccb12SMilan Broz struct mapped_device *md = q->queuedata; 874f6fccb12SMilan Broz struct dm_table *map = dm_get_table(md); 875f6fccb12SMilan Broz struct dm_target *ti; 876f6fccb12SMilan Broz sector_t max_sectors; 8775037108aSMikulas Patocka int max_size = 0; 878f6fccb12SMilan Broz 879f6fccb12SMilan Broz if (unlikely(!map)) 8805037108aSMikulas Patocka goto out; 881f6fccb12SMilan Broz 882f6fccb12SMilan Broz ti = dm_table_find_target(map, bvm->bi_sector); 883b01cd5acSMikulas Patocka if (!dm_target_is_valid(ti)) 884b01cd5acSMikulas Patocka goto out_table; 885f6fccb12SMilan Broz 886f6fccb12SMilan Broz /* 887f6fccb12SMilan Broz * Find maximum amount of I/O that won't need splitting 888f6fccb12SMilan Broz */ 889f6fccb12SMilan Broz max_sectors = min(max_io_len(md, bvm->bi_sector, ti), 890f6fccb12SMilan Broz (sector_t) BIO_MAX_SECTORS); 891f6fccb12SMilan Broz max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size; 892f6fccb12SMilan Broz if (max_size < 0) 893f6fccb12SMilan Broz max_size = 0; 894f6fccb12SMilan Broz 895f6fccb12SMilan Broz /* 896f6fccb12SMilan Broz * merge_bvec_fn() returns number of bytes 897f6fccb12SMilan Broz * it can accept at this offset 898f6fccb12SMilan Broz * max is precomputed maximal io size 899f6fccb12SMilan Broz */ 900f6fccb12SMilan Broz if (max_size && ti->type->merge) 901f6fccb12SMilan Broz max_size = ti->type->merge(ti, bvm, biovec, max_size); 902f6fccb12SMilan Broz 903b01cd5acSMikulas Patocka out_table: 9045037108aSMikulas Patocka dm_table_put(map); 9055037108aSMikulas Patocka 9065037108aSMikulas Patocka out: 907f6fccb12SMilan Broz /* 908f6fccb12SMilan Broz * Always allow an entire first page 909f6fccb12SMilan Broz */ 910f6fccb12SMilan Broz if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT)) 911f6fccb12SMilan Broz max_size = biovec->bv_len; 912f6fccb12SMilan Broz 913f6fccb12SMilan Broz return max_size; 914f6fccb12SMilan Broz } 915f6fccb12SMilan Broz 9161da177e4SLinus Torvalds /* 9171da177e4SLinus Torvalds * The request function that just remaps the bio built up by 9181da177e4SLinus Torvalds * dm_merge_bvec. 9191da177e4SLinus Torvalds */ 920165125e1SJens Axboe static int dm_request(struct request_queue *q, struct bio *bio) 9211da177e4SLinus Torvalds { 9229e4e5f87SMilan Broz int r = -EIO; 92312f03a49SKevin Corry int rw = bio_data_dir(bio); 9241da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 925c9959059STejun Heo int cpu; 9261da177e4SLinus Torvalds 9272ca3310eSAlasdair G Kergon down_read(&md->io_lock); 9281da177e4SLinus Torvalds 929074a7acaSTejun Heo cpu = part_stat_lock(); 930074a7acaSTejun Heo part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]); 931074a7acaSTejun Heo part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio)); 932074a7acaSTejun Heo part_stat_unlock(); 93312f03a49SKevin Corry 9341da177e4SLinus Torvalds /* 9351da177e4SLinus Torvalds * If we're suspended we have to queue 9361da177e4SLinus Torvalds * this io for later. 9371da177e4SLinus Torvalds */ 9381da177e4SLinus Torvalds while (test_bit(DMF_BLOCK_IO, &md->flags)) { 9392ca3310eSAlasdair G Kergon up_read(&md->io_lock); 9401da177e4SLinus Torvalds 9419e4e5f87SMilan Broz if (bio_rw(bio) != READA) 9421da177e4SLinus Torvalds r = queue_io(md, bio); 9431da177e4SLinus Torvalds 9449e4e5f87SMilan Broz if (r <= 0) 9459e4e5f87SMilan Broz goto out_req; 9461da177e4SLinus Torvalds 9471da177e4SLinus Torvalds /* 9481da177e4SLinus Torvalds * We're in a while loop, because someone could suspend 9491da177e4SLinus Torvalds * before we get to the following read lock. 9501da177e4SLinus Torvalds */ 9512ca3310eSAlasdair G Kergon down_read(&md->io_lock); 9521da177e4SLinus Torvalds } 9531da177e4SLinus Torvalds 954*8a53c28dSMikulas Patocka r = __split_and_process_bio(md, bio); 9552ca3310eSAlasdair G Kergon up_read(&md->io_lock); 9569e4e5f87SMilan Broz 9579e4e5f87SMilan Broz out_req: 9589e4e5f87SMilan Broz if (r < 0) 9599e4e5f87SMilan Broz bio_io_error(bio); 9609e4e5f87SMilan Broz 9611da177e4SLinus Torvalds return 0; 9621da177e4SLinus Torvalds } 9631da177e4SLinus Torvalds 964165125e1SJens Axboe static void dm_unplug_all(struct request_queue *q) 9651da177e4SLinus Torvalds { 9661da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 9671da177e4SLinus Torvalds struct dm_table *map = dm_get_table(md); 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds if (map) { 9701da177e4SLinus Torvalds dm_table_unplug_all(map); 9711da177e4SLinus Torvalds dm_table_put(map); 9721da177e4SLinus Torvalds } 9731da177e4SLinus Torvalds } 9741da177e4SLinus Torvalds 9751da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits) 9761da177e4SLinus Torvalds { 9778a57dfc6SChandra Seetharaman int r = bdi_bits; 9788a57dfc6SChandra Seetharaman struct mapped_device *md = congested_data; 9798a57dfc6SChandra Seetharaman struct dm_table *map; 9801da177e4SLinus Torvalds 9818a57dfc6SChandra Seetharaman if (!test_bit(DMF_BLOCK_IO, &md->flags)) { 9828a57dfc6SChandra Seetharaman map = dm_get_table(md); 9838a57dfc6SChandra Seetharaman if (map) { 9841da177e4SLinus Torvalds r = dm_table_any_congested(map, bdi_bits); 9851da177e4SLinus Torvalds dm_table_put(map); 9868a57dfc6SChandra Seetharaman } 9878a57dfc6SChandra Seetharaman } 9888a57dfc6SChandra Seetharaman 9891da177e4SLinus Torvalds return r; 9901da177e4SLinus Torvalds } 9911da177e4SLinus Torvalds 9921da177e4SLinus Torvalds /*----------------------------------------------------------------- 9931da177e4SLinus Torvalds * An IDR is used to keep track of allocated minor numbers. 9941da177e4SLinus Torvalds *---------------------------------------------------------------*/ 9951da177e4SLinus Torvalds static DEFINE_IDR(_minor_idr); 9961da177e4SLinus Torvalds 9972b06cfffSAlasdair G Kergon static void free_minor(int minor) 9981da177e4SLinus Torvalds { 999f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 10001da177e4SLinus Torvalds idr_remove(&_minor_idr, minor); 1001f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds 10041da177e4SLinus Torvalds /* 10051da177e4SLinus Torvalds * See if the device with a specific minor # is free. 10061da177e4SLinus Torvalds */ 1007cf13ab8eSFrederik Deweerdt static int specific_minor(int minor) 10081da177e4SLinus Torvalds { 10091da177e4SLinus Torvalds int r, m; 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds if (minor >= (1 << MINORBITS)) 10121da177e4SLinus Torvalds return -EINVAL; 10131da177e4SLinus Torvalds 101462f75c2fSJeff Mahoney r = idr_pre_get(&_minor_idr, GFP_KERNEL); 101562f75c2fSJeff Mahoney if (!r) 101662f75c2fSJeff Mahoney return -ENOMEM; 101762f75c2fSJeff Mahoney 1018f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds if (idr_find(&_minor_idr, minor)) { 10211da177e4SLinus Torvalds r = -EBUSY; 10221da177e4SLinus Torvalds goto out; 10231da177e4SLinus Torvalds } 10241da177e4SLinus Torvalds 1025ba61fdd1SJeff Mahoney r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m); 102662f75c2fSJeff Mahoney if (r) 10271da177e4SLinus Torvalds goto out; 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds if (m != minor) { 10301da177e4SLinus Torvalds idr_remove(&_minor_idr, m); 10311da177e4SLinus Torvalds r = -EBUSY; 10321da177e4SLinus Torvalds goto out; 10331da177e4SLinus Torvalds } 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds out: 1036f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 10371da177e4SLinus Torvalds return r; 10381da177e4SLinus Torvalds } 10391da177e4SLinus Torvalds 1040cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor) 10411da177e4SLinus Torvalds { 10422b06cfffSAlasdair G Kergon int r, m; 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds r = idr_pre_get(&_minor_idr, GFP_KERNEL); 104562f75c2fSJeff Mahoney if (!r) 104662f75c2fSJeff Mahoney return -ENOMEM; 104762f75c2fSJeff Mahoney 1048f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 10491da177e4SLinus Torvalds 1050ba61fdd1SJeff Mahoney r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m); 1051cf13ab8eSFrederik Deweerdt if (r) 10521da177e4SLinus Torvalds goto out; 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds if (m >= (1 << MINORBITS)) { 10551da177e4SLinus Torvalds idr_remove(&_minor_idr, m); 10561da177e4SLinus Torvalds r = -ENOSPC; 10571da177e4SLinus Torvalds goto out; 10581da177e4SLinus Torvalds } 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds *minor = m; 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds out: 1063f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 10641da177e4SLinus Torvalds return r; 10651da177e4SLinus Torvalds } 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops; 10681da177e4SLinus Torvalds 106953d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work); 107053d5914fSMikulas Patocka 10711da177e4SLinus Torvalds /* 10721da177e4SLinus Torvalds * Allocate and initialise a blank device with a given minor. 10731da177e4SLinus Torvalds */ 10742b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor) 10751da177e4SLinus Torvalds { 10761da177e4SLinus Torvalds int r; 1077cf13ab8eSFrederik Deweerdt struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL); 1078ba61fdd1SJeff Mahoney void *old_md; 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds if (!md) { 10811da177e4SLinus Torvalds DMWARN("unable to allocate device, out of memory."); 10821da177e4SLinus Torvalds return NULL; 10831da177e4SLinus Torvalds } 10841da177e4SLinus Torvalds 108510da4f79SJeff Mahoney if (!try_module_get(THIS_MODULE)) 10866ed7ade8SMilan Broz goto bad_module_get; 108710da4f79SJeff Mahoney 10881da177e4SLinus Torvalds /* get a minor number for the dev */ 10892b06cfffSAlasdair G Kergon if (minor == DM_ANY_MINOR) 1090cf13ab8eSFrederik Deweerdt r = next_free_minor(&minor); 10912b06cfffSAlasdair G Kergon else 1092cf13ab8eSFrederik Deweerdt r = specific_minor(minor); 10931da177e4SLinus Torvalds if (r < 0) 10946ed7ade8SMilan Broz goto bad_minor; 10951da177e4SLinus Torvalds 10962ca3310eSAlasdair G Kergon init_rwsem(&md->io_lock); 1097e61290a4SDaniel Walker mutex_init(&md->suspend_lock); 10982e93ccc1SKiyoshi Ueda spin_lock_init(&md->pushback_lock); 10991da177e4SLinus Torvalds rwlock_init(&md->map_lock); 11001da177e4SLinus Torvalds atomic_set(&md->holders, 1); 11015c6bd75dSAlasdair G Kergon atomic_set(&md->open_count, 0); 11021da177e4SLinus Torvalds atomic_set(&md->event_nr, 0); 11037a8c3d3bSMike Anderson atomic_set(&md->uevent_seq, 0); 11047a8c3d3bSMike Anderson INIT_LIST_HEAD(&md->uevent_list); 11057a8c3d3bSMike Anderson spin_lock_init(&md->uevent_lock); 11061da177e4SLinus Torvalds 11071da177e4SLinus Torvalds md->queue = blk_alloc_queue(GFP_KERNEL); 11081da177e4SLinus Torvalds if (!md->queue) 11096ed7ade8SMilan Broz goto bad_queue; 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds md->queue->queuedata = md; 11121da177e4SLinus Torvalds md->queue->backing_dev_info.congested_fn = dm_any_congested; 11131da177e4SLinus Torvalds md->queue->backing_dev_info.congested_data = md; 11141da177e4SLinus Torvalds blk_queue_make_request(md->queue, dm_request); 1115daef265fSJens Axboe blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY); 11161da177e4SLinus Torvalds md->queue->unplug_fn = dm_unplug_all; 1117f6fccb12SMilan Broz blk_queue_merge_bvec(md->queue, dm_merge_bvec); 11181da177e4SLinus Torvalds 111993d2341cSMatthew Dobson md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache); 11201da177e4SLinus Torvalds if (!md->io_pool) 11216ed7ade8SMilan Broz goto bad_io_pool; 11221da177e4SLinus Torvalds 112393d2341cSMatthew Dobson md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache); 11241da177e4SLinus Torvalds if (!md->tio_pool) 11256ed7ade8SMilan Broz goto bad_tio_pool; 11261da177e4SLinus Torvalds 1127bb799ca0SJens Axboe md->bs = bioset_create(16, 0); 11289faf400fSStefan Bader if (!md->bs) 11299faf400fSStefan Bader goto bad_no_bioset; 11309faf400fSStefan Bader 11311da177e4SLinus Torvalds md->disk = alloc_disk(1); 11321da177e4SLinus Torvalds if (!md->disk) 11336ed7ade8SMilan Broz goto bad_disk; 11341da177e4SLinus Torvalds 1135f0b04115SJeff Mahoney atomic_set(&md->pending, 0); 1136f0b04115SJeff Mahoney init_waitqueue_head(&md->wait); 113753d5914fSMikulas Patocka INIT_WORK(&md->work, dm_wq_work); 1138f0b04115SJeff Mahoney init_waitqueue_head(&md->eventq); 1139f0b04115SJeff Mahoney 11401da177e4SLinus Torvalds md->disk->major = _major; 11411da177e4SLinus Torvalds md->disk->first_minor = minor; 11421da177e4SLinus Torvalds md->disk->fops = &dm_blk_dops; 11431da177e4SLinus Torvalds md->disk->queue = md->queue; 11441da177e4SLinus Torvalds md->disk->private_data = md; 11451da177e4SLinus Torvalds sprintf(md->disk->disk_name, "dm-%d", minor); 11461da177e4SLinus Torvalds add_disk(md->disk); 11477e51f257SMike Anderson format_dev_t(md->name, MKDEV(_major, minor)); 11481da177e4SLinus Torvalds 1149304f3f6aSMilan Broz md->wq = create_singlethread_workqueue("kdmflush"); 1150304f3f6aSMilan Broz if (!md->wq) 1151304f3f6aSMilan Broz goto bad_thread; 1152304f3f6aSMilan Broz 1153ba61fdd1SJeff Mahoney /* Populate the mapping, nobody knows we exist yet */ 1154f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 1155ba61fdd1SJeff Mahoney old_md = idr_replace(&_minor_idr, md, minor); 1156f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1157ba61fdd1SJeff Mahoney 1158ba61fdd1SJeff Mahoney BUG_ON(old_md != MINOR_ALLOCED); 1159ba61fdd1SJeff Mahoney 11601da177e4SLinus Torvalds return md; 11611da177e4SLinus Torvalds 1162304f3f6aSMilan Broz bad_thread: 1163304f3f6aSMilan Broz put_disk(md->disk); 11646ed7ade8SMilan Broz bad_disk: 11659faf400fSStefan Bader bioset_free(md->bs); 11669faf400fSStefan Bader bad_no_bioset: 11671da177e4SLinus Torvalds mempool_destroy(md->tio_pool); 11686ed7ade8SMilan Broz bad_tio_pool: 11691da177e4SLinus Torvalds mempool_destroy(md->io_pool); 11706ed7ade8SMilan Broz bad_io_pool: 11711312f40eSAl Viro blk_cleanup_queue(md->queue); 11726ed7ade8SMilan Broz bad_queue: 11731da177e4SLinus Torvalds free_minor(minor); 11746ed7ade8SMilan Broz bad_minor: 117510da4f79SJeff Mahoney module_put(THIS_MODULE); 11766ed7ade8SMilan Broz bad_module_get: 11771da177e4SLinus Torvalds kfree(md); 11781da177e4SLinus Torvalds return NULL; 11791da177e4SLinus Torvalds } 11801da177e4SLinus Torvalds 1181ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md); 1182ae9da83fSJun'ichi Nomura 11831da177e4SLinus Torvalds static void free_dev(struct mapped_device *md) 11841da177e4SLinus Torvalds { 1185f331c029STejun Heo int minor = MINOR(disk_devt(md->disk)); 118663d94e48SJun'ichi Nomura 1187d9dde59bSJun'ichi Nomura if (md->suspended_bdev) { 1188ae9da83fSJun'ichi Nomura unlock_fs(md); 1189d9dde59bSJun'ichi Nomura bdput(md->suspended_bdev); 1190d9dde59bSJun'ichi Nomura } 1191304f3f6aSMilan Broz destroy_workqueue(md->wq); 11921da177e4SLinus Torvalds mempool_destroy(md->tio_pool); 11931da177e4SLinus Torvalds mempool_destroy(md->io_pool); 11949faf400fSStefan Bader bioset_free(md->bs); 11951da177e4SLinus Torvalds del_gendisk(md->disk); 119663d94e48SJun'ichi Nomura free_minor(minor); 1197fba9f90eSJeff Mahoney 1198fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 1199fba9f90eSJeff Mahoney md->disk->private_data = NULL; 1200fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 1201fba9f90eSJeff Mahoney 12021da177e4SLinus Torvalds put_disk(md->disk); 12031312f40eSAl Viro blk_cleanup_queue(md->queue); 120410da4f79SJeff Mahoney module_put(THIS_MODULE); 12051da177e4SLinus Torvalds kfree(md); 12061da177e4SLinus Torvalds } 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds /* 12091da177e4SLinus Torvalds * Bind a table to the device. 12101da177e4SLinus Torvalds */ 12111da177e4SLinus Torvalds static void event_callback(void *context) 12121da177e4SLinus Torvalds { 12137a8c3d3bSMike Anderson unsigned long flags; 12147a8c3d3bSMike Anderson LIST_HEAD(uevents); 12151da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) context; 12161da177e4SLinus Torvalds 12177a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 12187a8c3d3bSMike Anderson list_splice_init(&md->uevent_list, &uevents); 12197a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 12207a8c3d3bSMike Anderson 1221ed9e1982STejun Heo dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); 12227a8c3d3bSMike Anderson 12231da177e4SLinus Torvalds atomic_inc(&md->event_nr); 12241da177e4SLinus Torvalds wake_up(&md->eventq); 12251da177e4SLinus Torvalds } 12261da177e4SLinus Torvalds 12274e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size) 12281da177e4SLinus Torvalds { 12294e90188bSAlasdair G Kergon set_capacity(md->disk, size); 12301da177e4SLinus Torvalds 12311b1dcc1bSJes Sorensen mutex_lock(&md->suspended_bdev->bd_inode->i_mutex); 1232e39e2e95SAlasdair G Kergon i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT); 12331b1dcc1bSJes Sorensen mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex); 12341da177e4SLinus Torvalds } 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds static int __bind(struct mapped_device *md, struct dm_table *t) 12371da177e4SLinus Torvalds { 1238165125e1SJens Axboe struct request_queue *q = md->queue; 12391da177e4SLinus Torvalds sector_t size; 12401da177e4SLinus Torvalds 12411da177e4SLinus Torvalds size = dm_table_get_size(t); 12423ac51e74SDarrick J. Wong 12433ac51e74SDarrick J. Wong /* 12443ac51e74SDarrick J. Wong * Wipe any geometry if the size of the table changed. 12453ac51e74SDarrick J. Wong */ 12463ac51e74SDarrick J. Wong if (size != get_capacity(md->disk)) 12473ac51e74SDarrick J. Wong memset(&md->geometry, 0, sizeof(md->geometry)); 12483ac51e74SDarrick J. Wong 1249bfa152faSJun'ichi Nomura if (md->suspended_bdev) 12504e90188bSAlasdair G Kergon __set_size(md, size); 12511da177e4SLinus Torvalds 1252d5816876SMikulas Patocka if (!size) { 1253d5816876SMikulas Patocka dm_table_destroy(t); 1254d5816876SMikulas Patocka return 0; 1255d5816876SMikulas Patocka } 1256d5816876SMikulas Patocka 1257cf222b37SAlasdair G Kergon dm_table_event_callback(t, event_callback, md); 12582ca3310eSAlasdair G Kergon 12592ca3310eSAlasdair G Kergon write_lock(&md->map_lock); 12602ca3310eSAlasdair G Kergon md->map = t; 12611da177e4SLinus Torvalds dm_table_set_restrictions(t, q); 12622ca3310eSAlasdair G Kergon write_unlock(&md->map_lock); 12632ca3310eSAlasdair G Kergon 12641da177e4SLinus Torvalds return 0; 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds static void __unbind(struct mapped_device *md) 12681da177e4SLinus Torvalds { 12691da177e4SLinus Torvalds struct dm_table *map = md->map; 12701da177e4SLinus Torvalds 12711da177e4SLinus Torvalds if (!map) 12721da177e4SLinus Torvalds return; 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds dm_table_event_callback(map, NULL, NULL); 12751da177e4SLinus Torvalds write_lock(&md->map_lock); 12761da177e4SLinus Torvalds md->map = NULL; 12771da177e4SLinus Torvalds write_unlock(&md->map_lock); 1278d5816876SMikulas Patocka dm_table_destroy(map); 12791da177e4SLinus Torvalds } 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds /* 12821da177e4SLinus Torvalds * Constructor for a new device. 12831da177e4SLinus Torvalds */ 12842b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result) 12851da177e4SLinus Torvalds { 12861da177e4SLinus Torvalds struct mapped_device *md; 12871da177e4SLinus Torvalds 12882b06cfffSAlasdair G Kergon md = alloc_dev(minor); 12891da177e4SLinus Torvalds if (!md) 12901da177e4SLinus Torvalds return -ENXIO; 12911da177e4SLinus Torvalds 1292784aae73SMilan Broz dm_sysfs_init(md); 1293784aae73SMilan Broz 12941da177e4SLinus Torvalds *result = md; 12951da177e4SLinus Torvalds return 0; 12961da177e4SLinus Torvalds } 12971da177e4SLinus Torvalds 1298637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev) 12991da177e4SLinus Torvalds { 13001da177e4SLinus Torvalds struct mapped_device *md; 13011da177e4SLinus Torvalds unsigned minor = MINOR(dev); 13021da177e4SLinus Torvalds 13031da177e4SLinus Torvalds if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) 13041da177e4SLinus Torvalds return NULL; 13051da177e4SLinus Torvalds 1306f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 13071da177e4SLinus Torvalds 13081da177e4SLinus Torvalds md = idr_find(&_minor_idr, minor); 1309fba9f90eSJeff Mahoney if (md && (md == MINOR_ALLOCED || 1310f331c029STejun Heo (MINOR(disk_devt(dm_disk(md))) != minor) || 1311fba9f90eSJeff Mahoney test_bit(DMF_FREEING, &md->flags))) { 1312637842cfSDavid Teigland md = NULL; 1313fba9f90eSJeff Mahoney goto out; 1314fba9f90eSJeff Mahoney } 13151da177e4SLinus Torvalds 1316fba9f90eSJeff Mahoney out: 1317f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 13181da177e4SLinus Torvalds 1319637842cfSDavid Teigland return md; 1320637842cfSDavid Teigland } 1321637842cfSDavid Teigland 1322d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev) 1323d229a958SDavid Teigland { 1324d229a958SDavid Teigland struct mapped_device *md = dm_find_md(dev); 1325d229a958SDavid Teigland 1326d229a958SDavid Teigland if (md) 1327d229a958SDavid Teigland dm_get(md); 1328d229a958SDavid Teigland 1329d229a958SDavid Teigland return md; 1330d229a958SDavid Teigland } 1331d229a958SDavid Teigland 13329ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md) 1333637842cfSDavid Teigland { 13349ade92a9SAlasdair G Kergon return md->interface_ptr; 13351da177e4SLinus Torvalds } 13361da177e4SLinus Torvalds 13371da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr) 13381da177e4SLinus Torvalds { 13391da177e4SLinus Torvalds md->interface_ptr = ptr; 13401da177e4SLinus Torvalds } 13411da177e4SLinus Torvalds 13421da177e4SLinus Torvalds void dm_get(struct mapped_device *md) 13431da177e4SLinus Torvalds { 13441da177e4SLinus Torvalds atomic_inc(&md->holders); 13451da177e4SLinus Torvalds } 13461da177e4SLinus Torvalds 134772d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md) 134872d94861SAlasdair G Kergon { 134972d94861SAlasdair G Kergon return md->name; 135072d94861SAlasdair G Kergon } 135172d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name); 135272d94861SAlasdair G Kergon 13531da177e4SLinus Torvalds void dm_put(struct mapped_device *md) 13541da177e4SLinus Torvalds { 13551134e5aeSMike Anderson struct dm_table *map; 13561da177e4SLinus Torvalds 1357fba9f90eSJeff Mahoney BUG_ON(test_bit(DMF_FREEING, &md->flags)); 1358fba9f90eSJeff Mahoney 1359f32c10b0SJeff Mahoney if (atomic_dec_and_lock(&md->holders, &_minor_lock)) { 13601134e5aeSMike Anderson map = dm_get_table(md); 1361f331c029STejun Heo idr_replace(&_minor_idr, MINOR_ALLOCED, 1362f331c029STejun Heo MINOR(disk_devt(dm_disk(md)))); 1363fba9f90eSJeff Mahoney set_bit(DMF_FREEING, &md->flags); 1364f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1365cf222b37SAlasdair G Kergon if (!dm_suspended(md)) { 13661da177e4SLinus Torvalds dm_table_presuspend_targets(map); 13671da177e4SLinus Torvalds dm_table_postsuspend_targets(map); 13681da177e4SLinus Torvalds } 1369784aae73SMilan Broz dm_sysfs_exit(md); 13701134e5aeSMike Anderson dm_table_put(map); 1371a1b51e98SMikulas Patocka __unbind(md); 13721da177e4SLinus Torvalds free_dev(md); 13731da177e4SLinus Torvalds } 13741da177e4SLinus Torvalds } 137579eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put); 13761da177e4SLinus Torvalds 137746125c1cSMilan Broz static int dm_wait_for_completion(struct mapped_device *md) 137846125c1cSMilan Broz { 137946125c1cSMilan Broz int r = 0; 138046125c1cSMilan Broz 138146125c1cSMilan Broz while (1) { 138246125c1cSMilan Broz set_current_state(TASK_INTERRUPTIBLE); 138346125c1cSMilan Broz 138446125c1cSMilan Broz smp_mb(); 138546125c1cSMilan Broz if (!atomic_read(&md->pending)) 138646125c1cSMilan Broz break; 138746125c1cSMilan Broz 138846125c1cSMilan Broz if (signal_pending(current)) { 138946125c1cSMilan Broz r = -EINTR; 139046125c1cSMilan Broz break; 139146125c1cSMilan Broz } 139246125c1cSMilan Broz 139346125c1cSMilan Broz io_schedule(); 139446125c1cSMilan Broz } 139546125c1cSMilan Broz set_current_state(TASK_RUNNING); 139646125c1cSMilan Broz 139746125c1cSMilan Broz return r; 139846125c1cSMilan Broz } 139946125c1cSMilan Broz 14001da177e4SLinus Torvalds /* 14011da177e4SLinus Torvalds * Process the deferred bios 14021da177e4SLinus Torvalds */ 14036d6f10dfSMilan Broz static void __flush_deferred_io(struct mapped_device *md) 14041da177e4SLinus Torvalds { 14056d6f10dfSMilan Broz struct bio *c; 14061da177e4SLinus Torvalds 14076d6f10dfSMilan Broz while ((c = bio_list_pop(&md->deferred))) { 1408*8a53c28dSMikulas Patocka if (__split_and_process_bio(md, c)) 14099e4e5f87SMilan Broz bio_io_error(c); 14101da177e4SLinus Torvalds } 141173d410c0SMilan Broz 141273d410c0SMilan Broz clear_bit(DMF_BLOCK_IO, &md->flags); 14131da177e4SLinus Torvalds } 14141da177e4SLinus Torvalds 14156d6f10dfSMilan Broz static void __merge_pushback_list(struct mapped_device *md) 14166d6f10dfSMilan Broz { 14176d6f10dfSMilan Broz unsigned long flags; 14186d6f10dfSMilan Broz 14196d6f10dfSMilan Broz spin_lock_irqsave(&md->pushback_lock, flags); 14206d6f10dfSMilan Broz clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 14216d6f10dfSMilan Broz bio_list_merge_head(&md->deferred, &md->pushback); 14226d6f10dfSMilan Broz bio_list_init(&md->pushback); 14236d6f10dfSMilan Broz spin_unlock_irqrestore(&md->pushback_lock, flags); 14246d6f10dfSMilan Broz } 14256d6f10dfSMilan Broz 1426304f3f6aSMilan Broz static void dm_wq_work(struct work_struct *work) 1427304f3f6aSMilan Broz { 142853d5914fSMikulas Patocka struct mapped_device *md = container_of(work, struct mapped_device, 142953d5914fSMikulas Patocka work); 1430304f3f6aSMilan Broz 1431304f3f6aSMilan Broz down_write(&md->io_lock); 1432304f3f6aSMilan Broz __flush_deferred_io(md); 1433304f3f6aSMilan Broz up_write(&md->io_lock); 1434304f3f6aSMilan Broz } 1435304f3f6aSMilan Broz 14369a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md) 1437304f3f6aSMilan Broz { 143853d5914fSMikulas Patocka queue_work(md->wq, &md->work); 1439304f3f6aSMilan Broz flush_workqueue(md->wq); 1440304f3f6aSMilan Broz } 1441304f3f6aSMilan Broz 14421da177e4SLinus Torvalds /* 14431da177e4SLinus Torvalds * Swap in a new table (destroying old one). 14441da177e4SLinus Torvalds */ 14451da177e4SLinus Torvalds int dm_swap_table(struct mapped_device *md, struct dm_table *table) 14461da177e4SLinus Torvalds { 144793c534aeSAlasdair G Kergon int r = -EINVAL; 14481da177e4SLinus Torvalds 1449e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 14501da177e4SLinus Torvalds 14511da177e4SLinus Torvalds /* device must be suspended */ 1452cf222b37SAlasdair G Kergon if (!dm_suspended(md)) 145393c534aeSAlasdair G Kergon goto out; 14541da177e4SLinus Torvalds 1455bfa152faSJun'ichi Nomura /* without bdev, the device size cannot be changed */ 1456bfa152faSJun'ichi Nomura if (!md->suspended_bdev) 1457bfa152faSJun'ichi Nomura if (get_capacity(md->disk) != dm_table_get_size(table)) 1458bfa152faSJun'ichi Nomura goto out; 1459bfa152faSJun'ichi Nomura 14601da177e4SLinus Torvalds __unbind(md); 14611da177e4SLinus Torvalds r = __bind(md, table); 14621da177e4SLinus Torvalds 146393c534aeSAlasdair G Kergon out: 1464e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 146593c534aeSAlasdair G Kergon return r; 14661da177e4SLinus Torvalds } 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds /* 14691da177e4SLinus Torvalds * Functions to lock and unlock any filesystem running on the 14701da177e4SLinus Torvalds * device. 14711da177e4SLinus Torvalds */ 14722ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md) 14731da177e4SLinus Torvalds { 1474e39e2e95SAlasdair G Kergon int r; 14751da177e4SLinus Torvalds 14761da177e4SLinus Torvalds WARN_ON(md->frozen_sb); 1477dfbe03f6SAlasdair G Kergon 1478e39e2e95SAlasdair G Kergon md->frozen_sb = freeze_bdev(md->suspended_bdev); 1479dfbe03f6SAlasdair G Kergon if (IS_ERR(md->frozen_sb)) { 1480cf222b37SAlasdair G Kergon r = PTR_ERR(md->frozen_sb); 1481e39e2e95SAlasdair G Kergon md->frozen_sb = NULL; 1482e39e2e95SAlasdair G Kergon return r; 1483dfbe03f6SAlasdair G Kergon } 1484dfbe03f6SAlasdair G Kergon 1485aa8d7c2fSAlasdair G Kergon set_bit(DMF_FROZEN, &md->flags); 1486aa8d7c2fSAlasdair G Kergon 14871da177e4SLinus Torvalds /* don't bdput right now, we don't want the bdev 1488e39e2e95SAlasdair G Kergon * to go away while it is locked. 14891da177e4SLinus Torvalds */ 14901da177e4SLinus Torvalds return 0; 14911da177e4SLinus Torvalds } 14921da177e4SLinus Torvalds 14932ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md) 14941da177e4SLinus Torvalds { 1495aa8d7c2fSAlasdair G Kergon if (!test_bit(DMF_FROZEN, &md->flags)) 1496aa8d7c2fSAlasdair G Kergon return; 1497aa8d7c2fSAlasdair G Kergon 1498e39e2e95SAlasdair G Kergon thaw_bdev(md->suspended_bdev, md->frozen_sb); 14991da177e4SLinus Torvalds md->frozen_sb = NULL; 1500aa8d7c2fSAlasdair G Kergon clear_bit(DMF_FROZEN, &md->flags); 15011da177e4SLinus Torvalds } 15021da177e4SLinus Torvalds 15031da177e4SLinus Torvalds /* 15041da177e4SLinus Torvalds * We need to be able to change a mapping table under a mounted 15051da177e4SLinus Torvalds * filesystem. For example we might want to move some data in 15061da177e4SLinus Torvalds * the background. Before the table can be swapped with 15071da177e4SLinus Torvalds * dm_bind_table, dm_suspend must be called to flush any in 15081da177e4SLinus Torvalds * flight bios and ensure that any further io gets deferred. 15091da177e4SLinus Torvalds */ 1510a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags) 15111da177e4SLinus Torvalds { 15122ca3310eSAlasdair G Kergon struct dm_table *map = NULL; 15131da177e4SLinus Torvalds DECLARE_WAITQUEUE(wait, current); 151446125c1cSMilan Broz int r = 0; 1515a3d77d35SKiyoshi Ueda int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0; 15162e93ccc1SKiyoshi Ueda int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0; 15171da177e4SLinus Torvalds 1518e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 15192ca3310eSAlasdair G Kergon 152073d410c0SMilan Broz if (dm_suspended(md)) { 152173d410c0SMilan Broz r = -EINVAL; 1522d287483dSAlasdair G Kergon goto out_unlock; 152373d410c0SMilan Broz } 15241da177e4SLinus Torvalds 15251da177e4SLinus Torvalds map = dm_get_table(md); 1526cf222b37SAlasdair G Kergon 15272e93ccc1SKiyoshi Ueda /* 15282e93ccc1SKiyoshi Ueda * DMF_NOFLUSH_SUSPENDING must be set before presuspend. 15292e93ccc1SKiyoshi Ueda * This flag is cleared before dm_suspend returns. 15302e93ccc1SKiyoshi Ueda */ 15312e93ccc1SKiyoshi Ueda if (noflush) 15322e93ccc1SKiyoshi Ueda set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 15332e93ccc1SKiyoshi Ueda 1534436d4108SAlasdair G Kergon /* This does not get reverted if there's an error later. */ 15351da177e4SLinus Torvalds dm_table_presuspend_targets(map); 15361da177e4SLinus Torvalds 1537bfa152faSJun'ichi Nomura /* bdget() can stall if the pending I/Os are not flushed */ 1538bfa152faSJun'ichi Nomura if (!noflush) { 1539e39e2e95SAlasdair G Kergon md->suspended_bdev = bdget_disk(md->disk, 0); 1540e39e2e95SAlasdair G Kergon if (!md->suspended_bdev) { 1541e39e2e95SAlasdair G Kergon DMWARN("bdget failed in dm_suspend"); 1542e39e2e95SAlasdair G Kergon r = -ENOMEM; 1543f431d966SKiyoshi Ueda goto out; 1544e39e2e95SAlasdair G Kergon } 1545e39e2e95SAlasdair G Kergon 15462e93ccc1SKiyoshi Ueda /* 15476d6f10dfSMilan Broz * Flush I/O to the device. noflush supersedes do_lockfs, 15486d6f10dfSMilan Broz * because lock_fs() needs to flush I/Os. 15492e93ccc1SKiyoshi Ueda */ 15506d6f10dfSMilan Broz if (do_lockfs) { 15512ca3310eSAlasdair G Kergon r = lock_fs(md); 15522ca3310eSAlasdair G Kergon if (r) 15532ca3310eSAlasdair G Kergon goto out; 1554aa8d7c2fSAlasdair G Kergon } 15556d6f10dfSMilan Broz } 15561da177e4SLinus Torvalds 15571da177e4SLinus Torvalds /* 1558354e0071SAlasdair G Kergon * First we set the BLOCK_IO flag so no more ios will be mapped. 15591da177e4SLinus Torvalds */ 15602ca3310eSAlasdair G Kergon down_write(&md->io_lock); 15612ca3310eSAlasdair G Kergon set_bit(DMF_BLOCK_IO, &md->flags); 15621da177e4SLinus Torvalds 15631da177e4SLinus Torvalds add_wait_queue(&md->wait, &wait); 15642ca3310eSAlasdair G Kergon up_write(&md->io_lock); 15651da177e4SLinus Torvalds 15661da177e4SLinus Torvalds /* unplug */ 15672ca3310eSAlasdair G Kergon if (map) 15681da177e4SLinus Torvalds dm_table_unplug_all(map); 15691da177e4SLinus Torvalds 15701da177e4SLinus Torvalds /* 157146125c1cSMilan Broz * Wait for the already-mapped ios to complete. 15721da177e4SLinus Torvalds */ 157346125c1cSMilan Broz r = dm_wait_for_completion(md); 15741da177e4SLinus Torvalds 15752ca3310eSAlasdair G Kergon down_write(&md->io_lock); 15761da177e4SLinus Torvalds remove_wait_queue(&md->wait, &wait); 15771da177e4SLinus Torvalds 15786d6f10dfSMilan Broz if (noflush) 15796d6f10dfSMilan Broz __merge_pushback_list(md); 158094d6351eSMilan Broz up_write(&md->io_lock); 15812e93ccc1SKiyoshi Ueda 15821da177e4SLinus Torvalds /* were we interrupted ? */ 158346125c1cSMilan Broz if (r < 0) { 15849a1fb464SMikulas Patocka dm_queue_flush(md); 158573d410c0SMilan Broz 15862ca3310eSAlasdair G Kergon unlock_fs(md); 15872e93ccc1SKiyoshi Ueda goto out; /* pushback list is already flushed, so skip flush */ 15882ca3310eSAlasdair G Kergon } 15892ca3310eSAlasdair G Kergon 15902ca3310eSAlasdair G Kergon dm_table_postsuspend_targets(map); 15911da177e4SLinus Torvalds 15921da177e4SLinus Torvalds set_bit(DMF_SUSPENDED, &md->flags); 15931da177e4SLinus Torvalds 15942ca3310eSAlasdair G Kergon out: 1595e39e2e95SAlasdair G Kergon if (r && md->suspended_bdev) { 1596e39e2e95SAlasdair G Kergon bdput(md->suspended_bdev); 1597e39e2e95SAlasdair G Kergon md->suspended_bdev = NULL; 1598e39e2e95SAlasdair G Kergon } 1599e39e2e95SAlasdair G Kergon 16001da177e4SLinus Torvalds dm_table_put(map); 1601d287483dSAlasdair G Kergon 1602d287483dSAlasdair G Kergon out_unlock: 1603e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 1604cf222b37SAlasdair G Kergon return r; 16051da177e4SLinus Torvalds } 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds int dm_resume(struct mapped_device *md) 16081da177e4SLinus Torvalds { 1609cf222b37SAlasdair G Kergon int r = -EINVAL; 1610cf222b37SAlasdair G Kergon struct dm_table *map = NULL; 16111da177e4SLinus Torvalds 1612e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 16132ca3310eSAlasdair G Kergon if (!dm_suspended(md)) 1614cf222b37SAlasdair G Kergon goto out; 1615cf222b37SAlasdair G Kergon 1616cf222b37SAlasdair G Kergon map = dm_get_table(md); 16172ca3310eSAlasdair G Kergon if (!map || !dm_table_get_size(map)) 1618cf222b37SAlasdair G Kergon goto out; 16191da177e4SLinus Torvalds 16208757b776SMilan Broz r = dm_table_resume_targets(map); 16218757b776SMilan Broz if (r) 16228757b776SMilan Broz goto out; 16232ca3310eSAlasdair G Kergon 16249a1fb464SMikulas Patocka dm_queue_flush(md); 16252ca3310eSAlasdair G Kergon 16262ca3310eSAlasdair G Kergon unlock_fs(md); 16272ca3310eSAlasdair G Kergon 1628bfa152faSJun'ichi Nomura if (md->suspended_bdev) { 1629e39e2e95SAlasdair G Kergon bdput(md->suspended_bdev); 1630e39e2e95SAlasdair G Kergon md->suspended_bdev = NULL; 1631bfa152faSJun'ichi Nomura } 1632e39e2e95SAlasdair G Kergon 16332ca3310eSAlasdair G Kergon clear_bit(DMF_SUSPENDED, &md->flags); 16342ca3310eSAlasdair G Kergon 16351da177e4SLinus Torvalds dm_table_unplug_all(map); 16361da177e4SLinus Torvalds 163769267a30SAlasdair G Kergon dm_kobject_uevent(md); 16388560ed6fSHannes Reinecke 1639cf222b37SAlasdair G Kergon r = 0; 16402ca3310eSAlasdair G Kergon 1641cf222b37SAlasdair G Kergon out: 1642cf222b37SAlasdair G Kergon dm_table_put(map); 1643e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 16442ca3310eSAlasdair G Kergon 1645cf222b37SAlasdair G Kergon return r; 16461da177e4SLinus Torvalds } 16471da177e4SLinus Torvalds 16481da177e4SLinus Torvalds /*----------------------------------------------------------------- 16491da177e4SLinus Torvalds * Event notification. 16501da177e4SLinus Torvalds *---------------------------------------------------------------*/ 165169267a30SAlasdair G Kergon void dm_kobject_uevent(struct mapped_device *md) 165269267a30SAlasdair G Kergon { 1653ed9e1982STejun Heo kobject_uevent(&disk_to_dev(md->disk)->kobj, KOBJ_CHANGE); 165469267a30SAlasdair G Kergon } 165569267a30SAlasdair G Kergon 16567a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md) 16577a8c3d3bSMike Anderson { 16587a8c3d3bSMike Anderson return atomic_add_return(1, &md->uevent_seq); 16597a8c3d3bSMike Anderson } 16607a8c3d3bSMike Anderson 16611da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md) 16621da177e4SLinus Torvalds { 16631da177e4SLinus Torvalds return atomic_read(&md->event_nr); 16641da177e4SLinus Torvalds } 16651da177e4SLinus Torvalds 16661da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr) 16671da177e4SLinus Torvalds { 16681da177e4SLinus Torvalds return wait_event_interruptible(md->eventq, 16691da177e4SLinus Torvalds (event_nr != atomic_read(&md->event_nr))); 16701da177e4SLinus Torvalds } 16711da177e4SLinus Torvalds 16727a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist) 16737a8c3d3bSMike Anderson { 16747a8c3d3bSMike Anderson unsigned long flags; 16757a8c3d3bSMike Anderson 16767a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 16777a8c3d3bSMike Anderson list_add(elist, &md->uevent_list); 16787a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 16797a8c3d3bSMike Anderson } 16807a8c3d3bSMike Anderson 16811da177e4SLinus Torvalds /* 16821da177e4SLinus Torvalds * The gendisk is only valid as long as you have a reference 16831da177e4SLinus Torvalds * count on 'md'. 16841da177e4SLinus Torvalds */ 16851da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md) 16861da177e4SLinus Torvalds { 16871da177e4SLinus Torvalds return md->disk; 16881da177e4SLinus Torvalds } 16891da177e4SLinus Torvalds 1690784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md) 1691784aae73SMilan Broz { 1692784aae73SMilan Broz return &md->kobj; 1693784aae73SMilan Broz } 1694784aae73SMilan Broz 1695784aae73SMilan Broz /* 1696784aae73SMilan Broz * struct mapped_device should not be exported outside of dm.c 1697784aae73SMilan Broz * so use this check to verify that kobj is part of md structure 1698784aae73SMilan Broz */ 1699784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj) 1700784aae73SMilan Broz { 1701784aae73SMilan Broz struct mapped_device *md; 1702784aae73SMilan Broz 1703784aae73SMilan Broz md = container_of(kobj, struct mapped_device, kobj); 1704784aae73SMilan Broz if (&md->kobj != kobj) 1705784aae73SMilan Broz return NULL; 1706784aae73SMilan Broz 1707784aae73SMilan Broz dm_get(md); 1708784aae73SMilan Broz return md; 1709784aae73SMilan Broz } 1710784aae73SMilan Broz 17111da177e4SLinus Torvalds int dm_suspended(struct mapped_device *md) 17121da177e4SLinus Torvalds { 17131da177e4SLinus Torvalds return test_bit(DMF_SUSPENDED, &md->flags); 17141da177e4SLinus Torvalds } 17151da177e4SLinus Torvalds 17162e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti) 17172e93ccc1SKiyoshi Ueda { 17182e93ccc1SKiyoshi Ueda struct mapped_device *md = dm_table_get_md(ti->table); 17192e93ccc1SKiyoshi Ueda int r = __noflush_suspending(md); 17202e93ccc1SKiyoshi Ueda 17212e93ccc1SKiyoshi Ueda dm_put(md); 17222e93ccc1SKiyoshi Ueda 17232e93ccc1SKiyoshi Ueda return r; 17242e93ccc1SKiyoshi Ueda } 17252e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending); 17262e93ccc1SKiyoshi Ueda 17271da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops = { 17281da177e4SLinus Torvalds .open = dm_blk_open, 17291da177e4SLinus Torvalds .release = dm_blk_close, 1730aa129a22SMilan Broz .ioctl = dm_blk_ioctl, 17313ac51e74SDarrick J. Wong .getgeo = dm_blk_getgeo, 17321da177e4SLinus Torvalds .owner = THIS_MODULE 17331da177e4SLinus Torvalds }; 17341da177e4SLinus Torvalds 17351da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo); 17361da177e4SLinus Torvalds 17371da177e4SLinus Torvalds /* 17381da177e4SLinus Torvalds * module hooks 17391da177e4SLinus Torvalds */ 17401da177e4SLinus Torvalds module_init(dm_init); 17411da177e4SLinus Torvalds module_exit(dm_exit); 17421da177e4SLinus Torvalds 17431da177e4SLinus Torvalds module_param(major, uint, 0); 17441da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper"); 17451da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver"); 17461da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 17471da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1748