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 */ 481da177e4SLinus Torvalds struct 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) 571da177e4SLinus Torvalds return &((struct 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 */ 1351da177e4SLinus Torvalds _io_cache = kmem_cache_create("dm_io", 1361da177e4SLinus Torvalds sizeof(struct dm_io), 0, 0, NULL, NULL); 1371da177e4SLinus Torvalds if (!_io_cache) 1381da177e4SLinus Torvalds return -ENOMEM; 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds /* allocate a slab for the target ios */ 1411da177e4SLinus Torvalds _tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io), 1421da177e4SLinus Torvalds 0, 0, NULL, NULL); 1431da177e4SLinus Torvalds if (!_tio_cache) { 1441da177e4SLinus Torvalds kmem_cache_destroy(_io_cache); 1451da177e4SLinus Torvalds return -ENOMEM; 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds _major = major; 1491da177e4SLinus Torvalds r = register_blkdev(_major, _name); 1501da177e4SLinus Torvalds if (r < 0) { 1511da177e4SLinus Torvalds kmem_cache_destroy(_tio_cache); 1521da177e4SLinus Torvalds kmem_cache_destroy(_io_cache); 1531da177e4SLinus Torvalds return r; 1541da177e4SLinus Torvalds } 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds if (!_major) 1571da177e4SLinus Torvalds _major = r; 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds return 0; 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds static void local_exit(void) 1631da177e4SLinus Torvalds { 1641da177e4SLinus Torvalds kmem_cache_destroy(_tio_cache); 1651da177e4SLinus Torvalds kmem_cache_destroy(_io_cache); 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds if (unregister_blkdev(_major, _name) < 0) 168890fbae2SGreg Kroah-Hartman DMERR("unregister_blkdev failed"); 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds _major = 0; 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds DMINFO("cleaned up"); 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds int (*_inits[])(void) __initdata = { 1761da177e4SLinus Torvalds local_init, 1771da177e4SLinus Torvalds dm_target_init, 1781da177e4SLinus Torvalds dm_linear_init, 1791da177e4SLinus Torvalds dm_stripe_init, 1801da177e4SLinus Torvalds dm_interface_init, 1811da177e4SLinus Torvalds }; 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds void (*_exits[])(void) = { 1841da177e4SLinus Torvalds local_exit, 1851da177e4SLinus Torvalds dm_target_exit, 1861da177e4SLinus Torvalds dm_linear_exit, 1871da177e4SLinus Torvalds dm_stripe_exit, 1881da177e4SLinus Torvalds dm_interface_exit, 1891da177e4SLinus Torvalds }; 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds static int __init dm_init(void) 1921da177e4SLinus Torvalds { 1931da177e4SLinus Torvalds const int count = ARRAY_SIZE(_inits); 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds int r, i; 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds for (i = 0; i < count; i++) { 1981da177e4SLinus Torvalds r = _inits[i](); 1991da177e4SLinus Torvalds if (r) 2001da177e4SLinus Torvalds goto bad; 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds return 0; 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds bad: 2061da177e4SLinus Torvalds while (i--) 2071da177e4SLinus Torvalds _exits[i](); 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds return r; 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds static void __exit dm_exit(void) 2131da177e4SLinus Torvalds { 2141da177e4SLinus Torvalds int i = ARRAY_SIZE(_exits); 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds while (i--) 2171da177e4SLinus Torvalds _exits[i](); 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds /* 2211da177e4SLinus Torvalds * Block device functions 2221da177e4SLinus Torvalds */ 2231da177e4SLinus Torvalds static int dm_blk_open(struct inode *inode, struct file *file) 2241da177e4SLinus Torvalds { 2251da177e4SLinus Torvalds struct mapped_device *md; 2261da177e4SLinus Torvalds 227fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 228fba9f90eSJeff Mahoney 2291da177e4SLinus Torvalds md = inode->i_bdev->bd_disk->private_data; 230fba9f90eSJeff Mahoney if (!md) 231fba9f90eSJeff Mahoney goto out; 232fba9f90eSJeff Mahoney 2335c6bd75dSAlasdair G Kergon if (test_bit(DMF_FREEING, &md->flags) || 2345c6bd75dSAlasdair G Kergon test_bit(DMF_DELETING, &md->flags)) { 235fba9f90eSJeff Mahoney md = NULL; 236fba9f90eSJeff Mahoney goto out; 237fba9f90eSJeff Mahoney } 238fba9f90eSJeff Mahoney 2391da177e4SLinus Torvalds dm_get(md); 2405c6bd75dSAlasdair G Kergon atomic_inc(&md->open_count); 241fba9f90eSJeff Mahoney 242fba9f90eSJeff Mahoney out: 243fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 244fba9f90eSJeff Mahoney 245fba9f90eSJeff Mahoney return md ? 0 : -ENXIO; 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds static int dm_blk_close(struct inode *inode, struct file *file) 2491da177e4SLinus Torvalds { 2501da177e4SLinus Torvalds struct mapped_device *md; 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds md = inode->i_bdev->bd_disk->private_data; 2535c6bd75dSAlasdair G Kergon atomic_dec(&md->open_count); 2541da177e4SLinus Torvalds dm_put(md); 2551da177e4SLinus Torvalds return 0; 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds 2585c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md) 2595c6bd75dSAlasdair G Kergon { 2605c6bd75dSAlasdair G Kergon return atomic_read(&md->open_count); 2615c6bd75dSAlasdair G Kergon } 2625c6bd75dSAlasdair G Kergon 2635c6bd75dSAlasdair G Kergon /* 2645c6bd75dSAlasdair G Kergon * Guarantees nothing is using the device before it's deleted. 2655c6bd75dSAlasdair G Kergon */ 2665c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md) 2675c6bd75dSAlasdair G Kergon { 2685c6bd75dSAlasdair G Kergon int r = 0; 2695c6bd75dSAlasdair G Kergon 2705c6bd75dSAlasdair G Kergon spin_lock(&_minor_lock); 2715c6bd75dSAlasdair G Kergon 2725c6bd75dSAlasdair G Kergon if (dm_open_count(md)) 2735c6bd75dSAlasdair G Kergon r = -EBUSY; 2745c6bd75dSAlasdair G Kergon else 2755c6bd75dSAlasdair G Kergon set_bit(DMF_DELETING, &md->flags); 2765c6bd75dSAlasdair G Kergon 2775c6bd75dSAlasdair G Kergon spin_unlock(&_minor_lock); 2785c6bd75dSAlasdair G Kergon 2795c6bd75dSAlasdair G Kergon return r; 2805c6bd75dSAlasdair G Kergon } 2815c6bd75dSAlasdair G Kergon 2823ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 2833ac51e74SDarrick J. Wong { 2843ac51e74SDarrick J. Wong struct mapped_device *md = bdev->bd_disk->private_data; 2853ac51e74SDarrick J. Wong 2863ac51e74SDarrick J. Wong return dm_get_geometry(md, geo); 2873ac51e74SDarrick J. Wong } 2883ac51e74SDarrick J. Wong 289aa129a22SMilan Broz static int dm_blk_ioctl(struct inode *inode, struct file *file, 290aa129a22SMilan Broz unsigned int cmd, unsigned long arg) 291aa129a22SMilan Broz { 292aa129a22SMilan Broz struct mapped_device *md; 293aa129a22SMilan Broz struct dm_table *map; 294aa129a22SMilan Broz struct dm_target *tgt; 295aa129a22SMilan Broz int r = -ENOTTY; 296aa129a22SMilan Broz 297aa129a22SMilan Broz /* We don't really need this lock, but we do need 'inode'. */ 298aa129a22SMilan Broz unlock_kernel(); 299aa129a22SMilan Broz 300aa129a22SMilan Broz md = inode->i_bdev->bd_disk->private_data; 301aa129a22SMilan Broz 302aa129a22SMilan Broz map = dm_get_table(md); 303aa129a22SMilan Broz 304aa129a22SMilan Broz if (!map || !dm_table_get_size(map)) 305aa129a22SMilan Broz goto out; 306aa129a22SMilan Broz 307aa129a22SMilan Broz /* We only support devices that have a single target */ 308aa129a22SMilan Broz if (dm_table_get_num_targets(map) != 1) 309aa129a22SMilan Broz goto out; 310aa129a22SMilan Broz 311aa129a22SMilan Broz tgt = dm_table_get_target(map, 0); 312aa129a22SMilan Broz 313aa129a22SMilan Broz if (dm_suspended(md)) { 314aa129a22SMilan Broz r = -EAGAIN; 315aa129a22SMilan Broz goto out; 316aa129a22SMilan Broz } 317aa129a22SMilan Broz 318aa129a22SMilan Broz if (tgt->type->ioctl) 319aa129a22SMilan Broz r = tgt->type->ioctl(tgt, inode, file, cmd, arg); 320aa129a22SMilan Broz 321aa129a22SMilan Broz out: 322aa129a22SMilan Broz dm_table_put(map); 323aa129a22SMilan Broz 324aa129a22SMilan Broz lock_kernel(); 325aa129a22SMilan Broz return r; 326aa129a22SMilan Broz } 327aa129a22SMilan Broz 3281da177e4SLinus Torvalds static inline struct dm_io *alloc_io(struct mapped_device *md) 3291da177e4SLinus Torvalds { 3301da177e4SLinus Torvalds return mempool_alloc(md->io_pool, GFP_NOIO); 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds static inline void free_io(struct mapped_device *md, struct dm_io *io) 3341da177e4SLinus Torvalds { 3351da177e4SLinus Torvalds mempool_free(io, md->io_pool); 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds static inline struct target_io *alloc_tio(struct mapped_device *md) 3391da177e4SLinus Torvalds { 3401da177e4SLinus Torvalds return mempool_alloc(md->tio_pool, GFP_NOIO); 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds static inline void free_tio(struct mapped_device *md, struct target_io *tio) 3441da177e4SLinus Torvalds { 3451da177e4SLinus Torvalds mempool_free(tio, md->tio_pool); 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds 3483eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io) 3493eaf840eSJun'ichi "Nick" Nomura { 3503eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 3513eaf840eSJun'ichi "Nick" Nomura 3523eaf840eSJun'ichi "Nick" Nomura io->start_time = jiffies; 3533eaf840eSJun'ichi "Nick" Nomura 3543eaf840eSJun'ichi "Nick" Nomura preempt_disable(); 3553eaf840eSJun'ichi "Nick" Nomura disk_round_stats(dm_disk(md)); 3563eaf840eSJun'ichi "Nick" Nomura preempt_enable(); 3573eaf840eSJun'ichi "Nick" Nomura dm_disk(md)->in_flight = atomic_inc_return(&md->pending); 3583eaf840eSJun'ichi "Nick" Nomura } 3593eaf840eSJun'ichi "Nick" Nomura 3603eaf840eSJun'ichi "Nick" Nomura static int end_io_acct(struct dm_io *io) 3613eaf840eSJun'ichi "Nick" Nomura { 3623eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 3633eaf840eSJun'ichi "Nick" Nomura struct bio *bio = io->bio; 3643eaf840eSJun'ichi "Nick" Nomura unsigned long duration = jiffies - io->start_time; 3653eaf840eSJun'ichi "Nick" Nomura int pending; 3663eaf840eSJun'ichi "Nick" Nomura int rw = bio_data_dir(bio); 3673eaf840eSJun'ichi "Nick" Nomura 3683eaf840eSJun'ichi "Nick" Nomura preempt_disable(); 3693eaf840eSJun'ichi "Nick" Nomura disk_round_stats(dm_disk(md)); 3703eaf840eSJun'ichi "Nick" Nomura preempt_enable(); 3713eaf840eSJun'ichi "Nick" Nomura dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending); 3723eaf840eSJun'ichi "Nick" Nomura 3733eaf840eSJun'ichi "Nick" Nomura disk_stat_add(dm_disk(md), ticks[rw], duration); 3743eaf840eSJun'ichi "Nick" Nomura 3753eaf840eSJun'ichi "Nick" Nomura return !pending; 3763eaf840eSJun'ichi "Nick" Nomura } 3773eaf840eSJun'ichi "Nick" Nomura 3781da177e4SLinus Torvalds /* 3791da177e4SLinus Torvalds * Add the bio to the list of deferred io. 3801da177e4SLinus Torvalds */ 3811da177e4SLinus Torvalds static int queue_io(struct mapped_device *md, struct bio *bio) 3821da177e4SLinus Torvalds { 3832ca3310eSAlasdair G Kergon down_write(&md->io_lock); 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds if (!test_bit(DMF_BLOCK_IO, &md->flags)) { 3862ca3310eSAlasdair G Kergon up_write(&md->io_lock); 3871da177e4SLinus Torvalds return 1; 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds bio_list_add(&md->deferred, bio); 3911da177e4SLinus Torvalds 3922ca3310eSAlasdair G Kergon up_write(&md->io_lock); 3931da177e4SLinus Torvalds return 0; /* deferred successfully */ 3941da177e4SLinus Torvalds } 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds /* 3971da177e4SLinus Torvalds * Everyone (including functions in this file), should use this 3981da177e4SLinus Torvalds * function to access the md->map field, and make sure they call 3991da177e4SLinus Torvalds * dm_table_put() when finished. 4001da177e4SLinus Torvalds */ 4011da177e4SLinus Torvalds struct dm_table *dm_get_table(struct mapped_device *md) 4021da177e4SLinus Torvalds { 4031da177e4SLinus Torvalds struct dm_table *t; 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds read_lock(&md->map_lock); 4061da177e4SLinus Torvalds t = md->map; 4071da177e4SLinus Torvalds if (t) 4081da177e4SLinus Torvalds dm_table_get(t); 4091da177e4SLinus Torvalds read_unlock(&md->map_lock); 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds return t; 4121da177e4SLinus Torvalds } 4131da177e4SLinus Torvalds 4143ac51e74SDarrick J. Wong /* 4153ac51e74SDarrick J. Wong * Get the geometry associated with a dm device 4163ac51e74SDarrick J. Wong */ 4173ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) 4183ac51e74SDarrick J. Wong { 4193ac51e74SDarrick J. Wong *geo = md->geometry; 4203ac51e74SDarrick J. Wong 4213ac51e74SDarrick J. Wong return 0; 4223ac51e74SDarrick J. Wong } 4233ac51e74SDarrick J. Wong 4243ac51e74SDarrick J. Wong /* 4253ac51e74SDarrick J. Wong * Set the geometry of a device. 4263ac51e74SDarrick J. Wong */ 4273ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) 4283ac51e74SDarrick J. Wong { 4293ac51e74SDarrick J. Wong sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; 4303ac51e74SDarrick J. Wong 4313ac51e74SDarrick J. Wong if (geo->start > sz) { 4323ac51e74SDarrick J. Wong DMWARN("Start sector is beyond the geometry limits."); 4333ac51e74SDarrick J. Wong return -EINVAL; 4343ac51e74SDarrick J. Wong } 4353ac51e74SDarrick J. Wong 4363ac51e74SDarrick J. Wong md->geometry = *geo; 4373ac51e74SDarrick J. Wong 4383ac51e74SDarrick J. Wong return 0; 4393ac51e74SDarrick J. Wong } 4403ac51e74SDarrick J. Wong 4411da177e4SLinus Torvalds /*----------------------------------------------------------------- 4421da177e4SLinus Torvalds * CRUD START: 4431da177e4SLinus Torvalds * A more elegant soln is in the works that uses the queue 4441da177e4SLinus Torvalds * merge fn, unfortunately there are a couple of changes to 4451da177e4SLinus Torvalds * the block layer that I want to make for this. So in the 4461da177e4SLinus Torvalds * interests of getting something for people to use I give 4471da177e4SLinus Torvalds * you this clearly demarcated crap. 4481da177e4SLinus Torvalds *---------------------------------------------------------------*/ 4491da177e4SLinus Torvalds 4502e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md) 4512e93ccc1SKiyoshi Ueda { 4522e93ccc1SKiyoshi Ueda return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 4532e93ccc1SKiyoshi Ueda } 4542e93ccc1SKiyoshi Ueda 4551da177e4SLinus Torvalds /* 4561da177e4SLinus Torvalds * Decrements the number of outstanding ios that a bio has been 4571da177e4SLinus Torvalds * cloned into, completing the original io if necc. 4581da177e4SLinus Torvalds */ 459858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error) 4601da177e4SLinus Torvalds { 4612e93ccc1SKiyoshi Ueda unsigned long flags; 4622e93ccc1SKiyoshi Ueda 4632e93ccc1SKiyoshi Ueda /* Push-back supersedes any I/O errors */ 4642e93ccc1SKiyoshi Ueda if (error && !(io->error > 0 && __noflush_suspending(io->md))) 4651da177e4SLinus Torvalds io->error = error; 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds if (atomic_dec_and_test(&io->io_count)) { 4682e93ccc1SKiyoshi Ueda if (io->error == DM_ENDIO_REQUEUE) { 4692e93ccc1SKiyoshi Ueda /* 4702e93ccc1SKiyoshi Ueda * Target requested pushing back the I/O. 4712e93ccc1SKiyoshi Ueda * This must be handled before the sleeper on 4722e93ccc1SKiyoshi Ueda * suspend queue merges the pushback list. 4732e93ccc1SKiyoshi Ueda */ 4742e93ccc1SKiyoshi Ueda spin_lock_irqsave(&io->md->pushback_lock, flags); 4752e93ccc1SKiyoshi Ueda if (__noflush_suspending(io->md)) 4762e93ccc1SKiyoshi Ueda bio_list_add(&io->md->pushback, io->bio); 4772e93ccc1SKiyoshi Ueda else 4782e93ccc1SKiyoshi Ueda /* noflush suspend was interrupted. */ 4792e93ccc1SKiyoshi Ueda io->error = -EIO; 4802e93ccc1SKiyoshi Ueda spin_unlock_irqrestore(&io->md->pushback_lock, flags); 4812e93ccc1SKiyoshi Ueda } 4822e93ccc1SKiyoshi Ueda 4833eaf840eSJun'ichi "Nick" Nomura if (end_io_acct(io)) 4841da177e4SLinus Torvalds /* nudge anyone waiting on suspend queue */ 4851da177e4SLinus Torvalds wake_up(&io->md->wait); 4861da177e4SLinus Torvalds 4872e93ccc1SKiyoshi Ueda if (io->error != DM_ENDIO_REQUEUE) { 4882e93ccc1SKiyoshi Ueda blk_add_trace_bio(io->md->queue, io->bio, 4892e93ccc1SKiyoshi Ueda BLK_TA_COMPLETE); 4902056a782SJens Axboe 4911da177e4SLinus Torvalds bio_endio(io->bio, io->bio->bi_size, io->error); 4922e93ccc1SKiyoshi Ueda } 4932e93ccc1SKiyoshi Ueda 4941da177e4SLinus Torvalds free_io(io->md, io); 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds static int clone_endio(struct bio *bio, unsigned int done, int error) 4991da177e4SLinus Torvalds { 5001da177e4SLinus Torvalds int r = 0; 5011da177e4SLinus Torvalds struct target_io *tio = bio->bi_private; 5029faf400fSStefan Bader struct mapped_device *md = tio->io->md; 5031da177e4SLinus Torvalds dm_endio_fn endio = tio->ti->type->end_io; 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds if (bio->bi_size) 5061da177e4SLinus Torvalds return 1; 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds if (!bio_flagged(bio, BIO_UPTODATE) && !error) 5091da177e4SLinus Torvalds error = -EIO; 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds if (endio) { 5121da177e4SLinus Torvalds r = endio(tio->ti, bio, error, &tio->info); 5132e93ccc1SKiyoshi Ueda if (r < 0 || r == DM_ENDIO_REQUEUE) 5142e93ccc1SKiyoshi Ueda /* 5152e93ccc1SKiyoshi Ueda * error and requeue request are handled 5162e93ccc1SKiyoshi Ueda * in dec_pending(). 5172e93ccc1SKiyoshi Ueda */ 5181da177e4SLinus Torvalds error = r; 51945cbcd79SKiyoshi Ueda else if (r == DM_ENDIO_INCOMPLETE) 52045cbcd79SKiyoshi Ueda /* The target will handle the io */ 5211da177e4SLinus Torvalds return 1; 52245cbcd79SKiyoshi Ueda else if (r) { 52345cbcd79SKiyoshi Ueda DMWARN("unimplemented target endio return value: %d", r); 52445cbcd79SKiyoshi Ueda BUG(); 52545cbcd79SKiyoshi Ueda } 5261da177e4SLinus Torvalds } 5271da177e4SLinus Torvalds 5289faf400fSStefan Bader dec_pending(tio->io, error); 5299faf400fSStefan Bader 5309faf400fSStefan Bader /* 5319faf400fSStefan Bader * Store md for cleanup instead of tio which is about to get freed. 5329faf400fSStefan Bader */ 5339faf400fSStefan Bader bio->bi_private = md->bs; 5349faf400fSStefan Bader 5351da177e4SLinus Torvalds bio_put(bio); 5369faf400fSStefan Bader free_tio(md, tio); 5371da177e4SLinus Torvalds return r; 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds static sector_t max_io_len(struct mapped_device *md, 5411da177e4SLinus Torvalds sector_t sector, struct dm_target *ti) 5421da177e4SLinus Torvalds { 5431da177e4SLinus Torvalds sector_t offset = sector - ti->begin; 5441da177e4SLinus Torvalds sector_t len = ti->len - offset; 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds /* 5471da177e4SLinus Torvalds * Does the target need to split even further ? 5481da177e4SLinus Torvalds */ 5491da177e4SLinus Torvalds if (ti->split_io) { 5501da177e4SLinus Torvalds sector_t boundary; 5511da177e4SLinus Torvalds boundary = ((offset + ti->split_io) & ~(ti->split_io - 1)) 5521da177e4SLinus Torvalds - offset; 5531da177e4SLinus Torvalds if (len > boundary) 5541da177e4SLinus Torvalds len = boundary; 5551da177e4SLinus Torvalds } 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds return len; 5581da177e4SLinus Torvalds } 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds static void __map_bio(struct dm_target *ti, struct bio *clone, 5611da177e4SLinus Torvalds struct target_io *tio) 5621da177e4SLinus Torvalds { 5631da177e4SLinus Torvalds int r; 5642056a782SJens Axboe sector_t sector; 5659faf400fSStefan Bader struct mapped_device *md; 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds /* 5681da177e4SLinus Torvalds * Sanity checks. 5691da177e4SLinus Torvalds */ 5701da177e4SLinus Torvalds BUG_ON(!clone->bi_size); 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds clone->bi_end_io = clone_endio; 5731da177e4SLinus Torvalds clone->bi_private = tio; 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds /* 5761da177e4SLinus Torvalds * Map the clone. If r == 0 we don't need to do 5771da177e4SLinus Torvalds * anything, the target has assumed ownership of 5781da177e4SLinus Torvalds * this io. 5791da177e4SLinus Torvalds */ 5801da177e4SLinus Torvalds atomic_inc(&tio->io->io_count); 5812056a782SJens Axboe sector = clone->bi_sector; 5821da177e4SLinus Torvalds r = ti->type->map(ti, clone, &tio->info); 58345cbcd79SKiyoshi Ueda if (r == DM_MAPIO_REMAPPED) { 5841da177e4SLinus Torvalds /* the bio has been remapped so dispatch it */ 5852056a782SJens Axboe 5862056a782SJens Axboe blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone, 5872056a782SJens Axboe tio->io->bio->bi_bdev->bd_dev, sector, 5882056a782SJens Axboe clone->bi_sector); 5892056a782SJens Axboe 5901da177e4SLinus Torvalds generic_make_request(clone); 5912e93ccc1SKiyoshi Ueda } else if (r < 0 || r == DM_MAPIO_REQUEUE) { 5922e93ccc1SKiyoshi Ueda /* error the io and bail out, or requeue it if needed */ 5939faf400fSStefan Bader md = tio->io->md; 5949faf400fSStefan Bader dec_pending(tio->io, r); 5959faf400fSStefan Bader /* 5969faf400fSStefan Bader * Store bio_set for cleanup. 5979faf400fSStefan Bader */ 5989faf400fSStefan Bader clone->bi_private = md->bs; 5991da177e4SLinus Torvalds bio_put(clone); 6009faf400fSStefan Bader free_tio(md, tio); 60145cbcd79SKiyoshi Ueda } else if (r) { 60245cbcd79SKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 60345cbcd79SKiyoshi Ueda BUG(); 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds } 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds struct clone_info { 6081da177e4SLinus Torvalds struct mapped_device *md; 6091da177e4SLinus Torvalds struct dm_table *map; 6101da177e4SLinus Torvalds struct bio *bio; 6111da177e4SLinus Torvalds struct dm_io *io; 6121da177e4SLinus Torvalds sector_t sector; 6131da177e4SLinus Torvalds sector_t sector_count; 6141da177e4SLinus Torvalds unsigned short idx; 6151da177e4SLinus Torvalds }; 6161da177e4SLinus Torvalds 6173676347aSPeter Osterlund static void dm_bio_destructor(struct bio *bio) 6183676347aSPeter Osterlund { 6199faf400fSStefan Bader struct bio_set *bs = bio->bi_private; 6209faf400fSStefan Bader 6219faf400fSStefan Bader bio_free(bio, bs); 6223676347aSPeter Osterlund } 6233676347aSPeter Osterlund 6241da177e4SLinus Torvalds /* 6251da177e4SLinus Torvalds * Creates a little bio that is just does part of a bvec. 6261da177e4SLinus Torvalds */ 6271da177e4SLinus Torvalds static struct bio *split_bvec(struct bio *bio, sector_t sector, 6281da177e4SLinus Torvalds unsigned short idx, unsigned int offset, 6299faf400fSStefan Bader unsigned int len, struct bio_set *bs) 6301da177e4SLinus Torvalds { 6311da177e4SLinus Torvalds struct bio *clone; 6321da177e4SLinus Torvalds struct bio_vec *bv = bio->bi_io_vec + idx; 6331da177e4SLinus Torvalds 6349faf400fSStefan Bader clone = bio_alloc_bioset(GFP_NOIO, 1, bs); 6353676347aSPeter Osterlund clone->bi_destructor = dm_bio_destructor; 6361da177e4SLinus Torvalds *clone->bi_io_vec = *bv; 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds clone->bi_sector = sector; 6391da177e4SLinus Torvalds clone->bi_bdev = bio->bi_bdev; 6401da177e4SLinus Torvalds clone->bi_rw = bio->bi_rw; 6411da177e4SLinus Torvalds clone->bi_vcnt = 1; 6421da177e4SLinus Torvalds clone->bi_size = to_bytes(len); 6431da177e4SLinus Torvalds clone->bi_io_vec->bv_offset = offset; 6441da177e4SLinus Torvalds clone->bi_io_vec->bv_len = clone->bi_size; 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds return clone; 6471da177e4SLinus Torvalds } 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds /* 6501da177e4SLinus Torvalds * Creates a bio that consists of range of complete bvecs. 6511da177e4SLinus Torvalds */ 6521da177e4SLinus Torvalds static struct bio *clone_bio(struct bio *bio, sector_t sector, 6531da177e4SLinus Torvalds unsigned short idx, unsigned short bv_count, 6549faf400fSStefan Bader unsigned int len, struct bio_set *bs) 6551da177e4SLinus Torvalds { 6561da177e4SLinus Torvalds struct bio *clone; 6571da177e4SLinus Torvalds 6589faf400fSStefan Bader clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs); 6599faf400fSStefan Bader __bio_clone(clone, bio); 6609faf400fSStefan Bader clone->bi_destructor = dm_bio_destructor; 6611da177e4SLinus Torvalds clone->bi_sector = sector; 6621da177e4SLinus Torvalds clone->bi_idx = idx; 6631da177e4SLinus Torvalds clone->bi_vcnt = idx + bv_count; 6641da177e4SLinus Torvalds clone->bi_size = to_bytes(len); 6651da177e4SLinus Torvalds clone->bi_flags &= ~(1 << BIO_SEG_VALID); 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds return clone; 6681da177e4SLinus Torvalds } 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds static void __clone_and_map(struct clone_info *ci) 6711da177e4SLinus Torvalds { 6721da177e4SLinus Torvalds struct bio *clone, *bio = ci->bio; 6731da177e4SLinus Torvalds struct dm_target *ti = dm_table_find_target(ci->map, ci->sector); 6741da177e4SLinus Torvalds sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti); 6751da177e4SLinus Torvalds struct target_io *tio; 6761da177e4SLinus Torvalds 6771da177e4SLinus Torvalds /* 6781da177e4SLinus Torvalds * Allocate a target io object. 6791da177e4SLinus Torvalds */ 6801da177e4SLinus Torvalds tio = alloc_tio(ci->md); 6811da177e4SLinus Torvalds tio->io = ci->io; 6821da177e4SLinus Torvalds tio->ti = ti; 6831da177e4SLinus Torvalds memset(&tio->info, 0, sizeof(tio->info)); 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds if (ci->sector_count <= max) { 6861da177e4SLinus Torvalds /* 6871da177e4SLinus Torvalds * Optimise for the simple case where we can do all of 6881da177e4SLinus Torvalds * the remaining io with a single clone. 6891da177e4SLinus Torvalds */ 6901da177e4SLinus Torvalds clone = clone_bio(bio, ci->sector, ci->idx, 6919faf400fSStefan Bader bio->bi_vcnt - ci->idx, ci->sector_count, 6929faf400fSStefan Bader ci->md->bs); 6931da177e4SLinus Torvalds __map_bio(ti, clone, tio); 6941da177e4SLinus Torvalds ci->sector_count = 0; 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) { 6971da177e4SLinus Torvalds /* 6981da177e4SLinus Torvalds * There are some bvecs that don't span targets. 6991da177e4SLinus Torvalds * Do as many of these as possible. 7001da177e4SLinus Torvalds */ 7011da177e4SLinus Torvalds int i; 7021da177e4SLinus Torvalds sector_t remaining = max; 7031da177e4SLinus Torvalds sector_t bv_len; 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) { 7061da177e4SLinus Torvalds bv_len = to_sector(bio->bi_io_vec[i].bv_len); 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds if (bv_len > remaining) 7091da177e4SLinus Torvalds break; 7101da177e4SLinus Torvalds 7111da177e4SLinus Torvalds remaining -= bv_len; 7121da177e4SLinus Torvalds len += bv_len; 7131da177e4SLinus Torvalds } 7141da177e4SLinus Torvalds 7159faf400fSStefan Bader clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len, 7169faf400fSStefan Bader ci->md->bs); 7171da177e4SLinus Torvalds __map_bio(ti, clone, tio); 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds ci->sector += len; 7201da177e4SLinus Torvalds ci->sector_count -= len; 7211da177e4SLinus Torvalds ci->idx = i; 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds } else { 7241da177e4SLinus Torvalds /* 725d2044a94SAlasdair G Kergon * Handle a bvec that must be split between two or more targets. 7261da177e4SLinus Torvalds */ 7271da177e4SLinus Torvalds struct bio_vec *bv = bio->bi_io_vec + ci->idx; 728d2044a94SAlasdair G Kergon sector_t remaining = to_sector(bv->bv_len); 729d2044a94SAlasdair G Kergon unsigned int offset = 0; 7301da177e4SLinus Torvalds 731d2044a94SAlasdair G Kergon do { 732d2044a94SAlasdair G Kergon if (offset) { 7331da177e4SLinus Torvalds ti = dm_table_find_target(ci->map, ci->sector); 734d2044a94SAlasdair G Kergon max = max_io_len(ci->md, ci->sector, ti); 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds tio = alloc_tio(ci->md); 7371da177e4SLinus Torvalds tio->io = ci->io; 7381da177e4SLinus Torvalds tio->ti = ti; 7391da177e4SLinus Torvalds memset(&tio->info, 0, sizeof(tio->info)); 740d2044a94SAlasdair G Kergon } 741d2044a94SAlasdair G Kergon 742d2044a94SAlasdair G Kergon len = min(remaining, max); 743d2044a94SAlasdair G Kergon 744d2044a94SAlasdair G Kergon clone = split_bvec(bio, ci->sector, ci->idx, 7459faf400fSStefan Bader bv->bv_offset + offset, len, 7469faf400fSStefan Bader ci->md->bs); 747d2044a94SAlasdair G Kergon 7481da177e4SLinus Torvalds __map_bio(ti, clone, tio); 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds ci->sector += len; 7511da177e4SLinus Torvalds ci->sector_count -= len; 752d2044a94SAlasdair G Kergon offset += to_bytes(len); 753d2044a94SAlasdair G Kergon } while (remaining -= len); 754d2044a94SAlasdair G Kergon 7551da177e4SLinus Torvalds ci->idx++; 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds /* 7601da177e4SLinus Torvalds * Split the bio into several clones. 7611da177e4SLinus Torvalds */ 7621da177e4SLinus Torvalds static void __split_bio(struct mapped_device *md, struct bio *bio) 7631da177e4SLinus Torvalds { 7641da177e4SLinus Torvalds struct clone_info ci; 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds ci.map = dm_get_table(md); 7671da177e4SLinus Torvalds if (!ci.map) { 7681da177e4SLinus Torvalds bio_io_error(bio, bio->bi_size); 7691da177e4SLinus Torvalds return; 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds ci.md = md; 7731da177e4SLinus Torvalds ci.bio = bio; 7741da177e4SLinus Torvalds ci.io = alloc_io(md); 7751da177e4SLinus Torvalds ci.io->error = 0; 7761da177e4SLinus Torvalds atomic_set(&ci.io->io_count, 1); 7771da177e4SLinus Torvalds ci.io->bio = bio; 7781da177e4SLinus Torvalds ci.io->md = md; 7791da177e4SLinus Torvalds ci.sector = bio->bi_sector; 7801da177e4SLinus Torvalds ci.sector_count = bio_sectors(bio); 7811da177e4SLinus Torvalds ci.idx = bio->bi_idx; 7821da177e4SLinus Torvalds 7833eaf840eSJun'ichi "Nick" Nomura start_io_acct(ci.io); 7841da177e4SLinus Torvalds while (ci.sector_count) 7851da177e4SLinus Torvalds __clone_and_map(&ci); 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds /* drop the extra reference count */ 7881da177e4SLinus Torvalds dec_pending(ci.io, 0); 7891da177e4SLinus Torvalds dm_table_put(ci.map); 7901da177e4SLinus Torvalds } 7911da177e4SLinus Torvalds /*----------------------------------------------------------------- 7921da177e4SLinus Torvalds * CRUD END 7931da177e4SLinus Torvalds *---------------------------------------------------------------*/ 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds /* 7961da177e4SLinus Torvalds * The request function that just remaps the bio built up by 7971da177e4SLinus Torvalds * dm_merge_bvec. 7981da177e4SLinus Torvalds */ 7991da177e4SLinus Torvalds static int dm_request(request_queue_t *q, struct bio *bio) 8001da177e4SLinus Torvalds { 8011da177e4SLinus Torvalds int r; 80212f03a49SKevin Corry int rw = bio_data_dir(bio); 8031da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 8041da177e4SLinus Torvalds 8052ca3310eSAlasdair G Kergon down_read(&md->io_lock); 8061da177e4SLinus Torvalds 80712f03a49SKevin Corry disk_stat_inc(dm_disk(md), ios[rw]); 80812f03a49SKevin Corry disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio)); 80912f03a49SKevin Corry 8101da177e4SLinus Torvalds /* 8111da177e4SLinus Torvalds * If we're suspended we have to queue 8121da177e4SLinus Torvalds * this io for later. 8131da177e4SLinus Torvalds */ 8141da177e4SLinus Torvalds while (test_bit(DMF_BLOCK_IO, &md->flags)) { 8152ca3310eSAlasdair G Kergon up_read(&md->io_lock); 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds if (bio_rw(bio) == READA) { 8181da177e4SLinus Torvalds bio_io_error(bio, bio->bi_size); 8191da177e4SLinus Torvalds return 0; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds r = queue_io(md, bio); 8231da177e4SLinus Torvalds if (r < 0) { 8241da177e4SLinus Torvalds bio_io_error(bio, bio->bi_size); 8251da177e4SLinus Torvalds return 0; 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds } else if (r == 0) 8281da177e4SLinus Torvalds return 0; /* deferred successfully */ 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds /* 8311da177e4SLinus Torvalds * We're in a while loop, because someone could suspend 8321da177e4SLinus Torvalds * before we get to the following read lock. 8331da177e4SLinus Torvalds */ 8342ca3310eSAlasdair G Kergon down_read(&md->io_lock); 8351da177e4SLinus Torvalds } 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds __split_bio(md, bio); 8382ca3310eSAlasdair G Kergon up_read(&md->io_lock); 8391da177e4SLinus Torvalds return 0; 8401da177e4SLinus Torvalds } 8411da177e4SLinus Torvalds 8421da177e4SLinus Torvalds static int dm_flush_all(request_queue_t *q, struct gendisk *disk, 8431da177e4SLinus Torvalds sector_t *error_sector) 8441da177e4SLinus Torvalds { 8451da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 8461da177e4SLinus Torvalds struct dm_table *map = dm_get_table(md); 8471da177e4SLinus Torvalds int ret = -ENXIO; 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds if (map) { 850cf222b37SAlasdair G Kergon ret = dm_table_flush_all(map); 8511da177e4SLinus Torvalds dm_table_put(map); 8521da177e4SLinus Torvalds } 8531da177e4SLinus Torvalds 8541da177e4SLinus Torvalds return ret; 8551da177e4SLinus Torvalds } 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds static void dm_unplug_all(request_queue_t *q) 8581da177e4SLinus Torvalds { 8591da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 8601da177e4SLinus Torvalds struct dm_table *map = dm_get_table(md); 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds if (map) { 8631da177e4SLinus Torvalds dm_table_unplug_all(map); 8641da177e4SLinus Torvalds dm_table_put(map); 8651da177e4SLinus Torvalds } 8661da177e4SLinus Torvalds } 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits) 8691da177e4SLinus Torvalds { 8701da177e4SLinus Torvalds int r; 8711da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) congested_data; 8721da177e4SLinus Torvalds struct dm_table *map = dm_get_table(md); 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds if (!map || test_bit(DMF_BLOCK_IO, &md->flags)) 8751da177e4SLinus Torvalds r = bdi_bits; 8761da177e4SLinus Torvalds else 8771da177e4SLinus Torvalds r = dm_table_any_congested(map, bdi_bits); 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds dm_table_put(map); 8801da177e4SLinus Torvalds return r; 8811da177e4SLinus Torvalds } 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds /*----------------------------------------------------------------- 8841da177e4SLinus Torvalds * An IDR is used to keep track of allocated minor numbers. 8851da177e4SLinus Torvalds *---------------------------------------------------------------*/ 8861da177e4SLinus Torvalds static DEFINE_IDR(_minor_idr); 8871da177e4SLinus Torvalds 8882b06cfffSAlasdair G Kergon static void free_minor(int minor) 8891da177e4SLinus Torvalds { 890f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 8911da177e4SLinus Torvalds idr_remove(&_minor_idr, minor); 892f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 8931da177e4SLinus Torvalds } 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds /* 8961da177e4SLinus Torvalds * See if the device with a specific minor # is free. 8971da177e4SLinus Torvalds */ 8982b06cfffSAlasdair G Kergon static int specific_minor(struct mapped_device *md, int minor) 8991da177e4SLinus Torvalds { 9001da177e4SLinus Torvalds int r, m; 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds if (minor >= (1 << MINORBITS)) 9031da177e4SLinus Torvalds return -EINVAL; 9041da177e4SLinus Torvalds 90562f75c2fSJeff Mahoney r = idr_pre_get(&_minor_idr, GFP_KERNEL); 90662f75c2fSJeff Mahoney if (!r) 90762f75c2fSJeff Mahoney return -ENOMEM; 90862f75c2fSJeff Mahoney 909f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 9101da177e4SLinus Torvalds 9111da177e4SLinus Torvalds if (idr_find(&_minor_idr, minor)) { 9121da177e4SLinus Torvalds r = -EBUSY; 9131da177e4SLinus Torvalds goto out; 9141da177e4SLinus Torvalds } 9151da177e4SLinus Torvalds 916ba61fdd1SJeff Mahoney r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m); 91762f75c2fSJeff Mahoney if (r) 9181da177e4SLinus Torvalds goto out; 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds if (m != minor) { 9211da177e4SLinus Torvalds idr_remove(&_minor_idr, m); 9221da177e4SLinus Torvalds r = -EBUSY; 9231da177e4SLinus Torvalds goto out; 9241da177e4SLinus Torvalds } 9251da177e4SLinus Torvalds 9261da177e4SLinus Torvalds out: 927f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 9281da177e4SLinus Torvalds return r; 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds 9312b06cfffSAlasdair G Kergon static int next_free_minor(struct mapped_device *md, int *minor) 9321da177e4SLinus Torvalds { 9332b06cfffSAlasdair G Kergon int r, m; 9341da177e4SLinus Torvalds 9351da177e4SLinus Torvalds r = idr_pre_get(&_minor_idr, GFP_KERNEL); 93662f75c2fSJeff Mahoney if (!r) 93762f75c2fSJeff Mahoney return -ENOMEM; 93862f75c2fSJeff Mahoney 939f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 9401da177e4SLinus Torvalds 941ba61fdd1SJeff Mahoney r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m); 9421da177e4SLinus Torvalds if (r) { 9431da177e4SLinus Torvalds goto out; 9441da177e4SLinus Torvalds } 9451da177e4SLinus Torvalds 9461da177e4SLinus Torvalds if (m >= (1 << MINORBITS)) { 9471da177e4SLinus Torvalds idr_remove(&_minor_idr, m); 9481da177e4SLinus Torvalds r = -ENOSPC; 9491da177e4SLinus Torvalds goto out; 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds *minor = m; 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds out: 955f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 9561da177e4SLinus Torvalds return r; 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops; 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds /* 9621da177e4SLinus Torvalds * Allocate and initialise a blank device with a given minor. 9631da177e4SLinus Torvalds */ 9642b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor) 9651da177e4SLinus Torvalds { 9661da177e4SLinus Torvalds int r; 9671da177e4SLinus Torvalds struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL); 968ba61fdd1SJeff Mahoney void *old_md; 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds if (!md) { 9711da177e4SLinus Torvalds DMWARN("unable to allocate device, out of memory."); 9721da177e4SLinus Torvalds return NULL; 9731da177e4SLinus Torvalds } 9741da177e4SLinus Torvalds 97510da4f79SJeff Mahoney if (!try_module_get(THIS_MODULE)) 97610da4f79SJeff Mahoney goto bad0; 97710da4f79SJeff Mahoney 9781da177e4SLinus Torvalds /* get a minor number for the dev */ 9792b06cfffSAlasdair G Kergon if (minor == DM_ANY_MINOR) 9802b06cfffSAlasdair G Kergon r = next_free_minor(md, &minor); 9812b06cfffSAlasdair G Kergon else 9822b06cfffSAlasdair G Kergon r = specific_minor(md, minor); 9831da177e4SLinus Torvalds if (r < 0) 9841da177e4SLinus Torvalds goto bad1; 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds memset(md, 0, sizeof(*md)); 9872ca3310eSAlasdair G Kergon init_rwsem(&md->io_lock); 9882ca3310eSAlasdair G Kergon init_MUTEX(&md->suspend_lock); 9892e93ccc1SKiyoshi Ueda spin_lock_init(&md->pushback_lock); 9901da177e4SLinus Torvalds rwlock_init(&md->map_lock); 9911da177e4SLinus Torvalds atomic_set(&md->holders, 1); 9925c6bd75dSAlasdair G Kergon atomic_set(&md->open_count, 0); 9931da177e4SLinus Torvalds atomic_set(&md->event_nr, 0); 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds md->queue = blk_alloc_queue(GFP_KERNEL); 9961da177e4SLinus Torvalds if (!md->queue) 997e3f6ac61SIshai Rabinovitz goto bad1_free_minor; 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds md->queue->queuedata = md; 10001da177e4SLinus Torvalds md->queue->backing_dev_info.congested_fn = dm_any_congested; 10011da177e4SLinus Torvalds md->queue->backing_dev_info.congested_data = md; 10021da177e4SLinus Torvalds blk_queue_make_request(md->queue, dm_request); 1003daef265fSJens Axboe blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY); 10041da177e4SLinus Torvalds md->queue->unplug_fn = dm_unplug_all; 10051da177e4SLinus Torvalds md->queue->issue_flush_fn = dm_flush_all; 10061da177e4SLinus Torvalds 100793d2341cSMatthew Dobson md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache); 10081da177e4SLinus Torvalds if (!md->io_pool) 10091da177e4SLinus Torvalds goto bad2; 10101da177e4SLinus Torvalds 101193d2341cSMatthew Dobson md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache); 10121da177e4SLinus Torvalds if (!md->tio_pool) 10131da177e4SLinus Torvalds goto bad3; 10141da177e4SLinus Torvalds 10159faf400fSStefan Bader md->bs = bioset_create(16, 16, 4); 10169faf400fSStefan Bader if (!md->bs) 10179faf400fSStefan Bader goto bad_no_bioset; 10189faf400fSStefan Bader 10191da177e4SLinus Torvalds md->disk = alloc_disk(1); 10201da177e4SLinus Torvalds if (!md->disk) 10211da177e4SLinus Torvalds goto bad4; 10221da177e4SLinus Torvalds 1023f0b04115SJeff Mahoney atomic_set(&md->pending, 0); 1024f0b04115SJeff Mahoney init_waitqueue_head(&md->wait); 1025f0b04115SJeff Mahoney init_waitqueue_head(&md->eventq); 1026f0b04115SJeff Mahoney 10271da177e4SLinus Torvalds md->disk->major = _major; 10281da177e4SLinus Torvalds md->disk->first_minor = minor; 10291da177e4SLinus Torvalds md->disk->fops = &dm_blk_dops; 10301da177e4SLinus Torvalds md->disk->queue = md->queue; 10311da177e4SLinus Torvalds md->disk->private_data = md; 10321da177e4SLinus Torvalds sprintf(md->disk->disk_name, "dm-%d", minor); 10331da177e4SLinus Torvalds add_disk(md->disk); 10347e51f257SMike Anderson format_dev_t(md->name, MKDEV(_major, minor)); 10351da177e4SLinus Torvalds 1036ba61fdd1SJeff Mahoney /* Populate the mapping, nobody knows we exist yet */ 1037f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 1038ba61fdd1SJeff Mahoney old_md = idr_replace(&_minor_idr, md, minor); 1039f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1040ba61fdd1SJeff Mahoney 1041ba61fdd1SJeff Mahoney BUG_ON(old_md != MINOR_ALLOCED); 1042ba61fdd1SJeff Mahoney 10431da177e4SLinus Torvalds return md; 10441da177e4SLinus Torvalds 10451da177e4SLinus Torvalds bad4: 10469faf400fSStefan Bader bioset_free(md->bs); 10479faf400fSStefan Bader bad_no_bioset: 10481da177e4SLinus Torvalds mempool_destroy(md->tio_pool); 10491da177e4SLinus Torvalds bad3: 10501da177e4SLinus Torvalds mempool_destroy(md->io_pool); 10511da177e4SLinus Torvalds bad2: 10521312f40eSAl Viro blk_cleanup_queue(md->queue); 1053e3f6ac61SIshai Rabinovitz bad1_free_minor: 10541da177e4SLinus Torvalds free_minor(minor); 10551da177e4SLinus Torvalds bad1: 105610da4f79SJeff Mahoney module_put(THIS_MODULE); 105710da4f79SJeff Mahoney bad0: 10581da177e4SLinus Torvalds kfree(md); 10591da177e4SLinus Torvalds return NULL; 10601da177e4SLinus Torvalds } 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds static void free_dev(struct mapped_device *md) 10631da177e4SLinus Torvalds { 10642b06cfffSAlasdair G Kergon int minor = md->disk->first_minor; 106563d94e48SJun'ichi Nomura 1066d9dde59bSJun'ichi Nomura if (md->suspended_bdev) { 1067d9dde59bSJun'ichi Nomura thaw_bdev(md->suspended_bdev, NULL); 1068d9dde59bSJun'ichi Nomura bdput(md->suspended_bdev); 1069d9dde59bSJun'ichi Nomura } 10701da177e4SLinus Torvalds mempool_destroy(md->tio_pool); 10711da177e4SLinus Torvalds mempool_destroy(md->io_pool); 10729faf400fSStefan Bader bioset_free(md->bs); 10731da177e4SLinus Torvalds del_gendisk(md->disk); 107463d94e48SJun'ichi Nomura free_minor(minor); 1075fba9f90eSJeff Mahoney 1076fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 1077fba9f90eSJeff Mahoney md->disk->private_data = NULL; 1078fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 1079fba9f90eSJeff Mahoney 10801da177e4SLinus Torvalds put_disk(md->disk); 10811312f40eSAl Viro blk_cleanup_queue(md->queue); 108210da4f79SJeff Mahoney module_put(THIS_MODULE); 10831da177e4SLinus Torvalds kfree(md); 10841da177e4SLinus Torvalds } 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds /* 10871da177e4SLinus Torvalds * Bind a table to the device. 10881da177e4SLinus Torvalds */ 10891da177e4SLinus Torvalds static void event_callback(void *context) 10901da177e4SLinus Torvalds { 10911da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) context; 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds atomic_inc(&md->event_nr); 10941da177e4SLinus Torvalds wake_up(&md->eventq); 10951da177e4SLinus Torvalds } 10961da177e4SLinus Torvalds 10974e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size) 10981da177e4SLinus Torvalds { 10994e90188bSAlasdair G Kergon set_capacity(md->disk, size); 11001da177e4SLinus Torvalds 11011b1dcc1bSJes Sorensen mutex_lock(&md->suspended_bdev->bd_inode->i_mutex); 1102e39e2e95SAlasdair G Kergon i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT); 11031b1dcc1bSJes Sorensen mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex); 11041da177e4SLinus Torvalds } 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds static int __bind(struct mapped_device *md, struct dm_table *t) 11071da177e4SLinus Torvalds { 11081da177e4SLinus Torvalds request_queue_t *q = md->queue; 11091da177e4SLinus Torvalds sector_t size; 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds size = dm_table_get_size(t); 11123ac51e74SDarrick J. Wong 11133ac51e74SDarrick J. Wong /* 11143ac51e74SDarrick J. Wong * Wipe any geometry if the size of the table changed. 11153ac51e74SDarrick J. Wong */ 11163ac51e74SDarrick J. Wong if (size != get_capacity(md->disk)) 11173ac51e74SDarrick J. Wong memset(&md->geometry, 0, sizeof(md->geometry)); 11183ac51e74SDarrick J. Wong 1119*bfa152faSJun'ichi Nomura if (md->suspended_bdev) 11204e90188bSAlasdair G Kergon __set_size(md, size); 11211da177e4SLinus Torvalds if (size == 0) 11221da177e4SLinus Torvalds return 0; 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds dm_table_get(t); 1125cf222b37SAlasdair G Kergon dm_table_event_callback(t, event_callback, md); 11262ca3310eSAlasdair G Kergon 11272ca3310eSAlasdair G Kergon write_lock(&md->map_lock); 11282ca3310eSAlasdair G Kergon md->map = t; 11291da177e4SLinus Torvalds dm_table_set_restrictions(t, q); 11302ca3310eSAlasdair G Kergon write_unlock(&md->map_lock); 11312ca3310eSAlasdair G Kergon 11321da177e4SLinus Torvalds return 0; 11331da177e4SLinus Torvalds } 11341da177e4SLinus Torvalds 11351da177e4SLinus Torvalds static void __unbind(struct mapped_device *md) 11361da177e4SLinus Torvalds { 11371da177e4SLinus Torvalds struct dm_table *map = md->map; 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds if (!map) 11401da177e4SLinus Torvalds return; 11411da177e4SLinus Torvalds 11421da177e4SLinus Torvalds dm_table_event_callback(map, NULL, NULL); 11431da177e4SLinus Torvalds write_lock(&md->map_lock); 11441da177e4SLinus Torvalds md->map = NULL; 11451da177e4SLinus Torvalds write_unlock(&md->map_lock); 11461da177e4SLinus Torvalds dm_table_put(map); 11471da177e4SLinus Torvalds } 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds /* 11501da177e4SLinus Torvalds * Constructor for a new device. 11511da177e4SLinus Torvalds */ 11522b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result) 11531da177e4SLinus Torvalds { 11541da177e4SLinus Torvalds struct mapped_device *md; 11551da177e4SLinus Torvalds 11562b06cfffSAlasdair G Kergon md = alloc_dev(minor); 11571da177e4SLinus Torvalds if (!md) 11581da177e4SLinus Torvalds return -ENXIO; 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds *result = md; 11611da177e4SLinus Torvalds return 0; 11621da177e4SLinus Torvalds } 11631da177e4SLinus Torvalds 1164637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev) 11651da177e4SLinus Torvalds { 11661da177e4SLinus Torvalds struct mapped_device *md; 11671da177e4SLinus Torvalds unsigned minor = MINOR(dev); 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) 11701da177e4SLinus Torvalds return NULL; 11711da177e4SLinus Torvalds 1172f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 11731da177e4SLinus Torvalds 11741da177e4SLinus Torvalds md = idr_find(&_minor_idr, minor); 1175fba9f90eSJeff Mahoney if (md && (md == MINOR_ALLOCED || 1176fba9f90eSJeff Mahoney (dm_disk(md)->first_minor != minor) || 1177fba9f90eSJeff Mahoney test_bit(DMF_FREEING, &md->flags))) { 1178637842cfSDavid Teigland md = NULL; 1179fba9f90eSJeff Mahoney goto out; 1180fba9f90eSJeff Mahoney } 11811da177e4SLinus Torvalds 1182fba9f90eSJeff Mahoney out: 1183f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 11841da177e4SLinus Torvalds 1185637842cfSDavid Teigland return md; 1186637842cfSDavid Teigland } 1187637842cfSDavid Teigland 1188d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev) 1189d229a958SDavid Teigland { 1190d229a958SDavid Teigland struct mapped_device *md = dm_find_md(dev); 1191d229a958SDavid Teigland 1192d229a958SDavid Teigland if (md) 1193d229a958SDavid Teigland dm_get(md); 1194d229a958SDavid Teigland 1195d229a958SDavid Teigland return md; 1196d229a958SDavid Teigland } 1197d229a958SDavid Teigland 11989ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md) 1199637842cfSDavid Teigland { 12009ade92a9SAlasdair G Kergon return md->interface_ptr; 12011da177e4SLinus Torvalds } 12021da177e4SLinus Torvalds 12031da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr) 12041da177e4SLinus Torvalds { 12051da177e4SLinus Torvalds md->interface_ptr = ptr; 12061da177e4SLinus Torvalds } 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds void dm_get(struct mapped_device *md) 12091da177e4SLinus Torvalds { 12101da177e4SLinus Torvalds atomic_inc(&md->holders); 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds 121372d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md) 121472d94861SAlasdair G Kergon { 121572d94861SAlasdair G Kergon return md->name; 121672d94861SAlasdair G Kergon } 121772d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name); 121872d94861SAlasdair G Kergon 12191da177e4SLinus Torvalds void dm_put(struct mapped_device *md) 12201da177e4SLinus Torvalds { 12211134e5aeSMike Anderson struct dm_table *map; 12221da177e4SLinus Torvalds 1223fba9f90eSJeff Mahoney BUG_ON(test_bit(DMF_FREEING, &md->flags)); 1224fba9f90eSJeff Mahoney 1225f32c10b0SJeff Mahoney if (atomic_dec_and_lock(&md->holders, &_minor_lock)) { 12261134e5aeSMike Anderson map = dm_get_table(md); 1227ba61fdd1SJeff Mahoney idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor); 1228fba9f90eSJeff Mahoney set_bit(DMF_FREEING, &md->flags); 1229f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1230cf222b37SAlasdair G Kergon if (!dm_suspended(md)) { 12311da177e4SLinus Torvalds dm_table_presuspend_targets(map); 12321da177e4SLinus Torvalds dm_table_postsuspend_targets(map); 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds __unbind(md); 12351134e5aeSMike Anderson dm_table_put(map); 12361da177e4SLinus Torvalds free_dev(md); 12371da177e4SLinus Torvalds } 12381da177e4SLinus Torvalds } 12391da177e4SLinus Torvalds 12401da177e4SLinus Torvalds /* 12411da177e4SLinus Torvalds * Process the deferred bios 12421da177e4SLinus Torvalds */ 12431da177e4SLinus Torvalds static void __flush_deferred_io(struct mapped_device *md, struct bio *c) 12441da177e4SLinus Torvalds { 12451da177e4SLinus Torvalds struct bio *n; 12461da177e4SLinus Torvalds 12471da177e4SLinus Torvalds while (c) { 12481da177e4SLinus Torvalds n = c->bi_next; 12491da177e4SLinus Torvalds c->bi_next = NULL; 12501da177e4SLinus Torvalds __split_bio(md, c); 12511da177e4SLinus Torvalds c = n; 12521da177e4SLinus Torvalds } 12531da177e4SLinus Torvalds } 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds /* 12561da177e4SLinus Torvalds * Swap in a new table (destroying old one). 12571da177e4SLinus Torvalds */ 12581da177e4SLinus Torvalds int dm_swap_table(struct mapped_device *md, struct dm_table *table) 12591da177e4SLinus Torvalds { 126093c534aeSAlasdair G Kergon int r = -EINVAL; 12611da177e4SLinus Torvalds 12622ca3310eSAlasdair G Kergon down(&md->suspend_lock); 12631da177e4SLinus Torvalds 12641da177e4SLinus Torvalds /* device must be suspended */ 1265cf222b37SAlasdair G Kergon if (!dm_suspended(md)) 126693c534aeSAlasdair G Kergon goto out; 12671da177e4SLinus Torvalds 1268*bfa152faSJun'ichi Nomura /* without bdev, the device size cannot be changed */ 1269*bfa152faSJun'ichi Nomura if (!md->suspended_bdev) 1270*bfa152faSJun'ichi Nomura if (get_capacity(md->disk) != dm_table_get_size(table)) 1271*bfa152faSJun'ichi Nomura goto out; 1272*bfa152faSJun'ichi Nomura 12731da177e4SLinus Torvalds __unbind(md); 12741da177e4SLinus Torvalds r = __bind(md, table); 12751da177e4SLinus Torvalds 127693c534aeSAlasdair G Kergon out: 12772ca3310eSAlasdair G Kergon up(&md->suspend_lock); 127893c534aeSAlasdair G Kergon return r; 12791da177e4SLinus Torvalds } 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds /* 12821da177e4SLinus Torvalds * Functions to lock and unlock any filesystem running on the 12831da177e4SLinus Torvalds * device. 12841da177e4SLinus Torvalds */ 12852ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md) 12861da177e4SLinus Torvalds { 1287e39e2e95SAlasdair G Kergon int r; 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds WARN_ON(md->frozen_sb); 1290dfbe03f6SAlasdair G Kergon 1291e39e2e95SAlasdair G Kergon md->frozen_sb = freeze_bdev(md->suspended_bdev); 1292dfbe03f6SAlasdair G Kergon if (IS_ERR(md->frozen_sb)) { 1293cf222b37SAlasdair G Kergon r = PTR_ERR(md->frozen_sb); 1294e39e2e95SAlasdair G Kergon md->frozen_sb = NULL; 1295e39e2e95SAlasdair G Kergon return r; 1296dfbe03f6SAlasdair G Kergon } 1297dfbe03f6SAlasdair G Kergon 1298aa8d7c2fSAlasdair G Kergon set_bit(DMF_FROZEN, &md->flags); 1299aa8d7c2fSAlasdair G Kergon 13001da177e4SLinus Torvalds /* don't bdput right now, we don't want the bdev 1301e39e2e95SAlasdair G Kergon * to go away while it is locked. 13021da177e4SLinus Torvalds */ 13031da177e4SLinus Torvalds return 0; 13041da177e4SLinus Torvalds } 13051da177e4SLinus Torvalds 13062ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md) 13071da177e4SLinus Torvalds { 1308aa8d7c2fSAlasdair G Kergon if (!test_bit(DMF_FROZEN, &md->flags)) 1309aa8d7c2fSAlasdair G Kergon return; 1310aa8d7c2fSAlasdair G Kergon 1311e39e2e95SAlasdair G Kergon thaw_bdev(md->suspended_bdev, md->frozen_sb); 13121da177e4SLinus Torvalds md->frozen_sb = NULL; 1313aa8d7c2fSAlasdair G Kergon clear_bit(DMF_FROZEN, &md->flags); 13141da177e4SLinus Torvalds } 13151da177e4SLinus Torvalds 13161da177e4SLinus Torvalds /* 13171da177e4SLinus Torvalds * We need to be able to change a mapping table under a mounted 13181da177e4SLinus Torvalds * filesystem. For example we might want to move some data in 13191da177e4SLinus Torvalds * the background. Before the table can be swapped with 13201da177e4SLinus Torvalds * dm_bind_table, dm_suspend must be called to flush any in 13211da177e4SLinus Torvalds * flight bios and ensure that any further io gets deferred. 13221da177e4SLinus Torvalds */ 1323a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags) 13241da177e4SLinus Torvalds { 13252ca3310eSAlasdair G Kergon struct dm_table *map = NULL; 13262e93ccc1SKiyoshi Ueda unsigned long flags; 13271da177e4SLinus Torvalds DECLARE_WAITQUEUE(wait, current); 13281ecac7fdSJun'ichi Nomura struct bio *def; 1329cf222b37SAlasdair G Kergon int r = -EINVAL; 1330a3d77d35SKiyoshi Ueda int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0; 13312e93ccc1SKiyoshi Ueda int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0; 13321da177e4SLinus Torvalds 13332ca3310eSAlasdair G Kergon down(&md->suspend_lock); 13342ca3310eSAlasdair G Kergon 13352ca3310eSAlasdair G Kergon if (dm_suspended(md)) 1336d287483dSAlasdair G Kergon goto out_unlock; 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds map = dm_get_table(md); 1339cf222b37SAlasdair G Kergon 13402e93ccc1SKiyoshi Ueda /* 13412e93ccc1SKiyoshi Ueda * DMF_NOFLUSH_SUSPENDING must be set before presuspend. 13422e93ccc1SKiyoshi Ueda * This flag is cleared before dm_suspend returns. 13432e93ccc1SKiyoshi Ueda */ 13442e93ccc1SKiyoshi Ueda if (noflush) 13452e93ccc1SKiyoshi Ueda set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 13462e93ccc1SKiyoshi Ueda 1347436d4108SAlasdair G Kergon /* This does not get reverted if there's an error later. */ 13481da177e4SLinus Torvalds dm_table_presuspend_targets(map); 13491da177e4SLinus Torvalds 1350*bfa152faSJun'ichi Nomura /* bdget() can stall if the pending I/Os are not flushed */ 1351*bfa152faSJun'ichi Nomura if (!noflush) { 1352e39e2e95SAlasdair G Kergon md->suspended_bdev = bdget_disk(md->disk, 0); 1353e39e2e95SAlasdair G Kergon if (!md->suspended_bdev) { 1354e39e2e95SAlasdair G Kergon DMWARN("bdget failed in dm_suspend"); 1355e39e2e95SAlasdair G Kergon r = -ENOMEM; 13562e93ccc1SKiyoshi Ueda goto flush_and_out; 1357e39e2e95SAlasdair G Kergon } 1358*bfa152faSJun'ichi Nomura } 1359e39e2e95SAlasdair G Kergon 13602e93ccc1SKiyoshi Ueda /* 13612e93ccc1SKiyoshi Ueda * Flush I/O to the device. 13622e93ccc1SKiyoshi Ueda * noflush supersedes do_lockfs, because lock_fs() needs to flush I/Os. 13632e93ccc1SKiyoshi Ueda */ 13642e93ccc1SKiyoshi Ueda if (do_lockfs && !noflush) { 13652ca3310eSAlasdair G Kergon r = lock_fs(md); 13662ca3310eSAlasdair G Kergon if (r) 13672ca3310eSAlasdair G Kergon goto out; 1368aa8d7c2fSAlasdair G Kergon } 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds /* 1371354e0071SAlasdair G Kergon * First we set the BLOCK_IO flag so no more ios will be mapped. 13721da177e4SLinus Torvalds */ 13732ca3310eSAlasdair G Kergon down_write(&md->io_lock); 13742ca3310eSAlasdair G Kergon set_bit(DMF_BLOCK_IO, &md->flags); 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds add_wait_queue(&md->wait, &wait); 13772ca3310eSAlasdair G Kergon up_write(&md->io_lock); 13781da177e4SLinus Torvalds 13791da177e4SLinus Torvalds /* unplug */ 13802ca3310eSAlasdair G Kergon if (map) 13811da177e4SLinus Torvalds dm_table_unplug_all(map); 13821da177e4SLinus Torvalds 13831da177e4SLinus Torvalds /* 13841da177e4SLinus Torvalds * Then we wait for the already mapped ios to 13851da177e4SLinus Torvalds * complete. 13861da177e4SLinus Torvalds */ 13871da177e4SLinus Torvalds while (1) { 13881da177e4SLinus Torvalds set_current_state(TASK_INTERRUPTIBLE); 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds if (!atomic_read(&md->pending) || signal_pending(current)) 13911da177e4SLinus Torvalds break; 13921da177e4SLinus Torvalds 13931da177e4SLinus Torvalds io_schedule(); 13941da177e4SLinus Torvalds } 13951da177e4SLinus Torvalds set_current_state(TASK_RUNNING); 13961da177e4SLinus Torvalds 13972ca3310eSAlasdair G Kergon down_write(&md->io_lock); 13981da177e4SLinus Torvalds remove_wait_queue(&md->wait, &wait); 13991da177e4SLinus Torvalds 14002e93ccc1SKiyoshi Ueda if (noflush) { 14012e93ccc1SKiyoshi Ueda spin_lock_irqsave(&md->pushback_lock, flags); 14022e93ccc1SKiyoshi Ueda clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 14032e93ccc1SKiyoshi Ueda bio_list_merge_head(&md->deferred, &md->pushback); 14042e93ccc1SKiyoshi Ueda bio_list_init(&md->pushback); 14052e93ccc1SKiyoshi Ueda spin_unlock_irqrestore(&md->pushback_lock, flags); 14062e93ccc1SKiyoshi Ueda } 14072e93ccc1SKiyoshi Ueda 14081da177e4SLinus Torvalds /* were we interrupted ? */ 1409cf222b37SAlasdair G Kergon r = -EINTR; 14102ca3310eSAlasdair G Kergon if (atomic_read(&md->pending)) { 14111ecac7fdSJun'ichi Nomura clear_bit(DMF_BLOCK_IO, &md->flags); 14121ecac7fdSJun'ichi Nomura def = bio_list_get(&md->deferred); 14131ecac7fdSJun'ichi Nomura __flush_deferred_io(md, def); 14142ca3310eSAlasdair G Kergon up_write(&md->io_lock); 14152ca3310eSAlasdair G Kergon unlock_fs(md); 14162e93ccc1SKiyoshi Ueda goto out; /* pushback list is already flushed, so skip flush */ 14172ca3310eSAlasdair G Kergon } 14182ca3310eSAlasdair G Kergon up_write(&md->io_lock); 14192ca3310eSAlasdair G Kergon 14202ca3310eSAlasdair G Kergon dm_table_postsuspend_targets(map); 14211da177e4SLinus Torvalds 14221da177e4SLinus Torvalds set_bit(DMF_SUSPENDED, &md->flags); 14231da177e4SLinus Torvalds 14242ca3310eSAlasdair G Kergon r = 0; 14252ca3310eSAlasdair G Kergon 14262e93ccc1SKiyoshi Ueda flush_and_out: 14272e93ccc1SKiyoshi Ueda if (r && noflush) { 14282e93ccc1SKiyoshi Ueda /* 14292e93ccc1SKiyoshi Ueda * Because there may be already I/Os in the pushback list, 14302e93ccc1SKiyoshi Ueda * flush them before return. 14312e93ccc1SKiyoshi Ueda */ 14322e93ccc1SKiyoshi Ueda down_write(&md->io_lock); 14332e93ccc1SKiyoshi Ueda 14342e93ccc1SKiyoshi Ueda spin_lock_irqsave(&md->pushback_lock, flags); 14352e93ccc1SKiyoshi Ueda clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 14362e93ccc1SKiyoshi Ueda bio_list_merge_head(&md->deferred, &md->pushback); 14372e93ccc1SKiyoshi Ueda bio_list_init(&md->pushback); 14382e93ccc1SKiyoshi Ueda spin_unlock_irqrestore(&md->pushback_lock, flags); 14392e93ccc1SKiyoshi Ueda 14402e93ccc1SKiyoshi Ueda def = bio_list_get(&md->deferred); 14412e93ccc1SKiyoshi Ueda __flush_deferred_io(md, def); 14422e93ccc1SKiyoshi Ueda up_write(&md->io_lock); 14432e93ccc1SKiyoshi Ueda } 14442e93ccc1SKiyoshi Ueda 14452ca3310eSAlasdair G Kergon out: 1446e39e2e95SAlasdair G Kergon if (r && md->suspended_bdev) { 1447e39e2e95SAlasdair G Kergon bdput(md->suspended_bdev); 1448e39e2e95SAlasdair G Kergon md->suspended_bdev = NULL; 1449e39e2e95SAlasdair G Kergon } 1450e39e2e95SAlasdair G Kergon 14511da177e4SLinus Torvalds dm_table_put(map); 1452d287483dSAlasdair G Kergon 1453d287483dSAlasdair G Kergon out_unlock: 14542ca3310eSAlasdair G Kergon up(&md->suspend_lock); 1455cf222b37SAlasdair G Kergon return r; 14561da177e4SLinus Torvalds } 14571da177e4SLinus Torvalds 14581da177e4SLinus Torvalds int dm_resume(struct mapped_device *md) 14591da177e4SLinus Torvalds { 1460cf222b37SAlasdair G Kergon int r = -EINVAL; 14611da177e4SLinus Torvalds struct bio *def; 1462cf222b37SAlasdair G Kergon struct dm_table *map = NULL; 14631da177e4SLinus Torvalds 14642ca3310eSAlasdair G Kergon down(&md->suspend_lock); 14652ca3310eSAlasdair G Kergon if (!dm_suspended(md)) 1466cf222b37SAlasdair G Kergon goto out; 1467cf222b37SAlasdair G Kergon 1468cf222b37SAlasdair G Kergon map = dm_get_table(md); 14692ca3310eSAlasdair G Kergon if (!map || !dm_table_get_size(map)) 1470cf222b37SAlasdair G Kergon goto out; 14711da177e4SLinus Torvalds 14728757b776SMilan Broz r = dm_table_resume_targets(map); 14738757b776SMilan Broz if (r) 14748757b776SMilan Broz goto out; 14752ca3310eSAlasdair G Kergon 14762ca3310eSAlasdair G Kergon down_write(&md->io_lock); 14771da177e4SLinus Torvalds clear_bit(DMF_BLOCK_IO, &md->flags); 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds def = bio_list_get(&md->deferred); 14801da177e4SLinus Torvalds __flush_deferred_io(md, def); 14812ca3310eSAlasdair G Kergon up_write(&md->io_lock); 14822ca3310eSAlasdair G Kergon 14832ca3310eSAlasdair G Kergon unlock_fs(md); 14842ca3310eSAlasdair G Kergon 1485*bfa152faSJun'ichi Nomura if (md->suspended_bdev) { 1486e39e2e95SAlasdair G Kergon bdput(md->suspended_bdev); 1487e39e2e95SAlasdair G Kergon md->suspended_bdev = NULL; 1488*bfa152faSJun'ichi Nomura } 1489e39e2e95SAlasdair G Kergon 14902ca3310eSAlasdair G Kergon clear_bit(DMF_SUSPENDED, &md->flags); 14912ca3310eSAlasdair G Kergon 14921da177e4SLinus Torvalds dm_table_unplug_all(map); 14931da177e4SLinus Torvalds 14948560ed6fSHannes Reinecke kobject_uevent(&md->disk->kobj, KOBJ_CHANGE); 14958560ed6fSHannes Reinecke 1496cf222b37SAlasdair G Kergon r = 0; 14972ca3310eSAlasdair G Kergon 1498cf222b37SAlasdair G Kergon out: 1499cf222b37SAlasdair G Kergon dm_table_put(map); 15002ca3310eSAlasdair G Kergon up(&md->suspend_lock); 15012ca3310eSAlasdair G Kergon 1502cf222b37SAlasdair G Kergon return r; 15031da177e4SLinus Torvalds } 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds /*----------------------------------------------------------------- 15061da177e4SLinus Torvalds * Event notification. 15071da177e4SLinus Torvalds *---------------------------------------------------------------*/ 15081da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md) 15091da177e4SLinus Torvalds { 15101da177e4SLinus Torvalds return atomic_read(&md->event_nr); 15111da177e4SLinus Torvalds } 15121da177e4SLinus Torvalds 15131da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr) 15141da177e4SLinus Torvalds { 15151da177e4SLinus Torvalds return wait_event_interruptible(md->eventq, 15161da177e4SLinus Torvalds (event_nr != atomic_read(&md->event_nr))); 15171da177e4SLinus Torvalds } 15181da177e4SLinus Torvalds 15191da177e4SLinus Torvalds /* 15201da177e4SLinus Torvalds * The gendisk is only valid as long as you have a reference 15211da177e4SLinus Torvalds * count on 'md'. 15221da177e4SLinus Torvalds */ 15231da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md) 15241da177e4SLinus Torvalds { 15251da177e4SLinus Torvalds return md->disk; 15261da177e4SLinus Torvalds } 15271da177e4SLinus Torvalds 15281da177e4SLinus Torvalds int dm_suspended(struct mapped_device *md) 15291da177e4SLinus Torvalds { 15301da177e4SLinus Torvalds return test_bit(DMF_SUSPENDED, &md->flags); 15311da177e4SLinus Torvalds } 15321da177e4SLinus Torvalds 15332e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti) 15342e93ccc1SKiyoshi Ueda { 15352e93ccc1SKiyoshi Ueda struct mapped_device *md = dm_table_get_md(ti->table); 15362e93ccc1SKiyoshi Ueda int r = __noflush_suspending(md); 15372e93ccc1SKiyoshi Ueda 15382e93ccc1SKiyoshi Ueda dm_put(md); 15392e93ccc1SKiyoshi Ueda 15402e93ccc1SKiyoshi Ueda return r; 15412e93ccc1SKiyoshi Ueda } 15422e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending); 15432e93ccc1SKiyoshi Ueda 15441da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops = { 15451da177e4SLinus Torvalds .open = dm_blk_open, 15461da177e4SLinus Torvalds .release = dm_blk_close, 1547aa129a22SMilan Broz .ioctl = dm_blk_ioctl, 15483ac51e74SDarrick J. Wong .getgeo = dm_blk_getgeo, 15491da177e4SLinus Torvalds .owner = THIS_MODULE 15501da177e4SLinus Torvalds }; 15511da177e4SLinus Torvalds 15521da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo); 15531da177e4SLinus Torvalds 15541da177e4SLinus Torvalds /* 15551da177e4SLinus Torvalds * module hooks 15561da177e4SLinus Torvalds */ 15571da177e4SLinus Torvalds module_init(dm_init); 15581da177e4SLinus Torvalds module_exit(dm_exit); 15591da177e4SLinus Torvalds 15601da177e4SLinus Torvalds module_param(major, uint, 0); 15611da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper"); 15621da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver"); 15631da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 15641da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1565