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" 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds #include <linux/init.h> 121da177e4SLinus Torvalds #include <linux/module.h> 1348c9c27bSArjan van de Ven #include <linux/mutex.h> 141da177e4SLinus Torvalds #include <linux/moduleparam.h> 151da177e4SLinus Torvalds #include <linux/blkpg.h> 161da177e4SLinus Torvalds #include <linux/bio.h> 171da177e4SLinus Torvalds #include <linux/buffer_head.h> 181da177e4SLinus Torvalds #include <linux/mempool.h> 191da177e4SLinus Torvalds #include <linux/slab.h> 201da177e4SLinus Torvalds #include <linux/idr.h> 213ac51e74SDarrick J. Wong #include <linux/hdreg.h> 222056a782SJens Axboe #include <linux/blktrace_api.h> 23aa129a22SMilan Broz #include <linux/smp_lock.h> 241da177e4SLinus Torvalds 2572d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core" 2672d94861SAlasdair G Kergon 271da177e4SLinus Torvalds static const char *_name = DM_NAME; 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds static unsigned int major = 0; 301da177e4SLinus Torvalds static unsigned int _major = 0; 311da177e4SLinus Torvalds 32f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock); 331da177e4SLinus Torvalds /* 341da177e4SLinus Torvalds * One of these is allocated per bio. 351da177e4SLinus Torvalds */ 361da177e4SLinus Torvalds struct dm_io { 371da177e4SLinus Torvalds struct mapped_device *md; 381da177e4SLinus Torvalds int error; 391da177e4SLinus Torvalds struct bio *bio; 401da177e4SLinus Torvalds atomic_t io_count; 413eaf840eSJun'ichi "Nick" Nomura unsigned long start_time; 421da177e4SLinus Torvalds }; 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds /* 451da177e4SLinus Torvalds * One of these is allocated per target within a bio. Hopefully 461da177e4SLinus Torvalds * this will be simplified out one day. 471da177e4SLinus Torvalds */ 48028867acSAlasdair G Kergon struct dm_target_io { 491da177e4SLinus Torvalds struct dm_io *io; 501da177e4SLinus Torvalds struct dm_target *ti; 511da177e4SLinus Torvalds union map_info info; 521da177e4SLinus Torvalds }; 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds union map_info *dm_get_mapinfo(struct bio *bio) 551da177e4SLinus Torvalds { 561da177e4SLinus Torvalds if (bio && bio->bi_private) 57028867acSAlasdair G Kergon return &((struct dm_target_io *)bio->bi_private)->info; 581da177e4SLinus Torvalds return NULL; 591da177e4SLinus Torvalds } 601da177e4SLinus Torvalds 61ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1) 62ba61fdd1SJeff Mahoney 631da177e4SLinus Torvalds /* 641da177e4SLinus Torvalds * Bits for the md->flags field. 651da177e4SLinus Torvalds */ 661da177e4SLinus Torvalds #define DMF_BLOCK_IO 0 671da177e4SLinus Torvalds #define DMF_SUSPENDED 1 68aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2 69fba9f90eSJeff Mahoney #define DMF_FREEING 3 705c6bd75dSAlasdair G Kergon #define DMF_DELETING 4 712e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds struct mapped_device { 742ca3310eSAlasdair G Kergon struct rw_semaphore io_lock; 752ca3310eSAlasdair G Kergon struct semaphore suspend_lock; 762e93ccc1SKiyoshi Ueda spinlock_t pushback_lock; 771da177e4SLinus Torvalds rwlock_t map_lock; 781da177e4SLinus Torvalds atomic_t holders; 795c6bd75dSAlasdair G Kergon atomic_t open_count; 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds unsigned long flags; 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds request_queue_t *queue; 841da177e4SLinus Torvalds struct gendisk *disk; 857e51f257SMike Anderson char name[16]; 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds void *interface_ptr; 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds /* 901da177e4SLinus Torvalds * A list of ios that arrived while we were suspended. 911da177e4SLinus Torvalds */ 921da177e4SLinus Torvalds atomic_t pending; 931da177e4SLinus Torvalds wait_queue_head_t wait; 941da177e4SLinus Torvalds struct bio_list deferred; 952e93ccc1SKiyoshi Ueda struct bio_list pushback; 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /* 981da177e4SLinus Torvalds * The current mapping. 991da177e4SLinus Torvalds */ 1001da177e4SLinus Torvalds struct dm_table *map; 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds /* 1031da177e4SLinus Torvalds * io objects are allocated from here. 1041da177e4SLinus Torvalds */ 1051da177e4SLinus Torvalds mempool_t *io_pool; 1061da177e4SLinus Torvalds mempool_t *tio_pool; 1071da177e4SLinus Torvalds 1089faf400fSStefan Bader struct bio_set *bs; 1099faf400fSStefan Bader 1101da177e4SLinus Torvalds /* 1111da177e4SLinus Torvalds * Event handling. 1121da177e4SLinus Torvalds */ 1131da177e4SLinus Torvalds atomic_t event_nr; 1141da177e4SLinus Torvalds wait_queue_head_t eventq; 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds /* 1171da177e4SLinus Torvalds * freeze/thaw support require holding onto a super block 1181da177e4SLinus Torvalds */ 1191da177e4SLinus Torvalds struct super_block *frozen_sb; 120e39e2e95SAlasdair G Kergon struct block_device *suspended_bdev; 1213ac51e74SDarrick J. Wong 1223ac51e74SDarrick J. Wong /* forced geometry settings */ 1233ac51e74SDarrick J. Wong struct hd_geometry geometry; 1241da177e4SLinus Torvalds }; 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds #define MIN_IOS 256 127e18b890bSChristoph Lameter static struct kmem_cache *_io_cache; 128e18b890bSChristoph Lameter static struct kmem_cache *_tio_cache; 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds static int __init local_init(void) 1311da177e4SLinus Torvalds { 1321da177e4SLinus Torvalds int r; 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds /* allocate a slab for the dm_ios */ 135028867acSAlasdair G Kergon _io_cache = KMEM_CACHE(dm_io, 0); 1361da177e4SLinus Torvalds if (!_io_cache) 1371da177e4SLinus Torvalds return -ENOMEM; 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds /* allocate a slab for the target ios */ 140028867acSAlasdair G Kergon _tio_cache = KMEM_CACHE(dm_target_io, 0); 1411da177e4SLinus Torvalds if (!_tio_cache) { 1421da177e4SLinus Torvalds kmem_cache_destroy(_io_cache); 1431da177e4SLinus Torvalds return -ENOMEM; 1441da177e4SLinus Torvalds } 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds _major = major; 1471da177e4SLinus Torvalds r = register_blkdev(_major, _name); 1481da177e4SLinus Torvalds if (r < 0) { 1491da177e4SLinus Torvalds kmem_cache_destroy(_tio_cache); 1501da177e4SLinus Torvalds kmem_cache_destroy(_io_cache); 1511da177e4SLinus Torvalds return r; 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds if (!_major) 1551da177e4SLinus Torvalds _major = r; 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds return 0; 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds static void local_exit(void) 1611da177e4SLinus Torvalds { 1621da177e4SLinus Torvalds kmem_cache_destroy(_tio_cache); 1631da177e4SLinus Torvalds kmem_cache_destroy(_io_cache); 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds if (unregister_blkdev(_major, _name) < 0) 166890fbae2SGreg Kroah-Hartman DMERR("unregister_blkdev failed"); 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds _major = 0; 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds DMINFO("cleaned up"); 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds int (*_inits[])(void) __initdata = { 1741da177e4SLinus Torvalds local_init, 1751da177e4SLinus Torvalds dm_target_init, 1761da177e4SLinus Torvalds dm_linear_init, 1771da177e4SLinus Torvalds dm_stripe_init, 1781da177e4SLinus Torvalds dm_interface_init, 1791da177e4SLinus Torvalds }; 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds void (*_exits[])(void) = { 1821da177e4SLinus Torvalds local_exit, 1831da177e4SLinus Torvalds dm_target_exit, 1841da177e4SLinus Torvalds dm_linear_exit, 1851da177e4SLinus Torvalds dm_stripe_exit, 1861da177e4SLinus Torvalds dm_interface_exit, 1871da177e4SLinus Torvalds }; 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds static int __init dm_init(void) 1901da177e4SLinus Torvalds { 1911da177e4SLinus Torvalds const int count = ARRAY_SIZE(_inits); 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds int r, i; 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds for (i = 0; i < count; i++) { 1961da177e4SLinus Torvalds r = _inits[i](); 1971da177e4SLinus Torvalds if (r) 1981da177e4SLinus Torvalds goto bad; 1991da177e4SLinus Torvalds } 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds return 0; 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds bad: 2041da177e4SLinus Torvalds while (i--) 2051da177e4SLinus Torvalds _exits[i](); 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds return r; 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds static void __exit dm_exit(void) 2111da177e4SLinus Torvalds { 2121da177e4SLinus Torvalds int i = ARRAY_SIZE(_exits); 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds while (i--) 2151da177e4SLinus Torvalds _exits[i](); 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds /* 2191da177e4SLinus Torvalds * Block device functions 2201da177e4SLinus Torvalds */ 2211da177e4SLinus Torvalds static int dm_blk_open(struct inode *inode, struct file *file) 2221da177e4SLinus Torvalds { 2231da177e4SLinus Torvalds struct mapped_device *md; 2241da177e4SLinus Torvalds 225fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 226fba9f90eSJeff Mahoney 2271da177e4SLinus Torvalds md = inode->i_bdev->bd_disk->private_data; 228fba9f90eSJeff Mahoney if (!md) 229fba9f90eSJeff Mahoney goto out; 230fba9f90eSJeff Mahoney 2315c6bd75dSAlasdair G Kergon if (test_bit(DMF_FREEING, &md->flags) || 2325c6bd75dSAlasdair G Kergon test_bit(DMF_DELETING, &md->flags)) { 233fba9f90eSJeff Mahoney md = NULL; 234fba9f90eSJeff Mahoney goto out; 235fba9f90eSJeff Mahoney } 236fba9f90eSJeff Mahoney 2371da177e4SLinus Torvalds dm_get(md); 2385c6bd75dSAlasdair G Kergon atomic_inc(&md->open_count); 239fba9f90eSJeff Mahoney 240fba9f90eSJeff Mahoney out: 241fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 242fba9f90eSJeff Mahoney 243fba9f90eSJeff Mahoney return md ? 0 : -ENXIO; 2441da177e4SLinus Torvalds } 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds static int dm_blk_close(struct inode *inode, struct file *file) 2471da177e4SLinus Torvalds { 2481da177e4SLinus Torvalds struct mapped_device *md; 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds md = inode->i_bdev->bd_disk->private_data; 2515c6bd75dSAlasdair G Kergon atomic_dec(&md->open_count); 2521da177e4SLinus Torvalds dm_put(md); 2531da177e4SLinus Torvalds return 0; 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds 2565c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md) 2575c6bd75dSAlasdair G Kergon { 2585c6bd75dSAlasdair G Kergon return atomic_read(&md->open_count); 2595c6bd75dSAlasdair G Kergon } 2605c6bd75dSAlasdair G Kergon 2615c6bd75dSAlasdair G Kergon /* 2625c6bd75dSAlasdair G Kergon * Guarantees nothing is using the device before it's deleted. 2635c6bd75dSAlasdair G Kergon */ 2645c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md) 2655c6bd75dSAlasdair G Kergon { 2665c6bd75dSAlasdair G Kergon int r = 0; 2675c6bd75dSAlasdair G Kergon 2685c6bd75dSAlasdair G Kergon spin_lock(&_minor_lock); 2695c6bd75dSAlasdair G Kergon 2705c6bd75dSAlasdair G Kergon if (dm_open_count(md)) 2715c6bd75dSAlasdair G Kergon r = -EBUSY; 2725c6bd75dSAlasdair G Kergon else 2735c6bd75dSAlasdair G Kergon set_bit(DMF_DELETING, &md->flags); 2745c6bd75dSAlasdair G Kergon 2755c6bd75dSAlasdair G Kergon spin_unlock(&_minor_lock); 2765c6bd75dSAlasdair G Kergon 2775c6bd75dSAlasdair G Kergon return r; 2785c6bd75dSAlasdair G Kergon } 2795c6bd75dSAlasdair G Kergon 2803ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 2813ac51e74SDarrick J. Wong { 2823ac51e74SDarrick J. Wong struct mapped_device *md = bdev->bd_disk->private_data; 2833ac51e74SDarrick J. Wong 2843ac51e74SDarrick J. Wong return dm_get_geometry(md, geo); 2853ac51e74SDarrick J. Wong } 2863ac51e74SDarrick J. Wong 287aa129a22SMilan Broz static int dm_blk_ioctl(struct inode *inode, struct file *file, 288aa129a22SMilan Broz unsigned int cmd, unsigned long arg) 289aa129a22SMilan Broz { 290aa129a22SMilan Broz struct mapped_device *md; 291aa129a22SMilan Broz struct dm_table *map; 292aa129a22SMilan Broz struct dm_target *tgt; 293aa129a22SMilan Broz int r = -ENOTTY; 294aa129a22SMilan Broz 295aa129a22SMilan Broz /* We don't really need this lock, but we do need 'inode'. */ 296aa129a22SMilan Broz unlock_kernel(); 297aa129a22SMilan Broz 298aa129a22SMilan Broz md = inode->i_bdev->bd_disk->private_data; 299aa129a22SMilan Broz 300aa129a22SMilan Broz map = dm_get_table(md); 301aa129a22SMilan Broz 302aa129a22SMilan Broz if (!map || !dm_table_get_size(map)) 303aa129a22SMilan Broz goto out; 304aa129a22SMilan Broz 305aa129a22SMilan Broz /* We only support devices that have a single target */ 306aa129a22SMilan Broz if (dm_table_get_num_targets(map) != 1) 307aa129a22SMilan Broz goto out; 308aa129a22SMilan Broz 309aa129a22SMilan Broz tgt = dm_table_get_target(map, 0); 310aa129a22SMilan Broz 311aa129a22SMilan Broz if (dm_suspended(md)) { 312aa129a22SMilan Broz r = -EAGAIN; 313aa129a22SMilan Broz goto out; 314aa129a22SMilan Broz } 315aa129a22SMilan Broz 316aa129a22SMilan Broz if (tgt->type->ioctl) 317aa129a22SMilan Broz r = tgt->type->ioctl(tgt, inode, file, cmd, arg); 318aa129a22SMilan Broz 319aa129a22SMilan Broz out: 320aa129a22SMilan Broz dm_table_put(map); 321aa129a22SMilan Broz 322aa129a22SMilan Broz lock_kernel(); 323aa129a22SMilan Broz return r; 324aa129a22SMilan Broz } 325aa129a22SMilan Broz 326028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md) 3271da177e4SLinus Torvalds { 3281da177e4SLinus Torvalds return mempool_alloc(md->io_pool, GFP_NOIO); 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 331028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io) 3321da177e4SLinus Torvalds { 3331da177e4SLinus Torvalds mempool_free(io, md->io_pool); 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds 336028867acSAlasdair G Kergon static struct dm_target_io *alloc_tio(struct mapped_device *md) 3371da177e4SLinus Torvalds { 3381da177e4SLinus Torvalds return mempool_alloc(md->tio_pool, GFP_NOIO); 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds 341028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio) 3421da177e4SLinus Torvalds { 3431da177e4SLinus Torvalds mempool_free(tio, md->tio_pool); 3441da177e4SLinus Torvalds } 3451da177e4SLinus Torvalds 3463eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io) 3473eaf840eSJun'ichi "Nick" Nomura { 3483eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 3493eaf840eSJun'ichi "Nick" Nomura 3503eaf840eSJun'ichi "Nick" Nomura io->start_time = jiffies; 3513eaf840eSJun'ichi "Nick" Nomura 3523eaf840eSJun'ichi "Nick" Nomura preempt_disable(); 3533eaf840eSJun'ichi "Nick" Nomura disk_round_stats(dm_disk(md)); 3543eaf840eSJun'ichi "Nick" Nomura preempt_enable(); 3553eaf840eSJun'ichi "Nick" Nomura dm_disk(md)->in_flight = atomic_inc_return(&md->pending); 3563eaf840eSJun'ichi "Nick" Nomura } 3573eaf840eSJun'ichi "Nick" Nomura 3583eaf840eSJun'ichi "Nick" Nomura static int end_io_acct(struct dm_io *io) 3593eaf840eSJun'ichi "Nick" Nomura { 3603eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 3613eaf840eSJun'ichi "Nick" Nomura struct bio *bio = io->bio; 3623eaf840eSJun'ichi "Nick" Nomura unsigned long duration = jiffies - io->start_time; 3633eaf840eSJun'ichi "Nick" Nomura int pending; 3643eaf840eSJun'ichi "Nick" Nomura int rw = bio_data_dir(bio); 3653eaf840eSJun'ichi "Nick" Nomura 3663eaf840eSJun'ichi "Nick" Nomura preempt_disable(); 3673eaf840eSJun'ichi "Nick" Nomura disk_round_stats(dm_disk(md)); 3683eaf840eSJun'ichi "Nick" Nomura preempt_enable(); 3693eaf840eSJun'ichi "Nick" Nomura dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending); 3703eaf840eSJun'ichi "Nick" Nomura 3713eaf840eSJun'ichi "Nick" Nomura disk_stat_add(dm_disk(md), ticks[rw], duration); 3723eaf840eSJun'ichi "Nick" Nomura 3733eaf840eSJun'ichi "Nick" Nomura return !pending; 3743eaf840eSJun'ichi "Nick" Nomura } 3753eaf840eSJun'ichi "Nick" Nomura 3761da177e4SLinus Torvalds /* 3771da177e4SLinus Torvalds * Add the bio to the list of deferred io. 3781da177e4SLinus Torvalds */ 3791da177e4SLinus Torvalds static int queue_io(struct mapped_device *md, struct bio *bio) 3801da177e4SLinus Torvalds { 3812ca3310eSAlasdair G Kergon down_write(&md->io_lock); 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds if (!test_bit(DMF_BLOCK_IO, &md->flags)) { 3842ca3310eSAlasdair G Kergon up_write(&md->io_lock); 3851da177e4SLinus Torvalds return 1; 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds bio_list_add(&md->deferred, bio); 3891da177e4SLinus Torvalds 3902ca3310eSAlasdair G Kergon up_write(&md->io_lock); 3911da177e4SLinus Torvalds return 0; /* deferred successfully */ 3921da177e4SLinus Torvalds } 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds /* 3951da177e4SLinus Torvalds * Everyone (including functions in this file), should use this 3961da177e4SLinus Torvalds * function to access the md->map field, and make sure they call 3971da177e4SLinus Torvalds * dm_table_put() when finished. 3981da177e4SLinus Torvalds */ 3991da177e4SLinus Torvalds struct dm_table *dm_get_table(struct mapped_device *md) 4001da177e4SLinus Torvalds { 4011da177e4SLinus Torvalds struct dm_table *t; 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds read_lock(&md->map_lock); 4041da177e4SLinus Torvalds t = md->map; 4051da177e4SLinus Torvalds if (t) 4061da177e4SLinus Torvalds dm_table_get(t); 4071da177e4SLinus Torvalds read_unlock(&md->map_lock); 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds return t; 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds 4123ac51e74SDarrick J. Wong /* 4133ac51e74SDarrick J. Wong * Get the geometry associated with a dm device 4143ac51e74SDarrick J. Wong */ 4153ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) 4163ac51e74SDarrick J. Wong { 4173ac51e74SDarrick J. Wong *geo = md->geometry; 4183ac51e74SDarrick J. Wong 4193ac51e74SDarrick J. Wong return 0; 4203ac51e74SDarrick J. Wong } 4213ac51e74SDarrick J. Wong 4223ac51e74SDarrick J. Wong /* 4233ac51e74SDarrick J. Wong * Set the geometry of a device. 4243ac51e74SDarrick J. Wong */ 4253ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) 4263ac51e74SDarrick J. Wong { 4273ac51e74SDarrick J. Wong sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; 4283ac51e74SDarrick J. Wong 4293ac51e74SDarrick J. Wong if (geo->start > sz) { 4303ac51e74SDarrick J. Wong DMWARN("Start sector is beyond the geometry limits."); 4313ac51e74SDarrick J. Wong return -EINVAL; 4323ac51e74SDarrick J. Wong } 4333ac51e74SDarrick J. Wong 4343ac51e74SDarrick J. Wong md->geometry = *geo; 4353ac51e74SDarrick J. Wong 4363ac51e74SDarrick J. Wong return 0; 4373ac51e74SDarrick J. Wong } 4383ac51e74SDarrick J. Wong 4391da177e4SLinus Torvalds /*----------------------------------------------------------------- 4401da177e4SLinus Torvalds * CRUD START: 4411da177e4SLinus Torvalds * A more elegant soln is in the works that uses the queue 4421da177e4SLinus Torvalds * merge fn, unfortunately there are a couple of changes to 4431da177e4SLinus Torvalds * the block layer that I want to make for this. So in the 4441da177e4SLinus Torvalds * interests of getting something for people to use I give 4451da177e4SLinus Torvalds * you this clearly demarcated crap. 4461da177e4SLinus Torvalds *---------------------------------------------------------------*/ 4471da177e4SLinus Torvalds 4482e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md) 4492e93ccc1SKiyoshi Ueda { 4502e93ccc1SKiyoshi Ueda return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 4512e93ccc1SKiyoshi Ueda } 4522e93ccc1SKiyoshi Ueda 4531da177e4SLinus Torvalds /* 4541da177e4SLinus Torvalds * Decrements the number of outstanding ios that a bio has been 4551da177e4SLinus Torvalds * cloned into, completing the original io if necc. 4561da177e4SLinus Torvalds */ 457858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error) 4581da177e4SLinus Torvalds { 4592e93ccc1SKiyoshi Ueda unsigned long flags; 4602e93ccc1SKiyoshi Ueda 4612e93ccc1SKiyoshi Ueda /* Push-back supersedes any I/O errors */ 4622e93ccc1SKiyoshi Ueda if (error && !(io->error > 0 && __noflush_suspending(io->md))) 4631da177e4SLinus Torvalds io->error = error; 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds if (atomic_dec_and_test(&io->io_count)) { 4662e93ccc1SKiyoshi Ueda if (io->error == DM_ENDIO_REQUEUE) { 4672e93ccc1SKiyoshi Ueda /* 4682e93ccc1SKiyoshi Ueda * Target requested pushing back the I/O. 4692e93ccc1SKiyoshi Ueda * This must be handled before the sleeper on 4702e93ccc1SKiyoshi Ueda * suspend queue merges the pushback list. 4712e93ccc1SKiyoshi Ueda */ 4722e93ccc1SKiyoshi Ueda spin_lock_irqsave(&io->md->pushback_lock, flags); 4732e93ccc1SKiyoshi Ueda if (__noflush_suspending(io->md)) 4742e93ccc1SKiyoshi Ueda bio_list_add(&io->md->pushback, io->bio); 4752e93ccc1SKiyoshi Ueda else 4762e93ccc1SKiyoshi Ueda /* noflush suspend was interrupted. */ 4772e93ccc1SKiyoshi Ueda io->error = -EIO; 4782e93ccc1SKiyoshi Ueda spin_unlock_irqrestore(&io->md->pushback_lock, flags); 4792e93ccc1SKiyoshi Ueda } 4802e93ccc1SKiyoshi Ueda 4813eaf840eSJun'ichi "Nick" Nomura if (end_io_acct(io)) 4821da177e4SLinus Torvalds /* nudge anyone waiting on suspend queue */ 4831da177e4SLinus Torvalds wake_up(&io->md->wait); 4841da177e4SLinus Torvalds 4852e93ccc1SKiyoshi Ueda if (io->error != DM_ENDIO_REQUEUE) { 4862e93ccc1SKiyoshi Ueda blk_add_trace_bio(io->md->queue, io->bio, 4872e93ccc1SKiyoshi Ueda BLK_TA_COMPLETE); 4882056a782SJens Axboe 4891da177e4SLinus Torvalds bio_endio(io->bio, io->bio->bi_size, io->error); 4902e93ccc1SKiyoshi Ueda } 4912e93ccc1SKiyoshi Ueda 4921da177e4SLinus Torvalds free_io(io->md, io); 4931da177e4SLinus Torvalds } 4941da177e4SLinus Torvalds } 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds static int clone_endio(struct bio *bio, unsigned int done, int error) 4971da177e4SLinus Torvalds { 4981da177e4SLinus Torvalds int r = 0; 499028867acSAlasdair G Kergon struct dm_target_io *tio = bio->bi_private; 5009faf400fSStefan Bader struct mapped_device *md = tio->io->md; 5011da177e4SLinus Torvalds dm_endio_fn endio = tio->ti->type->end_io; 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds if (bio->bi_size) 5041da177e4SLinus Torvalds return 1; 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds if (!bio_flagged(bio, BIO_UPTODATE) && !error) 5071da177e4SLinus Torvalds error = -EIO; 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds if (endio) { 5101da177e4SLinus Torvalds r = endio(tio->ti, bio, error, &tio->info); 5112e93ccc1SKiyoshi Ueda if (r < 0 || r == DM_ENDIO_REQUEUE) 5122e93ccc1SKiyoshi Ueda /* 5132e93ccc1SKiyoshi Ueda * error and requeue request are handled 5142e93ccc1SKiyoshi Ueda * in dec_pending(). 5152e93ccc1SKiyoshi Ueda */ 5161da177e4SLinus Torvalds error = r; 51745cbcd79SKiyoshi Ueda else if (r == DM_ENDIO_INCOMPLETE) 51845cbcd79SKiyoshi Ueda /* The target will handle the io */ 5191da177e4SLinus Torvalds return 1; 52045cbcd79SKiyoshi Ueda else if (r) { 52145cbcd79SKiyoshi Ueda DMWARN("unimplemented target endio return value: %d", r); 52245cbcd79SKiyoshi Ueda BUG(); 52345cbcd79SKiyoshi Ueda } 5241da177e4SLinus Torvalds } 5251da177e4SLinus Torvalds 5269faf400fSStefan Bader dec_pending(tio->io, error); 5279faf400fSStefan Bader 5289faf400fSStefan Bader /* 5299faf400fSStefan Bader * Store md for cleanup instead of tio which is about to get freed. 5309faf400fSStefan Bader */ 5319faf400fSStefan Bader bio->bi_private = md->bs; 5329faf400fSStefan Bader 5331da177e4SLinus Torvalds bio_put(bio); 5349faf400fSStefan Bader free_tio(md, tio); 5351da177e4SLinus Torvalds return r; 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds static sector_t max_io_len(struct mapped_device *md, 5391da177e4SLinus Torvalds sector_t sector, struct dm_target *ti) 5401da177e4SLinus Torvalds { 5411da177e4SLinus Torvalds sector_t offset = sector - ti->begin; 5421da177e4SLinus Torvalds sector_t len = ti->len - offset; 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds /* 5451da177e4SLinus Torvalds * Does the target need to split even further ? 5461da177e4SLinus Torvalds */ 5471da177e4SLinus Torvalds if (ti->split_io) { 5481da177e4SLinus Torvalds sector_t boundary; 5491da177e4SLinus Torvalds boundary = ((offset + ti->split_io) & ~(ti->split_io - 1)) 5501da177e4SLinus Torvalds - offset; 5511da177e4SLinus Torvalds if (len > boundary) 5521da177e4SLinus Torvalds len = boundary; 5531da177e4SLinus Torvalds } 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds return len; 5561da177e4SLinus Torvalds } 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds static void __map_bio(struct dm_target *ti, struct bio *clone, 559028867acSAlasdair G Kergon struct dm_target_io *tio) 5601da177e4SLinus Torvalds { 5611da177e4SLinus Torvalds int r; 5622056a782SJens Axboe sector_t sector; 5639faf400fSStefan Bader struct mapped_device *md; 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds /* 5661da177e4SLinus Torvalds * Sanity checks. 5671da177e4SLinus Torvalds */ 5681da177e4SLinus Torvalds BUG_ON(!clone->bi_size); 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds clone->bi_end_io = clone_endio; 5711da177e4SLinus Torvalds clone->bi_private = tio; 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds /* 5741da177e4SLinus Torvalds * Map the clone. If r == 0 we don't need to do 5751da177e4SLinus Torvalds * anything, the target has assumed ownership of 5761da177e4SLinus Torvalds * this io. 5771da177e4SLinus Torvalds */ 5781da177e4SLinus Torvalds atomic_inc(&tio->io->io_count); 5792056a782SJens Axboe sector = clone->bi_sector; 5801da177e4SLinus Torvalds r = ti->type->map(ti, clone, &tio->info); 58145cbcd79SKiyoshi Ueda if (r == DM_MAPIO_REMAPPED) { 5821da177e4SLinus Torvalds /* the bio has been remapped so dispatch it */ 5832056a782SJens Axboe 5842056a782SJens Axboe blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone, 5852056a782SJens Axboe tio->io->bio->bi_bdev->bd_dev, sector, 5862056a782SJens Axboe clone->bi_sector); 5872056a782SJens Axboe 5881da177e4SLinus Torvalds generic_make_request(clone); 5892e93ccc1SKiyoshi Ueda } else if (r < 0 || r == DM_MAPIO_REQUEUE) { 5902e93ccc1SKiyoshi Ueda /* error the io and bail out, or requeue it if needed */ 5919faf400fSStefan Bader md = tio->io->md; 5929faf400fSStefan Bader dec_pending(tio->io, r); 5939faf400fSStefan Bader /* 5949faf400fSStefan Bader * Store bio_set for cleanup. 5959faf400fSStefan Bader */ 5969faf400fSStefan Bader clone->bi_private = md->bs; 5971da177e4SLinus Torvalds bio_put(clone); 5989faf400fSStefan Bader free_tio(md, tio); 59945cbcd79SKiyoshi Ueda } else if (r) { 60045cbcd79SKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 60145cbcd79SKiyoshi Ueda BUG(); 6021da177e4SLinus Torvalds } 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds struct clone_info { 6061da177e4SLinus Torvalds struct mapped_device *md; 6071da177e4SLinus Torvalds struct dm_table *map; 6081da177e4SLinus Torvalds struct bio *bio; 6091da177e4SLinus Torvalds struct dm_io *io; 6101da177e4SLinus Torvalds sector_t sector; 6111da177e4SLinus Torvalds sector_t sector_count; 6121da177e4SLinus Torvalds unsigned short idx; 6131da177e4SLinus Torvalds }; 6141da177e4SLinus Torvalds 6153676347aSPeter Osterlund static void dm_bio_destructor(struct bio *bio) 6163676347aSPeter Osterlund { 6179faf400fSStefan Bader struct bio_set *bs = bio->bi_private; 6189faf400fSStefan Bader 6199faf400fSStefan Bader bio_free(bio, bs); 6203676347aSPeter Osterlund } 6213676347aSPeter Osterlund 6221da177e4SLinus Torvalds /* 6231da177e4SLinus Torvalds * Creates a little bio that is just does part of a bvec. 6241da177e4SLinus Torvalds */ 6251da177e4SLinus Torvalds static struct bio *split_bvec(struct bio *bio, sector_t sector, 6261da177e4SLinus Torvalds unsigned short idx, unsigned int offset, 6279faf400fSStefan Bader unsigned int len, struct bio_set *bs) 6281da177e4SLinus Torvalds { 6291da177e4SLinus Torvalds struct bio *clone; 6301da177e4SLinus Torvalds struct bio_vec *bv = bio->bi_io_vec + idx; 6311da177e4SLinus Torvalds 6329faf400fSStefan Bader clone = bio_alloc_bioset(GFP_NOIO, 1, bs); 6333676347aSPeter Osterlund clone->bi_destructor = dm_bio_destructor; 6341da177e4SLinus Torvalds *clone->bi_io_vec = *bv; 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds clone->bi_sector = sector; 6371da177e4SLinus Torvalds clone->bi_bdev = bio->bi_bdev; 6381da177e4SLinus Torvalds clone->bi_rw = bio->bi_rw; 6391da177e4SLinus Torvalds clone->bi_vcnt = 1; 6401da177e4SLinus Torvalds clone->bi_size = to_bytes(len); 6411da177e4SLinus Torvalds clone->bi_io_vec->bv_offset = offset; 6421da177e4SLinus Torvalds clone->bi_io_vec->bv_len = clone->bi_size; 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds return clone; 6451da177e4SLinus Torvalds } 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds /* 6481da177e4SLinus Torvalds * Creates a bio that consists of range of complete bvecs. 6491da177e4SLinus Torvalds */ 6501da177e4SLinus Torvalds static struct bio *clone_bio(struct bio *bio, sector_t sector, 6511da177e4SLinus Torvalds unsigned short idx, unsigned short bv_count, 6529faf400fSStefan Bader unsigned int len, struct bio_set *bs) 6531da177e4SLinus Torvalds { 6541da177e4SLinus Torvalds struct bio *clone; 6551da177e4SLinus Torvalds 6569faf400fSStefan Bader clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs); 6579faf400fSStefan Bader __bio_clone(clone, bio); 6589faf400fSStefan Bader clone->bi_destructor = dm_bio_destructor; 6591da177e4SLinus Torvalds clone->bi_sector = sector; 6601da177e4SLinus Torvalds clone->bi_idx = idx; 6611da177e4SLinus Torvalds clone->bi_vcnt = idx + bv_count; 6621da177e4SLinus Torvalds clone->bi_size = to_bytes(len); 6631da177e4SLinus Torvalds clone->bi_flags &= ~(1 << BIO_SEG_VALID); 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds return clone; 6661da177e4SLinus Torvalds } 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds static void __clone_and_map(struct clone_info *ci) 6691da177e4SLinus Torvalds { 6701da177e4SLinus Torvalds struct bio *clone, *bio = ci->bio; 6711da177e4SLinus Torvalds struct dm_target *ti = dm_table_find_target(ci->map, ci->sector); 6721da177e4SLinus Torvalds sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti); 673028867acSAlasdair G Kergon struct dm_target_io *tio; 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds /* 6761da177e4SLinus Torvalds * Allocate a target io object. 6771da177e4SLinus Torvalds */ 6781da177e4SLinus Torvalds tio = alloc_tio(ci->md); 6791da177e4SLinus Torvalds tio->io = ci->io; 6801da177e4SLinus Torvalds tio->ti = ti; 6811da177e4SLinus Torvalds memset(&tio->info, 0, sizeof(tio->info)); 6821da177e4SLinus Torvalds 6831da177e4SLinus Torvalds if (ci->sector_count <= max) { 6841da177e4SLinus Torvalds /* 6851da177e4SLinus Torvalds * Optimise for the simple case where we can do all of 6861da177e4SLinus Torvalds * the remaining io with a single clone. 6871da177e4SLinus Torvalds */ 6881da177e4SLinus Torvalds clone = clone_bio(bio, ci->sector, ci->idx, 6899faf400fSStefan Bader bio->bi_vcnt - ci->idx, ci->sector_count, 6909faf400fSStefan Bader ci->md->bs); 6911da177e4SLinus Torvalds __map_bio(ti, clone, tio); 6921da177e4SLinus Torvalds ci->sector_count = 0; 6931da177e4SLinus Torvalds 6941da177e4SLinus Torvalds } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) { 6951da177e4SLinus Torvalds /* 6961da177e4SLinus Torvalds * There are some bvecs that don't span targets. 6971da177e4SLinus Torvalds * Do as many of these as possible. 6981da177e4SLinus Torvalds */ 6991da177e4SLinus Torvalds int i; 7001da177e4SLinus Torvalds sector_t remaining = max; 7011da177e4SLinus Torvalds sector_t bv_len; 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) { 7041da177e4SLinus Torvalds bv_len = to_sector(bio->bi_io_vec[i].bv_len); 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds if (bv_len > remaining) 7071da177e4SLinus Torvalds break; 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds remaining -= bv_len; 7101da177e4SLinus Torvalds len += bv_len; 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds 7139faf400fSStefan Bader clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len, 7149faf400fSStefan Bader ci->md->bs); 7151da177e4SLinus Torvalds __map_bio(ti, clone, tio); 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds ci->sector += len; 7181da177e4SLinus Torvalds ci->sector_count -= len; 7191da177e4SLinus Torvalds ci->idx = i; 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds } else { 7221da177e4SLinus Torvalds /* 723d2044a94SAlasdair G Kergon * Handle a bvec that must be split between two or more targets. 7241da177e4SLinus Torvalds */ 7251da177e4SLinus Torvalds struct bio_vec *bv = bio->bi_io_vec + ci->idx; 726d2044a94SAlasdair G Kergon sector_t remaining = to_sector(bv->bv_len); 727d2044a94SAlasdair G Kergon unsigned int offset = 0; 7281da177e4SLinus Torvalds 729d2044a94SAlasdair G Kergon do { 730d2044a94SAlasdair G Kergon if (offset) { 7311da177e4SLinus Torvalds ti = dm_table_find_target(ci->map, ci->sector); 732d2044a94SAlasdair G Kergon max = max_io_len(ci->md, ci->sector, ti); 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds tio = alloc_tio(ci->md); 7351da177e4SLinus Torvalds tio->io = ci->io; 7361da177e4SLinus Torvalds tio->ti = ti; 7371da177e4SLinus Torvalds memset(&tio->info, 0, sizeof(tio->info)); 738d2044a94SAlasdair G Kergon } 739d2044a94SAlasdair G Kergon 740d2044a94SAlasdair G Kergon len = min(remaining, max); 741d2044a94SAlasdair G Kergon 742d2044a94SAlasdair G Kergon clone = split_bvec(bio, ci->sector, ci->idx, 7439faf400fSStefan Bader bv->bv_offset + offset, len, 7449faf400fSStefan Bader ci->md->bs); 745d2044a94SAlasdair G Kergon 7461da177e4SLinus Torvalds __map_bio(ti, clone, tio); 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds ci->sector += len; 7491da177e4SLinus Torvalds ci->sector_count -= len; 750d2044a94SAlasdair G Kergon offset += to_bytes(len); 751d2044a94SAlasdair G Kergon } while (remaining -= len); 752d2044a94SAlasdair G Kergon 7531da177e4SLinus Torvalds ci->idx++; 7541da177e4SLinus Torvalds } 7551da177e4SLinus Torvalds } 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds /* 7581da177e4SLinus Torvalds * Split the bio into several clones. 7591da177e4SLinus Torvalds */ 7601da177e4SLinus Torvalds static void __split_bio(struct mapped_device *md, struct bio *bio) 7611da177e4SLinus Torvalds { 7621da177e4SLinus Torvalds struct clone_info ci; 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds ci.map = dm_get_table(md); 7651da177e4SLinus Torvalds if (!ci.map) { 7661da177e4SLinus Torvalds bio_io_error(bio, bio->bi_size); 7671da177e4SLinus Torvalds return; 7681da177e4SLinus Torvalds } 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds ci.md = md; 7711da177e4SLinus Torvalds ci.bio = bio; 7721da177e4SLinus Torvalds ci.io = alloc_io(md); 7731da177e4SLinus Torvalds ci.io->error = 0; 7741da177e4SLinus Torvalds atomic_set(&ci.io->io_count, 1); 7751da177e4SLinus Torvalds ci.io->bio = bio; 7761da177e4SLinus Torvalds ci.io->md = md; 7771da177e4SLinus Torvalds ci.sector = bio->bi_sector; 7781da177e4SLinus Torvalds ci.sector_count = bio_sectors(bio); 7791da177e4SLinus Torvalds ci.idx = bio->bi_idx; 7801da177e4SLinus Torvalds 7813eaf840eSJun'ichi "Nick" Nomura start_io_acct(ci.io); 7821da177e4SLinus Torvalds while (ci.sector_count) 7831da177e4SLinus Torvalds __clone_and_map(&ci); 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds /* drop the extra reference count */ 7861da177e4SLinus Torvalds dec_pending(ci.io, 0); 7871da177e4SLinus Torvalds dm_table_put(ci.map); 7881da177e4SLinus Torvalds } 7891da177e4SLinus Torvalds /*----------------------------------------------------------------- 7901da177e4SLinus Torvalds * CRUD END 7911da177e4SLinus Torvalds *---------------------------------------------------------------*/ 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds /* 7941da177e4SLinus Torvalds * The request function that just remaps the bio built up by 7951da177e4SLinus Torvalds * dm_merge_bvec. 7961da177e4SLinus Torvalds */ 7971da177e4SLinus Torvalds static int dm_request(request_queue_t *q, struct bio *bio) 7981da177e4SLinus Torvalds { 7991da177e4SLinus Torvalds int r; 80012f03a49SKevin Corry int rw = bio_data_dir(bio); 8011da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 8021da177e4SLinus Torvalds 803*07a83c47SStefan Bader /* 804*07a83c47SStefan Bader * There is no use in forwarding any barrier request since we can't 805*07a83c47SStefan Bader * guarantee it is (or can be) handled by the targets correctly. 806*07a83c47SStefan Bader */ 807*07a83c47SStefan Bader if (unlikely(bio_barrier(bio))) { 808*07a83c47SStefan Bader bio_endio(bio, bio->bi_size, -EOPNOTSUPP); 809*07a83c47SStefan Bader return 0; 810*07a83c47SStefan Bader } 811*07a83c47SStefan Bader 8122ca3310eSAlasdair G Kergon down_read(&md->io_lock); 8131da177e4SLinus Torvalds 81412f03a49SKevin Corry disk_stat_inc(dm_disk(md), ios[rw]); 81512f03a49SKevin Corry disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio)); 81612f03a49SKevin Corry 8171da177e4SLinus Torvalds /* 8181da177e4SLinus Torvalds * If we're suspended we have to queue 8191da177e4SLinus Torvalds * this io for later. 8201da177e4SLinus Torvalds */ 8211da177e4SLinus Torvalds while (test_bit(DMF_BLOCK_IO, &md->flags)) { 8222ca3310eSAlasdair G Kergon up_read(&md->io_lock); 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds if (bio_rw(bio) == READA) { 8251da177e4SLinus Torvalds bio_io_error(bio, bio->bi_size); 8261da177e4SLinus Torvalds return 0; 8271da177e4SLinus Torvalds } 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds r = queue_io(md, bio); 8301da177e4SLinus Torvalds if (r < 0) { 8311da177e4SLinus Torvalds bio_io_error(bio, bio->bi_size); 8321da177e4SLinus Torvalds return 0; 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds } else if (r == 0) 8351da177e4SLinus Torvalds return 0; /* deferred successfully */ 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds /* 8381da177e4SLinus Torvalds * We're in a while loop, because someone could suspend 8391da177e4SLinus Torvalds * before we get to the following read lock. 8401da177e4SLinus Torvalds */ 8412ca3310eSAlasdair G Kergon down_read(&md->io_lock); 8421da177e4SLinus Torvalds } 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds __split_bio(md, bio); 8452ca3310eSAlasdair G Kergon up_read(&md->io_lock); 8461da177e4SLinus Torvalds return 0; 8471da177e4SLinus Torvalds } 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds static int dm_flush_all(request_queue_t *q, struct gendisk *disk, 8501da177e4SLinus Torvalds sector_t *error_sector) 8511da177e4SLinus Torvalds { 8521da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 8531da177e4SLinus Torvalds struct dm_table *map = dm_get_table(md); 8541da177e4SLinus Torvalds int ret = -ENXIO; 8551da177e4SLinus Torvalds 8561da177e4SLinus Torvalds if (map) { 857cf222b37SAlasdair G Kergon ret = dm_table_flush_all(map); 8581da177e4SLinus Torvalds dm_table_put(map); 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds return ret; 8621da177e4SLinus Torvalds } 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds static void dm_unplug_all(request_queue_t *q) 8651da177e4SLinus Torvalds { 8661da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 8671da177e4SLinus Torvalds struct dm_table *map = dm_get_table(md); 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds if (map) { 8701da177e4SLinus Torvalds dm_table_unplug_all(map); 8711da177e4SLinus Torvalds dm_table_put(map); 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds } 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits) 8761da177e4SLinus Torvalds { 8771da177e4SLinus Torvalds int r; 8781da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) congested_data; 8791da177e4SLinus Torvalds struct dm_table *map = dm_get_table(md); 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds if (!map || test_bit(DMF_BLOCK_IO, &md->flags)) 8821da177e4SLinus Torvalds r = bdi_bits; 8831da177e4SLinus Torvalds else 8841da177e4SLinus Torvalds r = dm_table_any_congested(map, bdi_bits); 8851da177e4SLinus Torvalds 8861da177e4SLinus Torvalds dm_table_put(map); 8871da177e4SLinus Torvalds return r; 8881da177e4SLinus Torvalds } 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds /*----------------------------------------------------------------- 8911da177e4SLinus Torvalds * An IDR is used to keep track of allocated minor numbers. 8921da177e4SLinus Torvalds *---------------------------------------------------------------*/ 8931da177e4SLinus Torvalds static DEFINE_IDR(_minor_idr); 8941da177e4SLinus Torvalds 8952b06cfffSAlasdair G Kergon static void free_minor(int minor) 8961da177e4SLinus Torvalds { 897f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 8981da177e4SLinus Torvalds idr_remove(&_minor_idr, minor); 899f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 9001da177e4SLinus Torvalds } 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds /* 9031da177e4SLinus Torvalds * See if the device with a specific minor # is free. 9041da177e4SLinus Torvalds */ 9052b06cfffSAlasdair G Kergon static int specific_minor(struct mapped_device *md, int minor) 9061da177e4SLinus Torvalds { 9071da177e4SLinus Torvalds int r, m; 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds if (minor >= (1 << MINORBITS)) 9101da177e4SLinus Torvalds return -EINVAL; 9111da177e4SLinus Torvalds 91262f75c2fSJeff Mahoney r = idr_pre_get(&_minor_idr, GFP_KERNEL); 91362f75c2fSJeff Mahoney if (!r) 91462f75c2fSJeff Mahoney return -ENOMEM; 91562f75c2fSJeff Mahoney 916f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds if (idr_find(&_minor_idr, minor)) { 9191da177e4SLinus Torvalds r = -EBUSY; 9201da177e4SLinus Torvalds goto out; 9211da177e4SLinus Torvalds } 9221da177e4SLinus Torvalds 923ba61fdd1SJeff Mahoney r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m); 92462f75c2fSJeff Mahoney if (r) 9251da177e4SLinus Torvalds goto out; 9261da177e4SLinus Torvalds 9271da177e4SLinus Torvalds if (m != minor) { 9281da177e4SLinus Torvalds idr_remove(&_minor_idr, m); 9291da177e4SLinus Torvalds r = -EBUSY; 9301da177e4SLinus Torvalds goto out; 9311da177e4SLinus Torvalds } 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds out: 934f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 9351da177e4SLinus Torvalds return r; 9361da177e4SLinus Torvalds } 9371da177e4SLinus Torvalds 9382b06cfffSAlasdair G Kergon static int next_free_minor(struct mapped_device *md, int *minor) 9391da177e4SLinus Torvalds { 9402b06cfffSAlasdair G Kergon int r, m; 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds r = idr_pre_get(&_minor_idr, GFP_KERNEL); 94362f75c2fSJeff Mahoney if (!r) 94462f75c2fSJeff Mahoney return -ENOMEM; 94562f75c2fSJeff Mahoney 946f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 9471da177e4SLinus Torvalds 948ba61fdd1SJeff Mahoney r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m); 9491da177e4SLinus Torvalds if (r) { 9501da177e4SLinus Torvalds goto out; 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds if (m >= (1 << MINORBITS)) { 9541da177e4SLinus Torvalds idr_remove(&_minor_idr, m); 9551da177e4SLinus Torvalds r = -ENOSPC; 9561da177e4SLinus Torvalds goto out; 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds *minor = m; 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds out: 962f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 9631da177e4SLinus Torvalds return r; 9641da177e4SLinus Torvalds } 9651da177e4SLinus Torvalds 9661da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops; 9671da177e4SLinus Torvalds 9681da177e4SLinus Torvalds /* 9691da177e4SLinus Torvalds * Allocate and initialise a blank device with a given minor. 9701da177e4SLinus Torvalds */ 9712b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor) 9721da177e4SLinus Torvalds { 9731da177e4SLinus Torvalds int r; 9741da177e4SLinus Torvalds struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL); 975ba61fdd1SJeff Mahoney void *old_md; 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds if (!md) { 9781da177e4SLinus Torvalds DMWARN("unable to allocate device, out of memory."); 9791da177e4SLinus Torvalds return NULL; 9801da177e4SLinus Torvalds } 9811da177e4SLinus Torvalds 98210da4f79SJeff Mahoney if (!try_module_get(THIS_MODULE)) 98310da4f79SJeff Mahoney goto bad0; 98410da4f79SJeff Mahoney 9851da177e4SLinus Torvalds /* get a minor number for the dev */ 9862b06cfffSAlasdair G Kergon if (minor == DM_ANY_MINOR) 9872b06cfffSAlasdair G Kergon r = next_free_minor(md, &minor); 9882b06cfffSAlasdair G Kergon else 9892b06cfffSAlasdair G Kergon r = specific_minor(md, minor); 9901da177e4SLinus Torvalds if (r < 0) 9911da177e4SLinus Torvalds goto bad1; 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds memset(md, 0, sizeof(*md)); 9942ca3310eSAlasdair G Kergon init_rwsem(&md->io_lock); 9952ca3310eSAlasdair G Kergon init_MUTEX(&md->suspend_lock); 9962e93ccc1SKiyoshi Ueda spin_lock_init(&md->pushback_lock); 9971da177e4SLinus Torvalds rwlock_init(&md->map_lock); 9981da177e4SLinus Torvalds atomic_set(&md->holders, 1); 9995c6bd75dSAlasdair G Kergon atomic_set(&md->open_count, 0); 10001da177e4SLinus Torvalds atomic_set(&md->event_nr, 0); 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds md->queue = blk_alloc_queue(GFP_KERNEL); 10031da177e4SLinus Torvalds if (!md->queue) 1004e3f6ac61SIshai Rabinovitz goto bad1_free_minor; 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds md->queue->queuedata = md; 10071da177e4SLinus Torvalds md->queue->backing_dev_info.congested_fn = dm_any_congested; 10081da177e4SLinus Torvalds md->queue->backing_dev_info.congested_data = md; 10091da177e4SLinus Torvalds blk_queue_make_request(md->queue, dm_request); 1010daef265fSJens Axboe blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY); 10111da177e4SLinus Torvalds md->queue->unplug_fn = dm_unplug_all; 10121da177e4SLinus Torvalds md->queue->issue_flush_fn = dm_flush_all; 10131da177e4SLinus Torvalds 101493d2341cSMatthew Dobson md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache); 10151da177e4SLinus Torvalds if (!md->io_pool) 10161da177e4SLinus Torvalds goto bad2; 10171da177e4SLinus Torvalds 101893d2341cSMatthew Dobson md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache); 10191da177e4SLinus Torvalds if (!md->tio_pool) 10201da177e4SLinus Torvalds goto bad3; 10211da177e4SLinus Torvalds 10225972511bSJens Axboe md->bs = bioset_create(16, 16); 10239faf400fSStefan Bader if (!md->bs) 10249faf400fSStefan Bader goto bad_no_bioset; 10259faf400fSStefan Bader 10261da177e4SLinus Torvalds md->disk = alloc_disk(1); 10271da177e4SLinus Torvalds if (!md->disk) 10281da177e4SLinus Torvalds goto bad4; 10291da177e4SLinus Torvalds 1030f0b04115SJeff Mahoney atomic_set(&md->pending, 0); 1031f0b04115SJeff Mahoney init_waitqueue_head(&md->wait); 1032f0b04115SJeff Mahoney init_waitqueue_head(&md->eventq); 1033f0b04115SJeff Mahoney 10341da177e4SLinus Torvalds md->disk->major = _major; 10351da177e4SLinus Torvalds md->disk->first_minor = minor; 10361da177e4SLinus Torvalds md->disk->fops = &dm_blk_dops; 10371da177e4SLinus Torvalds md->disk->queue = md->queue; 10381da177e4SLinus Torvalds md->disk->private_data = md; 10391da177e4SLinus Torvalds sprintf(md->disk->disk_name, "dm-%d", minor); 10401da177e4SLinus Torvalds add_disk(md->disk); 10417e51f257SMike Anderson format_dev_t(md->name, MKDEV(_major, minor)); 10421da177e4SLinus Torvalds 1043ba61fdd1SJeff Mahoney /* Populate the mapping, nobody knows we exist yet */ 1044f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 1045ba61fdd1SJeff Mahoney old_md = idr_replace(&_minor_idr, md, minor); 1046f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1047ba61fdd1SJeff Mahoney 1048ba61fdd1SJeff Mahoney BUG_ON(old_md != MINOR_ALLOCED); 1049ba61fdd1SJeff Mahoney 10501da177e4SLinus Torvalds return md; 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds bad4: 10539faf400fSStefan Bader bioset_free(md->bs); 10549faf400fSStefan Bader bad_no_bioset: 10551da177e4SLinus Torvalds mempool_destroy(md->tio_pool); 10561da177e4SLinus Torvalds bad3: 10571da177e4SLinus Torvalds mempool_destroy(md->io_pool); 10581da177e4SLinus Torvalds bad2: 10591312f40eSAl Viro blk_cleanup_queue(md->queue); 1060e3f6ac61SIshai Rabinovitz bad1_free_minor: 10611da177e4SLinus Torvalds free_minor(minor); 10621da177e4SLinus Torvalds bad1: 106310da4f79SJeff Mahoney module_put(THIS_MODULE); 106410da4f79SJeff Mahoney bad0: 10651da177e4SLinus Torvalds kfree(md); 10661da177e4SLinus Torvalds return NULL; 10671da177e4SLinus Torvalds } 10681da177e4SLinus Torvalds 10691da177e4SLinus Torvalds static void free_dev(struct mapped_device *md) 10701da177e4SLinus Torvalds { 10712b06cfffSAlasdair G Kergon int minor = md->disk->first_minor; 107263d94e48SJun'ichi Nomura 1073d9dde59bSJun'ichi Nomura if (md->suspended_bdev) { 1074d9dde59bSJun'ichi Nomura thaw_bdev(md->suspended_bdev, NULL); 1075d9dde59bSJun'ichi Nomura bdput(md->suspended_bdev); 1076d9dde59bSJun'ichi Nomura } 10771da177e4SLinus Torvalds mempool_destroy(md->tio_pool); 10781da177e4SLinus Torvalds mempool_destroy(md->io_pool); 10799faf400fSStefan Bader bioset_free(md->bs); 10801da177e4SLinus Torvalds del_gendisk(md->disk); 108163d94e48SJun'ichi Nomura free_minor(minor); 1082fba9f90eSJeff Mahoney 1083fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 1084fba9f90eSJeff Mahoney md->disk->private_data = NULL; 1085fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 1086fba9f90eSJeff Mahoney 10871da177e4SLinus Torvalds put_disk(md->disk); 10881312f40eSAl Viro blk_cleanup_queue(md->queue); 108910da4f79SJeff Mahoney module_put(THIS_MODULE); 10901da177e4SLinus Torvalds kfree(md); 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds /* 10941da177e4SLinus Torvalds * Bind a table to the device. 10951da177e4SLinus Torvalds */ 10961da177e4SLinus Torvalds static void event_callback(void *context) 10971da177e4SLinus Torvalds { 10981da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) context; 10991da177e4SLinus Torvalds 11001da177e4SLinus Torvalds atomic_inc(&md->event_nr); 11011da177e4SLinus Torvalds wake_up(&md->eventq); 11021da177e4SLinus Torvalds } 11031da177e4SLinus Torvalds 11044e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size) 11051da177e4SLinus Torvalds { 11064e90188bSAlasdair G Kergon set_capacity(md->disk, size); 11071da177e4SLinus Torvalds 11081b1dcc1bSJes Sorensen mutex_lock(&md->suspended_bdev->bd_inode->i_mutex); 1109e39e2e95SAlasdair G Kergon i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT); 11101b1dcc1bSJes Sorensen mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex); 11111da177e4SLinus Torvalds } 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds static int __bind(struct mapped_device *md, struct dm_table *t) 11141da177e4SLinus Torvalds { 11151da177e4SLinus Torvalds request_queue_t *q = md->queue; 11161da177e4SLinus Torvalds sector_t size; 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds size = dm_table_get_size(t); 11193ac51e74SDarrick J. Wong 11203ac51e74SDarrick J. Wong /* 11213ac51e74SDarrick J. Wong * Wipe any geometry if the size of the table changed. 11223ac51e74SDarrick J. Wong */ 11233ac51e74SDarrick J. Wong if (size != get_capacity(md->disk)) 11243ac51e74SDarrick J. Wong memset(&md->geometry, 0, sizeof(md->geometry)); 11253ac51e74SDarrick J. Wong 1126bfa152faSJun'ichi Nomura if (md->suspended_bdev) 11274e90188bSAlasdair G Kergon __set_size(md, size); 11281da177e4SLinus Torvalds if (size == 0) 11291da177e4SLinus Torvalds return 0; 11301da177e4SLinus Torvalds 11311da177e4SLinus Torvalds dm_table_get(t); 1132cf222b37SAlasdair G Kergon dm_table_event_callback(t, event_callback, md); 11332ca3310eSAlasdair G Kergon 11342ca3310eSAlasdair G Kergon write_lock(&md->map_lock); 11352ca3310eSAlasdair G Kergon md->map = t; 11361da177e4SLinus Torvalds dm_table_set_restrictions(t, q); 11372ca3310eSAlasdair G Kergon write_unlock(&md->map_lock); 11382ca3310eSAlasdair G Kergon 11391da177e4SLinus Torvalds return 0; 11401da177e4SLinus Torvalds } 11411da177e4SLinus Torvalds 11421da177e4SLinus Torvalds static void __unbind(struct mapped_device *md) 11431da177e4SLinus Torvalds { 11441da177e4SLinus Torvalds struct dm_table *map = md->map; 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds if (!map) 11471da177e4SLinus Torvalds return; 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds dm_table_event_callback(map, NULL, NULL); 11501da177e4SLinus Torvalds write_lock(&md->map_lock); 11511da177e4SLinus Torvalds md->map = NULL; 11521da177e4SLinus Torvalds write_unlock(&md->map_lock); 11531da177e4SLinus Torvalds dm_table_put(map); 11541da177e4SLinus Torvalds } 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds /* 11571da177e4SLinus Torvalds * Constructor for a new device. 11581da177e4SLinus Torvalds */ 11592b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result) 11601da177e4SLinus Torvalds { 11611da177e4SLinus Torvalds struct mapped_device *md; 11621da177e4SLinus Torvalds 11632b06cfffSAlasdair G Kergon md = alloc_dev(minor); 11641da177e4SLinus Torvalds if (!md) 11651da177e4SLinus Torvalds return -ENXIO; 11661da177e4SLinus Torvalds 11671da177e4SLinus Torvalds *result = md; 11681da177e4SLinus Torvalds return 0; 11691da177e4SLinus Torvalds } 11701da177e4SLinus Torvalds 1171637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev) 11721da177e4SLinus Torvalds { 11731da177e4SLinus Torvalds struct mapped_device *md; 11741da177e4SLinus Torvalds unsigned minor = MINOR(dev); 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) 11771da177e4SLinus Torvalds return NULL; 11781da177e4SLinus Torvalds 1179f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds md = idr_find(&_minor_idr, minor); 1182fba9f90eSJeff Mahoney if (md && (md == MINOR_ALLOCED || 1183fba9f90eSJeff Mahoney (dm_disk(md)->first_minor != minor) || 1184fba9f90eSJeff Mahoney test_bit(DMF_FREEING, &md->flags))) { 1185637842cfSDavid Teigland md = NULL; 1186fba9f90eSJeff Mahoney goto out; 1187fba9f90eSJeff Mahoney } 11881da177e4SLinus Torvalds 1189fba9f90eSJeff Mahoney out: 1190f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 11911da177e4SLinus Torvalds 1192637842cfSDavid Teigland return md; 1193637842cfSDavid Teigland } 1194637842cfSDavid Teigland 1195d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev) 1196d229a958SDavid Teigland { 1197d229a958SDavid Teigland struct mapped_device *md = dm_find_md(dev); 1198d229a958SDavid Teigland 1199d229a958SDavid Teigland if (md) 1200d229a958SDavid Teigland dm_get(md); 1201d229a958SDavid Teigland 1202d229a958SDavid Teigland return md; 1203d229a958SDavid Teigland } 1204d229a958SDavid Teigland 12059ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md) 1206637842cfSDavid Teigland { 12079ade92a9SAlasdair G Kergon return md->interface_ptr; 12081da177e4SLinus Torvalds } 12091da177e4SLinus Torvalds 12101da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr) 12111da177e4SLinus Torvalds { 12121da177e4SLinus Torvalds md->interface_ptr = ptr; 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds void dm_get(struct mapped_device *md) 12161da177e4SLinus Torvalds { 12171da177e4SLinus Torvalds atomic_inc(&md->holders); 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds 122072d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md) 122172d94861SAlasdair G Kergon { 122272d94861SAlasdair G Kergon return md->name; 122372d94861SAlasdair G Kergon } 122472d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name); 122572d94861SAlasdair G Kergon 12261da177e4SLinus Torvalds void dm_put(struct mapped_device *md) 12271da177e4SLinus Torvalds { 12281134e5aeSMike Anderson struct dm_table *map; 12291da177e4SLinus Torvalds 1230fba9f90eSJeff Mahoney BUG_ON(test_bit(DMF_FREEING, &md->flags)); 1231fba9f90eSJeff Mahoney 1232f32c10b0SJeff Mahoney if (atomic_dec_and_lock(&md->holders, &_minor_lock)) { 12331134e5aeSMike Anderson map = dm_get_table(md); 1234ba61fdd1SJeff Mahoney idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor); 1235fba9f90eSJeff Mahoney set_bit(DMF_FREEING, &md->flags); 1236f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1237cf222b37SAlasdair G Kergon if (!dm_suspended(md)) { 12381da177e4SLinus Torvalds dm_table_presuspend_targets(map); 12391da177e4SLinus Torvalds dm_table_postsuspend_targets(map); 12401da177e4SLinus Torvalds } 12411da177e4SLinus Torvalds __unbind(md); 12421134e5aeSMike Anderson dm_table_put(map); 12431da177e4SLinus Torvalds free_dev(md); 12441da177e4SLinus Torvalds } 12451da177e4SLinus Torvalds } 124679eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put); 12471da177e4SLinus Torvalds 12481da177e4SLinus Torvalds /* 12491da177e4SLinus Torvalds * Process the deferred bios 12501da177e4SLinus Torvalds */ 12511da177e4SLinus Torvalds static void __flush_deferred_io(struct mapped_device *md, struct bio *c) 12521da177e4SLinus Torvalds { 12531da177e4SLinus Torvalds struct bio *n; 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds while (c) { 12561da177e4SLinus Torvalds n = c->bi_next; 12571da177e4SLinus Torvalds c->bi_next = NULL; 12581da177e4SLinus Torvalds __split_bio(md, c); 12591da177e4SLinus Torvalds c = n; 12601da177e4SLinus Torvalds } 12611da177e4SLinus Torvalds } 12621da177e4SLinus Torvalds 12631da177e4SLinus Torvalds /* 12641da177e4SLinus Torvalds * Swap in a new table (destroying old one). 12651da177e4SLinus Torvalds */ 12661da177e4SLinus Torvalds int dm_swap_table(struct mapped_device *md, struct dm_table *table) 12671da177e4SLinus Torvalds { 126893c534aeSAlasdair G Kergon int r = -EINVAL; 12691da177e4SLinus Torvalds 12702ca3310eSAlasdair G Kergon down(&md->suspend_lock); 12711da177e4SLinus Torvalds 12721da177e4SLinus Torvalds /* device must be suspended */ 1273cf222b37SAlasdair G Kergon if (!dm_suspended(md)) 127493c534aeSAlasdair G Kergon goto out; 12751da177e4SLinus Torvalds 1276bfa152faSJun'ichi Nomura /* without bdev, the device size cannot be changed */ 1277bfa152faSJun'ichi Nomura if (!md->suspended_bdev) 1278bfa152faSJun'ichi Nomura if (get_capacity(md->disk) != dm_table_get_size(table)) 1279bfa152faSJun'ichi Nomura goto out; 1280bfa152faSJun'ichi Nomura 12811da177e4SLinus Torvalds __unbind(md); 12821da177e4SLinus Torvalds r = __bind(md, table); 12831da177e4SLinus Torvalds 128493c534aeSAlasdair G Kergon out: 12852ca3310eSAlasdair G Kergon up(&md->suspend_lock); 128693c534aeSAlasdair G Kergon return r; 12871da177e4SLinus Torvalds } 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds /* 12901da177e4SLinus Torvalds * Functions to lock and unlock any filesystem running on the 12911da177e4SLinus Torvalds * device. 12921da177e4SLinus Torvalds */ 12932ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md) 12941da177e4SLinus Torvalds { 1295e39e2e95SAlasdair G Kergon int r; 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds WARN_ON(md->frozen_sb); 1298dfbe03f6SAlasdair G Kergon 1299e39e2e95SAlasdair G Kergon md->frozen_sb = freeze_bdev(md->suspended_bdev); 1300dfbe03f6SAlasdair G Kergon if (IS_ERR(md->frozen_sb)) { 1301cf222b37SAlasdair G Kergon r = PTR_ERR(md->frozen_sb); 1302e39e2e95SAlasdair G Kergon md->frozen_sb = NULL; 1303e39e2e95SAlasdair G Kergon return r; 1304dfbe03f6SAlasdair G Kergon } 1305dfbe03f6SAlasdair G Kergon 1306aa8d7c2fSAlasdair G Kergon set_bit(DMF_FROZEN, &md->flags); 1307aa8d7c2fSAlasdair G Kergon 13081da177e4SLinus Torvalds /* don't bdput right now, we don't want the bdev 1309e39e2e95SAlasdair G Kergon * to go away while it is locked. 13101da177e4SLinus Torvalds */ 13111da177e4SLinus Torvalds return 0; 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds 13142ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md) 13151da177e4SLinus Torvalds { 1316aa8d7c2fSAlasdair G Kergon if (!test_bit(DMF_FROZEN, &md->flags)) 1317aa8d7c2fSAlasdair G Kergon return; 1318aa8d7c2fSAlasdair G Kergon 1319e39e2e95SAlasdair G Kergon thaw_bdev(md->suspended_bdev, md->frozen_sb); 13201da177e4SLinus Torvalds md->frozen_sb = NULL; 1321aa8d7c2fSAlasdair G Kergon clear_bit(DMF_FROZEN, &md->flags); 13221da177e4SLinus Torvalds } 13231da177e4SLinus Torvalds 13241da177e4SLinus Torvalds /* 13251da177e4SLinus Torvalds * We need to be able to change a mapping table under a mounted 13261da177e4SLinus Torvalds * filesystem. For example we might want to move some data in 13271da177e4SLinus Torvalds * the background. Before the table can be swapped with 13281da177e4SLinus Torvalds * dm_bind_table, dm_suspend must be called to flush any in 13291da177e4SLinus Torvalds * flight bios and ensure that any further io gets deferred. 13301da177e4SLinus Torvalds */ 1331a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags) 13321da177e4SLinus Torvalds { 13332ca3310eSAlasdair G Kergon struct dm_table *map = NULL; 13342e93ccc1SKiyoshi Ueda unsigned long flags; 13351da177e4SLinus Torvalds DECLARE_WAITQUEUE(wait, current); 13361ecac7fdSJun'ichi Nomura struct bio *def; 1337cf222b37SAlasdair G Kergon int r = -EINVAL; 1338a3d77d35SKiyoshi Ueda int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0; 13392e93ccc1SKiyoshi Ueda int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0; 13401da177e4SLinus Torvalds 13412ca3310eSAlasdair G Kergon down(&md->suspend_lock); 13422ca3310eSAlasdair G Kergon 13432ca3310eSAlasdair G Kergon if (dm_suspended(md)) 1344d287483dSAlasdair G Kergon goto out_unlock; 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds map = dm_get_table(md); 1347cf222b37SAlasdair G Kergon 13482e93ccc1SKiyoshi Ueda /* 13492e93ccc1SKiyoshi Ueda * DMF_NOFLUSH_SUSPENDING must be set before presuspend. 13502e93ccc1SKiyoshi Ueda * This flag is cleared before dm_suspend returns. 13512e93ccc1SKiyoshi Ueda */ 13522e93ccc1SKiyoshi Ueda if (noflush) 13532e93ccc1SKiyoshi Ueda set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 13542e93ccc1SKiyoshi Ueda 1355436d4108SAlasdair G Kergon /* This does not get reverted if there's an error later. */ 13561da177e4SLinus Torvalds dm_table_presuspend_targets(map); 13571da177e4SLinus Torvalds 1358bfa152faSJun'ichi Nomura /* bdget() can stall if the pending I/Os are not flushed */ 1359bfa152faSJun'ichi Nomura if (!noflush) { 1360e39e2e95SAlasdair G Kergon md->suspended_bdev = bdget_disk(md->disk, 0); 1361e39e2e95SAlasdair G Kergon if (!md->suspended_bdev) { 1362e39e2e95SAlasdair G Kergon DMWARN("bdget failed in dm_suspend"); 1363e39e2e95SAlasdair G Kergon r = -ENOMEM; 13642e93ccc1SKiyoshi Ueda goto flush_and_out; 1365e39e2e95SAlasdair G Kergon } 1366bfa152faSJun'ichi Nomura } 1367e39e2e95SAlasdair G Kergon 13682e93ccc1SKiyoshi Ueda /* 13692e93ccc1SKiyoshi Ueda * Flush I/O to the device. 13702e93ccc1SKiyoshi Ueda * noflush supersedes do_lockfs, because lock_fs() needs to flush I/Os. 13712e93ccc1SKiyoshi Ueda */ 13722e93ccc1SKiyoshi Ueda if (do_lockfs && !noflush) { 13732ca3310eSAlasdair G Kergon r = lock_fs(md); 13742ca3310eSAlasdair G Kergon if (r) 13752ca3310eSAlasdair G Kergon goto out; 1376aa8d7c2fSAlasdair G Kergon } 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds /* 1379354e0071SAlasdair G Kergon * First we set the BLOCK_IO flag so no more ios will be mapped. 13801da177e4SLinus Torvalds */ 13812ca3310eSAlasdair G Kergon down_write(&md->io_lock); 13822ca3310eSAlasdair G Kergon set_bit(DMF_BLOCK_IO, &md->flags); 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds add_wait_queue(&md->wait, &wait); 13852ca3310eSAlasdair G Kergon up_write(&md->io_lock); 13861da177e4SLinus Torvalds 13871da177e4SLinus Torvalds /* unplug */ 13882ca3310eSAlasdair G Kergon if (map) 13891da177e4SLinus Torvalds dm_table_unplug_all(map); 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds /* 13921da177e4SLinus Torvalds * Then we wait for the already mapped ios to 13931da177e4SLinus Torvalds * complete. 13941da177e4SLinus Torvalds */ 13951da177e4SLinus Torvalds while (1) { 13961da177e4SLinus Torvalds set_current_state(TASK_INTERRUPTIBLE); 13971da177e4SLinus Torvalds 13981da177e4SLinus Torvalds if (!atomic_read(&md->pending) || signal_pending(current)) 13991da177e4SLinus Torvalds break; 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds io_schedule(); 14021da177e4SLinus Torvalds } 14031da177e4SLinus Torvalds set_current_state(TASK_RUNNING); 14041da177e4SLinus Torvalds 14052ca3310eSAlasdair G Kergon down_write(&md->io_lock); 14061da177e4SLinus Torvalds remove_wait_queue(&md->wait, &wait); 14071da177e4SLinus Torvalds 14082e93ccc1SKiyoshi Ueda if (noflush) { 14092e93ccc1SKiyoshi Ueda spin_lock_irqsave(&md->pushback_lock, flags); 14102e93ccc1SKiyoshi Ueda clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 14112e93ccc1SKiyoshi Ueda bio_list_merge_head(&md->deferred, &md->pushback); 14122e93ccc1SKiyoshi Ueda bio_list_init(&md->pushback); 14132e93ccc1SKiyoshi Ueda spin_unlock_irqrestore(&md->pushback_lock, flags); 14142e93ccc1SKiyoshi Ueda } 14152e93ccc1SKiyoshi Ueda 14161da177e4SLinus Torvalds /* were we interrupted ? */ 1417cf222b37SAlasdair G Kergon r = -EINTR; 14182ca3310eSAlasdair G Kergon if (atomic_read(&md->pending)) { 14191ecac7fdSJun'ichi Nomura clear_bit(DMF_BLOCK_IO, &md->flags); 14201ecac7fdSJun'ichi Nomura def = bio_list_get(&md->deferred); 14211ecac7fdSJun'ichi Nomura __flush_deferred_io(md, def); 14222ca3310eSAlasdair G Kergon up_write(&md->io_lock); 14232ca3310eSAlasdair G Kergon unlock_fs(md); 14242e93ccc1SKiyoshi Ueda goto out; /* pushback list is already flushed, so skip flush */ 14252ca3310eSAlasdair G Kergon } 14262ca3310eSAlasdair G Kergon up_write(&md->io_lock); 14272ca3310eSAlasdair G Kergon 14282ca3310eSAlasdair G Kergon dm_table_postsuspend_targets(map); 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds set_bit(DMF_SUSPENDED, &md->flags); 14311da177e4SLinus Torvalds 14322ca3310eSAlasdair G Kergon r = 0; 14332ca3310eSAlasdair G Kergon 14342e93ccc1SKiyoshi Ueda flush_and_out: 14352e93ccc1SKiyoshi Ueda if (r && noflush) { 14362e93ccc1SKiyoshi Ueda /* 14372e93ccc1SKiyoshi Ueda * Because there may be already I/Os in the pushback list, 14382e93ccc1SKiyoshi Ueda * flush them before return. 14392e93ccc1SKiyoshi Ueda */ 14402e93ccc1SKiyoshi Ueda down_write(&md->io_lock); 14412e93ccc1SKiyoshi Ueda 14422e93ccc1SKiyoshi Ueda spin_lock_irqsave(&md->pushback_lock, flags); 14432e93ccc1SKiyoshi Ueda clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 14442e93ccc1SKiyoshi Ueda bio_list_merge_head(&md->deferred, &md->pushback); 14452e93ccc1SKiyoshi Ueda bio_list_init(&md->pushback); 14462e93ccc1SKiyoshi Ueda spin_unlock_irqrestore(&md->pushback_lock, flags); 14472e93ccc1SKiyoshi Ueda 14482e93ccc1SKiyoshi Ueda def = bio_list_get(&md->deferred); 14492e93ccc1SKiyoshi Ueda __flush_deferred_io(md, def); 14502e93ccc1SKiyoshi Ueda up_write(&md->io_lock); 14512e93ccc1SKiyoshi Ueda } 14522e93ccc1SKiyoshi Ueda 14532ca3310eSAlasdair G Kergon out: 1454e39e2e95SAlasdair G Kergon if (r && md->suspended_bdev) { 1455e39e2e95SAlasdair G Kergon bdput(md->suspended_bdev); 1456e39e2e95SAlasdair G Kergon md->suspended_bdev = NULL; 1457e39e2e95SAlasdair G Kergon } 1458e39e2e95SAlasdair G Kergon 14591da177e4SLinus Torvalds dm_table_put(map); 1460d287483dSAlasdair G Kergon 1461d287483dSAlasdair G Kergon out_unlock: 14622ca3310eSAlasdair G Kergon up(&md->suspend_lock); 1463cf222b37SAlasdair G Kergon return r; 14641da177e4SLinus Torvalds } 14651da177e4SLinus Torvalds 14661da177e4SLinus Torvalds int dm_resume(struct mapped_device *md) 14671da177e4SLinus Torvalds { 1468cf222b37SAlasdair G Kergon int r = -EINVAL; 14691da177e4SLinus Torvalds struct bio *def; 1470cf222b37SAlasdair G Kergon struct dm_table *map = NULL; 14711da177e4SLinus Torvalds 14722ca3310eSAlasdair G Kergon down(&md->suspend_lock); 14732ca3310eSAlasdair G Kergon if (!dm_suspended(md)) 1474cf222b37SAlasdair G Kergon goto out; 1475cf222b37SAlasdair G Kergon 1476cf222b37SAlasdair G Kergon map = dm_get_table(md); 14772ca3310eSAlasdair G Kergon if (!map || !dm_table_get_size(map)) 1478cf222b37SAlasdair G Kergon goto out; 14791da177e4SLinus Torvalds 14808757b776SMilan Broz r = dm_table_resume_targets(map); 14818757b776SMilan Broz if (r) 14828757b776SMilan Broz goto out; 14832ca3310eSAlasdair G Kergon 14842ca3310eSAlasdair G Kergon down_write(&md->io_lock); 14851da177e4SLinus Torvalds clear_bit(DMF_BLOCK_IO, &md->flags); 14861da177e4SLinus Torvalds 14871da177e4SLinus Torvalds def = bio_list_get(&md->deferred); 14881da177e4SLinus Torvalds __flush_deferred_io(md, def); 14892ca3310eSAlasdair G Kergon up_write(&md->io_lock); 14902ca3310eSAlasdair G Kergon 14912ca3310eSAlasdair G Kergon unlock_fs(md); 14922ca3310eSAlasdair G Kergon 1493bfa152faSJun'ichi Nomura if (md->suspended_bdev) { 1494e39e2e95SAlasdair G Kergon bdput(md->suspended_bdev); 1495e39e2e95SAlasdair G Kergon md->suspended_bdev = NULL; 1496bfa152faSJun'ichi Nomura } 1497e39e2e95SAlasdair G Kergon 14982ca3310eSAlasdair G Kergon clear_bit(DMF_SUSPENDED, &md->flags); 14992ca3310eSAlasdair G Kergon 15001da177e4SLinus Torvalds dm_table_unplug_all(map); 15011da177e4SLinus Torvalds 15028560ed6fSHannes Reinecke kobject_uevent(&md->disk->kobj, KOBJ_CHANGE); 15038560ed6fSHannes Reinecke 1504cf222b37SAlasdair G Kergon r = 0; 15052ca3310eSAlasdair G Kergon 1506cf222b37SAlasdair G Kergon out: 1507cf222b37SAlasdair G Kergon dm_table_put(map); 15082ca3310eSAlasdair G Kergon up(&md->suspend_lock); 15092ca3310eSAlasdair G Kergon 1510cf222b37SAlasdair G Kergon return r; 15111da177e4SLinus Torvalds } 15121da177e4SLinus Torvalds 15131da177e4SLinus Torvalds /*----------------------------------------------------------------- 15141da177e4SLinus Torvalds * Event notification. 15151da177e4SLinus Torvalds *---------------------------------------------------------------*/ 15161da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md) 15171da177e4SLinus Torvalds { 15181da177e4SLinus Torvalds return atomic_read(&md->event_nr); 15191da177e4SLinus Torvalds } 15201da177e4SLinus Torvalds 15211da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr) 15221da177e4SLinus Torvalds { 15231da177e4SLinus Torvalds return wait_event_interruptible(md->eventq, 15241da177e4SLinus Torvalds (event_nr != atomic_read(&md->event_nr))); 15251da177e4SLinus Torvalds } 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds /* 15281da177e4SLinus Torvalds * The gendisk is only valid as long as you have a reference 15291da177e4SLinus Torvalds * count on 'md'. 15301da177e4SLinus Torvalds */ 15311da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md) 15321da177e4SLinus Torvalds { 15331da177e4SLinus Torvalds return md->disk; 15341da177e4SLinus Torvalds } 15351da177e4SLinus Torvalds 15361da177e4SLinus Torvalds int dm_suspended(struct mapped_device *md) 15371da177e4SLinus Torvalds { 15381da177e4SLinus Torvalds return test_bit(DMF_SUSPENDED, &md->flags); 15391da177e4SLinus Torvalds } 15401da177e4SLinus Torvalds 15412e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti) 15422e93ccc1SKiyoshi Ueda { 15432e93ccc1SKiyoshi Ueda struct mapped_device *md = dm_table_get_md(ti->table); 15442e93ccc1SKiyoshi Ueda int r = __noflush_suspending(md); 15452e93ccc1SKiyoshi Ueda 15462e93ccc1SKiyoshi Ueda dm_put(md); 15472e93ccc1SKiyoshi Ueda 15482e93ccc1SKiyoshi Ueda return r; 15492e93ccc1SKiyoshi Ueda } 15502e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending); 15512e93ccc1SKiyoshi Ueda 15521da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops = { 15531da177e4SLinus Torvalds .open = dm_blk_open, 15541da177e4SLinus Torvalds .release = dm_blk_close, 1555aa129a22SMilan Broz .ioctl = dm_blk_ioctl, 15563ac51e74SDarrick J. Wong .getgeo = dm_blk_getgeo, 15571da177e4SLinus Torvalds .owner = THIS_MODULE 15581da177e4SLinus Torvalds }; 15591da177e4SLinus Torvalds 15601da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo); 15611da177e4SLinus Torvalds 15621da177e4SLinus Torvalds /* 15631da177e4SLinus Torvalds * module hooks 15641da177e4SLinus Torvalds */ 15651da177e4SLinus Torvalds module_init(dm_init); 15661da177e4SLinus Torvalds module_exit(dm_exit); 15671da177e4SLinus Torvalds 15681da177e4SLinus Torvalds module_param(major, uint, 0); 15691da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper"); 15701da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver"); 15711da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 15721da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1573