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