Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * (C) 2003 David Woodhouse <dwmw2@infradead.org> |
| 3 | * |
| 4 | * Interface to Linux 2.5 block layer for MTD 'translation layers'. |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/list.h> |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/mtd/blktrans.h> |
| 14 | #include <linux/mtd/mtd.h> |
| 15 | #include <linux/blkdev.h> |
| 16 | #include <linux/blkpg.h> |
| 17 | #include <linux/spinlock.h> |
| 18 | #include <linux/hdreg.h> |
| 19 | #include <linux/init.h> |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 20 | #include <linux/mutex.h> |
Eric W. Biederman | 99f9b24 | 2007-04-19 01:58:33 -0600 | [diff] [blame] | 21 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Ben Dooks | 356d70f | 2007-05-28 20:28:34 +0100 | [diff] [blame] | 24 | #include "mtdcore.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Ben Dooks | 356d70f | 2007-05-28 20:28:34 +0100 | [diff] [blame] | 26 | static LIST_HEAD(blktrans_majors); |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 27 | static DEFINE_MUTEX(blktrans_ref_mutex); |
| 28 | |
| 29 | void blktrans_dev_release(struct kref *kref) |
| 30 | { |
| 31 | struct mtd_blktrans_dev *dev = |
| 32 | container_of(kref, struct mtd_blktrans_dev, ref); |
| 33 | |
| 34 | dev->disk->private_data = NULL; |
Maxim Levitsky | e4d64ca | 2010-02-27 02:31:51 +0200 | [diff] [blame] | 35 | blk_cleanup_queue(dev->rq); |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 36 | put_disk(dev->disk); |
| 37 | list_del(&dev->list); |
| 38 | kfree(dev); |
| 39 | } |
| 40 | |
| 41 | static struct mtd_blktrans_dev *blktrans_dev_get(struct gendisk *disk) |
| 42 | { |
| 43 | struct mtd_blktrans_dev *dev; |
| 44 | |
| 45 | mutex_lock(&blktrans_ref_mutex); |
| 46 | dev = disk->private_data; |
| 47 | |
| 48 | if (!dev) |
| 49 | goto unlock; |
| 50 | kref_get(&dev->ref); |
| 51 | unlock: |
| 52 | mutex_unlock(&blktrans_ref_mutex); |
| 53 | return dev; |
| 54 | } |
| 55 | |
| 56 | void blktrans_dev_put(struct mtd_blktrans_dev *dev) |
| 57 | { |
| 58 | mutex_lock(&blktrans_ref_mutex); |
| 59 | kref_put(&dev->ref, blktrans_dev_release); |
| 60 | mutex_unlock(&blktrans_ref_mutex); |
| 61 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | static int do_blktrans_request(struct mtd_blktrans_ops *tr, |
| 65 | struct mtd_blktrans_dev *dev, |
| 66 | struct request *req) |
| 67 | { |
| 68 | unsigned long block, nsect; |
| 69 | char *buf; |
| 70 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 71 | block = blk_rq_pos(req) << 9 >> tr->blkshift; |
Tejun Heo | 1011c1b | 2009-05-07 22:24:45 +0900 | [diff] [blame] | 72 | nsect = blk_rq_cur_bytes(req) >> tr->blkshift; |
Richard Purdie | 1918767 | 2006-10-27 09:09:33 +0100 | [diff] [blame] | 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | buf = req->buffer; |
| 75 | |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 76 | if (!blk_fs_request(req)) |
Tejun Heo | f06d9a2 | 2009-04-23 11:05:19 +0900 | [diff] [blame] | 77 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 79 | if (blk_rq_pos(req) + blk_rq_cur_sectors(req) > |
| 80 | get_capacity(req->rq_disk)) |
Tejun Heo | f06d9a2 | 2009-04-23 11:05:19 +0900 | [diff] [blame] | 81 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Christoph Hellwig | 1122a26 | 2009-09-30 13:52:12 +0200 | [diff] [blame] | 83 | if (blk_discard_rq(req)) |
| 84 | return tr->discard(dev, block, nsect); |
| 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | switch(rq_data_dir(req)) { |
| 87 | case READ: |
Richard Purdie | 1918767 | 2006-10-27 09:09:33 +0100 | [diff] [blame] | 88 | for (; nsect > 0; nsect--, block++, buf += tr->blksize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | if (tr->readsect(dev, block, buf)) |
Tejun Heo | f06d9a2 | 2009-04-23 11:05:19 +0900 | [diff] [blame] | 90 | return -EIO; |
Ilya Loginov | 2d4dc89 | 2009-11-26 09:16:19 +0100 | [diff] [blame] | 91 | rq_flush_dcache_pages(req); |
Tejun Heo | f06d9a2 | 2009-04-23 11:05:19 +0900 | [diff] [blame] | 92 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | case WRITE: |
| 94 | if (!tr->writesect) |
Tejun Heo | f06d9a2 | 2009-04-23 11:05:19 +0900 | [diff] [blame] | 95 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Ilya Loginov | 2d4dc89 | 2009-11-26 09:16:19 +0100 | [diff] [blame] | 97 | rq_flush_dcache_pages(req); |
Richard Purdie | 1918767 | 2006-10-27 09:09:33 +0100 | [diff] [blame] | 98 | for (; nsect > 0; nsect--, block++, buf += tr->blksize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | if (tr->writesect(dev, block, buf)) |
Tejun Heo | f06d9a2 | 2009-04-23 11:05:19 +0900 | [diff] [blame] | 100 | return -EIO; |
| 101 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | default: |
Jeff Garzik | 9a29230 | 2006-10-01 12:16:00 -0400 | [diff] [blame] | 103 | printk(KERN_NOTICE "Unknown request %u\n", rq_data_dir(req)); |
Tejun Heo | f06d9a2 | 2009-04-23 11:05:19 +0900 | [diff] [blame] | 104 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | static int mtd_blktrans_thread(void *arg) |
| 109 | { |
Maxim Levitsky | a863862 | 2010-02-22 20:39:29 +0200 | [diff] [blame] | 110 | struct mtd_blktrans_dev *dev = arg; |
| 111 | struct request_queue *rq = dev->rq; |
Tejun Heo | 1498ada | 2009-05-08 11:54:11 +0900 | [diff] [blame] | 112 | struct request *req = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | spin_lock_irq(rq->queue_lock); |
Tejun Heo | 1498ada | 2009-05-08 11:54:11 +0900 | [diff] [blame] | 115 | |
Christoph Hellwig | 3e67fe4 | 2007-04-22 20:40:57 +0100 | [diff] [blame] | 116 | while (!kthread_should_stop()) { |
Tejun Heo | f06d9a2 | 2009-04-23 11:05:19 +0900 | [diff] [blame] | 117 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 119 | if (!req && !(req = blk_fetch_request(rq))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | set_current_state(TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | spin_unlock_irq(rq->queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | schedule(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | spin_lock_irq(rq->queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | continue; |
| 125 | } |
| 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | spin_unlock_irq(rq->queue_lock); |
| 128 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 129 | mutex_lock(&dev->lock); |
Maxim Levitsky | a863862 | 2010-02-22 20:39:29 +0200 | [diff] [blame] | 130 | res = do_blktrans_request(dev->tr, dev, req); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 131 | mutex_unlock(&dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| 133 | spin_lock_irq(rq->queue_lock); |
| 134 | |
Tejun Heo | 1498ada | 2009-05-08 11:54:11 +0900 | [diff] [blame] | 135 | if (!__blk_end_request_cur(req, res)) |
| 136 | req = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
Tejun Heo | 1498ada | 2009-05-08 11:54:11 +0900 | [diff] [blame] | 138 | |
| 139 | if (req) |
| 140 | __blk_end_request_all(req, -EIO); |
| 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | spin_unlock_irq(rq->queue_lock); |
| 143 | |
Christoph Hellwig | 3e67fe4 | 2007-04-22 20:40:57 +0100 | [diff] [blame] | 144 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static void mtd_blktrans_request(struct request_queue *rq) |
| 148 | { |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 149 | struct mtd_blktrans_dev *dev; |
| 150 | struct request *req = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 152 | dev = rq->queuedata; |
| 153 | |
| 154 | if (!dev) |
| 155 | while ((req = blk_fetch_request(rq)) != NULL) |
| 156 | __blk_end_request_all(req, -ENODEV); |
| 157 | else |
| 158 | wake_up_process(dev->thread); |
| 159 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Al Viro | af0e2a0 | 2008-03-02 10:35:06 -0500 | [diff] [blame] | 161 | static int blktrans_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 163 | struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk); |
| 164 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 166 | if (!dev) |
| 167 | return -ERESTARTSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 169 | mutex_lock(&dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 171 | if (!dev->mtd) { |
| 172 | ret = -ENXIO; |
| 173 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 175 | |
| 176 | ret = !dev->open++ && dev->tr->open ? dev->tr->open(dev) : 0; |
| 177 | |
| 178 | /* Take another reference on the device so it won't go away till |
| 179 | last release */ |
| 180 | if (!ret) |
| 181 | kref_get(&dev->ref); |
| 182 | unlock: |
| 183 | mutex_unlock(&dev->lock); |
| 184 | blktrans_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | return ret; |
| 186 | } |
| 187 | |
Al Viro | af0e2a0 | 2008-03-02 10:35:06 -0500 | [diff] [blame] | 188 | static int blktrans_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | { |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 190 | struct mtd_blktrans_dev *dev = blktrans_dev_get(disk); |
| 191 | int ret = -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 193 | if (!dev) |
| 194 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 196 | mutex_lock(&dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 198 | /* Release one reference, we sure its not the last one here*/ |
| 199 | kref_put(&dev->ref, blktrans_dev_release); |
| 200 | |
| 201 | if (!dev->mtd) |
| 202 | goto unlock; |
| 203 | |
| 204 | ret = !--dev->open && dev->tr->release ? dev->tr->release(dev) : 0; |
| 205 | unlock: |
| 206 | mutex_unlock(&dev->lock); |
| 207 | blktrans_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | return ret; |
| 209 | } |
| 210 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 211 | static int blktrans_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 212 | { |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 213 | struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk); |
| 214 | int ret = -ENXIO; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 215 | |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 216 | if (!dev) |
| 217 | return ret; |
| 218 | |
| 219 | mutex_lock(&dev->lock); |
| 220 | |
| 221 | if (!dev->mtd) |
| 222 | goto unlock; |
| 223 | |
| 224 | ret = dev->tr->getgeo ? dev->tr->getgeo(dev, geo) : 0; |
| 225 | unlock: |
| 226 | mutex_unlock(&dev->lock); |
| 227 | blktrans_dev_put(dev); |
| 228 | return ret; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 229 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Al Viro | af0e2a0 | 2008-03-02 10:35:06 -0500 | [diff] [blame] | 231 | static int blktrans_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | unsigned int cmd, unsigned long arg) |
| 233 | { |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 234 | struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk); |
| 235 | int ret = -ENXIO; |
| 236 | |
| 237 | if (!dev) |
| 238 | return ret; |
| 239 | |
| 240 | mutex_lock(&dev->lock); |
| 241 | |
| 242 | if (!dev->mtd) |
| 243 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
| 245 | switch (cmd) { |
| 246 | case BLKFLSBUF: |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 247 | ret = dev->tr->flush ? dev->tr->flush(dev) : 0; |
Dan Carpenter | 007c2d8 | 2010-05-31 16:03:38 +0200 | [diff] [blame] | 248 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | default: |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 250 | ret = -ENOTTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 252 | unlock: |
| 253 | mutex_unlock(&dev->lock); |
| 254 | blktrans_dev_put(dev); |
| 255 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 258 | static const struct block_device_operations mtd_blktrans_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | .owner = THIS_MODULE, |
Al Viro | af0e2a0 | 2008-03-02 10:35:06 -0500 | [diff] [blame] | 260 | .open = blktrans_open, |
| 261 | .release = blktrans_release, |
| 262 | .locked_ioctl = blktrans_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 263 | .getgeo = blktrans_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new) |
| 267 | { |
| 268 | struct mtd_blktrans_ops *tr = new->tr; |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 269 | struct mtd_blktrans_dev *d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | int last_devnum = -1; |
| 271 | struct gendisk *gd; |
Maxim Levitsky | a863862 | 2010-02-22 20:39:29 +0200 | [diff] [blame] | 272 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
Jean Delvare | b3561ea | 2007-05-08 00:30:46 -0700 | [diff] [blame] | 274 | if (mutex_trylock(&mtd_table_mutex)) { |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 275 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | BUG(); |
| 277 | } |
| 278 | |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 279 | mutex_lock(&blktrans_ref_mutex); |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 280 | list_for_each_entry(d, &tr->devs, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | if (new->devnum == -1) { |
| 282 | /* Use first free number */ |
| 283 | if (d->devnum != last_devnum+1) { |
| 284 | /* Found a free devnum. Plug it in here */ |
| 285 | new->devnum = last_devnum+1; |
| 286 | list_add_tail(&new->list, &d->list); |
| 287 | goto added; |
| 288 | } |
| 289 | } else if (d->devnum == new->devnum) { |
| 290 | /* Required number taken */ |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 291 | mutex_unlock(&blktrans_ref_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | return -EBUSY; |
| 293 | } else if (d->devnum > new->devnum) { |
| 294 | /* Required number was free */ |
| 295 | list_add_tail(&new->list, &d->list); |
| 296 | goto added; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 297 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | last_devnum = d->devnum; |
| 299 | } |
Maxim Levitsky | a863862 | 2010-02-22 20:39:29 +0200 | [diff] [blame] | 300 | |
| 301 | ret = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | if (new->devnum == -1) |
| 303 | new->devnum = last_devnum+1; |
| 304 | |
Ben Hutchings | 4d3a853 | 2010-01-29 20:59:53 +0000 | [diff] [blame] | 305 | /* Check that the device and any partitions will get valid |
| 306 | * minor numbers and that the disk naming code below can cope |
| 307 | * with this number. */ |
| 308 | if (new->devnum > (MINORMASK >> tr->part_bits) || |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 309 | (tr->part_bits && new->devnum >= 27 * 26)) { |
| 310 | mutex_unlock(&blktrans_ref_mutex); |
Maxim Levitsky | a863862 | 2010-02-22 20:39:29 +0200 | [diff] [blame] | 311 | goto error1; |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 312 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | list_add_tail(&new->list, &tr->devs); |
| 315 | added: |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 316 | mutex_unlock(&blktrans_ref_mutex); |
| 317 | |
David Woodhouse | ce37ab4 | 2007-12-03 12:46:12 +0000 | [diff] [blame] | 318 | mutex_init(&new->lock); |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 319 | kref_init(&new->ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | if (!tr->writesect) |
| 321 | new->readonly = 1; |
| 322 | |
Maxim Levitsky | a863862 | 2010-02-22 20:39:29 +0200 | [diff] [blame] | 323 | /* Create gendisk */ |
| 324 | ret = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | gd = alloc_disk(1 << tr->part_bits); |
Maxim Levitsky | a863862 | 2010-02-22 20:39:29 +0200 | [diff] [blame] | 326 | |
| 327 | if (!gd) |
| 328 | goto error2; |
| 329 | |
| 330 | new->disk = gd; |
| 331 | gd->private_data = new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | gd->major = tr->major; |
| 333 | gd->first_minor = (new->devnum) << tr->part_bits; |
| 334 | gd->fops = &mtd_blktrans_ops; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 335 | |
Todd Poynor | 65a8de3 | 2005-07-29 20:42:07 +0100 | [diff] [blame] | 336 | if (tr->part_bits) |
| 337 | if (new->devnum < 26) |
| 338 | snprintf(gd->disk_name, sizeof(gd->disk_name), |
| 339 | "%s%c", tr->name, 'a' + new->devnum); |
| 340 | else |
| 341 | snprintf(gd->disk_name, sizeof(gd->disk_name), |
| 342 | "%s%c%c", tr->name, |
| 343 | 'a' - 1 + new->devnum / 26, |
| 344 | 'a' + new->devnum % 26); |
| 345 | else |
| 346 | snprintf(gd->disk_name, sizeof(gd->disk_name), |
| 347 | "%s%d", tr->name, new->devnum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
Richard Purdie | 1918767 | 2006-10-27 09:09:33 +0100 | [diff] [blame] | 349 | set_capacity(gd, (new->size * tr->blksize) >> 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
Maxim Levitsky | a863862 | 2010-02-22 20:39:29 +0200 | [diff] [blame] | 351 | /* Create the request queue */ |
| 352 | spin_lock_init(&new->queue_lock); |
| 353 | new->rq = blk_init_queue(mtd_blktrans_request, &new->queue_lock); |
| 354 | |
| 355 | if (!new->rq) |
| 356 | goto error3; |
| 357 | |
| 358 | new->rq->queuedata = new; |
| 359 | blk_queue_logical_block_size(new->rq, tr->blksize); |
| 360 | |
| 361 | if (tr->discard) |
| 362 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, |
| 363 | new->rq); |
| 364 | |
| 365 | gd->queue = new->rq; |
| 366 | |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 367 | __get_mtd_device(new->mtd); |
| 368 | __module_get(tr->owner); |
| 369 | |
Maxim Levitsky | a863862 | 2010-02-22 20:39:29 +0200 | [diff] [blame] | 370 | /* Create processing thread */ |
| 371 | /* TODO: workqueue ? */ |
| 372 | new->thread = kthread_run(mtd_blktrans_thread, new, |
| 373 | "%s%d", tr->name, new->mtd->index); |
| 374 | if (IS_ERR(new->thread)) { |
| 375 | ret = PTR_ERR(new->thread); |
| 376 | goto error4; |
| 377 | } |
David Woodhouse | d694846 | 2009-04-05 07:38:33 -0700 | [diff] [blame] | 378 | gd->driverfs_dev = &new->mtd->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
| 380 | if (new->readonly) |
| 381 | set_disk_ro(gd, 1); |
| 382 | |
| 383 | add_disk(gd); |
Maxim Levitsky | 026ec57 | 2010-02-22 20:39:33 +0200 | [diff] [blame] | 384 | |
Maxim Levitsky | 133fa8c | 2010-02-26 22:08:40 +0200 | [diff] [blame] | 385 | if (new->disk_attributes) { |
| 386 | ret = sysfs_create_group(&disk_to_dev(gd)->kobj, |
Maxim Levitsky | 026ec57 | 2010-02-22 20:39:33 +0200 | [diff] [blame] | 387 | new->disk_attributes); |
Maxim Levitsky | 133fa8c | 2010-02-26 22:08:40 +0200 | [diff] [blame] | 388 | WARN_ON(ret); |
| 389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | return 0; |
Maxim Levitsky | a863862 | 2010-02-22 20:39:29 +0200 | [diff] [blame] | 391 | error4: |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 392 | module_put(tr->owner); |
| 393 | __put_mtd_device(new->mtd); |
Maxim Levitsky | a863862 | 2010-02-22 20:39:29 +0200 | [diff] [blame] | 394 | blk_cleanup_queue(new->rq); |
| 395 | error3: |
| 396 | put_disk(new->disk); |
| 397 | error2: |
| 398 | list_del(&new->list); |
| 399 | error1: |
| 400 | kfree(new); |
| 401 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old) |
| 405 | { |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 406 | unsigned long flags; |
| 407 | |
Jean Delvare | b3561ea | 2007-05-08 00:30:46 -0700 | [diff] [blame] | 408 | if (mutex_trylock(&mtd_table_mutex)) { |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 409 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | BUG(); |
| 411 | } |
| 412 | |
Maxim Levitsky | 026ec57 | 2010-02-22 20:39:33 +0200 | [diff] [blame] | 413 | if (old->disk_attributes) |
| 414 | sysfs_remove_group(&disk_to_dev(old->disk)->kobj, |
| 415 | old->disk_attributes); |
| 416 | |
Maxim Levitsky | dba76c0 | 2010-07-28 18:53:16 +0300 | [diff] [blame^] | 417 | /* Stop new requests to arrive */ |
| 418 | del_gendisk(old->disk); |
| 419 | |
| 420 | |
Maxim Levitsky | a863862 | 2010-02-22 20:39:29 +0200 | [diff] [blame] | 421 | /* Stop the thread */ |
| 422 | kthread_stop(old->thread); |
| 423 | |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 424 | /* Kill current requests */ |
| 425 | spin_lock_irqsave(&old->queue_lock, flags); |
| 426 | old->rq->queuedata = NULL; |
| 427 | blk_start_queue(old->rq); |
| 428 | spin_unlock_irqrestore(&old->queue_lock, flags); |
Maxim Levitsky | 048d871 | 2010-02-22 20:39:30 +0200 | [diff] [blame] | 429 | |
| 430 | /* Ask trans driver for release to the mtd device */ |
| 431 | mutex_lock(&old->lock); |
| 432 | if (old->open && old->tr->release) { |
| 433 | old->tr->release(old); |
| 434 | old->open = 0; |
| 435 | } |
| 436 | |
| 437 | __put_mtd_device(old->mtd); |
| 438 | module_put(old->tr->owner); |
| 439 | |
| 440 | /* At that point, we don't touch the mtd anymore */ |
| 441 | old->mtd = NULL; |
| 442 | |
| 443 | mutex_unlock(&old->lock); |
| 444 | blktrans_dev_put(old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | static void blktrans_notify_remove(struct mtd_info *mtd) |
| 449 | { |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 450 | struct mtd_blktrans_ops *tr; |
| 451 | struct mtd_blktrans_dev *dev, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 453 | list_for_each_entry(tr, &blktrans_majors, list) |
| 454 | list_for_each_entry_safe(dev, next, &tr->devs, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | if (dev->mtd == mtd) |
| 456 | tr->remove_dev(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | static void blktrans_notify_add(struct mtd_info *mtd) |
| 460 | { |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 461 | struct mtd_blktrans_ops *tr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
| 463 | if (mtd->type == MTD_ABSENT) |
| 464 | return; |
| 465 | |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 466 | list_for_each_entry(tr, &blktrans_majors, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | tr->add_mtd(tr, mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | static struct mtd_notifier blktrans_notifier = { |
| 471 | .add = blktrans_notify_add, |
| 472 | .remove = blktrans_notify_remove, |
| 473 | }; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | int register_mtd_blktrans(struct mtd_blktrans_ops *tr) |
| 476 | { |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 477 | struct mtd_info *mtd; |
| 478 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 480 | /* Register the notifier if/when the first device type is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | registered, to prevent the link/init ordering from fucking |
| 482 | us over. */ |
| 483 | if (!blktrans_notifier.list.next) |
| 484 | register_mtd_user(&blktrans_notifier); |
| 485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 487 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
| 489 | ret = register_blkdev(tr->major, tr->name); |
| 490 | if (ret) { |
| 491 | printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n", |
| 492 | tr->name, tr->major, ret); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 493 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | return ret; |
| 495 | } |
David Woodhouse | eae9acd | 2008-08-05 18:08:25 +0100 | [diff] [blame] | 496 | |
Richard Purdie | 1918767 | 2006-10-27 09:09:33 +0100 | [diff] [blame] | 497 | tr->blkshift = ffs(tr->blksize) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | INIT_LIST_HEAD(&tr->devs); |
| 500 | list_add(&tr->list, &blktrans_majors); |
| 501 | |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 502 | mtd_for_each_device(mtd) |
| 503 | if (mtd->type != MTD_ABSENT) |
| 504 | tr->add_mtd(tr, mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 506 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr) |
| 511 | { |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 512 | struct mtd_blktrans_dev *dev, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 514 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | /* Remove it from the list of active majors */ |
| 517 | list_del(&tr->list); |
| 518 | |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 519 | list_for_each_entry_safe(dev, next, &tr->devs, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | tr->remove_dev(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | unregister_blkdev(tr->major, tr->name); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 523 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | |
Eric Sesterhenn | 373ebfb | 2006-03-26 18:15:12 +0200 | [diff] [blame] | 525 | BUG_ON(!list_empty(&tr->devs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | static void __exit mtd_blktrans_exit(void) |
| 530 | { |
| 531 | /* No race here -- if someone's currently in register_mtd_blktrans |
| 532 | we're screwed anyway. */ |
| 533 | if (blktrans_notifier.list.next) |
| 534 | unregister_mtd_user(&blktrans_notifier); |
| 535 | } |
| 536 | |
| 537 | module_exit(mtd_blktrans_exit); |
| 538 | |
| 539 | EXPORT_SYMBOL_GPL(register_mtd_blktrans); |
| 540 | EXPORT_SYMBOL_GPL(deregister_mtd_blktrans); |
| 541 | EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev); |
| 542 | EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev); |
| 543 | |
| 544 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
| 545 | MODULE_LICENSE("GPL"); |
| 546 | MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'"); |