blob: 646cc84ae69211259be4c4c2437d3dc121be72bd [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>
18#include <linux/hdreg.h>
19#include <linux/init.h>
Ingo Molnar48b19262006-03-31 02:29:41 -080020#include <linux/mutex.h>
Eric W. Biederman99f9b242007-04-19 01:58:33 -060021#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Ben Dooks356d70f2007-05-28 20:28:34 +010024#include "mtdcore.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Ben Dooks356d70f2007-05-28 20:28:34 +010026static LIST_HEAD(blktrans_majors);
Maxim Levitsky048d8712010-02-22 20:39:30 +020027static DEFINE_MUTEX(blktrans_ref_mutex);
28
29void 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
40static 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);
50unlock:
51 mutex_unlock(&blktrans_ref_mutex);
52 return dev;
53}
54
55void 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 Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63static 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 Heo83096eb2009-05-07 22:24:39 +090070 block = blk_rq_pos(req) << 9 >> tr->blkshift;
Tejun Heo1011c1b2009-05-07 22:24:45 +090071 nsect = blk_rq_cur_bytes(req) >> tr->blkshift;
Richard Purdie19187672006-10-27 09:09:33 +010072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 buf = req->buffer;
74
Jens Axboe4aff5e22006-08-10 08:44:47 +020075 if (!blk_fs_request(req))
Tejun Heof06d9a22009-04-23 11:05:19 +090076 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Tejun Heo83096eb2009-05-07 22:24:39 +090078 if (blk_rq_pos(req) + blk_rq_cur_sectors(req) >
79 get_capacity(req->rq_disk))
Tejun Heof06d9a22009-04-23 11:05:19 +090080 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Christoph Hellwig1122a262009-09-30 13:52:12 +020082 if (blk_discard_rq(req))
83 return tr->discard(dev, block, nsect);
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 switch(rq_data_dir(req)) {
86 case READ:
Richard Purdie19187672006-10-27 09:09:33 +010087 for (; nsect > 0; nsect--, block++, buf += tr->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (tr->readsect(dev, block, buf))
Tejun Heof06d9a22009-04-23 11:05:19 +090089 return -EIO;
Ilya Loginov2d4dc892009-11-26 09:16:19 +010090 rq_flush_dcache_pages(req);
Tejun Heof06d9a22009-04-23 11:05:19 +090091 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 case WRITE:
93 if (!tr->writesect)
Tejun Heof06d9a22009-04-23 11:05:19 +090094 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Ilya Loginov2d4dc892009-11-26 09:16:19 +010096 rq_flush_dcache_pages(req);
Richard Purdie19187672006-10-27 09:09:33 +010097 for (; nsect > 0; nsect--, block++, buf += tr->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (tr->writesect(dev, block, buf))
Tejun Heof06d9a22009-04-23 11:05:19 +090099 return -EIO;
100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 default:
Jeff Garzik9a292302006-10-01 12:16:00 -0400102 printk(KERN_NOTICE "Unknown request %u\n", rq_data_dir(req));
Tejun Heof06d9a22009-04-23 11:05:19 +0900103 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105}
106
107static int mtd_blktrans_thread(void *arg)
108{
Maxim Levitskya8638622010-02-22 20:39:29 +0200109 struct mtd_blktrans_dev *dev = arg;
110 struct request_queue *rq = dev->rq;
Tejun Heo1498ada2009-05-08 11:54:11 +0900111 struct request *req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 spin_lock_irq(rq->queue_lock);
Tejun Heo1498ada2009-05-08 11:54:11 +0900114
Christoph Hellwig3e67fe42007-04-22 20:40:57 +0100115 while (!kthread_should_stop()) {
Tejun Heof06d9a22009-04-23 11:05:19 +0900116 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Tejun Heo9934c8c2009-05-08 11:54:16 +0900118 if (!req && !(req = blk_fetch_request(rq))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 spin_unlock_irq(rq->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 spin_lock_irq(rq->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 continue;
124 }
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 spin_unlock_irq(rq->queue_lock);
127
Ingo Molnar48b19262006-03-31 02:29:41 -0800128 mutex_lock(&dev->lock);
Maxim Levitskya8638622010-02-22 20:39:29 +0200129 res = do_blktrans_request(dev->tr, dev, req);
Ingo Molnar48b19262006-03-31 02:29:41 -0800130 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 spin_lock_irq(rq->queue_lock);
133
Tejun Heo1498ada2009-05-08 11:54:11 +0900134 if (!__blk_end_request_cur(req, res))
135 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
Tejun Heo1498ada2009-05-08 11:54:11 +0900137
138 if (req)
139 __blk_end_request_all(req, -EIO);
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 spin_unlock_irq(rq->queue_lock);
142
Christoph Hellwig3e67fe42007-04-22 20:40:57 +0100143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
146static void mtd_blktrans_request(struct request_queue *rq)
147{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200148 struct mtd_blktrans_dev *dev;
149 struct request *req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Maxim Levitsky048d8712010-02-22 20:39:30 +0200151 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 Torvalds1da177e2005-04-16 15:20:36 -0700159
Al Viroaf0e2a02008-03-02 10:35:06 -0500160static int blktrans_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200162 struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
163 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Maxim Levitsky048d8712010-02-22 20:39:30 +0200165 if (!dev)
166 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Maxim Levitsky048d8712010-02-22 20:39:30 +0200168 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Maxim Levitsky048d8712010-02-22 20:39:30 +0200170 if (!dev->mtd) {
171 ret = -ENXIO;
172 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
Maxim Levitsky048d8712010-02-22 20:39:30 +0200174
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);
181unlock:
182 mutex_unlock(&dev->lock);
183 blktrans_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return ret;
185}
186
Al Viroaf0e2a02008-03-02 10:35:06 -0500187static int blktrans_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200189 struct mtd_blktrans_dev *dev = blktrans_dev_get(disk);
190 int ret = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Maxim Levitsky048d8712010-02-22 20:39:30 +0200192 if (!dev)
193 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Maxim Levitsky048d8712010-02-22 20:39:30 +0200195 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Maxim Levitsky048d8712010-02-22 20:39:30 +0200197 /* 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;
204unlock:
205 mutex_unlock(&dev->lock);
206 blktrans_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return ret;
208}
209
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800210static int blktrans_getgeo(struct block_device *bdev, struct hd_geometry *geo)
211{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200212 struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
213 int ret = -ENXIO;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800214
Maxim Levitsky048d8712010-02-22 20:39:30 +0200215 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;
224unlock:
225 mutex_unlock(&dev->lock);
226 blktrans_dev_put(dev);
227 return ret;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800228}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Al Viroaf0e2a02008-03-02 10:35:06 -0500230static int blktrans_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 unsigned int cmd, unsigned long arg)
232{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200233 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 Torvalds1da177e2005-04-16 15:20:36 -0700243
244 switch (cmd) {
245 case BLKFLSBUF:
Maxim Levitsky048d8712010-02-22 20:39:30 +0200246 ret = dev->tr->flush ? dev->tr->flush(dev) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 default:
Maxim Levitsky048d8712010-02-22 20:39:30 +0200248 ret = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
Maxim Levitsky048d8712010-02-22 20:39:30 +0200250unlock:
251 mutex_unlock(&dev->lock);
252 blktrans_dev_put(dev);
253 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700256static const struct block_device_operations mtd_blktrans_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 .owner = THIS_MODULE,
Al Viroaf0e2a02008-03-02 10:35:06 -0500258 .open = blktrans_open,
259 .release = blktrans_release,
260 .locked_ioctl = blktrans_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800261 .getgeo = blktrans_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262};
263
264int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
265{
266 struct mtd_blktrans_ops *tr = new->tr;
Chris Malley71a928c2008-05-19 20:11:50 +0100267 struct mtd_blktrans_dev *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 int last_devnum = -1;
269 struct gendisk *gd;
Maxim Levitskya8638622010-02-22 20:39:29 +0200270 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Jean Delvareb3561ea2007-05-08 00:30:46 -0700272 if (mutex_trylock(&mtd_table_mutex)) {
Ingo Molnar48b19262006-03-31 02:29:41 -0800273 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 BUG();
275 }
276
Maxim Levitsky048d8712010-02-22 20:39:30 +0200277 mutex_lock(&blktrans_ref_mutex);
Chris Malley71a928c2008-05-19 20:11:50 +0100278 list_for_each_entry(d, &tr->devs, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 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 Levitsky048d8712010-02-22 20:39:30 +0200289 mutex_unlock(&blktrans_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 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 Gleixner97894cd2005-11-07 11:15:26 +0000295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 last_devnum = d->devnum;
297 }
Maxim Levitskya8638622010-02-22 20:39:29 +0200298
299 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (new->devnum == -1)
301 new->devnum = last_devnum+1;
302
Ben Hutchings4d3a8532010-01-29 20:59:53 +0000303 /* 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 Levitsky048d8712010-02-22 20:39:30 +0200307 (tr->part_bits && new->devnum >= 27 * 26)) {
308 mutex_unlock(&blktrans_ref_mutex);
Maxim Levitskya8638622010-02-22 20:39:29 +0200309 goto error1;
Maxim Levitsky048d8712010-02-22 20:39:30 +0200310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 list_add_tail(&new->list, &tr->devs);
313 added:
Maxim Levitsky048d8712010-02-22 20:39:30 +0200314 mutex_unlock(&blktrans_ref_mutex);
315
David Woodhousece37ab42007-12-03 12:46:12 +0000316 mutex_init(&new->lock);
Maxim Levitsky048d8712010-02-22 20:39:30 +0200317 kref_init(&new->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (!tr->writesect)
319 new->readonly = 1;
320
Maxim Levitskya8638622010-02-22 20:39:29 +0200321 /* Create gendisk */
322 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 gd = alloc_disk(1 << tr->part_bits);
Maxim Levitskya8638622010-02-22 20:39:29 +0200324
325 if (!gd)
326 goto error2;
327
328 new->disk = gd;
329 gd->private_data = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 gd->major = tr->major;
331 gd->first_minor = (new->devnum) << tr->part_bits;
332 gd->fops = &mtd_blktrans_ops;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000333
Todd Poynor65a8de32005-07-29 20:42:07 +0100334 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 Torvalds1da177e2005-04-16 15:20:36 -0700346
Richard Purdie19187672006-10-27 09:09:33 +0100347 set_capacity(gd, (new->size * tr->blksize) >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Maxim Levitskya8638622010-02-22 20:39:29 +0200349 /* 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 Levitsky048d8712010-02-22 20:39:30 +0200365 __get_mtd_device(new->mtd);
366 __module_get(tr->owner);
367
Maxim Levitskya8638622010-02-22 20:39:29 +0200368 /* 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 Woodhoused6948462009-04-05 07:38:33 -0700376 gd->driverfs_dev = &new->mtd->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 if (new->readonly)
379 set_disk_ro(gd, 1);
380
381 add_disk(gd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return 0;
Maxim Levitskya8638622010-02-22 20:39:29 +0200383error4:
Maxim Levitsky048d8712010-02-22 20:39:30 +0200384 module_put(tr->owner);
385 __put_mtd_device(new->mtd);
Maxim Levitskya8638622010-02-22 20:39:29 +0200386 blk_cleanup_queue(new->rq);
387error3:
388 put_disk(new->disk);
389error2:
390 list_del(&new->list);
391error1:
392 kfree(new);
393 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
397{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200398 unsigned long flags;
399
Jean Delvareb3561ea2007-05-08 00:30:46 -0700400 if (mutex_trylock(&mtd_table_mutex)) {
Ingo Molnar48b19262006-03-31 02:29:41 -0800401 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 BUG();
403 }
404
Maxim Levitsky048d8712010-02-22 20:39:30 +0200405 /* Stop new requests to arrive */
Maxim Levitskya8638622010-02-22 20:39:29 +0200406 del_gendisk(old->disk);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000407
Maxim Levitskya8638622010-02-22 20:39:29 +0200408 /* Stop the thread */
409 kthread_stop(old->thread);
410
Maxim Levitsky048d8712010-02-22 20:39:30 +0200411 /* 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 Levitskya8638622010-02-22 20:39:29 +0200416 blk_cleanup_queue(old->rq);
Maxim Levitsky048d8712010-02-22 20:39:30 +0200417
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 Torvalds1da177e2005-04-16 15:20:36 -0700433 return 0;
434}
435
436static void blktrans_notify_remove(struct mtd_info *mtd)
437{
Chris Malley71a928c2008-05-19 20:11:50 +0100438 struct mtd_blktrans_ops *tr;
439 struct mtd_blktrans_dev *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Chris Malley71a928c2008-05-19 20:11:50 +0100441 list_for_each_entry(tr, &blktrans_majors, list)
442 list_for_each_entry_safe(dev, next, &tr->devs, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (dev->mtd == mtd)
444 tr->remove_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
447static void blktrans_notify_add(struct mtd_info *mtd)
448{
Chris Malley71a928c2008-05-19 20:11:50 +0100449 struct mtd_blktrans_ops *tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 if (mtd->type == MTD_ABSENT)
452 return;
453
Chris Malley71a928c2008-05-19 20:11:50 +0100454 list_for_each_entry(tr, &blktrans_majors, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 tr->add_mtd(tr, mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458static struct mtd_notifier blktrans_notifier = {
459 .add = blktrans_notify_add,
460 .remove = blktrans_notify_remove,
461};
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
464{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000465 struct mtd_info *mtd;
466 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000468 /* Register the notifier if/when the first device type is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 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 Torvalds1da177e2005-04-16 15:20:36 -0700474
Ingo Molnar48b19262006-03-31 02:29:41 -0800475 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
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 Molnar48b19262006-03-31 02:29:41 -0800481 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return ret;
483 }
David Woodhouseeae9acd2008-08-05 18:08:25 +0100484
Richard Purdie19187672006-10-27 09:09:33 +0100485 tr->blkshift = ffs(tr->blksize) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 INIT_LIST_HEAD(&tr->devs);
488 list_add(&tr->list, &blktrans_majors);
489
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000490 mtd_for_each_device(mtd)
491 if (mtd->type != MTD_ABSENT)
492 tr->add_mtd(tr, mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Ingo Molnar48b19262006-03-31 02:29:41 -0800494 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return 0;
496}
497
498int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr)
499{
Chris Malley71a928c2008-05-19 20:11:50 +0100500 struct mtd_blktrans_dev *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Ingo Molnar48b19262006-03-31 02:29:41 -0800502 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /* Remove it from the list of active majors */
505 list_del(&tr->list);
506
Chris Malley71a928c2008-05-19 20:11:50 +0100507 list_for_each_entry_safe(dev, next, &tr->devs, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 tr->remove_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 unregister_blkdev(tr->major, tr->name);
Ingo Molnar48b19262006-03-31 02:29:41 -0800511 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Eric Sesterhenn373ebfb2006-03-26 18:15:12 +0200513 BUG_ON(!list_empty(&tr->devs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return 0;
515}
516
517static 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
525module_exit(mtd_blktrans_exit);
526
527EXPORT_SYMBOL_GPL(register_mtd_blktrans);
528EXPORT_SYMBOL_GPL(deregister_mtd_blktrans);
529EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev);
530EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev);
531
532MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
533MODULE_LICENSE("GPL");
534MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'");