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> 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds static const char *_name = DM_NAME; 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds static unsigned int major = 0; 271da177e4SLinus Torvalds static unsigned int _major = 0; 281da177e4SLinus Torvalds 29f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock); 301da177e4SLinus Torvalds /* 311da177e4SLinus Torvalds * One of these is allocated per bio. 321da177e4SLinus Torvalds */ 331da177e4SLinus Torvalds struct dm_io { 341da177e4SLinus Torvalds struct mapped_device *md; 351da177e4SLinus Torvalds int error; 361da177e4SLinus Torvalds struct bio *bio; 371da177e4SLinus Torvalds atomic_t io_count; 383eaf840eSJun'ichi "Nick" Nomura unsigned long start_time; 391da177e4SLinus Torvalds }; 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds /* 421da177e4SLinus Torvalds * One of these is allocated per target within a bio. Hopefully 431da177e4SLinus Torvalds * this will be simplified out one day. 441da177e4SLinus Torvalds */ 451da177e4SLinus Torvalds struct target_io { 461da177e4SLinus Torvalds struct dm_io *io; 471da177e4SLinus Torvalds struct dm_target *ti; 481da177e4SLinus Torvalds union map_info info; 491da177e4SLinus Torvalds }; 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds union map_info *dm_get_mapinfo(struct bio *bio) 521da177e4SLinus Torvalds { 531da177e4SLinus Torvalds if (bio && bio->bi_private) 541da177e4SLinus Torvalds return &((struct target_io *)bio->bi_private)->info; 551da177e4SLinus Torvalds return NULL; 561da177e4SLinus Torvalds } 571da177e4SLinus Torvalds 58ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1) 59ba61fdd1SJeff Mahoney 601da177e4SLinus Torvalds /* 611da177e4SLinus Torvalds * Bits for the md->flags field. 621da177e4SLinus Torvalds */ 631da177e4SLinus Torvalds #define DMF_BLOCK_IO 0 641da177e4SLinus Torvalds #define DMF_SUSPENDED 1 65aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2 66fba9f90eSJeff Mahoney #define DMF_FREEING 3 67*5c6bd75dSAlasdair G Kergon #define DMF_DELETING 4 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds struct mapped_device { 702ca3310eSAlasdair G Kergon struct rw_semaphore io_lock; 712ca3310eSAlasdair G Kergon struct semaphore suspend_lock; 721da177e4SLinus Torvalds rwlock_t map_lock; 731da177e4SLinus Torvalds atomic_t holders; 74*5c6bd75dSAlasdair G Kergon atomic_t open_count; 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds unsigned long flags; 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds request_queue_t *queue; 791da177e4SLinus Torvalds struct gendisk *disk; 807e51f257SMike Anderson char name[16]; 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds void *interface_ptr; 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds /* 851da177e4SLinus Torvalds * A list of ios that arrived while we were suspended. 861da177e4SLinus Torvalds */ 871da177e4SLinus Torvalds atomic_t pending; 881da177e4SLinus Torvalds wait_queue_head_t wait; 891da177e4SLinus Torvalds struct bio_list deferred; 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds /* 921da177e4SLinus Torvalds * The current mapping. 931da177e4SLinus Torvalds */ 941da177e4SLinus Torvalds struct dm_table *map; 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds /* 971da177e4SLinus Torvalds * io objects are allocated from here. 981da177e4SLinus Torvalds */ 991da177e4SLinus Torvalds mempool_t *io_pool; 1001da177e4SLinus Torvalds mempool_t *tio_pool; 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds /* 1031da177e4SLinus Torvalds * Event handling. 1041da177e4SLinus Torvalds */ 1051da177e4SLinus Torvalds atomic_t event_nr; 1061da177e4SLinus Torvalds wait_queue_head_t eventq; 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds /* 1091da177e4SLinus Torvalds * freeze/thaw support require holding onto a super block 1101da177e4SLinus Torvalds */ 1111da177e4SLinus Torvalds struct super_block *frozen_sb; 112e39e2e95SAlasdair G Kergon struct block_device *suspended_bdev; 1133ac51e74SDarrick J. Wong 1143ac51e74SDarrick J. Wong /* forced geometry settings */ 1153ac51e74SDarrick J. Wong struct hd_geometry geometry; 1161da177e4SLinus Torvalds }; 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds #define MIN_IOS 256 1191da177e4SLinus Torvalds static kmem_cache_t *_io_cache; 1201da177e4SLinus Torvalds static kmem_cache_t *_tio_cache; 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds static struct bio_set *dm_set; 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds static int __init local_init(void) 1251da177e4SLinus Torvalds { 1261da177e4SLinus Torvalds int r; 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds dm_set = bioset_create(16, 16, 4); 1291da177e4SLinus Torvalds if (!dm_set) 1301da177e4SLinus Torvalds return -ENOMEM; 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds /* allocate a slab for the dm_ios */ 1331da177e4SLinus Torvalds _io_cache = kmem_cache_create("dm_io", 1341da177e4SLinus Torvalds sizeof(struct dm_io), 0, 0, NULL, NULL); 1351da177e4SLinus Torvalds if (!_io_cache) 1361da177e4SLinus Torvalds return -ENOMEM; 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds /* allocate a slab for the target ios */ 1391da177e4SLinus Torvalds _tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io), 1401da177e4SLinus Torvalds 0, 0, NULL, NULL); 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 bioset_free(dm_set); 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds if (unregister_blkdev(_major, _name) < 0) 1681da177e4SLinus Torvalds DMERR("devfs_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 233*5c6bd75dSAlasdair G Kergon if (test_bit(DMF_FREEING, &md->flags) || 234*5c6bd75dSAlasdair 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); 240*5c6bd75dSAlasdair 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; 253*5c6bd75dSAlasdair G Kergon atomic_dec(&md->open_count); 2541da177e4SLinus Torvalds dm_put(md); 2551da177e4SLinus Torvalds return 0; 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds 258*5c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md) 259*5c6bd75dSAlasdair G Kergon { 260*5c6bd75dSAlasdair G Kergon return atomic_read(&md->open_count); 261*5c6bd75dSAlasdair G Kergon } 262*5c6bd75dSAlasdair G Kergon 263*5c6bd75dSAlasdair G Kergon /* 264*5c6bd75dSAlasdair G Kergon * Guarantees nothing is using the device before it's deleted. 265*5c6bd75dSAlasdair G Kergon */ 266*5c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md) 267*5c6bd75dSAlasdair G Kergon { 268*5c6bd75dSAlasdair G Kergon int r = 0; 269*5c6bd75dSAlasdair G Kergon 270*5c6bd75dSAlasdair G Kergon spin_lock(&_minor_lock); 271*5c6bd75dSAlasdair G Kergon 272*5c6bd75dSAlasdair G Kergon if (dm_open_count(md)) 273*5c6bd75dSAlasdair G Kergon r = -EBUSY; 274*5c6bd75dSAlasdair G Kergon else 275*5c6bd75dSAlasdair G Kergon set_bit(DMF_DELETING, &md->flags); 276*5c6bd75dSAlasdair G Kergon 277*5c6bd75dSAlasdair G Kergon spin_unlock(&_minor_lock); 278*5c6bd75dSAlasdair G Kergon 279*5c6bd75dSAlasdair G Kergon return r; 280*5c6bd75dSAlasdair G Kergon } 281*5c6bd75dSAlasdair 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 2891da177e4SLinus Torvalds static inline struct dm_io *alloc_io(struct mapped_device *md) 2901da177e4SLinus Torvalds { 2911da177e4SLinus Torvalds return mempool_alloc(md->io_pool, GFP_NOIO); 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds static inline void free_io(struct mapped_device *md, struct dm_io *io) 2951da177e4SLinus Torvalds { 2961da177e4SLinus Torvalds mempool_free(io, md->io_pool); 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds static inline struct target_io *alloc_tio(struct mapped_device *md) 3001da177e4SLinus Torvalds { 3011da177e4SLinus Torvalds return mempool_alloc(md->tio_pool, GFP_NOIO); 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds static inline void free_tio(struct mapped_device *md, struct target_io *tio) 3051da177e4SLinus Torvalds { 3061da177e4SLinus Torvalds mempool_free(tio, md->tio_pool); 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds 3093eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io) 3103eaf840eSJun'ichi "Nick" Nomura { 3113eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 3123eaf840eSJun'ichi "Nick" Nomura 3133eaf840eSJun'ichi "Nick" Nomura io->start_time = jiffies; 3143eaf840eSJun'ichi "Nick" Nomura 3153eaf840eSJun'ichi "Nick" Nomura preempt_disable(); 3163eaf840eSJun'ichi "Nick" Nomura disk_round_stats(dm_disk(md)); 3173eaf840eSJun'ichi "Nick" Nomura preempt_enable(); 3183eaf840eSJun'ichi "Nick" Nomura dm_disk(md)->in_flight = atomic_inc_return(&md->pending); 3193eaf840eSJun'ichi "Nick" Nomura } 3203eaf840eSJun'ichi "Nick" Nomura 3213eaf840eSJun'ichi "Nick" Nomura static int end_io_acct(struct dm_io *io) 3223eaf840eSJun'ichi "Nick" Nomura { 3233eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 3243eaf840eSJun'ichi "Nick" Nomura struct bio *bio = io->bio; 3253eaf840eSJun'ichi "Nick" Nomura unsigned long duration = jiffies - io->start_time; 3263eaf840eSJun'ichi "Nick" Nomura int pending; 3273eaf840eSJun'ichi "Nick" Nomura int rw = bio_data_dir(bio); 3283eaf840eSJun'ichi "Nick" Nomura 3293eaf840eSJun'ichi "Nick" Nomura preempt_disable(); 3303eaf840eSJun'ichi "Nick" Nomura disk_round_stats(dm_disk(md)); 3313eaf840eSJun'ichi "Nick" Nomura preempt_enable(); 3323eaf840eSJun'ichi "Nick" Nomura dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending); 3333eaf840eSJun'ichi "Nick" Nomura 3343eaf840eSJun'ichi "Nick" Nomura disk_stat_add(dm_disk(md), ticks[rw], duration); 3353eaf840eSJun'ichi "Nick" Nomura 3363eaf840eSJun'ichi "Nick" Nomura return !pending; 3373eaf840eSJun'ichi "Nick" Nomura } 3383eaf840eSJun'ichi "Nick" Nomura 3391da177e4SLinus Torvalds /* 3401da177e4SLinus Torvalds * Add the bio to the list of deferred io. 3411da177e4SLinus Torvalds */ 3421da177e4SLinus Torvalds static int queue_io(struct mapped_device *md, struct bio *bio) 3431da177e4SLinus Torvalds { 3442ca3310eSAlasdair G Kergon down_write(&md->io_lock); 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds if (!test_bit(DMF_BLOCK_IO, &md->flags)) { 3472ca3310eSAlasdair G Kergon up_write(&md->io_lock); 3481da177e4SLinus Torvalds return 1; 3491da177e4SLinus Torvalds } 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds bio_list_add(&md->deferred, bio); 3521da177e4SLinus Torvalds 3532ca3310eSAlasdair G Kergon up_write(&md->io_lock); 3541da177e4SLinus Torvalds return 0; /* deferred successfully */ 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds /* 3581da177e4SLinus Torvalds * Everyone (including functions in this file), should use this 3591da177e4SLinus Torvalds * function to access the md->map field, and make sure they call 3601da177e4SLinus Torvalds * dm_table_put() when finished. 3611da177e4SLinus Torvalds */ 3621da177e4SLinus Torvalds struct dm_table *dm_get_table(struct mapped_device *md) 3631da177e4SLinus Torvalds { 3641da177e4SLinus Torvalds struct dm_table *t; 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds read_lock(&md->map_lock); 3671da177e4SLinus Torvalds t = md->map; 3681da177e4SLinus Torvalds if (t) 3691da177e4SLinus Torvalds dm_table_get(t); 3701da177e4SLinus Torvalds read_unlock(&md->map_lock); 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds return t; 3731da177e4SLinus Torvalds } 3741da177e4SLinus Torvalds 3753ac51e74SDarrick J. Wong /* 3763ac51e74SDarrick J. Wong * Get the geometry associated with a dm device 3773ac51e74SDarrick J. Wong */ 3783ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) 3793ac51e74SDarrick J. Wong { 3803ac51e74SDarrick J. Wong *geo = md->geometry; 3813ac51e74SDarrick J. Wong 3823ac51e74SDarrick J. Wong return 0; 3833ac51e74SDarrick J. Wong } 3843ac51e74SDarrick J. Wong 3853ac51e74SDarrick J. Wong /* 3863ac51e74SDarrick J. Wong * Set the geometry of a device. 3873ac51e74SDarrick J. Wong */ 3883ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) 3893ac51e74SDarrick J. Wong { 3903ac51e74SDarrick J. Wong sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; 3913ac51e74SDarrick J. Wong 3923ac51e74SDarrick J. Wong if (geo->start > sz) { 3933ac51e74SDarrick J. Wong DMWARN("Start sector is beyond the geometry limits."); 3943ac51e74SDarrick J. Wong return -EINVAL; 3953ac51e74SDarrick J. Wong } 3963ac51e74SDarrick J. Wong 3973ac51e74SDarrick J. Wong md->geometry = *geo; 3983ac51e74SDarrick J. Wong 3993ac51e74SDarrick J. Wong return 0; 4003ac51e74SDarrick J. Wong } 4013ac51e74SDarrick J. Wong 4021da177e4SLinus Torvalds /*----------------------------------------------------------------- 4031da177e4SLinus Torvalds * CRUD START: 4041da177e4SLinus Torvalds * A more elegant soln is in the works that uses the queue 4051da177e4SLinus Torvalds * merge fn, unfortunately there are a couple of changes to 4061da177e4SLinus Torvalds * the block layer that I want to make for this. So in the 4071da177e4SLinus Torvalds * interests of getting something for people to use I give 4081da177e4SLinus Torvalds * you this clearly demarcated crap. 4091da177e4SLinus Torvalds *---------------------------------------------------------------*/ 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds /* 4121da177e4SLinus Torvalds * Decrements the number of outstanding ios that a bio has been 4131da177e4SLinus Torvalds * cloned into, completing the original io if necc. 4141da177e4SLinus Torvalds */ 415858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error) 4161da177e4SLinus Torvalds { 4171da177e4SLinus Torvalds if (error) 4181da177e4SLinus Torvalds io->error = error; 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds if (atomic_dec_and_test(&io->io_count)) { 4213eaf840eSJun'ichi "Nick" Nomura if (end_io_acct(io)) 4221da177e4SLinus Torvalds /* nudge anyone waiting on suspend queue */ 4231da177e4SLinus Torvalds wake_up(&io->md->wait); 4241da177e4SLinus Torvalds 4252056a782SJens Axboe blk_add_trace_bio(io->md->queue, io->bio, BLK_TA_COMPLETE); 4262056a782SJens Axboe 4271da177e4SLinus Torvalds bio_endio(io->bio, io->bio->bi_size, io->error); 4281da177e4SLinus Torvalds free_io(io->md, io); 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds 4321da177e4SLinus Torvalds static int clone_endio(struct bio *bio, unsigned int done, int error) 4331da177e4SLinus Torvalds { 4341da177e4SLinus Torvalds int r = 0; 4351da177e4SLinus Torvalds struct target_io *tio = bio->bi_private; 4361da177e4SLinus Torvalds struct dm_io *io = tio->io; 4371da177e4SLinus Torvalds dm_endio_fn endio = tio->ti->type->end_io; 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds if (bio->bi_size) 4401da177e4SLinus Torvalds return 1; 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds if (!bio_flagged(bio, BIO_UPTODATE) && !error) 4431da177e4SLinus Torvalds error = -EIO; 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds if (endio) { 4461da177e4SLinus Torvalds r = endio(tio->ti, bio, error, &tio->info); 4471da177e4SLinus Torvalds if (r < 0) 4481da177e4SLinus Torvalds error = r; 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds else if (r > 0) 4511da177e4SLinus Torvalds /* the target wants another shot at the io */ 4521da177e4SLinus Torvalds return 1; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds free_tio(io->md, tio); 4561da177e4SLinus Torvalds dec_pending(io, error); 4571da177e4SLinus Torvalds bio_put(bio); 4581da177e4SLinus Torvalds return r; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds static sector_t max_io_len(struct mapped_device *md, 4621da177e4SLinus Torvalds sector_t sector, struct dm_target *ti) 4631da177e4SLinus Torvalds { 4641da177e4SLinus Torvalds sector_t offset = sector - ti->begin; 4651da177e4SLinus Torvalds sector_t len = ti->len - offset; 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds /* 4681da177e4SLinus Torvalds * Does the target need to split even further ? 4691da177e4SLinus Torvalds */ 4701da177e4SLinus Torvalds if (ti->split_io) { 4711da177e4SLinus Torvalds sector_t boundary; 4721da177e4SLinus Torvalds boundary = ((offset + ti->split_io) & ~(ti->split_io - 1)) 4731da177e4SLinus Torvalds - offset; 4741da177e4SLinus Torvalds if (len > boundary) 4751da177e4SLinus Torvalds len = boundary; 4761da177e4SLinus Torvalds } 4771da177e4SLinus Torvalds 4781da177e4SLinus Torvalds return len; 4791da177e4SLinus Torvalds } 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds static void __map_bio(struct dm_target *ti, struct bio *clone, 4821da177e4SLinus Torvalds struct target_io *tio) 4831da177e4SLinus Torvalds { 4841da177e4SLinus Torvalds int r; 4852056a782SJens Axboe sector_t sector; 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds /* 4881da177e4SLinus Torvalds * Sanity checks. 4891da177e4SLinus Torvalds */ 4901da177e4SLinus Torvalds BUG_ON(!clone->bi_size); 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds clone->bi_end_io = clone_endio; 4931da177e4SLinus Torvalds clone->bi_private = tio; 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds /* 4961da177e4SLinus Torvalds * Map the clone. If r == 0 we don't need to do 4971da177e4SLinus Torvalds * anything, the target has assumed ownership of 4981da177e4SLinus Torvalds * this io. 4991da177e4SLinus Torvalds */ 5001da177e4SLinus Torvalds atomic_inc(&tio->io->io_count); 5012056a782SJens Axboe sector = clone->bi_sector; 5021da177e4SLinus Torvalds r = ti->type->map(ti, clone, &tio->info); 5032056a782SJens Axboe if (r > 0) { 5041da177e4SLinus Torvalds /* the bio has been remapped so dispatch it */ 5052056a782SJens Axboe 5062056a782SJens Axboe blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone, 5072056a782SJens Axboe tio->io->bio->bi_bdev->bd_dev, sector, 5082056a782SJens Axboe clone->bi_sector); 5092056a782SJens Axboe 5101da177e4SLinus Torvalds generic_make_request(clone); 5112056a782SJens Axboe } 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds else if (r < 0) { 5141da177e4SLinus Torvalds /* error the io and bail out */ 5151da177e4SLinus Torvalds struct dm_io *io = tio->io; 5161da177e4SLinus Torvalds free_tio(tio->io->md, tio); 517f6a80ea8SAlasdair G Kergon dec_pending(io, r); 5181da177e4SLinus Torvalds bio_put(clone); 5191da177e4SLinus Torvalds } 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds struct clone_info { 5231da177e4SLinus Torvalds struct mapped_device *md; 5241da177e4SLinus Torvalds struct dm_table *map; 5251da177e4SLinus Torvalds struct bio *bio; 5261da177e4SLinus Torvalds struct dm_io *io; 5271da177e4SLinus Torvalds sector_t sector; 5281da177e4SLinus Torvalds sector_t sector_count; 5291da177e4SLinus Torvalds unsigned short idx; 5301da177e4SLinus Torvalds }; 5311da177e4SLinus Torvalds 5323676347aSPeter Osterlund static void dm_bio_destructor(struct bio *bio) 5333676347aSPeter Osterlund { 5343676347aSPeter Osterlund bio_free(bio, dm_set); 5353676347aSPeter Osterlund } 5363676347aSPeter Osterlund 5371da177e4SLinus Torvalds /* 5381da177e4SLinus Torvalds * Creates a little bio that is just does part of a bvec. 5391da177e4SLinus Torvalds */ 5401da177e4SLinus Torvalds static struct bio *split_bvec(struct bio *bio, sector_t sector, 5411da177e4SLinus Torvalds unsigned short idx, unsigned int offset, 5421da177e4SLinus Torvalds unsigned int len) 5431da177e4SLinus Torvalds { 5441da177e4SLinus Torvalds struct bio *clone; 5451da177e4SLinus Torvalds struct bio_vec *bv = bio->bi_io_vec + idx; 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds clone = bio_alloc_bioset(GFP_NOIO, 1, dm_set); 5483676347aSPeter Osterlund clone->bi_destructor = dm_bio_destructor; 5491da177e4SLinus Torvalds *clone->bi_io_vec = *bv; 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds clone->bi_sector = sector; 5521da177e4SLinus Torvalds clone->bi_bdev = bio->bi_bdev; 5531da177e4SLinus Torvalds clone->bi_rw = bio->bi_rw; 5541da177e4SLinus Torvalds clone->bi_vcnt = 1; 5551da177e4SLinus Torvalds clone->bi_size = to_bytes(len); 5561da177e4SLinus Torvalds clone->bi_io_vec->bv_offset = offset; 5571da177e4SLinus Torvalds clone->bi_io_vec->bv_len = clone->bi_size; 5581da177e4SLinus Torvalds 5591da177e4SLinus Torvalds return clone; 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds /* 5631da177e4SLinus Torvalds * Creates a bio that consists of range of complete bvecs. 5641da177e4SLinus Torvalds */ 5651da177e4SLinus Torvalds static struct bio *clone_bio(struct bio *bio, sector_t sector, 5661da177e4SLinus Torvalds unsigned short idx, unsigned short bv_count, 5671da177e4SLinus Torvalds unsigned int len) 5681da177e4SLinus Torvalds { 5691da177e4SLinus Torvalds struct bio *clone; 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds clone = bio_clone(bio, GFP_NOIO); 5721da177e4SLinus Torvalds clone->bi_sector = sector; 5731da177e4SLinus Torvalds clone->bi_idx = idx; 5741da177e4SLinus Torvalds clone->bi_vcnt = idx + bv_count; 5751da177e4SLinus Torvalds clone->bi_size = to_bytes(len); 5761da177e4SLinus Torvalds clone->bi_flags &= ~(1 << BIO_SEG_VALID); 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds return clone; 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds static void __clone_and_map(struct clone_info *ci) 5821da177e4SLinus Torvalds { 5831da177e4SLinus Torvalds struct bio *clone, *bio = ci->bio; 5841da177e4SLinus Torvalds struct dm_target *ti = dm_table_find_target(ci->map, ci->sector); 5851da177e4SLinus Torvalds sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti); 5861da177e4SLinus Torvalds struct target_io *tio; 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds /* 5891da177e4SLinus Torvalds * Allocate a target io object. 5901da177e4SLinus Torvalds */ 5911da177e4SLinus Torvalds tio = alloc_tio(ci->md); 5921da177e4SLinus Torvalds tio->io = ci->io; 5931da177e4SLinus Torvalds tio->ti = ti; 5941da177e4SLinus Torvalds memset(&tio->info, 0, sizeof(tio->info)); 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds if (ci->sector_count <= max) { 5971da177e4SLinus Torvalds /* 5981da177e4SLinus Torvalds * Optimise for the simple case where we can do all of 5991da177e4SLinus Torvalds * the remaining io with a single clone. 6001da177e4SLinus Torvalds */ 6011da177e4SLinus Torvalds clone = clone_bio(bio, ci->sector, ci->idx, 6021da177e4SLinus Torvalds bio->bi_vcnt - ci->idx, ci->sector_count); 6031da177e4SLinus Torvalds __map_bio(ti, clone, tio); 6041da177e4SLinus Torvalds ci->sector_count = 0; 6051da177e4SLinus Torvalds 6061da177e4SLinus Torvalds } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) { 6071da177e4SLinus Torvalds /* 6081da177e4SLinus Torvalds * There are some bvecs that don't span targets. 6091da177e4SLinus Torvalds * Do as many of these as possible. 6101da177e4SLinus Torvalds */ 6111da177e4SLinus Torvalds int i; 6121da177e4SLinus Torvalds sector_t remaining = max; 6131da177e4SLinus Torvalds sector_t bv_len; 6141da177e4SLinus Torvalds 6151da177e4SLinus Torvalds for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) { 6161da177e4SLinus Torvalds bv_len = to_sector(bio->bi_io_vec[i].bv_len); 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds if (bv_len > remaining) 6191da177e4SLinus Torvalds break; 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds remaining -= bv_len; 6221da177e4SLinus Torvalds len += bv_len; 6231da177e4SLinus Torvalds } 6241da177e4SLinus Torvalds 6251da177e4SLinus Torvalds clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len); 6261da177e4SLinus Torvalds __map_bio(ti, clone, tio); 6271da177e4SLinus Torvalds 6281da177e4SLinus Torvalds ci->sector += len; 6291da177e4SLinus Torvalds ci->sector_count -= len; 6301da177e4SLinus Torvalds ci->idx = i; 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds } else { 6331da177e4SLinus Torvalds /* 634d2044a94SAlasdair G Kergon * Handle a bvec that must be split between two or more targets. 6351da177e4SLinus Torvalds */ 6361da177e4SLinus Torvalds struct bio_vec *bv = bio->bi_io_vec + ci->idx; 637d2044a94SAlasdair G Kergon sector_t remaining = to_sector(bv->bv_len); 638d2044a94SAlasdair G Kergon unsigned int offset = 0; 6391da177e4SLinus Torvalds 640d2044a94SAlasdair G Kergon do { 641d2044a94SAlasdair G Kergon if (offset) { 6421da177e4SLinus Torvalds ti = dm_table_find_target(ci->map, ci->sector); 643d2044a94SAlasdair G Kergon max = max_io_len(ci->md, ci->sector, ti); 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds tio = alloc_tio(ci->md); 6461da177e4SLinus Torvalds tio->io = ci->io; 6471da177e4SLinus Torvalds tio->ti = ti; 6481da177e4SLinus Torvalds memset(&tio->info, 0, sizeof(tio->info)); 649d2044a94SAlasdair G Kergon } 650d2044a94SAlasdair G Kergon 651d2044a94SAlasdair G Kergon len = min(remaining, max); 652d2044a94SAlasdair G Kergon 653d2044a94SAlasdair G Kergon clone = split_bvec(bio, ci->sector, ci->idx, 654d2044a94SAlasdair G Kergon bv->bv_offset + offset, len); 655d2044a94SAlasdair G Kergon 6561da177e4SLinus Torvalds __map_bio(ti, clone, tio); 6571da177e4SLinus Torvalds 6581da177e4SLinus Torvalds ci->sector += len; 6591da177e4SLinus Torvalds ci->sector_count -= len; 660d2044a94SAlasdair G Kergon offset += to_bytes(len); 661d2044a94SAlasdair G Kergon } while (remaining -= len); 662d2044a94SAlasdair G Kergon 6631da177e4SLinus Torvalds ci->idx++; 6641da177e4SLinus Torvalds } 6651da177e4SLinus Torvalds } 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds /* 6681da177e4SLinus Torvalds * Split the bio into several clones. 6691da177e4SLinus Torvalds */ 6701da177e4SLinus Torvalds static void __split_bio(struct mapped_device *md, struct bio *bio) 6711da177e4SLinus Torvalds { 6721da177e4SLinus Torvalds struct clone_info ci; 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds ci.map = dm_get_table(md); 6751da177e4SLinus Torvalds if (!ci.map) { 6761da177e4SLinus Torvalds bio_io_error(bio, bio->bi_size); 6771da177e4SLinus Torvalds return; 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds ci.md = md; 6811da177e4SLinus Torvalds ci.bio = bio; 6821da177e4SLinus Torvalds ci.io = alloc_io(md); 6831da177e4SLinus Torvalds ci.io->error = 0; 6841da177e4SLinus Torvalds atomic_set(&ci.io->io_count, 1); 6851da177e4SLinus Torvalds ci.io->bio = bio; 6861da177e4SLinus Torvalds ci.io->md = md; 6871da177e4SLinus Torvalds ci.sector = bio->bi_sector; 6881da177e4SLinus Torvalds ci.sector_count = bio_sectors(bio); 6891da177e4SLinus Torvalds ci.idx = bio->bi_idx; 6901da177e4SLinus Torvalds 6913eaf840eSJun'ichi "Nick" Nomura start_io_acct(ci.io); 6921da177e4SLinus Torvalds while (ci.sector_count) 6931da177e4SLinus Torvalds __clone_and_map(&ci); 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds /* drop the extra reference count */ 6961da177e4SLinus Torvalds dec_pending(ci.io, 0); 6971da177e4SLinus Torvalds dm_table_put(ci.map); 6981da177e4SLinus Torvalds } 6991da177e4SLinus Torvalds /*----------------------------------------------------------------- 7001da177e4SLinus Torvalds * CRUD END 7011da177e4SLinus Torvalds *---------------------------------------------------------------*/ 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds /* 7041da177e4SLinus Torvalds * The request function that just remaps the bio built up by 7051da177e4SLinus Torvalds * dm_merge_bvec. 7061da177e4SLinus Torvalds */ 7071da177e4SLinus Torvalds static int dm_request(request_queue_t *q, struct bio *bio) 7081da177e4SLinus Torvalds { 7091da177e4SLinus Torvalds int r; 71012f03a49SKevin Corry int rw = bio_data_dir(bio); 7111da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 7121da177e4SLinus Torvalds 7132ca3310eSAlasdair G Kergon down_read(&md->io_lock); 7141da177e4SLinus Torvalds 71512f03a49SKevin Corry disk_stat_inc(dm_disk(md), ios[rw]); 71612f03a49SKevin Corry disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio)); 71712f03a49SKevin Corry 7181da177e4SLinus Torvalds /* 7191da177e4SLinus Torvalds * If we're suspended we have to queue 7201da177e4SLinus Torvalds * this io for later. 7211da177e4SLinus Torvalds */ 7221da177e4SLinus Torvalds while (test_bit(DMF_BLOCK_IO, &md->flags)) { 7232ca3310eSAlasdair G Kergon up_read(&md->io_lock); 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds if (bio_rw(bio) == READA) { 7261da177e4SLinus Torvalds bio_io_error(bio, bio->bi_size); 7271da177e4SLinus Torvalds return 0; 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds r = queue_io(md, bio); 7311da177e4SLinus Torvalds if (r < 0) { 7321da177e4SLinus Torvalds bio_io_error(bio, bio->bi_size); 7331da177e4SLinus Torvalds return 0; 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds } else if (r == 0) 7361da177e4SLinus Torvalds return 0; /* deferred successfully */ 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds /* 7391da177e4SLinus Torvalds * We're in a while loop, because someone could suspend 7401da177e4SLinus Torvalds * before we get to the following read lock. 7411da177e4SLinus Torvalds */ 7422ca3310eSAlasdair G Kergon down_read(&md->io_lock); 7431da177e4SLinus Torvalds } 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds __split_bio(md, bio); 7462ca3310eSAlasdair G Kergon up_read(&md->io_lock); 7471da177e4SLinus Torvalds return 0; 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds static int dm_flush_all(request_queue_t *q, struct gendisk *disk, 7511da177e4SLinus Torvalds sector_t *error_sector) 7521da177e4SLinus Torvalds { 7531da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 7541da177e4SLinus Torvalds struct dm_table *map = dm_get_table(md); 7551da177e4SLinus Torvalds int ret = -ENXIO; 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds if (map) { 758cf222b37SAlasdair G Kergon ret = dm_table_flush_all(map); 7591da177e4SLinus Torvalds dm_table_put(map); 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds return ret; 7631da177e4SLinus Torvalds } 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds static void dm_unplug_all(request_queue_t *q) 7661da177e4SLinus Torvalds { 7671da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 7681da177e4SLinus Torvalds struct dm_table *map = dm_get_table(md); 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds if (map) { 7711da177e4SLinus Torvalds dm_table_unplug_all(map); 7721da177e4SLinus Torvalds dm_table_put(map); 7731da177e4SLinus Torvalds } 7741da177e4SLinus Torvalds } 7751da177e4SLinus Torvalds 7761da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits) 7771da177e4SLinus Torvalds { 7781da177e4SLinus Torvalds int r; 7791da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) congested_data; 7801da177e4SLinus Torvalds struct dm_table *map = dm_get_table(md); 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds if (!map || test_bit(DMF_BLOCK_IO, &md->flags)) 7831da177e4SLinus Torvalds r = bdi_bits; 7841da177e4SLinus Torvalds else 7851da177e4SLinus Torvalds r = dm_table_any_congested(map, bdi_bits); 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds dm_table_put(map); 7881da177e4SLinus Torvalds return r; 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds /*----------------------------------------------------------------- 7921da177e4SLinus Torvalds * An IDR is used to keep track of allocated minor numbers. 7931da177e4SLinus Torvalds *---------------------------------------------------------------*/ 7941da177e4SLinus Torvalds static DEFINE_IDR(_minor_idr); 7951da177e4SLinus Torvalds 7962b06cfffSAlasdair G Kergon static void free_minor(int minor) 7971da177e4SLinus Torvalds { 798f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 7991da177e4SLinus Torvalds idr_remove(&_minor_idr, minor); 800f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds /* 8041da177e4SLinus Torvalds * See if the device with a specific minor # is free. 8051da177e4SLinus Torvalds */ 8062b06cfffSAlasdair G Kergon static int specific_minor(struct mapped_device *md, int minor) 8071da177e4SLinus Torvalds { 8081da177e4SLinus Torvalds int r, m; 8091da177e4SLinus Torvalds 8101da177e4SLinus Torvalds if (minor >= (1 << MINORBITS)) 8111da177e4SLinus Torvalds return -EINVAL; 8121da177e4SLinus Torvalds 81362f75c2fSJeff Mahoney r = idr_pre_get(&_minor_idr, GFP_KERNEL); 81462f75c2fSJeff Mahoney if (!r) 81562f75c2fSJeff Mahoney return -ENOMEM; 81662f75c2fSJeff Mahoney 817f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 8181da177e4SLinus Torvalds 8191da177e4SLinus Torvalds if (idr_find(&_minor_idr, minor)) { 8201da177e4SLinus Torvalds r = -EBUSY; 8211da177e4SLinus Torvalds goto out; 8221da177e4SLinus Torvalds } 8231da177e4SLinus Torvalds 824ba61fdd1SJeff Mahoney r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m); 82562f75c2fSJeff Mahoney if (r) 8261da177e4SLinus Torvalds goto out; 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds if (m != minor) { 8291da177e4SLinus Torvalds idr_remove(&_minor_idr, m); 8301da177e4SLinus Torvalds r = -EBUSY; 8311da177e4SLinus Torvalds goto out; 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds out: 835f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 8361da177e4SLinus Torvalds return r; 8371da177e4SLinus Torvalds } 8381da177e4SLinus Torvalds 8392b06cfffSAlasdair G Kergon static int next_free_minor(struct mapped_device *md, int *minor) 8401da177e4SLinus Torvalds { 8412b06cfffSAlasdair G Kergon int r, m; 8421da177e4SLinus Torvalds 8431da177e4SLinus Torvalds r = idr_pre_get(&_minor_idr, GFP_KERNEL); 84462f75c2fSJeff Mahoney if (!r) 84562f75c2fSJeff Mahoney return -ENOMEM; 84662f75c2fSJeff Mahoney 847f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 8481da177e4SLinus Torvalds 849ba61fdd1SJeff Mahoney r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m); 8501da177e4SLinus Torvalds if (r) { 8511da177e4SLinus Torvalds goto out; 8521da177e4SLinus Torvalds } 8531da177e4SLinus Torvalds 8541da177e4SLinus Torvalds if (m >= (1 << MINORBITS)) { 8551da177e4SLinus Torvalds idr_remove(&_minor_idr, m); 8561da177e4SLinus Torvalds r = -ENOSPC; 8571da177e4SLinus Torvalds goto out; 8581da177e4SLinus Torvalds } 8591da177e4SLinus Torvalds 8601da177e4SLinus Torvalds *minor = m; 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds out: 863f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 8641da177e4SLinus Torvalds return r; 8651da177e4SLinus Torvalds } 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops; 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds /* 8701da177e4SLinus Torvalds * Allocate and initialise a blank device with a given minor. 8711da177e4SLinus Torvalds */ 8722b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor) 8731da177e4SLinus Torvalds { 8741da177e4SLinus Torvalds int r; 8751da177e4SLinus Torvalds struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL); 876ba61fdd1SJeff Mahoney void *old_md; 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds if (!md) { 8791da177e4SLinus Torvalds DMWARN("unable to allocate device, out of memory."); 8801da177e4SLinus Torvalds return NULL; 8811da177e4SLinus Torvalds } 8821da177e4SLinus Torvalds 88310da4f79SJeff Mahoney if (!try_module_get(THIS_MODULE)) 88410da4f79SJeff Mahoney goto bad0; 88510da4f79SJeff Mahoney 8861da177e4SLinus Torvalds /* get a minor number for the dev */ 8872b06cfffSAlasdair G Kergon if (minor == DM_ANY_MINOR) 8882b06cfffSAlasdair G Kergon r = next_free_minor(md, &minor); 8892b06cfffSAlasdair G Kergon else 8902b06cfffSAlasdair G Kergon r = specific_minor(md, minor); 8911da177e4SLinus Torvalds if (r < 0) 8921da177e4SLinus Torvalds goto bad1; 8931da177e4SLinus Torvalds 8941da177e4SLinus Torvalds memset(md, 0, sizeof(*md)); 8952ca3310eSAlasdair G Kergon init_rwsem(&md->io_lock); 8962ca3310eSAlasdair G Kergon init_MUTEX(&md->suspend_lock); 8971da177e4SLinus Torvalds rwlock_init(&md->map_lock); 8981da177e4SLinus Torvalds atomic_set(&md->holders, 1); 899*5c6bd75dSAlasdair G Kergon atomic_set(&md->open_count, 0); 9001da177e4SLinus Torvalds atomic_set(&md->event_nr, 0); 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds md->queue = blk_alloc_queue(GFP_KERNEL); 9031da177e4SLinus Torvalds if (!md->queue) 9041da177e4SLinus Torvalds goto bad1; 9051da177e4SLinus Torvalds 9061da177e4SLinus Torvalds md->queue->queuedata = md; 9071da177e4SLinus Torvalds md->queue->backing_dev_info.congested_fn = dm_any_congested; 9081da177e4SLinus Torvalds md->queue->backing_dev_info.congested_data = md; 9091da177e4SLinus Torvalds blk_queue_make_request(md->queue, dm_request); 910daef265fSJens Axboe blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY); 9111da177e4SLinus Torvalds md->queue->unplug_fn = dm_unplug_all; 9121da177e4SLinus Torvalds md->queue->issue_flush_fn = dm_flush_all; 9131da177e4SLinus Torvalds 91493d2341cSMatthew Dobson md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache); 9151da177e4SLinus Torvalds if (!md->io_pool) 9161da177e4SLinus Torvalds goto bad2; 9171da177e4SLinus Torvalds 91893d2341cSMatthew Dobson md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache); 9191da177e4SLinus Torvalds if (!md->tio_pool) 9201da177e4SLinus Torvalds goto bad3; 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds md->disk = alloc_disk(1); 9231da177e4SLinus Torvalds if (!md->disk) 9241da177e4SLinus Torvalds goto bad4; 9251da177e4SLinus Torvalds 926f0b04115SJeff Mahoney atomic_set(&md->pending, 0); 927f0b04115SJeff Mahoney init_waitqueue_head(&md->wait); 928f0b04115SJeff Mahoney init_waitqueue_head(&md->eventq); 929f0b04115SJeff Mahoney 9301da177e4SLinus Torvalds md->disk->major = _major; 9311da177e4SLinus Torvalds md->disk->first_minor = minor; 9321da177e4SLinus Torvalds md->disk->fops = &dm_blk_dops; 9331da177e4SLinus Torvalds md->disk->queue = md->queue; 9341da177e4SLinus Torvalds md->disk->private_data = md; 9351da177e4SLinus Torvalds sprintf(md->disk->disk_name, "dm-%d", minor); 9361da177e4SLinus Torvalds add_disk(md->disk); 9377e51f257SMike Anderson format_dev_t(md->name, MKDEV(_major, minor)); 9381da177e4SLinus Torvalds 939ba61fdd1SJeff Mahoney /* Populate the mapping, nobody knows we exist yet */ 940f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 941ba61fdd1SJeff Mahoney old_md = idr_replace(&_minor_idr, md, minor); 942f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 943ba61fdd1SJeff Mahoney 944ba61fdd1SJeff Mahoney BUG_ON(old_md != MINOR_ALLOCED); 945ba61fdd1SJeff Mahoney 9461da177e4SLinus Torvalds return md; 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds bad4: 9491da177e4SLinus Torvalds mempool_destroy(md->tio_pool); 9501da177e4SLinus Torvalds bad3: 9511da177e4SLinus Torvalds mempool_destroy(md->io_pool); 9521da177e4SLinus Torvalds bad2: 9531312f40eSAl Viro blk_cleanup_queue(md->queue); 9541da177e4SLinus Torvalds free_minor(minor); 9551da177e4SLinus Torvalds bad1: 95610da4f79SJeff Mahoney module_put(THIS_MODULE); 95710da4f79SJeff Mahoney bad0: 9581da177e4SLinus Torvalds kfree(md); 9591da177e4SLinus Torvalds return NULL; 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds static void free_dev(struct mapped_device *md) 9631da177e4SLinus Torvalds { 9642b06cfffSAlasdair G Kergon int minor = md->disk->first_minor; 96563d94e48SJun'ichi Nomura 966d9dde59bSJun'ichi Nomura if (md->suspended_bdev) { 967d9dde59bSJun'ichi Nomura thaw_bdev(md->suspended_bdev, NULL); 968d9dde59bSJun'ichi Nomura bdput(md->suspended_bdev); 969d9dde59bSJun'ichi Nomura } 9701da177e4SLinus Torvalds mempool_destroy(md->tio_pool); 9711da177e4SLinus Torvalds mempool_destroy(md->io_pool); 9721da177e4SLinus Torvalds del_gendisk(md->disk); 97363d94e48SJun'ichi Nomura free_minor(minor); 974fba9f90eSJeff Mahoney 975fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 976fba9f90eSJeff Mahoney md->disk->private_data = NULL; 977fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 978fba9f90eSJeff Mahoney 9791da177e4SLinus Torvalds put_disk(md->disk); 9801312f40eSAl Viro blk_cleanup_queue(md->queue); 98110da4f79SJeff Mahoney module_put(THIS_MODULE); 9821da177e4SLinus Torvalds kfree(md); 9831da177e4SLinus Torvalds } 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds /* 9861da177e4SLinus Torvalds * Bind a table to the device. 9871da177e4SLinus Torvalds */ 9881da177e4SLinus Torvalds static void event_callback(void *context) 9891da177e4SLinus Torvalds { 9901da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) context; 9911da177e4SLinus Torvalds 9921da177e4SLinus Torvalds atomic_inc(&md->event_nr); 9931da177e4SLinus Torvalds wake_up(&md->eventq); 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds 9964e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size) 9971da177e4SLinus Torvalds { 9984e90188bSAlasdair G Kergon set_capacity(md->disk, size); 9991da177e4SLinus Torvalds 10001b1dcc1bSJes Sorensen mutex_lock(&md->suspended_bdev->bd_inode->i_mutex); 1001e39e2e95SAlasdair G Kergon i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT); 10021b1dcc1bSJes Sorensen mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex); 10031da177e4SLinus Torvalds } 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds static int __bind(struct mapped_device *md, struct dm_table *t) 10061da177e4SLinus Torvalds { 10071da177e4SLinus Torvalds request_queue_t *q = md->queue; 10081da177e4SLinus Torvalds sector_t size; 10091da177e4SLinus Torvalds 10101da177e4SLinus Torvalds size = dm_table_get_size(t); 10113ac51e74SDarrick J. Wong 10123ac51e74SDarrick J. Wong /* 10133ac51e74SDarrick J. Wong * Wipe any geometry if the size of the table changed. 10143ac51e74SDarrick J. Wong */ 10153ac51e74SDarrick J. Wong if (size != get_capacity(md->disk)) 10163ac51e74SDarrick J. Wong memset(&md->geometry, 0, sizeof(md->geometry)); 10173ac51e74SDarrick J. Wong 10184e90188bSAlasdair G Kergon __set_size(md, size); 10191da177e4SLinus Torvalds if (size == 0) 10201da177e4SLinus Torvalds return 0; 10211da177e4SLinus Torvalds 10221da177e4SLinus Torvalds dm_table_get(t); 1023cf222b37SAlasdair G Kergon dm_table_event_callback(t, event_callback, md); 10242ca3310eSAlasdair G Kergon 10252ca3310eSAlasdair G Kergon write_lock(&md->map_lock); 10262ca3310eSAlasdair G Kergon md->map = t; 10271da177e4SLinus Torvalds dm_table_set_restrictions(t, q); 10282ca3310eSAlasdair G Kergon write_unlock(&md->map_lock); 10292ca3310eSAlasdair G Kergon 10301da177e4SLinus Torvalds return 0; 10311da177e4SLinus Torvalds } 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds static void __unbind(struct mapped_device *md) 10341da177e4SLinus Torvalds { 10351da177e4SLinus Torvalds struct dm_table *map = md->map; 10361da177e4SLinus Torvalds 10371da177e4SLinus Torvalds if (!map) 10381da177e4SLinus Torvalds return; 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds dm_table_event_callback(map, NULL, NULL); 10411da177e4SLinus Torvalds write_lock(&md->map_lock); 10421da177e4SLinus Torvalds md->map = NULL; 10431da177e4SLinus Torvalds write_unlock(&md->map_lock); 10441da177e4SLinus Torvalds dm_table_put(map); 10451da177e4SLinus Torvalds } 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds /* 10481da177e4SLinus Torvalds * Constructor for a new device. 10491da177e4SLinus Torvalds */ 10502b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result) 10511da177e4SLinus Torvalds { 10521da177e4SLinus Torvalds struct mapped_device *md; 10531da177e4SLinus Torvalds 10542b06cfffSAlasdair G Kergon md = alloc_dev(minor); 10551da177e4SLinus Torvalds if (!md) 10561da177e4SLinus Torvalds return -ENXIO; 10571da177e4SLinus Torvalds 10581da177e4SLinus Torvalds *result = md; 10591da177e4SLinus Torvalds return 0; 10601da177e4SLinus Torvalds } 10611da177e4SLinus Torvalds 1062637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev) 10631da177e4SLinus Torvalds { 10641da177e4SLinus Torvalds struct mapped_device *md; 10651da177e4SLinus Torvalds unsigned minor = MINOR(dev); 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) 10681da177e4SLinus Torvalds return NULL; 10691da177e4SLinus Torvalds 1070f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 10711da177e4SLinus Torvalds 10721da177e4SLinus Torvalds md = idr_find(&_minor_idr, minor); 1073fba9f90eSJeff Mahoney if (md && (md == MINOR_ALLOCED || 1074fba9f90eSJeff Mahoney (dm_disk(md)->first_minor != minor) || 1075fba9f90eSJeff Mahoney test_bit(DMF_FREEING, &md->flags))) { 1076637842cfSDavid Teigland md = NULL; 1077fba9f90eSJeff Mahoney goto out; 1078fba9f90eSJeff Mahoney } 10791da177e4SLinus Torvalds 1080fba9f90eSJeff Mahoney out: 1081f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 10821da177e4SLinus Torvalds 1083637842cfSDavid Teigland return md; 1084637842cfSDavid Teigland } 1085637842cfSDavid Teigland 1086d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev) 1087d229a958SDavid Teigland { 1088d229a958SDavid Teigland struct mapped_device *md = dm_find_md(dev); 1089d229a958SDavid Teigland 1090d229a958SDavid Teigland if (md) 1091d229a958SDavid Teigland dm_get(md); 1092d229a958SDavid Teigland 1093d229a958SDavid Teigland return md; 1094d229a958SDavid Teigland } 1095d229a958SDavid Teigland 10969ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md) 1097637842cfSDavid Teigland { 10989ade92a9SAlasdair G Kergon return md->interface_ptr; 10991da177e4SLinus Torvalds } 11001da177e4SLinus Torvalds 11011da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr) 11021da177e4SLinus Torvalds { 11031da177e4SLinus Torvalds md->interface_ptr = ptr; 11041da177e4SLinus Torvalds } 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds void dm_get(struct mapped_device *md) 11071da177e4SLinus Torvalds { 11081da177e4SLinus Torvalds atomic_inc(&md->holders); 11091da177e4SLinus Torvalds } 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds void dm_put(struct mapped_device *md) 11121da177e4SLinus Torvalds { 11131134e5aeSMike Anderson struct dm_table *map; 11141da177e4SLinus Torvalds 1115fba9f90eSJeff Mahoney BUG_ON(test_bit(DMF_FREEING, &md->flags)); 1116fba9f90eSJeff Mahoney 1117f32c10b0SJeff Mahoney if (atomic_dec_and_lock(&md->holders, &_minor_lock)) { 11181134e5aeSMike Anderson map = dm_get_table(md); 1119ba61fdd1SJeff Mahoney idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor); 1120fba9f90eSJeff Mahoney set_bit(DMF_FREEING, &md->flags); 1121f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1122cf222b37SAlasdair G Kergon if (!dm_suspended(md)) { 11231da177e4SLinus Torvalds dm_table_presuspend_targets(map); 11241da177e4SLinus Torvalds dm_table_postsuspend_targets(map); 11251da177e4SLinus Torvalds } 11261da177e4SLinus Torvalds __unbind(md); 11271134e5aeSMike Anderson dm_table_put(map); 11281da177e4SLinus Torvalds free_dev(md); 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds } 11311da177e4SLinus Torvalds 11321da177e4SLinus Torvalds /* 11331da177e4SLinus Torvalds * Process the deferred bios 11341da177e4SLinus Torvalds */ 11351da177e4SLinus Torvalds static void __flush_deferred_io(struct mapped_device *md, struct bio *c) 11361da177e4SLinus Torvalds { 11371da177e4SLinus Torvalds struct bio *n; 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds while (c) { 11401da177e4SLinus Torvalds n = c->bi_next; 11411da177e4SLinus Torvalds c->bi_next = NULL; 11421da177e4SLinus Torvalds __split_bio(md, c); 11431da177e4SLinus Torvalds c = n; 11441da177e4SLinus Torvalds } 11451da177e4SLinus Torvalds } 11461da177e4SLinus Torvalds 11471da177e4SLinus Torvalds /* 11481da177e4SLinus Torvalds * Swap in a new table (destroying old one). 11491da177e4SLinus Torvalds */ 11501da177e4SLinus Torvalds int dm_swap_table(struct mapped_device *md, struct dm_table *table) 11511da177e4SLinus Torvalds { 115293c534aeSAlasdair G Kergon int r = -EINVAL; 11531da177e4SLinus Torvalds 11542ca3310eSAlasdair G Kergon down(&md->suspend_lock); 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds /* device must be suspended */ 1157cf222b37SAlasdair G Kergon if (!dm_suspended(md)) 115893c534aeSAlasdair G Kergon goto out; 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds __unbind(md); 11611da177e4SLinus Torvalds r = __bind(md, table); 11621da177e4SLinus Torvalds 116393c534aeSAlasdair G Kergon out: 11642ca3310eSAlasdair G Kergon up(&md->suspend_lock); 116593c534aeSAlasdair G Kergon return r; 11661da177e4SLinus Torvalds } 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds /* 11691da177e4SLinus Torvalds * Functions to lock and unlock any filesystem running on the 11701da177e4SLinus Torvalds * device. 11711da177e4SLinus Torvalds */ 11722ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md) 11731da177e4SLinus Torvalds { 1174e39e2e95SAlasdair G Kergon int r; 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds WARN_ON(md->frozen_sb); 1177dfbe03f6SAlasdair G Kergon 1178e39e2e95SAlasdair G Kergon md->frozen_sb = freeze_bdev(md->suspended_bdev); 1179dfbe03f6SAlasdair G Kergon if (IS_ERR(md->frozen_sb)) { 1180cf222b37SAlasdair G Kergon r = PTR_ERR(md->frozen_sb); 1181e39e2e95SAlasdair G Kergon md->frozen_sb = NULL; 1182e39e2e95SAlasdair G Kergon return r; 1183dfbe03f6SAlasdair G Kergon } 1184dfbe03f6SAlasdair G Kergon 1185aa8d7c2fSAlasdair G Kergon set_bit(DMF_FROZEN, &md->flags); 1186aa8d7c2fSAlasdair G Kergon 11871da177e4SLinus Torvalds /* don't bdput right now, we don't want the bdev 1188e39e2e95SAlasdair G Kergon * to go away while it is locked. 11891da177e4SLinus Torvalds */ 11901da177e4SLinus Torvalds return 0; 11911da177e4SLinus Torvalds } 11921da177e4SLinus Torvalds 11932ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md) 11941da177e4SLinus Torvalds { 1195aa8d7c2fSAlasdair G Kergon if (!test_bit(DMF_FROZEN, &md->flags)) 1196aa8d7c2fSAlasdair G Kergon return; 1197aa8d7c2fSAlasdair G Kergon 1198e39e2e95SAlasdair G Kergon thaw_bdev(md->suspended_bdev, md->frozen_sb); 11991da177e4SLinus Torvalds md->frozen_sb = NULL; 1200aa8d7c2fSAlasdair G Kergon clear_bit(DMF_FROZEN, &md->flags); 12011da177e4SLinus Torvalds } 12021da177e4SLinus Torvalds 12031da177e4SLinus Torvalds /* 12041da177e4SLinus Torvalds * We need to be able to change a mapping table under a mounted 12051da177e4SLinus Torvalds * filesystem. For example we might want to move some data in 12061da177e4SLinus Torvalds * the background. Before the table can be swapped with 12071da177e4SLinus Torvalds * dm_bind_table, dm_suspend must be called to flush any in 12081da177e4SLinus Torvalds * flight bios and ensure that any further io gets deferred. 12091da177e4SLinus Torvalds */ 1210aa8d7c2fSAlasdair G Kergon int dm_suspend(struct mapped_device *md, int do_lockfs) 12111da177e4SLinus Torvalds { 12122ca3310eSAlasdair G Kergon struct dm_table *map = NULL; 12131da177e4SLinus Torvalds DECLARE_WAITQUEUE(wait, current); 12141ecac7fdSJun'ichi Nomura struct bio *def; 1215cf222b37SAlasdair G Kergon int r = -EINVAL; 12161da177e4SLinus Torvalds 12172ca3310eSAlasdair G Kergon down(&md->suspend_lock); 12182ca3310eSAlasdair G Kergon 12192ca3310eSAlasdair G Kergon if (dm_suspended(md)) 12202ca3310eSAlasdair G Kergon goto out; 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds map = dm_get_table(md); 1223cf222b37SAlasdair G Kergon 1224436d4108SAlasdair G Kergon /* This does not get reverted if there's an error later. */ 12251da177e4SLinus Torvalds dm_table_presuspend_targets(map); 12261da177e4SLinus Torvalds 1227e39e2e95SAlasdair G Kergon md->suspended_bdev = bdget_disk(md->disk, 0); 1228e39e2e95SAlasdair G Kergon if (!md->suspended_bdev) { 1229e39e2e95SAlasdair G Kergon DMWARN("bdget failed in dm_suspend"); 1230e39e2e95SAlasdair G Kergon r = -ENOMEM; 1231e39e2e95SAlasdair G Kergon goto out; 1232e39e2e95SAlasdair G Kergon } 1233e39e2e95SAlasdair G Kergon 1234cf222b37SAlasdair G Kergon /* Flush I/O to the device. */ 1235aa8d7c2fSAlasdair G Kergon if (do_lockfs) { 12362ca3310eSAlasdair G Kergon r = lock_fs(md); 12372ca3310eSAlasdair G Kergon if (r) 12382ca3310eSAlasdair G Kergon goto out; 1239aa8d7c2fSAlasdair G Kergon } 12401da177e4SLinus Torvalds 12411da177e4SLinus Torvalds /* 1242354e0071SAlasdair G Kergon * First we set the BLOCK_IO flag so no more ios will be mapped. 12431da177e4SLinus Torvalds */ 12442ca3310eSAlasdair G Kergon down_write(&md->io_lock); 12452ca3310eSAlasdair G Kergon set_bit(DMF_BLOCK_IO, &md->flags); 12461da177e4SLinus Torvalds 12471da177e4SLinus Torvalds add_wait_queue(&md->wait, &wait); 12482ca3310eSAlasdair G Kergon up_write(&md->io_lock); 12491da177e4SLinus Torvalds 12501da177e4SLinus Torvalds /* unplug */ 12512ca3310eSAlasdair G Kergon if (map) 12521da177e4SLinus Torvalds dm_table_unplug_all(map); 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds /* 12551da177e4SLinus Torvalds * Then we wait for the already mapped ios to 12561da177e4SLinus Torvalds * complete. 12571da177e4SLinus Torvalds */ 12581da177e4SLinus Torvalds while (1) { 12591da177e4SLinus Torvalds set_current_state(TASK_INTERRUPTIBLE); 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds if (!atomic_read(&md->pending) || signal_pending(current)) 12621da177e4SLinus Torvalds break; 12631da177e4SLinus Torvalds 12641da177e4SLinus Torvalds io_schedule(); 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds set_current_state(TASK_RUNNING); 12671da177e4SLinus Torvalds 12682ca3310eSAlasdair G Kergon down_write(&md->io_lock); 12691da177e4SLinus Torvalds remove_wait_queue(&md->wait, &wait); 12701da177e4SLinus Torvalds 12711da177e4SLinus Torvalds /* were we interrupted ? */ 1272cf222b37SAlasdair G Kergon r = -EINTR; 12732ca3310eSAlasdair G Kergon if (atomic_read(&md->pending)) { 12741ecac7fdSJun'ichi Nomura clear_bit(DMF_BLOCK_IO, &md->flags); 12751ecac7fdSJun'ichi Nomura def = bio_list_get(&md->deferred); 12761ecac7fdSJun'ichi Nomura __flush_deferred_io(md, def); 12772ca3310eSAlasdair G Kergon up_write(&md->io_lock); 12782ca3310eSAlasdair G Kergon unlock_fs(md); 12792ca3310eSAlasdair G Kergon goto out; 12802ca3310eSAlasdair G Kergon } 12812ca3310eSAlasdair G Kergon up_write(&md->io_lock); 12822ca3310eSAlasdair G Kergon 12832ca3310eSAlasdair G Kergon dm_table_postsuspend_targets(map); 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds set_bit(DMF_SUSPENDED, &md->flags); 12861da177e4SLinus Torvalds 12872ca3310eSAlasdair G Kergon r = 0; 12882ca3310eSAlasdair G Kergon 12892ca3310eSAlasdair G Kergon out: 1290e39e2e95SAlasdair G Kergon if (r && md->suspended_bdev) { 1291e39e2e95SAlasdair G Kergon bdput(md->suspended_bdev); 1292e39e2e95SAlasdair G Kergon md->suspended_bdev = NULL; 1293e39e2e95SAlasdair G Kergon } 1294e39e2e95SAlasdair G Kergon 12951da177e4SLinus Torvalds dm_table_put(map); 12962ca3310eSAlasdair G Kergon up(&md->suspend_lock); 1297cf222b37SAlasdair G Kergon return r; 12981da177e4SLinus Torvalds } 12991da177e4SLinus Torvalds 13001da177e4SLinus Torvalds int dm_resume(struct mapped_device *md) 13011da177e4SLinus Torvalds { 1302cf222b37SAlasdair G Kergon int r = -EINVAL; 13031da177e4SLinus Torvalds struct bio *def; 1304cf222b37SAlasdair G Kergon struct dm_table *map = NULL; 13051da177e4SLinus Torvalds 13062ca3310eSAlasdair G Kergon down(&md->suspend_lock); 13072ca3310eSAlasdair G Kergon if (!dm_suspended(md)) 1308cf222b37SAlasdair G Kergon goto out; 1309cf222b37SAlasdair G Kergon 1310cf222b37SAlasdair G Kergon map = dm_get_table(md); 13112ca3310eSAlasdair G Kergon if (!map || !dm_table_get_size(map)) 1312cf222b37SAlasdair G Kergon goto out; 13131da177e4SLinus Torvalds 13141da177e4SLinus Torvalds dm_table_resume_targets(map); 13152ca3310eSAlasdair G Kergon 13162ca3310eSAlasdair G Kergon down_write(&md->io_lock); 13171da177e4SLinus Torvalds clear_bit(DMF_BLOCK_IO, &md->flags); 13181da177e4SLinus Torvalds 13191da177e4SLinus Torvalds def = bio_list_get(&md->deferred); 13201da177e4SLinus Torvalds __flush_deferred_io(md, def); 13212ca3310eSAlasdair G Kergon up_write(&md->io_lock); 13222ca3310eSAlasdair G Kergon 13232ca3310eSAlasdair G Kergon unlock_fs(md); 13242ca3310eSAlasdair G Kergon 1325e39e2e95SAlasdair G Kergon bdput(md->suspended_bdev); 1326e39e2e95SAlasdair G Kergon md->suspended_bdev = NULL; 1327e39e2e95SAlasdair G Kergon 13282ca3310eSAlasdair G Kergon clear_bit(DMF_SUSPENDED, &md->flags); 13292ca3310eSAlasdair G Kergon 13301da177e4SLinus Torvalds dm_table_unplug_all(map); 13311da177e4SLinus Torvalds 1332cf222b37SAlasdair G Kergon r = 0; 13332ca3310eSAlasdair G Kergon 1334cf222b37SAlasdair G Kergon out: 1335cf222b37SAlasdair G Kergon dm_table_put(map); 13362ca3310eSAlasdair G Kergon up(&md->suspend_lock); 13372ca3310eSAlasdair G Kergon 1338cf222b37SAlasdair G Kergon return r; 13391da177e4SLinus Torvalds } 13401da177e4SLinus Torvalds 13411da177e4SLinus Torvalds /*----------------------------------------------------------------- 13421da177e4SLinus Torvalds * Event notification. 13431da177e4SLinus Torvalds *---------------------------------------------------------------*/ 13441da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md) 13451da177e4SLinus Torvalds { 13461da177e4SLinus Torvalds return atomic_read(&md->event_nr); 13471da177e4SLinus Torvalds } 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr) 13501da177e4SLinus Torvalds { 13511da177e4SLinus Torvalds return wait_event_interruptible(md->eventq, 13521da177e4SLinus Torvalds (event_nr != atomic_read(&md->event_nr))); 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds 13551da177e4SLinus Torvalds /* 13561da177e4SLinus Torvalds * The gendisk is only valid as long as you have a reference 13571da177e4SLinus Torvalds * count on 'md'. 13581da177e4SLinus Torvalds */ 13591da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md) 13601da177e4SLinus Torvalds { 13611da177e4SLinus Torvalds return md->disk; 13621da177e4SLinus Torvalds } 13631da177e4SLinus Torvalds 13641da177e4SLinus Torvalds int dm_suspended(struct mapped_device *md) 13651da177e4SLinus Torvalds { 13661da177e4SLinus Torvalds return test_bit(DMF_SUSPENDED, &md->flags); 13671da177e4SLinus Torvalds } 13681da177e4SLinus Torvalds 13691da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops = { 13701da177e4SLinus Torvalds .open = dm_blk_open, 13711da177e4SLinus Torvalds .release = dm_blk_close, 13723ac51e74SDarrick J. Wong .getgeo = dm_blk_getgeo, 13731da177e4SLinus Torvalds .owner = THIS_MODULE 13741da177e4SLinus Torvalds }; 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo); 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds /* 13791da177e4SLinus Torvalds * module hooks 13801da177e4SLinus Torvalds */ 13811da177e4SLinus Torvalds module_init(dm_init); 13821da177e4SLinus Torvalds module_exit(dm_exit); 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds module_param(major, uint, 0); 13851da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper"); 13861da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver"); 13871da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 13881da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1389