blob: 8c83b11a77d595123009f3a5d97832b29ac7fea6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (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>
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +020018#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/hdreg.h>
20#include <linux/init.h>
Ingo Molnar48b19262006-03-31 02:29:41 -080021#include <linux/mutex.h>
Eric W. Biederman99f9b242007-04-19 01:58:33 -060022#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Ben Dooks356d70f2007-05-28 20:28:34 +010025#include "mtdcore.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Ben Dooks356d70f2007-05-28 20:28:34 +010027static LIST_HEAD(blktrans_majors);
Maxim Levitsky048d8712010-02-22 20:39:30 +020028static DEFINE_MUTEX(blktrans_ref_mutex);
29
30void blktrans_dev_release(struct kref *kref)
31{
32 struct mtd_blktrans_dev *dev =
33 container_of(kref, struct mtd_blktrans_dev, ref);
34
35 dev->disk->private_data = NULL;
Maxim Levitskye4d64ca2010-02-27 02:31:51 +020036 blk_cleanup_queue(dev->rq);
Maxim Levitsky048d8712010-02-22 20:39:30 +020037 put_disk(dev->disk);
38 list_del(&dev->list);
39 kfree(dev);
40}
41
42static struct mtd_blktrans_dev *blktrans_dev_get(struct gendisk *disk)
43{
44 struct mtd_blktrans_dev *dev;
45
46 mutex_lock(&blktrans_ref_mutex);
47 dev = disk->private_data;
48
49 if (!dev)
50 goto unlock;
51 kref_get(&dev->ref);
52unlock:
53 mutex_unlock(&blktrans_ref_mutex);
54 return dev;
55}
56
57void blktrans_dev_put(struct mtd_blktrans_dev *dev)
58{
59 mutex_lock(&blktrans_ref_mutex);
60 kref_put(&dev->ref, blktrans_dev_release);
61 mutex_unlock(&blktrans_ref_mutex);
62}
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65static int do_blktrans_request(struct mtd_blktrans_ops *tr,
66 struct mtd_blktrans_dev *dev,
67 struct request *req)
68{
69 unsigned long block, nsect;
70 char *buf;
71
Tejun Heo83096eb2009-05-07 22:24:39 +090072 block = blk_rq_pos(req) << 9 >> tr->blkshift;
Tejun Heo1011c1b2009-05-07 22:24:45 +090073 nsect = blk_rq_cur_bytes(req) >> tr->blkshift;
Richard Purdie19187672006-10-27 09:09:33 +010074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 buf = req->buffer;
76
Christoph Hellwig33659eb2010-08-07 18:17:56 +020077 if (req->cmd_type != REQ_TYPE_FS)
Tejun Heof06d9a22009-04-23 11:05:19 +090078 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Tejun Heo83096eb2009-05-07 22:24:39 +090080 if (blk_rq_pos(req) + blk_rq_cur_sectors(req) >
81 get_capacity(req->rq_disk))
Tejun Heof06d9a22009-04-23 11:05:19 +090082 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Christoph Hellwig33659eb2010-08-07 18:17:56 +020084 if (req->cmd_flags & REQ_DISCARD)
Christoph Hellwig1122a262009-09-30 13:52:12 +020085 return tr->discard(dev, block, nsect);
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 switch(rq_data_dir(req)) {
88 case READ:
Richard Purdie19187672006-10-27 09:09:33 +010089 for (; nsect > 0; nsect--, block++, buf += tr->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (tr->readsect(dev, block, buf))
Tejun Heof06d9a22009-04-23 11:05:19 +090091 return -EIO;
Ilya Loginov2d4dc892009-11-26 09:16:19 +010092 rq_flush_dcache_pages(req);
Tejun Heof06d9a22009-04-23 11:05:19 +090093 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 case WRITE:
95 if (!tr->writesect)
Tejun Heof06d9a22009-04-23 11:05:19 +090096 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Ilya Loginov2d4dc892009-11-26 09:16:19 +010098 rq_flush_dcache_pages(req);
Richard Purdie19187672006-10-27 09:09:33 +010099 for (; nsect > 0; nsect--, block++, buf += tr->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (tr->writesect(dev, block, buf))
Tejun Heof06d9a22009-04-23 11:05:19 +0900101 return -EIO;
102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 default:
Jeff Garzik9a292302006-10-01 12:16:00 -0400104 printk(KERN_NOTICE "Unknown request %u\n", rq_data_dir(req));
Tejun Heof06d9a22009-04-23 11:05:19 +0900105 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107}
108
109static int mtd_blktrans_thread(void *arg)
110{
Maxim Levitskya8638622010-02-22 20:39:29 +0200111 struct mtd_blktrans_dev *dev = arg;
112 struct request_queue *rq = dev->rq;
Tejun Heo1498ada2009-05-08 11:54:11 +0900113 struct request *req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 spin_lock_irq(rq->queue_lock);
Tejun Heo1498ada2009-05-08 11:54:11 +0900116
Christoph Hellwig3e67fe42007-04-22 20:40:57 +0100117 while (!kthread_should_stop()) {
Tejun Heof06d9a22009-04-23 11:05:19 +0900118 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Tejun Heo9934c8c2009-05-08 11:54:16 +0900120 if (!req && !(req = blk_fetch_request(rq))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 spin_unlock_irq(rq->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 spin_lock_irq(rq->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 continue;
126 }
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 spin_unlock_irq(rq->queue_lock);
129
Ingo Molnar48b19262006-03-31 02:29:41 -0800130 mutex_lock(&dev->lock);
Maxim Levitskya8638622010-02-22 20:39:29 +0200131 res = do_blktrans_request(dev->tr, dev, req);
Ingo Molnar48b19262006-03-31 02:29:41 -0800132 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 spin_lock_irq(rq->queue_lock);
135
Tejun Heo1498ada2009-05-08 11:54:11 +0900136 if (!__blk_end_request_cur(req, res))
137 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
Tejun Heo1498ada2009-05-08 11:54:11 +0900139
140 if (req)
141 __blk_end_request_all(req, -EIO);
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 spin_unlock_irq(rq->queue_lock);
144
Christoph Hellwig3e67fe42007-04-22 20:40:57 +0100145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148static void mtd_blktrans_request(struct request_queue *rq)
149{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200150 struct mtd_blktrans_dev *dev;
151 struct request *req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Maxim Levitsky048d8712010-02-22 20:39:30 +0200153 dev = rq->queuedata;
154
155 if (!dev)
156 while ((req = blk_fetch_request(rq)) != NULL)
157 __blk_end_request_all(req, -ENODEV);
158 else
159 wake_up_process(dev->thread);
160}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Al Viroaf0e2a02008-03-02 10:35:06 -0500162static int blktrans_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200164 struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
165 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Maxim Levitsky048d8712010-02-22 20:39:30 +0200167 if (!dev)
168 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Maxim Levitsky048d8712010-02-22 20:39:30 +0200170 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Maxim Levitsky048d8712010-02-22 20:39:30 +0200172 if (!dev->mtd) {
173 ret = -ENXIO;
174 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
Maxim Levitsky048d8712010-02-22 20:39:30 +0200176
177 ret = !dev->open++ && dev->tr->open ? dev->tr->open(dev) : 0;
178
179 /* Take another reference on the device so it won't go away till
180 last release */
181 if (!ret)
182 kref_get(&dev->ref);
183unlock:
184 mutex_unlock(&dev->lock);
185 blktrans_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return ret;
187}
188
Al Viroaf0e2a02008-03-02 10:35:06 -0500189static int blktrans_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200191 struct mtd_blktrans_dev *dev = blktrans_dev_get(disk);
192 int ret = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Maxim Levitsky048d8712010-02-22 20:39:30 +0200194 if (!dev)
195 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Maxim Levitsky048d8712010-02-22 20:39:30 +0200197 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Maxim Levitsky048d8712010-02-22 20:39:30 +0200199 /* Release one reference, we sure its not the last one here*/
200 kref_put(&dev->ref, blktrans_dev_release);
201
202 if (!dev->mtd)
203 goto unlock;
204
205 ret = !--dev->open && dev->tr->release ? dev->tr->release(dev) : 0;
206unlock:
207 mutex_unlock(&dev->lock);
208 blktrans_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return ret;
210}
211
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800212static int blktrans_getgeo(struct block_device *bdev, struct hd_geometry *geo)
213{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200214 struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
215 int ret = -ENXIO;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800216
Maxim Levitsky048d8712010-02-22 20:39:30 +0200217 if (!dev)
218 return ret;
219
220 mutex_lock(&dev->lock);
221
222 if (!dev->mtd)
223 goto unlock;
224
225 ret = dev->tr->getgeo ? dev->tr->getgeo(dev, geo) : 0;
226unlock:
227 mutex_unlock(&dev->lock);
228 blktrans_dev_put(dev);
229 return ret;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800230}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Al Viroaf0e2a02008-03-02 10:35:06 -0500232static int blktrans_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 unsigned int cmd, unsigned long arg)
234{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200235 struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
236 int ret = -ENXIO;
237
238 if (!dev)
239 return ret;
240
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200241 lock_kernel();
Maxim Levitsky048d8712010-02-22 20:39:30 +0200242 mutex_lock(&dev->lock);
243
244 if (!dev->mtd)
245 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 switch (cmd) {
248 case BLKFLSBUF:
Maxim Levitsky048d8712010-02-22 20:39:30 +0200249 ret = dev->tr->flush ? dev->tr->flush(dev) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 default:
Maxim Levitsky048d8712010-02-22 20:39:30 +0200251 ret = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Maxim Levitsky048d8712010-02-22 20:39:30 +0200253unlock:
254 mutex_unlock(&dev->lock);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200255 unlock_kernel();
Maxim Levitsky048d8712010-02-22 20:39:30 +0200256 blktrans_dev_put(dev);
257 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700260static const struct block_device_operations mtd_blktrans_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 .owner = THIS_MODULE,
Al Viroaf0e2a02008-03-02 10:35:06 -0500262 .open = blktrans_open,
263 .release = blktrans_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200264 .ioctl = blktrans_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800265 .getgeo = blktrans_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266};
267
268int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
269{
270 struct mtd_blktrans_ops *tr = new->tr;
Chris Malley71a928c2008-05-19 20:11:50 +0100271 struct mtd_blktrans_dev *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 int last_devnum = -1;
273 struct gendisk *gd;
Maxim Levitskya8638622010-02-22 20:39:29 +0200274 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Jean Delvareb3561ea2007-05-08 00:30:46 -0700276 if (mutex_trylock(&mtd_table_mutex)) {
Ingo Molnar48b19262006-03-31 02:29:41 -0800277 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 BUG();
279 }
280
Maxim Levitsky048d8712010-02-22 20:39:30 +0200281 mutex_lock(&blktrans_ref_mutex);
Chris Malley71a928c2008-05-19 20:11:50 +0100282 list_for_each_entry(d, &tr->devs, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (new->devnum == -1) {
284 /* Use first free number */
285 if (d->devnum != last_devnum+1) {
286 /* Found a free devnum. Plug it in here */
287 new->devnum = last_devnum+1;
288 list_add_tail(&new->list, &d->list);
289 goto added;
290 }
291 } else if (d->devnum == new->devnum) {
292 /* Required number taken */
Maxim Levitsky048d8712010-02-22 20:39:30 +0200293 mutex_unlock(&blktrans_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return -EBUSY;
295 } else if (d->devnum > new->devnum) {
296 /* Required number was free */
297 list_add_tail(&new->list, &d->list);
298 goto added;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 last_devnum = d->devnum;
301 }
Maxim Levitskya8638622010-02-22 20:39:29 +0200302
303 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (new->devnum == -1)
305 new->devnum = last_devnum+1;
306
Ben Hutchings4d3a8532010-01-29 20:59:53 +0000307 /* Check that the device and any partitions will get valid
308 * minor numbers and that the disk naming code below can cope
309 * with this number. */
310 if (new->devnum > (MINORMASK >> tr->part_bits) ||
Maxim Levitsky048d8712010-02-22 20:39:30 +0200311 (tr->part_bits && new->devnum >= 27 * 26)) {
312 mutex_unlock(&blktrans_ref_mutex);
Maxim Levitskya8638622010-02-22 20:39:29 +0200313 goto error1;
Maxim Levitsky048d8712010-02-22 20:39:30 +0200314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 list_add_tail(&new->list, &tr->devs);
317 added:
Maxim Levitsky048d8712010-02-22 20:39:30 +0200318 mutex_unlock(&blktrans_ref_mutex);
319
David Woodhousece37ab42007-12-03 12:46:12 +0000320 mutex_init(&new->lock);
Maxim Levitsky048d8712010-02-22 20:39:30 +0200321 kref_init(&new->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (!tr->writesect)
323 new->readonly = 1;
324
Maxim Levitskya8638622010-02-22 20:39:29 +0200325 /* Create gendisk */
326 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 gd = alloc_disk(1 << tr->part_bits);
Maxim Levitskya8638622010-02-22 20:39:29 +0200328
329 if (!gd)
330 goto error2;
331
332 new->disk = gd;
333 gd->private_data = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 gd->major = tr->major;
335 gd->first_minor = (new->devnum) << tr->part_bits;
336 gd->fops = &mtd_blktrans_ops;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000337
Todd Poynor65a8de32005-07-29 20:42:07 +0100338 if (tr->part_bits)
339 if (new->devnum < 26)
340 snprintf(gd->disk_name, sizeof(gd->disk_name),
341 "%s%c", tr->name, 'a' + new->devnum);
342 else
343 snprintf(gd->disk_name, sizeof(gd->disk_name),
344 "%s%c%c", tr->name,
345 'a' - 1 + new->devnum / 26,
346 'a' + new->devnum % 26);
347 else
348 snprintf(gd->disk_name, sizeof(gd->disk_name),
349 "%s%d", tr->name, new->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Richard Purdie19187672006-10-27 09:09:33 +0100351 set_capacity(gd, (new->size * tr->blksize) >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Maxim Levitskya8638622010-02-22 20:39:29 +0200353 /* Create the request queue */
354 spin_lock_init(&new->queue_lock);
355 new->rq = blk_init_queue(mtd_blktrans_request, &new->queue_lock);
356
357 if (!new->rq)
358 goto error3;
359
360 new->rq->queuedata = new;
361 blk_queue_logical_block_size(new->rq, tr->blksize);
362
363 if (tr->discard)
364 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
365 new->rq);
366
367 gd->queue = new->rq;
368
Maxim Levitsky048d8712010-02-22 20:39:30 +0200369 __get_mtd_device(new->mtd);
370 __module_get(tr->owner);
371
Maxim Levitskya8638622010-02-22 20:39:29 +0200372 /* Create processing thread */
373 /* TODO: workqueue ? */
374 new->thread = kthread_run(mtd_blktrans_thread, new,
375 "%s%d", tr->name, new->mtd->index);
376 if (IS_ERR(new->thread)) {
377 ret = PTR_ERR(new->thread);
378 goto error4;
379 }
David Woodhoused6948462009-04-05 07:38:33 -0700380 gd->driverfs_dev = &new->mtd->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 if (new->readonly)
383 set_disk_ro(gd, 1);
384
385 add_disk(gd);
Maxim Levitsky026ec572010-02-22 20:39:33 +0200386
Maxim Levitsky133fa8c2010-02-26 22:08:40 +0200387 if (new->disk_attributes) {
388 ret = sysfs_create_group(&disk_to_dev(gd)->kobj,
Maxim Levitsky026ec572010-02-22 20:39:33 +0200389 new->disk_attributes);
Maxim Levitsky133fa8c2010-02-26 22:08:40 +0200390 WARN_ON(ret);
391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return 0;
Maxim Levitskya8638622010-02-22 20:39:29 +0200393error4:
Maxim Levitsky048d8712010-02-22 20:39:30 +0200394 module_put(tr->owner);
395 __put_mtd_device(new->mtd);
Maxim Levitskya8638622010-02-22 20:39:29 +0200396 blk_cleanup_queue(new->rq);
397error3:
398 put_disk(new->disk);
399error2:
400 list_del(&new->list);
401error1:
402 kfree(new);
403 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
407{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200408 unsigned long flags;
409
Jean Delvareb3561ea2007-05-08 00:30:46 -0700410 if (mutex_trylock(&mtd_table_mutex)) {
Ingo Molnar48b19262006-03-31 02:29:41 -0800411 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 BUG();
413 }
414
Maxim Levitsky048d8712010-02-22 20:39:30 +0200415 /* Stop new requests to arrive */
Maxim Levitskya8638622010-02-22 20:39:29 +0200416 del_gendisk(old->disk);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000417
Maxim Levitsky026ec572010-02-22 20:39:33 +0200418 if (old->disk_attributes)
419 sysfs_remove_group(&disk_to_dev(old->disk)->kobj,
420 old->disk_attributes);
421
Maxim Levitskya8638622010-02-22 20:39:29 +0200422 /* Stop the thread */
423 kthread_stop(old->thread);
424
Maxim Levitsky048d8712010-02-22 20:39:30 +0200425 /* Kill current requests */
426 spin_lock_irqsave(&old->queue_lock, flags);
427 old->rq->queuedata = NULL;
428 blk_start_queue(old->rq);
429 spin_unlock_irqrestore(&old->queue_lock, flags);
Maxim Levitsky048d8712010-02-22 20:39:30 +0200430
431 /* Ask trans driver for release to the mtd device */
432 mutex_lock(&old->lock);
433 if (old->open && old->tr->release) {
434 old->tr->release(old);
435 old->open = 0;
436 }
437
438 __put_mtd_device(old->mtd);
439 module_put(old->tr->owner);
440
441 /* At that point, we don't touch the mtd anymore */
442 old->mtd = NULL;
443
444 mutex_unlock(&old->lock);
445 blktrans_dev_put(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return 0;
447}
448
449static void blktrans_notify_remove(struct mtd_info *mtd)
450{
Chris Malley71a928c2008-05-19 20:11:50 +0100451 struct mtd_blktrans_ops *tr;
452 struct mtd_blktrans_dev *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Chris Malley71a928c2008-05-19 20:11:50 +0100454 list_for_each_entry(tr, &blktrans_majors, list)
455 list_for_each_entry_safe(dev, next, &tr->devs, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (dev->mtd == mtd)
457 tr->remove_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460static void blktrans_notify_add(struct mtd_info *mtd)
461{
Chris Malley71a928c2008-05-19 20:11:50 +0100462 struct mtd_blktrans_ops *tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 if (mtd->type == MTD_ABSENT)
465 return;
466
Chris Malley71a928c2008-05-19 20:11:50 +0100467 list_for_each_entry(tr, &blktrans_majors, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 tr->add_mtd(tr, mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
471static struct mtd_notifier blktrans_notifier = {
472 .add = blktrans_notify_add,
473 .remove = blktrans_notify_remove,
474};
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
477{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000478 struct mtd_info *mtd;
479 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000481 /* Register the notifier if/when the first device type is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 registered, to prevent the link/init ordering from fucking
483 us over. */
484 if (!blktrans_notifier.list.next)
485 register_mtd_user(&blktrans_notifier);
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Ingo Molnar48b19262006-03-31 02:29:41 -0800488 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 ret = register_blkdev(tr->major, tr->name);
491 if (ret) {
492 printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n",
493 tr->name, tr->major, ret);
Ingo Molnar48b19262006-03-31 02:29:41 -0800494 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return ret;
496 }
David Woodhouseeae9acd2008-08-05 18:08:25 +0100497
Richard Purdie19187672006-10-27 09:09:33 +0100498 tr->blkshift = ffs(tr->blksize) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 INIT_LIST_HEAD(&tr->devs);
501 list_add(&tr->list, &blktrans_majors);
502
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000503 mtd_for_each_device(mtd)
504 if (mtd->type != MTD_ABSENT)
505 tr->add_mtd(tr, mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Ingo Molnar48b19262006-03-31 02:29:41 -0800507 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return 0;
509}
510
511int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr)
512{
Chris Malley71a928c2008-05-19 20:11:50 +0100513 struct mtd_blktrans_dev *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Ingo Molnar48b19262006-03-31 02:29:41 -0800515 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /* Remove it from the list of active majors */
518 list_del(&tr->list);
519
Chris Malley71a928c2008-05-19 20:11:50 +0100520 list_for_each_entry_safe(dev, next, &tr->devs, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 tr->remove_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 unregister_blkdev(tr->major, tr->name);
Ingo Molnar48b19262006-03-31 02:29:41 -0800524 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Eric Sesterhenn373ebfb2006-03-26 18:15:12 +0200526 BUG_ON(!list_empty(&tr->devs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return 0;
528}
529
530static void __exit mtd_blktrans_exit(void)
531{
532 /* No race here -- if someone's currently in register_mtd_blktrans
533 we're screwed anyway. */
534 if (blktrans_notifier.list.next)
535 unregister_mtd_user(&blktrans_notifier);
536}
537
538module_exit(mtd_blktrans_exit);
539
540EXPORT_SYMBOL_GPL(register_mtd_blktrans);
541EXPORT_SYMBOL_GPL(deregister_mtd_blktrans);
542EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev);
543EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev);
544
545MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
546MODULE_LICENSE("GPL");
547MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'");