11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Copyright (C) 2001, 2002 Sistina Software (UK) Limited. 32b06cfffSAlasdair G Kergon * Copyright (C) 2004-2006 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> 24aa129a22SMilan Broz #include <linux/smp_lock.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 /* 351da177e4SLinus Torvalds * One of these is allocated per bio. 361da177e4SLinus Torvalds */ 371da177e4SLinus Torvalds struct dm_io { 381da177e4SLinus Torvalds struct mapped_device *md; 391da177e4SLinus Torvalds int error; 401da177e4SLinus Torvalds struct bio *bio; 411da177e4SLinus Torvalds atomic_t io_count; 423eaf840eSJun'ichi "Nick" Nomura unsigned long start_time; 431da177e4SLinus Torvalds }; 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds /* 461da177e4SLinus Torvalds * One of these is allocated per target within a bio. Hopefully 471da177e4SLinus Torvalds * this will be simplified out one day. 481da177e4SLinus Torvalds */ 49028867acSAlasdair G Kergon struct dm_target_io { 501da177e4SLinus Torvalds struct dm_io *io; 511da177e4SLinus Torvalds struct dm_target *ti; 521da177e4SLinus Torvalds union map_info info; 531da177e4SLinus Torvalds }; 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds union map_info *dm_get_mapinfo(struct bio *bio) 561da177e4SLinus Torvalds { 571da177e4SLinus Torvalds if (bio && bio->bi_private) 58028867acSAlasdair G Kergon return &((struct dm_target_io *)bio->bi_private)->info; 591da177e4SLinus Torvalds return NULL; 601da177e4SLinus Torvalds } 611da177e4SLinus Torvalds 62ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1) 63ba61fdd1SJeff Mahoney 641da177e4SLinus Torvalds /* 651da177e4SLinus Torvalds * Bits for the md->flags field. 661da177e4SLinus Torvalds */ 671da177e4SLinus Torvalds #define DMF_BLOCK_IO 0 681da177e4SLinus Torvalds #define DMF_SUSPENDED 1 69aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2 70fba9f90eSJeff Mahoney #define DMF_FREEING 3 715c6bd75dSAlasdair G Kergon #define DMF_DELETING 4 722e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5 731da177e4SLinus Torvalds 74304f3f6aSMilan Broz /* 75304f3f6aSMilan Broz * Work processed by per-device workqueue. 76304f3f6aSMilan Broz */ 77304f3f6aSMilan Broz struct dm_wq_req { 78304f3f6aSMilan Broz enum { 79304f3f6aSMilan Broz DM_WQ_FLUSH_ALL, 80304f3f6aSMilan Broz DM_WQ_FLUSH_DEFERRED, 81304f3f6aSMilan Broz } type; 82304f3f6aSMilan Broz struct work_struct work; 83304f3f6aSMilan Broz struct mapped_device *md; 84304f3f6aSMilan Broz void *context; 85304f3f6aSMilan Broz }; 86304f3f6aSMilan Broz 871da177e4SLinus Torvalds struct mapped_device { 882ca3310eSAlasdair G Kergon struct rw_semaphore io_lock; 89e61290a4SDaniel Walker struct mutex suspend_lock; 902e93ccc1SKiyoshi Ueda spinlock_t pushback_lock; 911da177e4SLinus Torvalds rwlock_t map_lock; 921da177e4SLinus Torvalds atomic_t holders; 935c6bd75dSAlasdair G Kergon atomic_t open_count; 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds unsigned long flags; 961da177e4SLinus Torvalds 97165125e1SJens Axboe struct request_queue *queue; 981da177e4SLinus Torvalds struct gendisk *disk; 997e51f257SMike Anderson char name[16]; 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds void *interface_ptr; 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds /* 1041da177e4SLinus Torvalds * A list of ios that arrived while we were suspended. 1051da177e4SLinus Torvalds */ 1061da177e4SLinus Torvalds atomic_t pending; 1071da177e4SLinus Torvalds wait_queue_head_t wait; 1081da177e4SLinus Torvalds struct bio_list deferred; 1092e93ccc1SKiyoshi Ueda struct bio_list pushback; 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds /* 112304f3f6aSMilan Broz * Processing queue (flush/barriers) 113304f3f6aSMilan Broz */ 114304f3f6aSMilan Broz struct workqueue_struct *wq; 115304f3f6aSMilan Broz 116304f3f6aSMilan Broz /* 1171da177e4SLinus Torvalds * The current mapping. 1181da177e4SLinus Torvalds */ 1191da177e4SLinus Torvalds struct dm_table *map; 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds /* 1221da177e4SLinus Torvalds * io objects are allocated from here. 1231da177e4SLinus Torvalds */ 1241da177e4SLinus Torvalds mempool_t *io_pool; 1251da177e4SLinus Torvalds mempool_t *tio_pool; 1261da177e4SLinus Torvalds 1279faf400fSStefan Bader struct bio_set *bs; 1289faf400fSStefan Bader 1291da177e4SLinus Torvalds /* 1301da177e4SLinus Torvalds * Event handling. 1311da177e4SLinus Torvalds */ 1321da177e4SLinus Torvalds atomic_t event_nr; 1331da177e4SLinus Torvalds wait_queue_head_t eventq; 1347a8c3d3bSMike Anderson atomic_t uevent_seq; 1357a8c3d3bSMike Anderson struct list_head uevent_list; 1367a8c3d3bSMike Anderson spinlock_t uevent_lock; /* Protect access to uevent_list */ 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds /* 1391da177e4SLinus Torvalds * freeze/thaw support require holding onto a super block 1401da177e4SLinus Torvalds */ 1411da177e4SLinus Torvalds struct super_block *frozen_sb; 142e39e2e95SAlasdair G Kergon struct block_device *suspended_bdev; 1433ac51e74SDarrick J. Wong 1443ac51e74SDarrick J. Wong /* forced geometry settings */ 1453ac51e74SDarrick J. Wong struct hd_geometry geometry; 1461da177e4SLinus Torvalds }; 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds #define MIN_IOS 256 149e18b890bSChristoph Lameter static struct kmem_cache *_io_cache; 150e18b890bSChristoph Lameter static struct kmem_cache *_tio_cache; 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds static int __init local_init(void) 1531da177e4SLinus Torvalds { 1541da177e4SLinus Torvalds int r; 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds /* allocate a slab for the dm_ios */ 157028867acSAlasdair G Kergon _io_cache = KMEM_CACHE(dm_io, 0); 1581da177e4SLinus Torvalds if (!_io_cache) 1591da177e4SLinus Torvalds return -ENOMEM; 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds /* allocate a slab for the target ios */ 162028867acSAlasdair G Kergon _tio_cache = KMEM_CACHE(dm_target_io, 0); 1631da177e4SLinus Torvalds if (!_tio_cache) { 1641da177e4SLinus Torvalds kmem_cache_destroy(_io_cache); 1651da177e4SLinus Torvalds return -ENOMEM; 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds 16851e5b2bdSMike Anderson r = dm_uevent_init(); 16951e5b2bdSMike Anderson if (r) { 17051e5b2bdSMike Anderson kmem_cache_destroy(_tio_cache); 17151e5b2bdSMike Anderson kmem_cache_destroy(_io_cache); 17251e5b2bdSMike Anderson return r; 17351e5b2bdSMike Anderson } 17451e5b2bdSMike Anderson 1751da177e4SLinus Torvalds _major = major; 1761da177e4SLinus Torvalds r = register_blkdev(_major, _name); 1771da177e4SLinus Torvalds if (r < 0) { 1781da177e4SLinus Torvalds kmem_cache_destroy(_tio_cache); 1791da177e4SLinus Torvalds kmem_cache_destroy(_io_cache); 18051e5b2bdSMike Anderson dm_uevent_exit(); 1811da177e4SLinus Torvalds return r; 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds if (!_major) 1851da177e4SLinus Torvalds _major = r; 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds return 0; 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds static void local_exit(void) 1911da177e4SLinus Torvalds { 1921da177e4SLinus Torvalds kmem_cache_destroy(_tio_cache); 1931da177e4SLinus Torvalds kmem_cache_destroy(_io_cache); 19400d59405SAkinobu Mita unregister_blkdev(_major, _name); 19551e5b2bdSMike Anderson dm_uevent_exit(); 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds _major = 0; 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds DMINFO("cleaned up"); 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds 202b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = { 2031da177e4SLinus Torvalds local_init, 2041da177e4SLinus Torvalds dm_target_init, 2051da177e4SLinus Torvalds dm_linear_init, 2061da177e4SLinus Torvalds dm_stripe_init, 207*945fa4d2SMikulas Patocka dm_kcopyd_init, 2081da177e4SLinus Torvalds dm_interface_init, 2091da177e4SLinus Torvalds }; 2101da177e4SLinus Torvalds 211b9249e55SAlasdair G Kergon static void (*_exits[])(void) = { 2121da177e4SLinus Torvalds local_exit, 2131da177e4SLinus Torvalds dm_target_exit, 2141da177e4SLinus Torvalds dm_linear_exit, 2151da177e4SLinus Torvalds dm_stripe_exit, 216*945fa4d2SMikulas Patocka dm_kcopyd_exit, 2171da177e4SLinus Torvalds dm_interface_exit, 2181da177e4SLinus Torvalds }; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds static int __init dm_init(void) 2211da177e4SLinus Torvalds { 2221da177e4SLinus Torvalds const int count = ARRAY_SIZE(_inits); 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds int r, i; 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds for (i = 0; i < count; i++) { 2271da177e4SLinus Torvalds r = _inits[i](); 2281da177e4SLinus Torvalds if (r) 2291da177e4SLinus Torvalds goto bad; 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds return 0; 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds bad: 2351da177e4SLinus Torvalds while (i--) 2361da177e4SLinus Torvalds _exits[i](); 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds return r; 2391da177e4SLinus Torvalds } 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds static void __exit dm_exit(void) 2421da177e4SLinus Torvalds { 2431da177e4SLinus Torvalds int i = ARRAY_SIZE(_exits); 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds while (i--) 2461da177e4SLinus Torvalds _exits[i](); 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds /* 2501da177e4SLinus Torvalds * Block device functions 2511da177e4SLinus Torvalds */ 2521da177e4SLinus Torvalds static int dm_blk_open(struct inode *inode, struct file *file) 2531da177e4SLinus Torvalds { 2541da177e4SLinus Torvalds struct mapped_device *md; 2551da177e4SLinus Torvalds 256fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 257fba9f90eSJeff Mahoney 2581da177e4SLinus Torvalds md = inode->i_bdev->bd_disk->private_data; 259fba9f90eSJeff Mahoney if (!md) 260fba9f90eSJeff Mahoney goto out; 261fba9f90eSJeff Mahoney 2625c6bd75dSAlasdair G Kergon if (test_bit(DMF_FREEING, &md->flags) || 2635c6bd75dSAlasdair G Kergon test_bit(DMF_DELETING, &md->flags)) { 264fba9f90eSJeff Mahoney md = NULL; 265fba9f90eSJeff Mahoney goto out; 266fba9f90eSJeff Mahoney } 267fba9f90eSJeff Mahoney 2681da177e4SLinus Torvalds dm_get(md); 2695c6bd75dSAlasdair G Kergon atomic_inc(&md->open_count); 270fba9f90eSJeff Mahoney 271fba9f90eSJeff Mahoney out: 272fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 273fba9f90eSJeff Mahoney 274fba9f90eSJeff Mahoney return md ? 0 : -ENXIO; 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds static int dm_blk_close(struct inode *inode, struct file *file) 2781da177e4SLinus Torvalds { 2791da177e4SLinus Torvalds struct mapped_device *md; 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds md = inode->i_bdev->bd_disk->private_data; 2825c6bd75dSAlasdair G Kergon atomic_dec(&md->open_count); 2831da177e4SLinus Torvalds dm_put(md); 2841da177e4SLinus Torvalds return 0; 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds 2875c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md) 2885c6bd75dSAlasdair G Kergon { 2895c6bd75dSAlasdair G Kergon return atomic_read(&md->open_count); 2905c6bd75dSAlasdair G Kergon } 2915c6bd75dSAlasdair G Kergon 2925c6bd75dSAlasdair G Kergon /* 2935c6bd75dSAlasdair G Kergon * Guarantees nothing is using the device before it's deleted. 2945c6bd75dSAlasdair G Kergon */ 2955c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md) 2965c6bd75dSAlasdair G Kergon { 2975c6bd75dSAlasdair G Kergon int r = 0; 2985c6bd75dSAlasdair G Kergon 2995c6bd75dSAlasdair G Kergon spin_lock(&_minor_lock); 3005c6bd75dSAlasdair G Kergon 3015c6bd75dSAlasdair G Kergon if (dm_open_count(md)) 3025c6bd75dSAlasdair G Kergon r = -EBUSY; 3035c6bd75dSAlasdair G Kergon else 3045c6bd75dSAlasdair G Kergon set_bit(DMF_DELETING, &md->flags); 3055c6bd75dSAlasdair G Kergon 3065c6bd75dSAlasdair G Kergon spin_unlock(&_minor_lock); 3075c6bd75dSAlasdair G Kergon 3085c6bd75dSAlasdair G Kergon return r; 3095c6bd75dSAlasdair G Kergon } 3105c6bd75dSAlasdair G Kergon 3113ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 3123ac51e74SDarrick J. Wong { 3133ac51e74SDarrick J. Wong struct mapped_device *md = bdev->bd_disk->private_data; 3143ac51e74SDarrick J. Wong 3153ac51e74SDarrick J. Wong return dm_get_geometry(md, geo); 3163ac51e74SDarrick J. Wong } 3173ac51e74SDarrick J. Wong 318aa129a22SMilan Broz static int dm_blk_ioctl(struct inode *inode, struct file *file, 319aa129a22SMilan Broz unsigned int cmd, unsigned long arg) 320aa129a22SMilan Broz { 321aa129a22SMilan Broz struct mapped_device *md; 322aa129a22SMilan Broz struct dm_table *map; 323aa129a22SMilan Broz struct dm_target *tgt; 324aa129a22SMilan Broz int r = -ENOTTY; 325aa129a22SMilan Broz 326aa129a22SMilan Broz /* We don't really need this lock, but we do need 'inode'. */ 327aa129a22SMilan Broz unlock_kernel(); 328aa129a22SMilan Broz 329aa129a22SMilan Broz md = inode->i_bdev->bd_disk->private_data; 330aa129a22SMilan Broz 331aa129a22SMilan Broz map = dm_get_table(md); 332aa129a22SMilan Broz 333aa129a22SMilan Broz if (!map || !dm_table_get_size(map)) 334aa129a22SMilan Broz goto out; 335aa129a22SMilan Broz 336aa129a22SMilan Broz /* We only support devices that have a single target */ 337aa129a22SMilan Broz if (dm_table_get_num_targets(map) != 1) 338aa129a22SMilan Broz goto out; 339aa129a22SMilan Broz 340aa129a22SMilan Broz tgt = dm_table_get_target(map, 0); 341aa129a22SMilan Broz 342aa129a22SMilan Broz if (dm_suspended(md)) { 343aa129a22SMilan Broz r = -EAGAIN; 344aa129a22SMilan Broz goto out; 345aa129a22SMilan Broz } 346aa129a22SMilan Broz 347aa129a22SMilan Broz if (tgt->type->ioctl) 348aa129a22SMilan Broz r = tgt->type->ioctl(tgt, inode, file, cmd, arg); 349aa129a22SMilan Broz 350aa129a22SMilan Broz out: 351aa129a22SMilan Broz dm_table_put(map); 352aa129a22SMilan Broz 353aa129a22SMilan Broz lock_kernel(); 354aa129a22SMilan Broz return r; 355aa129a22SMilan Broz } 356aa129a22SMilan Broz 357028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md) 3581da177e4SLinus Torvalds { 3591da177e4SLinus Torvalds return mempool_alloc(md->io_pool, GFP_NOIO); 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds 362028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io) 3631da177e4SLinus Torvalds { 3641da177e4SLinus Torvalds mempool_free(io, md->io_pool); 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 367028867acSAlasdair G Kergon static struct dm_target_io *alloc_tio(struct mapped_device *md) 3681da177e4SLinus Torvalds { 3691da177e4SLinus Torvalds return mempool_alloc(md->tio_pool, GFP_NOIO); 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 372028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio) 3731da177e4SLinus Torvalds { 3741da177e4SLinus Torvalds mempool_free(tio, md->tio_pool); 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 3773eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io) 3783eaf840eSJun'ichi "Nick" Nomura { 3793eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 3803eaf840eSJun'ichi "Nick" Nomura 3813eaf840eSJun'ichi "Nick" Nomura io->start_time = jiffies; 3823eaf840eSJun'ichi "Nick" Nomura 3833eaf840eSJun'ichi "Nick" Nomura preempt_disable(); 3843eaf840eSJun'ichi "Nick" Nomura disk_round_stats(dm_disk(md)); 3853eaf840eSJun'ichi "Nick" Nomura preempt_enable(); 3863eaf840eSJun'ichi "Nick" Nomura dm_disk(md)->in_flight = atomic_inc_return(&md->pending); 3873eaf840eSJun'ichi "Nick" Nomura } 3883eaf840eSJun'ichi "Nick" Nomura 3893eaf840eSJun'ichi "Nick" Nomura static int end_io_acct(struct dm_io *io) 3903eaf840eSJun'ichi "Nick" Nomura { 3913eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 3923eaf840eSJun'ichi "Nick" Nomura struct bio *bio = io->bio; 3933eaf840eSJun'ichi "Nick" Nomura unsigned long duration = jiffies - io->start_time; 3943eaf840eSJun'ichi "Nick" Nomura int pending; 3953eaf840eSJun'ichi "Nick" Nomura int rw = bio_data_dir(bio); 3963eaf840eSJun'ichi "Nick" Nomura 3973eaf840eSJun'ichi "Nick" Nomura preempt_disable(); 3983eaf840eSJun'ichi "Nick" Nomura disk_round_stats(dm_disk(md)); 3993eaf840eSJun'ichi "Nick" Nomura preempt_enable(); 4003eaf840eSJun'ichi "Nick" Nomura dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending); 4013eaf840eSJun'ichi "Nick" Nomura 4023eaf840eSJun'ichi "Nick" Nomura disk_stat_add(dm_disk(md), ticks[rw], duration); 4033eaf840eSJun'ichi "Nick" Nomura 4043eaf840eSJun'ichi "Nick" Nomura return !pending; 4053eaf840eSJun'ichi "Nick" Nomura } 4063eaf840eSJun'ichi "Nick" Nomura 4071da177e4SLinus Torvalds /* 4081da177e4SLinus Torvalds * Add the bio to the list of deferred io. 4091da177e4SLinus Torvalds */ 4101da177e4SLinus Torvalds static int queue_io(struct mapped_device *md, struct bio *bio) 4111da177e4SLinus Torvalds { 4122ca3310eSAlasdair G Kergon down_write(&md->io_lock); 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds if (!test_bit(DMF_BLOCK_IO, &md->flags)) { 4152ca3310eSAlasdair G Kergon up_write(&md->io_lock); 4161da177e4SLinus Torvalds return 1; 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds bio_list_add(&md->deferred, bio); 4201da177e4SLinus Torvalds 4212ca3310eSAlasdair G Kergon up_write(&md->io_lock); 4221da177e4SLinus Torvalds return 0; /* deferred successfully */ 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds /* 4261da177e4SLinus Torvalds * Everyone (including functions in this file), should use this 4271da177e4SLinus Torvalds * function to access the md->map field, and make sure they call 4281da177e4SLinus Torvalds * dm_table_put() when finished. 4291da177e4SLinus Torvalds */ 4301da177e4SLinus Torvalds struct dm_table *dm_get_table(struct mapped_device *md) 4311da177e4SLinus Torvalds { 4321da177e4SLinus Torvalds struct dm_table *t; 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds read_lock(&md->map_lock); 4351da177e4SLinus Torvalds t = md->map; 4361da177e4SLinus Torvalds if (t) 4371da177e4SLinus Torvalds dm_table_get(t); 4381da177e4SLinus Torvalds read_unlock(&md->map_lock); 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds return t; 4411da177e4SLinus Torvalds } 4421da177e4SLinus Torvalds 4433ac51e74SDarrick J. Wong /* 4443ac51e74SDarrick J. Wong * Get the geometry associated with a dm device 4453ac51e74SDarrick J. Wong */ 4463ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) 4473ac51e74SDarrick J. Wong { 4483ac51e74SDarrick J. Wong *geo = md->geometry; 4493ac51e74SDarrick J. Wong 4503ac51e74SDarrick J. Wong return 0; 4513ac51e74SDarrick J. Wong } 4523ac51e74SDarrick J. Wong 4533ac51e74SDarrick J. Wong /* 4543ac51e74SDarrick J. Wong * Set the geometry of a device. 4553ac51e74SDarrick J. Wong */ 4563ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) 4573ac51e74SDarrick J. Wong { 4583ac51e74SDarrick J. Wong sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; 4593ac51e74SDarrick J. Wong 4603ac51e74SDarrick J. Wong if (geo->start > sz) { 4613ac51e74SDarrick J. Wong DMWARN("Start sector is beyond the geometry limits."); 4623ac51e74SDarrick J. Wong return -EINVAL; 4633ac51e74SDarrick J. Wong } 4643ac51e74SDarrick J. Wong 4653ac51e74SDarrick J. Wong md->geometry = *geo; 4663ac51e74SDarrick J. Wong 4673ac51e74SDarrick J. Wong return 0; 4683ac51e74SDarrick J. Wong } 4693ac51e74SDarrick J. Wong 4701da177e4SLinus Torvalds /*----------------------------------------------------------------- 4711da177e4SLinus Torvalds * CRUD START: 4721da177e4SLinus Torvalds * A more elegant soln is in the works that uses the queue 4731da177e4SLinus Torvalds * merge fn, unfortunately there are a couple of changes to 4741da177e4SLinus Torvalds * the block layer that I want to make for this. So in the 4751da177e4SLinus Torvalds * interests of getting something for people to use I give 4761da177e4SLinus Torvalds * you this clearly demarcated crap. 4771da177e4SLinus Torvalds *---------------------------------------------------------------*/ 4781da177e4SLinus Torvalds 4792e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md) 4802e93ccc1SKiyoshi Ueda { 4812e93ccc1SKiyoshi Ueda return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 4822e93ccc1SKiyoshi Ueda } 4832e93ccc1SKiyoshi Ueda 4841da177e4SLinus Torvalds /* 4851da177e4SLinus Torvalds * Decrements the number of outstanding ios that a bio has been 4861da177e4SLinus Torvalds * cloned into, completing the original io if necc. 4871da177e4SLinus Torvalds */ 488858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error) 4891da177e4SLinus Torvalds { 4902e93ccc1SKiyoshi Ueda unsigned long flags; 4912e93ccc1SKiyoshi Ueda 4922e93ccc1SKiyoshi Ueda /* Push-back supersedes any I/O errors */ 4932e93ccc1SKiyoshi Ueda if (error && !(io->error > 0 && __noflush_suspending(io->md))) 4941da177e4SLinus Torvalds io->error = error; 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds if (atomic_dec_and_test(&io->io_count)) { 4972e93ccc1SKiyoshi Ueda if (io->error == DM_ENDIO_REQUEUE) { 4982e93ccc1SKiyoshi Ueda /* 4992e93ccc1SKiyoshi Ueda * Target requested pushing back the I/O. 5002e93ccc1SKiyoshi Ueda * This must be handled before the sleeper on 5012e93ccc1SKiyoshi Ueda * suspend queue merges the pushback list. 5022e93ccc1SKiyoshi Ueda */ 5032e93ccc1SKiyoshi Ueda spin_lock_irqsave(&io->md->pushback_lock, flags); 5042e93ccc1SKiyoshi Ueda if (__noflush_suspending(io->md)) 5052e93ccc1SKiyoshi Ueda bio_list_add(&io->md->pushback, io->bio); 5062e93ccc1SKiyoshi Ueda else 5072e93ccc1SKiyoshi Ueda /* noflush suspend was interrupted. */ 5082e93ccc1SKiyoshi Ueda io->error = -EIO; 5092e93ccc1SKiyoshi Ueda spin_unlock_irqrestore(&io->md->pushback_lock, flags); 5102e93ccc1SKiyoshi Ueda } 5112e93ccc1SKiyoshi Ueda 5123eaf840eSJun'ichi "Nick" Nomura if (end_io_acct(io)) 5131da177e4SLinus Torvalds /* nudge anyone waiting on suspend queue */ 5141da177e4SLinus Torvalds wake_up(&io->md->wait); 5151da177e4SLinus Torvalds 5162e93ccc1SKiyoshi Ueda if (io->error != DM_ENDIO_REQUEUE) { 5172e93ccc1SKiyoshi Ueda blk_add_trace_bio(io->md->queue, io->bio, 5182e93ccc1SKiyoshi Ueda BLK_TA_COMPLETE); 5192056a782SJens Axboe 5206712ecf8SNeilBrown bio_endio(io->bio, io->error); 5212e93ccc1SKiyoshi Ueda } 5222e93ccc1SKiyoshi Ueda 5231da177e4SLinus Torvalds free_io(io->md, io); 5241da177e4SLinus Torvalds } 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds 5276712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error) 5281da177e4SLinus Torvalds { 5291da177e4SLinus Torvalds int r = 0; 530028867acSAlasdair G Kergon struct dm_target_io *tio = bio->bi_private; 5319faf400fSStefan Bader struct mapped_device *md = tio->io->md; 5321da177e4SLinus Torvalds dm_endio_fn endio = tio->ti->type->end_io; 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds if (!bio_flagged(bio, BIO_UPTODATE) && !error) 5351da177e4SLinus Torvalds error = -EIO; 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds if (endio) { 5381da177e4SLinus Torvalds r = endio(tio->ti, bio, error, &tio->info); 5392e93ccc1SKiyoshi Ueda if (r < 0 || r == DM_ENDIO_REQUEUE) 5402e93ccc1SKiyoshi Ueda /* 5412e93ccc1SKiyoshi Ueda * error and requeue request are handled 5422e93ccc1SKiyoshi Ueda * in dec_pending(). 5432e93ccc1SKiyoshi Ueda */ 5441da177e4SLinus Torvalds error = r; 54545cbcd79SKiyoshi Ueda else if (r == DM_ENDIO_INCOMPLETE) 54645cbcd79SKiyoshi Ueda /* The target will handle the io */ 5476712ecf8SNeilBrown return; 54845cbcd79SKiyoshi Ueda else if (r) { 54945cbcd79SKiyoshi Ueda DMWARN("unimplemented target endio return value: %d", r); 55045cbcd79SKiyoshi Ueda BUG(); 55145cbcd79SKiyoshi Ueda } 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 5549faf400fSStefan Bader dec_pending(tio->io, error); 5559faf400fSStefan Bader 5569faf400fSStefan Bader /* 5579faf400fSStefan Bader * Store md for cleanup instead of tio which is about to get freed. 5589faf400fSStefan Bader */ 5599faf400fSStefan Bader bio->bi_private = md->bs; 5609faf400fSStefan Bader 5611da177e4SLinus Torvalds bio_put(bio); 5629faf400fSStefan Bader free_tio(md, tio); 5631da177e4SLinus Torvalds } 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds static sector_t max_io_len(struct mapped_device *md, 5661da177e4SLinus Torvalds sector_t sector, struct dm_target *ti) 5671da177e4SLinus Torvalds { 5681da177e4SLinus Torvalds sector_t offset = sector - ti->begin; 5691da177e4SLinus Torvalds sector_t len = ti->len - offset; 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds /* 5721da177e4SLinus Torvalds * Does the target need to split even further ? 5731da177e4SLinus Torvalds */ 5741da177e4SLinus Torvalds if (ti->split_io) { 5751da177e4SLinus Torvalds sector_t boundary; 5761da177e4SLinus Torvalds boundary = ((offset + ti->split_io) & ~(ti->split_io - 1)) 5771da177e4SLinus Torvalds - offset; 5781da177e4SLinus Torvalds if (len > boundary) 5791da177e4SLinus Torvalds len = boundary; 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds return len; 5831da177e4SLinus Torvalds } 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds static void __map_bio(struct dm_target *ti, struct bio *clone, 586028867acSAlasdair G Kergon struct dm_target_io *tio) 5871da177e4SLinus Torvalds { 5881da177e4SLinus Torvalds int r; 5892056a782SJens Axboe sector_t sector; 5909faf400fSStefan Bader struct mapped_device *md; 5911da177e4SLinus Torvalds 5921da177e4SLinus Torvalds /* 5931da177e4SLinus Torvalds * Sanity checks. 5941da177e4SLinus Torvalds */ 5951da177e4SLinus Torvalds BUG_ON(!clone->bi_size); 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds clone->bi_end_io = clone_endio; 5981da177e4SLinus Torvalds clone->bi_private = tio; 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds /* 6011da177e4SLinus Torvalds * Map the clone. If r == 0 we don't need to do 6021da177e4SLinus Torvalds * anything, the target has assumed ownership of 6031da177e4SLinus Torvalds * this io. 6041da177e4SLinus Torvalds */ 6051da177e4SLinus Torvalds atomic_inc(&tio->io->io_count); 6062056a782SJens Axboe sector = clone->bi_sector; 6071da177e4SLinus Torvalds r = ti->type->map(ti, clone, &tio->info); 60845cbcd79SKiyoshi Ueda if (r == DM_MAPIO_REMAPPED) { 6091da177e4SLinus Torvalds /* the bio has been remapped so dispatch it */ 6102056a782SJens Axboe 6112056a782SJens Axboe blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone, 612c7149d6bSAlan D. Brunelle tio->io->bio->bi_bdev->bd_dev, 613c7149d6bSAlan D. Brunelle clone->bi_sector, sector); 6142056a782SJens Axboe 6151da177e4SLinus Torvalds generic_make_request(clone); 6162e93ccc1SKiyoshi Ueda } else if (r < 0 || r == DM_MAPIO_REQUEUE) { 6172e93ccc1SKiyoshi Ueda /* error the io and bail out, or requeue it if needed */ 6189faf400fSStefan Bader md = tio->io->md; 6199faf400fSStefan Bader dec_pending(tio->io, r); 6209faf400fSStefan Bader /* 6219faf400fSStefan Bader * Store bio_set for cleanup. 6229faf400fSStefan Bader */ 6239faf400fSStefan Bader clone->bi_private = md->bs; 6241da177e4SLinus Torvalds bio_put(clone); 6259faf400fSStefan Bader free_tio(md, tio); 62645cbcd79SKiyoshi Ueda } else if (r) { 62745cbcd79SKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 62845cbcd79SKiyoshi Ueda BUG(); 6291da177e4SLinus Torvalds } 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds struct clone_info { 6331da177e4SLinus Torvalds struct mapped_device *md; 6341da177e4SLinus Torvalds struct dm_table *map; 6351da177e4SLinus Torvalds struct bio *bio; 6361da177e4SLinus Torvalds struct dm_io *io; 6371da177e4SLinus Torvalds sector_t sector; 6381da177e4SLinus Torvalds sector_t sector_count; 6391da177e4SLinus Torvalds unsigned short idx; 6401da177e4SLinus Torvalds }; 6411da177e4SLinus Torvalds 6423676347aSPeter Osterlund static void dm_bio_destructor(struct bio *bio) 6433676347aSPeter Osterlund { 6449faf400fSStefan Bader struct bio_set *bs = bio->bi_private; 6459faf400fSStefan Bader 6469faf400fSStefan Bader bio_free(bio, bs); 6473676347aSPeter Osterlund } 6483676347aSPeter Osterlund 6491da177e4SLinus Torvalds /* 6501da177e4SLinus Torvalds * Creates a little bio that is just does part of a bvec. 6511da177e4SLinus Torvalds */ 6521da177e4SLinus Torvalds static struct bio *split_bvec(struct bio *bio, sector_t sector, 6531da177e4SLinus Torvalds unsigned short idx, unsigned int offset, 6549faf400fSStefan Bader unsigned int len, struct bio_set *bs) 6551da177e4SLinus Torvalds { 6561da177e4SLinus Torvalds struct bio *clone; 6571da177e4SLinus Torvalds struct bio_vec *bv = bio->bi_io_vec + idx; 6581da177e4SLinus Torvalds 6599faf400fSStefan Bader clone = bio_alloc_bioset(GFP_NOIO, 1, bs); 6603676347aSPeter Osterlund clone->bi_destructor = dm_bio_destructor; 6611da177e4SLinus Torvalds *clone->bi_io_vec = *bv; 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds clone->bi_sector = sector; 6641da177e4SLinus Torvalds clone->bi_bdev = bio->bi_bdev; 6651da177e4SLinus Torvalds clone->bi_rw = bio->bi_rw; 6661da177e4SLinus Torvalds clone->bi_vcnt = 1; 6671da177e4SLinus Torvalds clone->bi_size = to_bytes(len); 6681da177e4SLinus Torvalds clone->bi_io_vec->bv_offset = offset; 6691da177e4SLinus Torvalds clone->bi_io_vec->bv_len = clone->bi_size; 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds return clone; 6721da177e4SLinus Torvalds } 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds /* 6751da177e4SLinus Torvalds * Creates a bio that consists of range of complete bvecs. 6761da177e4SLinus Torvalds */ 6771da177e4SLinus Torvalds static struct bio *clone_bio(struct bio *bio, sector_t sector, 6781da177e4SLinus Torvalds unsigned short idx, unsigned short bv_count, 6799faf400fSStefan Bader unsigned int len, struct bio_set *bs) 6801da177e4SLinus Torvalds { 6811da177e4SLinus Torvalds struct bio *clone; 6821da177e4SLinus Torvalds 6839faf400fSStefan Bader clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs); 6849faf400fSStefan Bader __bio_clone(clone, bio); 6859faf400fSStefan Bader clone->bi_destructor = dm_bio_destructor; 6861da177e4SLinus Torvalds clone->bi_sector = sector; 6871da177e4SLinus Torvalds clone->bi_idx = idx; 6881da177e4SLinus Torvalds clone->bi_vcnt = idx + bv_count; 6891da177e4SLinus Torvalds clone->bi_size = to_bytes(len); 6901da177e4SLinus Torvalds clone->bi_flags &= ~(1 << BIO_SEG_VALID); 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds return clone; 6931da177e4SLinus Torvalds } 6941da177e4SLinus Torvalds 695512875bdSJun'ichi Nomura static int __clone_and_map(struct clone_info *ci) 6961da177e4SLinus Torvalds { 6971da177e4SLinus Torvalds struct bio *clone, *bio = ci->bio; 698512875bdSJun'ichi Nomura struct dm_target *ti; 699512875bdSJun'ichi Nomura sector_t len = 0, max; 700028867acSAlasdair G Kergon struct dm_target_io *tio; 7011da177e4SLinus Torvalds 702512875bdSJun'ichi Nomura ti = dm_table_find_target(ci->map, ci->sector); 703512875bdSJun'ichi Nomura if (!dm_target_is_valid(ti)) 704512875bdSJun'ichi Nomura return -EIO; 705512875bdSJun'ichi Nomura 706512875bdSJun'ichi Nomura max = max_io_len(ci->md, ci->sector, ti); 707512875bdSJun'ichi Nomura 7081da177e4SLinus Torvalds /* 7091da177e4SLinus Torvalds * Allocate a target io object. 7101da177e4SLinus Torvalds */ 7111da177e4SLinus Torvalds tio = alloc_tio(ci->md); 7121da177e4SLinus Torvalds tio->io = ci->io; 7131da177e4SLinus Torvalds tio->ti = ti; 7141da177e4SLinus Torvalds memset(&tio->info, 0, sizeof(tio->info)); 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds if (ci->sector_count <= max) { 7171da177e4SLinus Torvalds /* 7181da177e4SLinus Torvalds * Optimise for the simple case where we can do all of 7191da177e4SLinus Torvalds * the remaining io with a single clone. 7201da177e4SLinus Torvalds */ 7211da177e4SLinus Torvalds clone = clone_bio(bio, ci->sector, ci->idx, 7229faf400fSStefan Bader bio->bi_vcnt - ci->idx, ci->sector_count, 7239faf400fSStefan Bader ci->md->bs); 7241da177e4SLinus Torvalds __map_bio(ti, clone, tio); 7251da177e4SLinus Torvalds ci->sector_count = 0; 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) { 7281da177e4SLinus Torvalds /* 7291da177e4SLinus Torvalds * There are some bvecs that don't span targets. 7301da177e4SLinus Torvalds * Do as many of these as possible. 7311da177e4SLinus Torvalds */ 7321da177e4SLinus Torvalds int i; 7331da177e4SLinus Torvalds sector_t remaining = max; 7341da177e4SLinus Torvalds sector_t bv_len; 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) { 7371da177e4SLinus Torvalds bv_len = to_sector(bio->bi_io_vec[i].bv_len); 7381da177e4SLinus Torvalds 7391da177e4SLinus Torvalds if (bv_len > remaining) 7401da177e4SLinus Torvalds break; 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds remaining -= bv_len; 7431da177e4SLinus Torvalds len += bv_len; 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds 7469faf400fSStefan Bader clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len, 7479faf400fSStefan Bader ci->md->bs); 7481da177e4SLinus Torvalds __map_bio(ti, clone, tio); 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds ci->sector += len; 7511da177e4SLinus Torvalds ci->sector_count -= len; 7521da177e4SLinus Torvalds ci->idx = i; 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds } else { 7551da177e4SLinus Torvalds /* 756d2044a94SAlasdair G Kergon * Handle a bvec that must be split between two or more targets. 7571da177e4SLinus Torvalds */ 7581da177e4SLinus Torvalds struct bio_vec *bv = bio->bi_io_vec + ci->idx; 759d2044a94SAlasdair G Kergon sector_t remaining = to_sector(bv->bv_len); 760d2044a94SAlasdair G Kergon unsigned int offset = 0; 7611da177e4SLinus Torvalds 762d2044a94SAlasdair G Kergon do { 763d2044a94SAlasdair G Kergon if (offset) { 7641da177e4SLinus Torvalds ti = dm_table_find_target(ci->map, ci->sector); 765512875bdSJun'ichi Nomura if (!dm_target_is_valid(ti)) 766512875bdSJun'ichi Nomura return -EIO; 767512875bdSJun'ichi Nomura 768d2044a94SAlasdair G Kergon max = max_io_len(ci->md, ci->sector, ti); 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds tio = alloc_tio(ci->md); 7711da177e4SLinus Torvalds tio->io = ci->io; 7721da177e4SLinus Torvalds tio->ti = ti; 7731da177e4SLinus Torvalds memset(&tio->info, 0, sizeof(tio->info)); 774d2044a94SAlasdair G Kergon } 775d2044a94SAlasdair G Kergon 776d2044a94SAlasdair G Kergon len = min(remaining, max); 777d2044a94SAlasdair G Kergon 778d2044a94SAlasdair G Kergon clone = split_bvec(bio, ci->sector, ci->idx, 7799faf400fSStefan Bader bv->bv_offset + offset, len, 7809faf400fSStefan Bader ci->md->bs); 781d2044a94SAlasdair G Kergon 7821da177e4SLinus Torvalds __map_bio(ti, clone, tio); 7831da177e4SLinus Torvalds 7841da177e4SLinus Torvalds ci->sector += len; 7851da177e4SLinus Torvalds ci->sector_count -= len; 786d2044a94SAlasdair G Kergon offset += to_bytes(len); 787d2044a94SAlasdair G Kergon } while (remaining -= len); 788d2044a94SAlasdair G Kergon 7891da177e4SLinus Torvalds ci->idx++; 7901da177e4SLinus Torvalds } 791512875bdSJun'ichi Nomura 792512875bdSJun'ichi Nomura return 0; 7931da177e4SLinus Torvalds } 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds /* 7961da177e4SLinus Torvalds * Split the bio into several clones. 7971da177e4SLinus Torvalds */ 7989e4e5f87SMilan Broz static int __split_bio(struct mapped_device *md, struct bio *bio) 7991da177e4SLinus Torvalds { 8001da177e4SLinus Torvalds struct clone_info ci; 801512875bdSJun'ichi Nomura int error = 0; 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds ci.map = dm_get_table(md); 8049e4e5f87SMilan Broz if (unlikely(!ci.map)) 8059e4e5f87SMilan Broz return -EIO; 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds ci.md = md; 8081da177e4SLinus Torvalds ci.bio = bio; 8091da177e4SLinus Torvalds ci.io = alloc_io(md); 8101da177e4SLinus Torvalds ci.io->error = 0; 8111da177e4SLinus Torvalds atomic_set(&ci.io->io_count, 1); 8121da177e4SLinus Torvalds ci.io->bio = bio; 8131da177e4SLinus Torvalds ci.io->md = md; 8141da177e4SLinus Torvalds ci.sector = bio->bi_sector; 8151da177e4SLinus Torvalds ci.sector_count = bio_sectors(bio); 8161da177e4SLinus Torvalds ci.idx = bio->bi_idx; 8171da177e4SLinus Torvalds 8183eaf840eSJun'ichi "Nick" Nomura start_io_acct(ci.io); 819512875bdSJun'ichi Nomura while (ci.sector_count && !error) 820512875bdSJun'ichi Nomura error = __clone_and_map(&ci); 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds /* drop the extra reference count */ 823512875bdSJun'ichi Nomura dec_pending(ci.io, error); 8241da177e4SLinus Torvalds dm_table_put(ci.map); 8259e4e5f87SMilan Broz 8269e4e5f87SMilan Broz return 0; 8271da177e4SLinus Torvalds } 8281da177e4SLinus Torvalds /*----------------------------------------------------------------- 8291da177e4SLinus Torvalds * CRUD END 8301da177e4SLinus Torvalds *---------------------------------------------------------------*/ 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds /* 8331da177e4SLinus Torvalds * The request function that just remaps the bio built up by 8341da177e4SLinus Torvalds * dm_merge_bvec. 8351da177e4SLinus Torvalds */ 836165125e1SJens Axboe static int dm_request(struct request_queue *q, struct bio *bio) 8371da177e4SLinus Torvalds { 8389e4e5f87SMilan Broz int r = -EIO; 83912f03a49SKevin Corry int rw = bio_data_dir(bio); 8401da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 8411da177e4SLinus Torvalds 84207a83c47SStefan Bader /* 84307a83c47SStefan Bader * There is no use in forwarding any barrier request since we can't 84407a83c47SStefan Bader * guarantee it is (or can be) handled by the targets correctly. 84507a83c47SStefan Bader */ 84607a83c47SStefan Bader if (unlikely(bio_barrier(bio))) { 8476712ecf8SNeilBrown bio_endio(bio, -EOPNOTSUPP); 84807a83c47SStefan Bader return 0; 84907a83c47SStefan Bader } 85007a83c47SStefan Bader 8512ca3310eSAlasdair G Kergon down_read(&md->io_lock); 8521da177e4SLinus Torvalds 85312f03a49SKevin Corry disk_stat_inc(dm_disk(md), ios[rw]); 85412f03a49SKevin Corry disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio)); 85512f03a49SKevin Corry 8561da177e4SLinus Torvalds /* 8571da177e4SLinus Torvalds * If we're suspended we have to queue 8581da177e4SLinus Torvalds * this io for later. 8591da177e4SLinus Torvalds */ 8601da177e4SLinus Torvalds while (test_bit(DMF_BLOCK_IO, &md->flags)) { 8612ca3310eSAlasdair G Kergon up_read(&md->io_lock); 8621da177e4SLinus Torvalds 8639e4e5f87SMilan Broz if (bio_rw(bio) != READA) 8641da177e4SLinus Torvalds r = queue_io(md, bio); 8651da177e4SLinus Torvalds 8669e4e5f87SMilan Broz if (r <= 0) 8679e4e5f87SMilan Broz goto out_req; 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds /* 8701da177e4SLinus Torvalds * We're in a while loop, because someone could suspend 8711da177e4SLinus Torvalds * before we get to the following read lock. 8721da177e4SLinus Torvalds */ 8732ca3310eSAlasdair G Kergon down_read(&md->io_lock); 8741da177e4SLinus Torvalds } 8751da177e4SLinus Torvalds 8769e4e5f87SMilan Broz r = __split_bio(md, bio); 8772ca3310eSAlasdair G Kergon up_read(&md->io_lock); 8789e4e5f87SMilan Broz 8799e4e5f87SMilan Broz out_req: 8809e4e5f87SMilan Broz if (r < 0) 8819e4e5f87SMilan Broz bio_io_error(bio); 8829e4e5f87SMilan Broz 8831da177e4SLinus Torvalds return 0; 8841da177e4SLinus Torvalds } 8851da177e4SLinus Torvalds 886165125e1SJens Axboe static void dm_unplug_all(struct request_queue *q) 8871da177e4SLinus Torvalds { 8881da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 8891da177e4SLinus Torvalds struct dm_table *map = dm_get_table(md); 8901da177e4SLinus Torvalds 8911da177e4SLinus Torvalds if (map) { 8921da177e4SLinus Torvalds dm_table_unplug_all(map); 8931da177e4SLinus Torvalds dm_table_put(map); 8941da177e4SLinus Torvalds } 8951da177e4SLinus Torvalds } 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits) 8981da177e4SLinus Torvalds { 8991da177e4SLinus Torvalds int r; 9001da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) congested_data; 9011da177e4SLinus Torvalds struct dm_table *map = dm_get_table(md); 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds if (!map || test_bit(DMF_BLOCK_IO, &md->flags)) 9041da177e4SLinus Torvalds r = bdi_bits; 9051da177e4SLinus Torvalds else 9061da177e4SLinus Torvalds r = dm_table_any_congested(map, bdi_bits); 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds dm_table_put(map); 9091da177e4SLinus Torvalds return r; 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds /*----------------------------------------------------------------- 9131da177e4SLinus Torvalds * An IDR is used to keep track of allocated minor numbers. 9141da177e4SLinus Torvalds *---------------------------------------------------------------*/ 9151da177e4SLinus Torvalds static DEFINE_IDR(_minor_idr); 9161da177e4SLinus Torvalds 9172b06cfffSAlasdair G Kergon static void free_minor(int minor) 9181da177e4SLinus Torvalds { 919f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 9201da177e4SLinus Torvalds idr_remove(&_minor_idr, minor); 921f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 9221da177e4SLinus Torvalds } 9231da177e4SLinus Torvalds 9241da177e4SLinus Torvalds /* 9251da177e4SLinus Torvalds * See if the device with a specific minor # is free. 9261da177e4SLinus Torvalds */ 9272b06cfffSAlasdair G Kergon static int specific_minor(struct mapped_device *md, int minor) 9281da177e4SLinus Torvalds { 9291da177e4SLinus Torvalds int r, m; 9301da177e4SLinus Torvalds 9311da177e4SLinus Torvalds if (minor >= (1 << MINORBITS)) 9321da177e4SLinus Torvalds return -EINVAL; 9331da177e4SLinus Torvalds 93462f75c2fSJeff Mahoney r = idr_pre_get(&_minor_idr, GFP_KERNEL); 93562f75c2fSJeff Mahoney if (!r) 93662f75c2fSJeff Mahoney return -ENOMEM; 93762f75c2fSJeff Mahoney 938f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 9391da177e4SLinus Torvalds 9401da177e4SLinus Torvalds if (idr_find(&_minor_idr, minor)) { 9411da177e4SLinus Torvalds r = -EBUSY; 9421da177e4SLinus Torvalds goto out; 9431da177e4SLinus Torvalds } 9441da177e4SLinus Torvalds 945ba61fdd1SJeff Mahoney r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m); 94662f75c2fSJeff Mahoney if (r) 9471da177e4SLinus Torvalds goto out; 9481da177e4SLinus Torvalds 9491da177e4SLinus Torvalds if (m != minor) { 9501da177e4SLinus Torvalds idr_remove(&_minor_idr, m); 9511da177e4SLinus Torvalds r = -EBUSY; 9521da177e4SLinus Torvalds goto out; 9531da177e4SLinus Torvalds } 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds out: 956f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 9571da177e4SLinus Torvalds return r; 9581da177e4SLinus Torvalds } 9591da177e4SLinus Torvalds 9602b06cfffSAlasdair G Kergon static int next_free_minor(struct mapped_device *md, int *minor) 9611da177e4SLinus Torvalds { 9622b06cfffSAlasdair G Kergon int r, m; 9631da177e4SLinus Torvalds 9641da177e4SLinus Torvalds r = idr_pre_get(&_minor_idr, GFP_KERNEL); 96562f75c2fSJeff Mahoney if (!r) 96662f75c2fSJeff Mahoney return -ENOMEM; 96762f75c2fSJeff Mahoney 968f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 9691da177e4SLinus Torvalds 970ba61fdd1SJeff Mahoney r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m); 9711da177e4SLinus Torvalds if (r) { 9721da177e4SLinus Torvalds goto out; 9731da177e4SLinus Torvalds } 9741da177e4SLinus Torvalds 9751da177e4SLinus Torvalds if (m >= (1 << MINORBITS)) { 9761da177e4SLinus Torvalds idr_remove(&_minor_idr, m); 9771da177e4SLinus Torvalds r = -ENOSPC; 9781da177e4SLinus Torvalds goto out; 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds 9811da177e4SLinus Torvalds *minor = m; 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds out: 984f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 9851da177e4SLinus Torvalds return r; 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops; 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds /* 9911da177e4SLinus Torvalds * Allocate and initialise a blank device with a given minor. 9921da177e4SLinus Torvalds */ 9932b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor) 9941da177e4SLinus Torvalds { 9951da177e4SLinus Torvalds int r; 9961da177e4SLinus Torvalds struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL); 997ba61fdd1SJeff Mahoney void *old_md; 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds if (!md) { 10001da177e4SLinus Torvalds DMWARN("unable to allocate device, out of memory."); 10011da177e4SLinus Torvalds return NULL; 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds 100410da4f79SJeff Mahoney if (!try_module_get(THIS_MODULE)) 10056ed7ade8SMilan Broz goto bad_module_get; 100610da4f79SJeff Mahoney 10071da177e4SLinus Torvalds /* get a minor number for the dev */ 10082b06cfffSAlasdair G Kergon if (minor == DM_ANY_MINOR) 10092b06cfffSAlasdair G Kergon r = next_free_minor(md, &minor); 10102b06cfffSAlasdair G Kergon else 10112b06cfffSAlasdair G Kergon r = specific_minor(md, minor); 10121da177e4SLinus Torvalds if (r < 0) 10136ed7ade8SMilan Broz goto bad_minor; 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds memset(md, 0, sizeof(*md)); 10162ca3310eSAlasdair G Kergon init_rwsem(&md->io_lock); 1017e61290a4SDaniel Walker mutex_init(&md->suspend_lock); 10182e93ccc1SKiyoshi Ueda spin_lock_init(&md->pushback_lock); 10191da177e4SLinus Torvalds rwlock_init(&md->map_lock); 10201da177e4SLinus Torvalds atomic_set(&md->holders, 1); 10215c6bd75dSAlasdair G Kergon atomic_set(&md->open_count, 0); 10221da177e4SLinus Torvalds atomic_set(&md->event_nr, 0); 10237a8c3d3bSMike Anderson atomic_set(&md->uevent_seq, 0); 10247a8c3d3bSMike Anderson INIT_LIST_HEAD(&md->uevent_list); 10257a8c3d3bSMike Anderson spin_lock_init(&md->uevent_lock); 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds md->queue = blk_alloc_queue(GFP_KERNEL); 10281da177e4SLinus Torvalds if (!md->queue) 10296ed7ade8SMilan Broz goto bad_queue; 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds md->queue->queuedata = md; 10321da177e4SLinus Torvalds md->queue->backing_dev_info.congested_fn = dm_any_congested; 10331da177e4SLinus Torvalds md->queue->backing_dev_info.congested_data = md; 10341da177e4SLinus Torvalds blk_queue_make_request(md->queue, dm_request); 1035daef265fSJens Axboe blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY); 10361da177e4SLinus Torvalds md->queue->unplug_fn = dm_unplug_all; 10371da177e4SLinus Torvalds 103893d2341cSMatthew Dobson md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache); 10391da177e4SLinus Torvalds if (!md->io_pool) 10406ed7ade8SMilan Broz goto bad_io_pool; 10411da177e4SLinus Torvalds 104293d2341cSMatthew Dobson md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache); 10431da177e4SLinus Torvalds if (!md->tio_pool) 10446ed7ade8SMilan Broz goto bad_tio_pool; 10451da177e4SLinus Torvalds 10465972511bSJens Axboe md->bs = bioset_create(16, 16); 10479faf400fSStefan Bader if (!md->bs) 10489faf400fSStefan Bader goto bad_no_bioset; 10499faf400fSStefan Bader 10501da177e4SLinus Torvalds md->disk = alloc_disk(1); 10511da177e4SLinus Torvalds if (!md->disk) 10526ed7ade8SMilan Broz goto bad_disk; 10531da177e4SLinus Torvalds 1054f0b04115SJeff Mahoney atomic_set(&md->pending, 0); 1055f0b04115SJeff Mahoney init_waitqueue_head(&md->wait); 1056f0b04115SJeff Mahoney init_waitqueue_head(&md->eventq); 1057f0b04115SJeff Mahoney 10581da177e4SLinus Torvalds md->disk->major = _major; 10591da177e4SLinus Torvalds md->disk->first_minor = minor; 10601da177e4SLinus Torvalds md->disk->fops = &dm_blk_dops; 10611da177e4SLinus Torvalds md->disk->queue = md->queue; 10621da177e4SLinus Torvalds md->disk->private_data = md; 10631da177e4SLinus Torvalds sprintf(md->disk->disk_name, "dm-%d", minor); 10641da177e4SLinus Torvalds add_disk(md->disk); 10657e51f257SMike Anderson format_dev_t(md->name, MKDEV(_major, minor)); 10661da177e4SLinus Torvalds 1067304f3f6aSMilan Broz md->wq = create_singlethread_workqueue("kdmflush"); 1068304f3f6aSMilan Broz if (!md->wq) 1069304f3f6aSMilan Broz goto bad_thread; 1070304f3f6aSMilan Broz 1071ba61fdd1SJeff Mahoney /* Populate the mapping, nobody knows we exist yet */ 1072f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 1073ba61fdd1SJeff Mahoney old_md = idr_replace(&_minor_idr, md, minor); 1074f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1075ba61fdd1SJeff Mahoney 1076ba61fdd1SJeff Mahoney BUG_ON(old_md != MINOR_ALLOCED); 1077ba61fdd1SJeff Mahoney 10781da177e4SLinus Torvalds return md; 10791da177e4SLinus Torvalds 1080304f3f6aSMilan Broz bad_thread: 1081304f3f6aSMilan Broz put_disk(md->disk); 10826ed7ade8SMilan Broz bad_disk: 10839faf400fSStefan Bader bioset_free(md->bs); 10849faf400fSStefan Bader bad_no_bioset: 10851da177e4SLinus Torvalds mempool_destroy(md->tio_pool); 10866ed7ade8SMilan Broz bad_tio_pool: 10871da177e4SLinus Torvalds mempool_destroy(md->io_pool); 10886ed7ade8SMilan Broz bad_io_pool: 10891312f40eSAl Viro blk_cleanup_queue(md->queue); 10906ed7ade8SMilan Broz bad_queue: 10911da177e4SLinus Torvalds free_minor(minor); 10926ed7ade8SMilan Broz bad_minor: 109310da4f79SJeff Mahoney module_put(THIS_MODULE); 10946ed7ade8SMilan Broz bad_module_get: 10951da177e4SLinus Torvalds kfree(md); 10961da177e4SLinus Torvalds return NULL; 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds 1099ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md); 1100ae9da83fSJun'ichi Nomura 11011da177e4SLinus Torvalds static void free_dev(struct mapped_device *md) 11021da177e4SLinus Torvalds { 11032b06cfffSAlasdair G Kergon int minor = md->disk->first_minor; 110463d94e48SJun'ichi Nomura 1105d9dde59bSJun'ichi Nomura if (md->suspended_bdev) { 1106ae9da83fSJun'ichi Nomura unlock_fs(md); 1107d9dde59bSJun'ichi Nomura bdput(md->suspended_bdev); 1108d9dde59bSJun'ichi Nomura } 1109304f3f6aSMilan Broz destroy_workqueue(md->wq); 11101da177e4SLinus Torvalds mempool_destroy(md->tio_pool); 11111da177e4SLinus Torvalds mempool_destroy(md->io_pool); 11129faf400fSStefan Bader bioset_free(md->bs); 11131da177e4SLinus Torvalds del_gendisk(md->disk); 111463d94e48SJun'ichi Nomura free_minor(minor); 1115fba9f90eSJeff Mahoney 1116fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 1117fba9f90eSJeff Mahoney md->disk->private_data = NULL; 1118fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 1119fba9f90eSJeff Mahoney 11201da177e4SLinus Torvalds put_disk(md->disk); 11211312f40eSAl Viro blk_cleanup_queue(md->queue); 112210da4f79SJeff Mahoney module_put(THIS_MODULE); 11231da177e4SLinus Torvalds kfree(md); 11241da177e4SLinus Torvalds } 11251da177e4SLinus Torvalds 11261da177e4SLinus Torvalds /* 11271da177e4SLinus Torvalds * Bind a table to the device. 11281da177e4SLinus Torvalds */ 11291da177e4SLinus Torvalds static void event_callback(void *context) 11301da177e4SLinus Torvalds { 11317a8c3d3bSMike Anderson unsigned long flags; 11327a8c3d3bSMike Anderson LIST_HEAD(uevents); 11331da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) context; 11341da177e4SLinus Torvalds 11357a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 11367a8c3d3bSMike Anderson list_splice_init(&md->uevent_list, &uevents); 11377a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 11387a8c3d3bSMike Anderson 1139edfaa7c3SKay Sievers dm_send_uevents(&uevents, &md->disk->dev.kobj); 11407a8c3d3bSMike Anderson 11411da177e4SLinus Torvalds atomic_inc(&md->event_nr); 11421da177e4SLinus Torvalds wake_up(&md->eventq); 11431da177e4SLinus Torvalds } 11441da177e4SLinus Torvalds 11454e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size) 11461da177e4SLinus Torvalds { 11474e90188bSAlasdair G Kergon set_capacity(md->disk, size); 11481da177e4SLinus Torvalds 11491b1dcc1bSJes Sorensen mutex_lock(&md->suspended_bdev->bd_inode->i_mutex); 1150e39e2e95SAlasdair G Kergon i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT); 11511b1dcc1bSJes Sorensen mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex); 11521da177e4SLinus Torvalds } 11531da177e4SLinus Torvalds 11541da177e4SLinus Torvalds static int __bind(struct mapped_device *md, struct dm_table *t) 11551da177e4SLinus Torvalds { 1156165125e1SJens Axboe struct request_queue *q = md->queue; 11571da177e4SLinus Torvalds sector_t size; 11581da177e4SLinus Torvalds 11591da177e4SLinus Torvalds size = dm_table_get_size(t); 11603ac51e74SDarrick J. Wong 11613ac51e74SDarrick J. Wong /* 11623ac51e74SDarrick J. Wong * Wipe any geometry if the size of the table changed. 11633ac51e74SDarrick J. Wong */ 11643ac51e74SDarrick J. Wong if (size != get_capacity(md->disk)) 11653ac51e74SDarrick J. Wong memset(&md->geometry, 0, sizeof(md->geometry)); 11663ac51e74SDarrick J. Wong 1167bfa152faSJun'ichi Nomura if (md->suspended_bdev) 11684e90188bSAlasdair G Kergon __set_size(md, size); 11691da177e4SLinus Torvalds if (size == 0) 11701da177e4SLinus Torvalds return 0; 11711da177e4SLinus Torvalds 11721da177e4SLinus Torvalds dm_table_get(t); 1173cf222b37SAlasdair G Kergon dm_table_event_callback(t, event_callback, md); 11742ca3310eSAlasdair G Kergon 11752ca3310eSAlasdair G Kergon write_lock(&md->map_lock); 11762ca3310eSAlasdair G Kergon md->map = t; 11771da177e4SLinus Torvalds dm_table_set_restrictions(t, q); 11782ca3310eSAlasdair G Kergon write_unlock(&md->map_lock); 11792ca3310eSAlasdair G Kergon 11801da177e4SLinus Torvalds return 0; 11811da177e4SLinus Torvalds } 11821da177e4SLinus Torvalds 11831da177e4SLinus Torvalds static void __unbind(struct mapped_device *md) 11841da177e4SLinus Torvalds { 11851da177e4SLinus Torvalds struct dm_table *map = md->map; 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds if (!map) 11881da177e4SLinus Torvalds return; 11891da177e4SLinus Torvalds 11901da177e4SLinus Torvalds dm_table_event_callback(map, NULL, NULL); 11911da177e4SLinus Torvalds write_lock(&md->map_lock); 11921da177e4SLinus Torvalds md->map = NULL; 11931da177e4SLinus Torvalds write_unlock(&md->map_lock); 11941da177e4SLinus Torvalds dm_table_put(map); 11951da177e4SLinus Torvalds } 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds /* 11981da177e4SLinus Torvalds * Constructor for a new device. 11991da177e4SLinus Torvalds */ 12002b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result) 12011da177e4SLinus Torvalds { 12021da177e4SLinus Torvalds struct mapped_device *md; 12031da177e4SLinus Torvalds 12042b06cfffSAlasdair G Kergon md = alloc_dev(minor); 12051da177e4SLinus Torvalds if (!md) 12061da177e4SLinus Torvalds return -ENXIO; 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds *result = md; 12091da177e4SLinus Torvalds return 0; 12101da177e4SLinus Torvalds } 12111da177e4SLinus Torvalds 1212637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev) 12131da177e4SLinus Torvalds { 12141da177e4SLinus Torvalds struct mapped_device *md; 12151da177e4SLinus Torvalds unsigned minor = MINOR(dev); 12161da177e4SLinus Torvalds 12171da177e4SLinus Torvalds if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) 12181da177e4SLinus Torvalds return NULL; 12191da177e4SLinus Torvalds 1220f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds md = idr_find(&_minor_idr, minor); 1223fba9f90eSJeff Mahoney if (md && (md == MINOR_ALLOCED || 1224fba9f90eSJeff Mahoney (dm_disk(md)->first_minor != minor) || 1225fba9f90eSJeff Mahoney test_bit(DMF_FREEING, &md->flags))) { 1226637842cfSDavid Teigland md = NULL; 1227fba9f90eSJeff Mahoney goto out; 1228fba9f90eSJeff Mahoney } 12291da177e4SLinus Torvalds 1230fba9f90eSJeff Mahoney out: 1231f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 12321da177e4SLinus Torvalds 1233637842cfSDavid Teigland return md; 1234637842cfSDavid Teigland } 1235637842cfSDavid Teigland 1236d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev) 1237d229a958SDavid Teigland { 1238d229a958SDavid Teigland struct mapped_device *md = dm_find_md(dev); 1239d229a958SDavid Teigland 1240d229a958SDavid Teigland if (md) 1241d229a958SDavid Teigland dm_get(md); 1242d229a958SDavid Teigland 1243d229a958SDavid Teigland return md; 1244d229a958SDavid Teigland } 1245d229a958SDavid Teigland 12469ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md) 1247637842cfSDavid Teigland { 12489ade92a9SAlasdair G Kergon return md->interface_ptr; 12491da177e4SLinus Torvalds } 12501da177e4SLinus Torvalds 12511da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr) 12521da177e4SLinus Torvalds { 12531da177e4SLinus Torvalds md->interface_ptr = ptr; 12541da177e4SLinus Torvalds } 12551da177e4SLinus Torvalds 12561da177e4SLinus Torvalds void dm_get(struct mapped_device *md) 12571da177e4SLinus Torvalds { 12581da177e4SLinus Torvalds atomic_inc(&md->holders); 12591da177e4SLinus Torvalds } 12601da177e4SLinus Torvalds 126172d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md) 126272d94861SAlasdair G Kergon { 126372d94861SAlasdair G Kergon return md->name; 126472d94861SAlasdair G Kergon } 126572d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name); 126672d94861SAlasdair G Kergon 12671da177e4SLinus Torvalds void dm_put(struct mapped_device *md) 12681da177e4SLinus Torvalds { 12691134e5aeSMike Anderson struct dm_table *map; 12701da177e4SLinus Torvalds 1271fba9f90eSJeff Mahoney BUG_ON(test_bit(DMF_FREEING, &md->flags)); 1272fba9f90eSJeff Mahoney 1273f32c10b0SJeff Mahoney if (atomic_dec_and_lock(&md->holders, &_minor_lock)) { 12741134e5aeSMike Anderson map = dm_get_table(md); 1275ba61fdd1SJeff Mahoney idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor); 1276fba9f90eSJeff Mahoney set_bit(DMF_FREEING, &md->flags); 1277f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1278cf222b37SAlasdair G Kergon if (!dm_suspended(md)) { 12791da177e4SLinus Torvalds dm_table_presuspend_targets(map); 12801da177e4SLinus Torvalds dm_table_postsuspend_targets(map); 12811da177e4SLinus Torvalds } 12821da177e4SLinus Torvalds __unbind(md); 12831134e5aeSMike Anderson dm_table_put(map); 12841da177e4SLinus Torvalds free_dev(md); 12851da177e4SLinus Torvalds } 12861da177e4SLinus Torvalds } 128779eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put); 12881da177e4SLinus Torvalds 128946125c1cSMilan Broz static int dm_wait_for_completion(struct mapped_device *md) 129046125c1cSMilan Broz { 129146125c1cSMilan Broz int r = 0; 129246125c1cSMilan Broz 129346125c1cSMilan Broz while (1) { 129446125c1cSMilan Broz set_current_state(TASK_INTERRUPTIBLE); 129546125c1cSMilan Broz 129646125c1cSMilan Broz smp_mb(); 129746125c1cSMilan Broz if (!atomic_read(&md->pending)) 129846125c1cSMilan Broz break; 129946125c1cSMilan Broz 130046125c1cSMilan Broz if (signal_pending(current)) { 130146125c1cSMilan Broz r = -EINTR; 130246125c1cSMilan Broz break; 130346125c1cSMilan Broz } 130446125c1cSMilan Broz 130546125c1cSMilan Broz io_schedule(); 130646125c1cSMilan Broz } 130746125c1cSMilan Broz set_current_state(TASK_RUNNING); 130846125c1cSMilan Broz 130946125c1cSMilan Broz return r; 131046125c1cSMilan Broz } 131146125c1cSMilan Broz 13121da177e4SLinus Torvalds /* 13131da177e4SLinus Torvalds * Process the deferred bios 13141da177e4SLinus Torvalds */ 13156d6f10dfSMilan Broz static void __flush_deferred_io(struct mapped_device *md) 13161da177e4SLinus Torvalds { 13176d6f10dfSMilan Broz struct bio *c; 13181da177e4SLinus Torvalds 13196d6f10dfSMilan Broz while ((c = bio_list_pop(&md->deferred))) { 13209e4e5f87SMilan Broz if (__split_bio(md, c)) 13219e4e5f87SMilan Broz bio_io_error(c); 13221da177e4SLinus Torvalds } 132373d410c0SMilan Broz 132473d410c0SMilan Broz clear_bit(DMF_BLOCK_IO, &md->flags); 13251da177e4SLinus Torvalds } 13261da177e4SLinus Torvalds 13276d6f10dfSMilan Broz static void __merge_pushback_list(struct mapped_device *md) 13286d6f10dfSMilan Broz { 13296d6f10dfSMilan Broz unsigned long flags; 13306d6f10dfSMilan Broz 13316d6f10dfSMilan Broz spin_lock_irqsave(&md->pushback_lock, flags); 13326d6f10dfSMilan Broz clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 13336d6f10dfSMilan Broz bio_list_merge_head(&md->deferred, &md->pushback); 13346d6f10dfSMilan Broz bio_list_init(&md->pushback); 13356d6f10dfSMilan Broz spin_unlock_irqrestore(&md->pushback_lock, flags); 13366d6f10dfSMilan Broz } 13376d6f10dfSMilan Broz 1338304f3f6aSMilan Broz static void dm_wq_work(struct work_struct *work) 1339304f3f6aSMilan Broz { 1340304f3f6aSMilan Broz struct dm_wq_req *req = container_of(work, struct dm_wq_req, work); 1341304f3f6aSMilan Broz struct mapped_device *md = req->md; 1342304f3f6aSMilan Broz 1343304f3f6aSMilan Broz down_write(&md->io_lock); 1344304f3f6aSMilan Broz switch (req->type) { 1345304f3f6aSMilan Broz case DM_WQ_FLUSH_ALL: 1346304f3f6aSMilan Broz __merge_pushback_list(md); 1347304f3f6aSMilan Broz /* pass through */ 1348304f3f6aSMilan Broz case DM_WQ_FLUSH_DEFERRED: 1349304f3f6aSMilan Broz __flush_deferred_io(md); 1350304f3f6aSMilan Broz break; 1351304f3f6aSMilan Broz default: 1352304f3f6aSMilan Broz DMERR("dm_wq_work: unrecognised work type %d", req->type); 1353304f3f6aSMilan Broz BUG(); 1354304f3f6aSMilan Broz } 1355304f3f6aSMilan Broz up_write(&md->io_lock); 1356304f3f6aSMilan Broz } 1357304f3f6aSMilan Broz 1358304f3f6aSMilan Broz static void dm_wq_queue(struct mapped_device *md, int type, void *context, 1359304f3f6aSMilan Broz struct dm_wq_req *req) 1360304f3f6aSMilan Broz { 1361304f3f6aSMilan Broz req->type = type; 1362304f3f6aSMilan Broz req->md = md; 1363304f3f6aSMilan Broz req->context = context; 1364304f3f6aSMilan Broz INIT_WORK(&req->work, dm_wq_work); 1365304f3f6aSMilan Broz queue_work(md->wq, &req->work); 1366304f3f6aSMilan Broz } 1367304f3f6aSMilan Broz 1368304f3f6aSMilan Broz static void dm_queue_flush(struct mapped_device *md, int type, void *context) 1369304f3f6aSMilan Broz { 1370304f3f6aSMilan Broz struct dm_wq_req req; 1371304f3f6aSMilan Broz 1372304f3f6aSMilan Broz dm_wq_queue(md, type, context, &req); 1373304f3f6aSMilan Broz flush_workqueue(md->wq); 1374304f3f6aSMilan Broz } 1375304f3f6aSMilan Broz 13761da177e4SLinus Torvalds /* 13771da177e4SLinus Torvalds * Swap in a new table (destroying old one). 13781da177e4SLinus Torvalds */ 13791da177e4SLinus Torvalds int dm_swap_table(struct mapped_device *md, struct dm_table *table) 13801da177e4SLinus Torvalds { 138193c534aeSAlasdair G Kergon int r = -EINVAL; 13821da177e4SLinus Torvalds 1383e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds /* device must be suspended */ 1386cf222b37SAlasdair G Kergon if (!dm_suspended(md)) 138793c534aeSAlasdair G Kergon goto out; 13881da177e4SLinus Torvalds 1389bfa152faSJun'ichi Nomura /* without bdev, the device size cannot be changed */ 1390bfa152faSJun'ichi Nomura if (!md->suspended_bdev) 1391bfa152faSJun'ichi Nomura if (get_capacity(md->disk) != dm_table_get_size(table)) 1392bfa152faSJun'ichi Nomura goto out; 1393bfa152faSJun'ichi Nomura 13941da177e4SLinus Torvalds __unbind(md); 13951da177e4SLinus Torvalds r = __bind(md, table); 13961da177e4SLinus Torvalds 139793c534aeSAlasdair G Kergon out: 1398e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 139993c534aeSAlasdair G Kergon return r; 14001da177e4SLinus Torvalds } 14011da177e4SLinus Torvalds 14021da177e4SLinus Torvalds /* 14031da177e4SLinus Torvalds * Functions to lock and unlock any filesystem running on the 14041da177e4SLinus Torvalds * device. 14051da177e4SLinus Torvalds */ 14062ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md) 14071da177e4SLinus Torvalds { 1408e39e2e95SAlasdair G Kergon int r; 14091da177e4SLinus Torvalds 14101da177e4SLinus Torvalds WARN_ON(md->frozen_sb); 1411dfbe03f6SAlasdair G Kergon 1412e39e2e95SAlasdair G Kergon md->frozen_sb = freeze_bdev(md->suspended_bdev); 1413dfbe03f6SAlasdair G Kergon if (IS_ERR(md->frozen_sb)) { 1414cf222b37SAlasdair G Kergon r = PTR_ERR(md->frozen_sb); 1415e39e2e95SAlasdair G Kergon md->frozen_sb = NULL; 1416e39e2e95SAlasdair G Kergon return r; 1417dfbe03f6SAlasdair G Kergon } 1418dfbe03f6SAlasdair G Kergon 1419aa8d7c2fSAlasdair G Kergon set_bit(DMF_FROZEN, &md->flags); 1420aa8d7c2fSAlasdair G Kergon 14211da177e4SLinus Torvalds /* don't bdput right now, we don't want the bdev 1422e39e2e95SAlasdair G Kergon * to go away while it is locked. 14231da177e4SLinus Torvalds */ 14241da177e4SLinus Torvalds return 0; 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds 14272ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md) 14281da177e4SLinus Torvalds { 1429aa8d7c2fSAlasdair G Kergon if (!test_bit(DMF_FROZEN, &md->flags)) 1430aa8d7c2fSAlasdair G Kergon return; 1431aa8d7c2fSAlasdair G Kergon 1432e39e2e95SAlasdair G Kergon thaw_bdev(md->suspended_bdev, md->frozen_sb); 14331da177e4SLinus Torvalds md->frozen_sb = NULL; 1434aa8d7c2fSAlasdair G Kergon clear_bit(DMF_FROZEN, &md->flags); 14351da177e4SLinus Torvalds } 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds /* 14381da177e4SLinus Torvalds * We need to be able to change a mapping table under a mounted 14391da177e4SLinus Torvalds * filesystem. For example we might want to move some data in 14401da177e4SLinus Torvalds * the background. Before the table can be swapped with 14411da177e4SLinus Torvalds * dm_bind_table, dm_suspend must be called to flush any in 14421da177e4SLinus Torvalds * flight bios and ensure that any further io gets deferred. 14431da177e4SLinus Torvalds */ 1444a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags) 14451da177e4SLinus Torvalds { 14462ca3310eSAlasdair G Kergon struct dm_table *map = NULL; 14471da177e4SLinus Torvalds DECLARE_WAITQUEUE(wait, current); 144846125c1cSMilan Broz int r = 0; 1449a3d77d35SKiyoshi Ueda int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0; 14502e93ccc1SKiyoshi Ueda int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0; 14511da177e4SLinus Torvalds 1452e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 14532ca3310eSAlasdair G Kergon 145473d410c0SMilan Broz if (dm_suspended(md)) { 145573d410c0SMilan Broz r = -EINVAL; 1456d287483dSAlasdair G Kergon goto out_unlock; 145773d410c0SMilan Broz } 14581da177e4SLinus Torvalds 14591da177e4SLinus Torvalds map = dm_get_table(md); 1460cf222b37SAlasdair G Kergon 14612e93ccc1SKiyoshi Ueda /* 14622e93ccc1SKiyoshi Ueda * DMF_NOFLUSH_SUSPENDING must be set before presuspend. 14632e93ccc1SKiyoshi Ueda * This flag is cleared before dm_suspend returns. 14642e93ccc1SKiyoshi Ueda */ 14652e93ccc1SKiyoshi Ueda if (noflush) 14662e93ccc1SKiyoshi Ueda set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 14672e93ccc1SKiyoshi Ueda 1468436d4108SAlasdair G Kergon /* This does not get reverted if there's an error later. */ 14691da177e4SLinus Torvalds dm_table_presuspend_targets(map); 14701da177e4SLinus Torvalds 1471bfa152faSJun'ichi Nomura /* bdget() can stall if the pending I/Os are not flushed */ 1472bfa152faSJun'ichi Nomura if (!noflush) { 1473e39e2e95SAlasdair G Kergon md->suspended_bdev = bdget_disk(md->disk, 0); 1474e39e2e95SAlasdair G Kergon if (!md->suspended_bdev) { 1475e39e2e95SAlasdair G Kergon DMWARN("bdget failed in dm_suspend"); 1476e39e2e95SAlasdair G Kergon r = -ENOMEM; 14772e93ccc1SKiyoshi Ueda goto flush_and_out; 1478e39e2e95SAlasdair G Kergon } 1479e39e2e95SAlasdair G Kergon 14802e93ccc1SKiyoshi Ueda /* 14816d6f10dfSMilan Broz * Flush I/O to the device. noflush supersedes do_lockfs, 14826d6f10dfSMilan Broz * because lock_fs() needs to flush I/Os. 14832e93ccc1SKiyoshi Ueda */ 14846d6f10dfSMilan Broz if (do_lockfs) { 14852ca3310eSAlasdair G Kergon r = lock_fs(md); 14862ca3310eSAlasdair G Kergon if (r) 14872ca3310eSAlasdair G Kergon goto out; 1488aa8d7c2fSAlasdair G Kergon } 14896d6f10dfSMilan Broz } 14901da177e4SLinus Torvalds 14911da177e4SLinus Torvalds /* 1492354e0071SAlasdair G Kergon * First we set the BLOCK_IO flag so no more ios will be mapped. 14931da177e4SLinus Torvalds */ 14942ca3310eSAlasdair G Kergon down_write(&md->io_lock); 14952ca3310eSAlasdair G Kergon set_bit(DMF_BLOCK_IO, &md->flags); 14961da177e4SLinus Torvalds 14971da177e4SLinus Torvalds add_wait_queue(&md->wait, &wait); 14982ca3310eSAlasdair G Kergon up_write(&md->io_lock); 14991da177e4SLinus Torvalds 15001da177e4SLinus Torvalds /* unplug */ 15012ca3310eSAlasdair G Kergon if (map) 15021da177e4SLinus Torvalds dm_table_unplug_all(map); 15031da177e4SLinus Torvalds 15041da177e4SLinus Torvalds /* 150546125c1cSMilan Broz * Wait for the already-mapped ios to complete. 15061da177e4SLinus Torvalds */ 150746125c1cSMilan Broz r = dm_wait_for_completion(md); 15081da177e4SLinus Torvalds 15092ca3310eSAlasdair G Kergon down_write(&md->io_lock); 15101da177e4SLinus Torvalds remove_wait_queue(&md->wait, &wait); 15111da177e4SLinus Torvalds 15126d6f10dfSMilan Broz if (noflush) 15136d6f10dfSMilan Broz __merge_pushback_list(md); 151494d6351eSMilan Broz up_write(&md->io_lock); 15152e93ccc1SKiyoshi Ueda 15161da177e4SLinus Torvalds /* were we interrupted ? */ 151746125c1cSMilan Broz if (r < 0) { 1518304f3f6aSMilan Broz dm_queue_flush(md, DM_WQ_FLUSH_DEFERRED, NULL); 151973d410c0SMilan Broz 15202ca3310eSAlasdair G Kergon unlock_fs(md); 15212e93ccc1SKiyoshi Ueda goto out; /* pushback list is already flushed, so skip flush */ 15222ca3310eSAlasdair G Kergon } 15232ca3310eSAlasdair G Kergon 15242ca3310eSAlasdair G Kergon dm_table_postsuspend_targets(map); 15251da177e4SLinus Torvalds 15261da177e4SLinus Torvalds set_bit(DMF_SUSPENDED, &md->flags); 15271da177e4SLinus Torvalds 15282e93ccc1SKiyoshi Ueda flush_and_out: 1529304f3f6aSMilan Broz if (r && noflush) 15302e93ccc1SKiyoshi Ueda /* 15312e93ccc1SKiyoshi Ueda * Because there may be already I/Os in the pushback list, 15322e93ccc1SKiyoshi Ueda * flush them before return. 15332e93ccc1SKiyoshi Ueda */ 1534304f3f6aSMilan Broz dm_queue_flush(md, DM_WQ_FLUSH_ALL, NULL); 15352e93ccc1SKiyoshi Ueda 15362ca3310eSAlasdair G Kergon out: 1537e39e2e95SAlasdair G Kergon if (r && md->suspended_bdev) { 1538e39e2e95SAlasdair G Kergon bdput(md->suspended_bdev); 1539e39e2e95SAlasdair G Kergon md->suspended_bdev = NULL; 1540e39e2e95SAlasdair G Kergon } 1541e39e2e95SAlasdair G Kergon 15421da177e4SLinus Torvalds dm_table_put(map); 1543d287483dSAlasdair G Kergon 1544d287483dSAlasdair G Kergon out_unlock: 1545e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 1546cf222b37SAlasdair G Kergon return r; 15471da177e4SLinus Torvalds } 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds int dm_resume(struct mapped_device *md) 15501da177e4SLinus Torvalds { 1551cf222b37SAlasdair G Kergon int r = -EINVAL; 1552cf222b37SAlasdair G Kergon struct dm_table *map = NULL; 15531da177e4SLinus Torvalds 1554e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 15552ca3310eSAlasdair G Kergon if (!dm_suspended(md)) 1556cf222b37SAlasdair G Kergon goto out; 1557cf222b37SAlasdair G Kergon 1558cf222b37SAlasdair G Kergon map = dm_get_table(md); 15592ca3310eSAlasdair G Kergon if (!map || !dm_table_get_size(map)) 1560cf222b37SAlasdair G Kergon goto out; 15611da177e4SLinus Torvalds 15628757b776SMilan Broz r = dm_table_resume_targets(map); 15638757b776SMilan Broz if (r) 15648757b776SMilan Broz goto out; 15652ca3310eSAlasdair G Kergon 1566304f3f6aSMilan Broz dm_queue_flush(md, DM_WQ_FLUSH_DEFERRED, NULL); 15672ca3310eSAlasdair G Kergon 15682ca3310eSAlasdair G Kergon unlock_fs(md); 15692ca3310eSAlasdair G Kergon 1570bfa152faSJun'ichi Nomura if (md->suspended_bdev) { 1571e39e2e95SAlasdair G Kergon bdput(md->suspended_bdev); 1572e39e2e95SAlasdair G Kergon md->suspended_bdev = NULL; 1573bfa152faSJun'ichi Nomura } 1574e39e2e95SAlasdair G Kergon 15752ca3310eSAlasdair G Kergon clear_bit(DMF_SUSPENDED, &md->flags); 15762ca3310eSAlasdair G Kergon 15771da177e4SLinus Torvalds dm_table_unplug_all(map); 15781da177e4SLinus Torvalds 157969267a30SAlasdair G Kergon dm_kobject_uevent(md); 15808560ed6fSHannes Reinecke 1581cf222b37SAlasdair G Kergon r = 0; 15822ca3310eSAlasdair G Kergon 1583cf222b37SAlasdair G Kergon out: 1584cf222b37SAlasdair G Kergon dm_table_put(map); 1585e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 15862ca3310eSAlasdair G Kergon 1587cf222b37SAlasdair G Kergon return r; 15881da177e4SLinus Torvalds } 15891da177e4SLinus Torvalds 15901da177e4SLinus Torvalds /*----------------------------------------------------------------- 15911da177e4SLinus Torvalds * Event notification. 15921da177e4SLinus Torvalds *---------------------------------------------------------------*/ 159369267a30SAlasdair G Kergon void dm_kobject_uevent(struct mapped_device *md) 159469267a30SAlasdair G Kergon { 1595edfaa7c3SKay Sievers kobject_uevent(&md->disk->dev.kobj, KOBJ_CHANGE); 159669267a30SAlasdair G Kergon } 159769267a30SAlasdair G Kergon 15987a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md) 15997a8c3d3bSMike Anderson { 16007a8c3d3bSMike Anderson return atomic_add_return(1, &md->uevent_seq); 16017a8c3d3bSMike Anderson } 16027a8c3d3bSMike Anderson 16031da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md) 16041da177e4SLinus Torvalds { 16051da177e4SLinus Torvalds return atomic_read(&md->event_nr); 16061da177e4SLinus Torvalds } 16071da177e4SLinus Torvalds 16081da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr) 16091da177e4SLinus Torvalds { 16101da177e4SLinus Torvalds return wait_event_interruptible(md->eventq, 16111da177e4SLinus Torvalds (event_nr != atomic_read(&md->event_nr))); 16121da177e4SLinus Torvalds } 16131da177e4SLinus Torvalds 16147a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist) 16157a8c3d3bSMike Anderson { 16167a8c3d3bSMike Anderson unsigned long flags; 16177a8c3d3bSMike Anderson 16187a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 16197a8c3d3bSMike Anderson list_add(elist, &md->uevent_list); 16207a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 16217a8c3d3bSMike Anderson } 16227a8c3d3bSMike Anderson 16231da177e4SLinus Torvalds /* 16241da177e4SLinus Torvalds * The gendisk is only valid as long as you have a reference 16251da177e4SLinus Torvalds * count on 'md'. 16261da177e4SLinus Torvalds */ 16271da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md) 16281da177e4SLinus Torvalds { 16291da177e4SLinus Torvalds return md->disk; 16301da177e4SLinus Torvalds } 16311da177e4SLinus Torvalds 16321da177e4SLinus Torvalds int dm_suspended(struct mapped_device *md) 16331da177e4SLinus Torvalds { 16341da177e4SLinus Torvalds return test_bit(DMF_SUSPENDED, &md->flags); 16351da177e4SLinus Torvalds } 16361da177e4SLinus Torvalds 16372e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti) 16382e93ccc1SKiyoshi Ueda { 16392e93ccc1SKiyoshi Ueda struct mapped_device *md = dm_table_get_md(ti->table); 16402e93ccc1SKiyoshi Ueda int r = __noflush_suspending(md); 16412e93ccc1SKiyoshi Ueda 16422e93ccc1SKiyoshi Ueda dm_put(md); 16432e93ccc1SKiyoshi Ueda 16442e93ccc1SKiyoshi Ueda return r; 16452e93ccc1SKiyoshi Ueda } 16462e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending); 16472e93ccc1SKiyoshi Ueda 16481da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops = { 16491da177e4SLinus Torvalds .open = dm_blk_open, 16501da177e4SLinus Torvalds .release = dm_blk_close, 1651aa129a22SMilan Broz .ioctl = dm_blk_ioctl, 16523ac51e74SDarrick J. Wong .getgeo = dm_blk_getgeo, 16531da177e4SLinus Torvalds .owner = THIS_MODULE 16541da177e4SLinus Torvalds }; 16551da177e4SLinus Torvalds 16561da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo); 16571da177e4SLinus Torvalds 16581da177e4SLinus Torvalds /* 16591da177e4SLinus Torvalds * module hooks 16601da177e4SLinus Torvalds */ 16611da177e4SLinus Torvalds module_init(dm_init); 16621da177e4SLinus Torvalds module_exit(dm_exit); 16631da177e4SLinus Torvalds 16641da177e4SLinus Torvalds module_param(major, uint, 0); 16651da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper"); 16661da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver"); 16671da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 16681da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1669