blob: e32c49cb4005964e4c1c13ce4982e495a6569b50 [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);
Maxim Levitsky026ec572010-02-22 20:39:33 +0200382
Maxim Levitsky133fa8c2010-02-26 22:08:40 +0200383 if (new->disk_attributes) {
384 ret = sysfs_create_group(&disk_to_dev(gd)->kobj,
Maxim Levitsky026ec572010-02-22 20:39:33 +0200385 new->disk_attributes);
Maxim Levitsky133fa8c2010-02-26 22:08:40 +0200386 WARN_ON(ret);
387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return 0;
Maxim Levitskya8638622010-02-22 20:39:29 +0200389error4:
Maxim Levitsky048d8712010-02-22 20:39:30 +0200390 module_put(tr->owner);
391 __put_mtd_device(new->mtd);
Maxim Levitskya8638622010-02-22 20:39:29 +0200392 blk_cleanup_queue(new->rq);
393error3:
394 put_disk(new->disk);
395error2:
396 list_del(&new->list);
397error1:
398 kfree(new);
399 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
403{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200404 unsigned long flags;
405
Jean Delvareb3561ea2007-05-08 00:30:46 -0700406 if (mutex_trylock(&mtd_table_mutex)) {
Ingo Molnar48b19262006-03-31 02:29:41 -0800407 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 BUG();
409 }
410
Maxim Levitsky048d8712010-02-22 20:39:30 +0200411 /* Stop new requests to arrive */
Maxim Levitskya8638622010-02-22 20:39:29 +0200412 del_gendisk(old->disk);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000413
Maxim Levitsky026ec572010-02-22 20:39:33 +0200414 if (old->disk_attributes)
415 sysfs_remove_group(&disk_to_dev(old->disk)->kobj,
416 old->disk_attributes);
417
Maxim Levitskya8638622010-02-22 20:39:29 +0200418 /* Stop the thread */
419 kthread_stop(old->thread);
420
Maxim Levitsky048d8712010-02-22 20:39:30 +0200421 /* Kill current requests */
422 spin_lock_irqsave(&old->queue_lock, flags);
423 old->rq->queuedata = NULL;
424 blk_start_queue(old->rq);
425 spin_unlock_irqrestore(&old->queue_lock, flags);
Maxim Levitskya8638622010-02-22 20:39:29 +0200426 blk_cleanup_queue(old->rq);
Maxim Levitsky048d8712010-02-22 20:39:30 +0200427
428 /* Ask trans driver for release to the mtd device */
429 mutex_lock(&old->lock);
430 if (old->open && old->tr->release) {
431 old->tr->release(old);
432 old->open = 0;
433 }
434
435 __put_mtd_device(old->mtd);
436 module_put(old->tr->owner);
437
438 /* At that point, we don't touch the mtd anymore */
439 old->mtd = NULL;
440
441 mutex_unlock(&old->lock);
442 blktrans_dev_put(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return 0;
444}
445
446static void blktrans_notify_remove(struct mtd_info *mtd)
447{
Chris Malley71a928c2008-05-19 20:11:50 +0100448 struct mtd_blktrans_ops *tr;
449 struct mtd_blktrans_dev *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Chris Malley71a928c2008-05-19 20:11:50 +0100451 list_for_each_entry(tr, &blktrans_majors, list)
452 list_for_each_entry_safe(dev, next, &tr->devs, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (dev->mtd == mtd)
454 tr->remove_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457static void blktrans_notify_add(struct mtd_info *mtd)
458{
Chris Malley71a928c2008-05-19 20:11:50 +0100459 struct mtd_blktrans_ops *tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 if (mtd->type == MTD_ABSENT)
462 return;
463
Chris Malley71a928c2008-05-19 20:11:50 +0100464 list_for_each_entry(tr, &blktrans_majors, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 tr->add_mtd(tr, mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
468static struct mtd_notifier blktrans_notifier = {
469 .add = blktrans_notify_add,
470 .remove = blktrans_notify_remove,
471};
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
474{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000475 struct mtd_info *mtd;
476 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000478 /* Register the notifier if/when the first device type is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 registered, to prevent the link/init ordering from fucking
480 us over. */
481 if (!blktrans_notifier.list.next)
482 register_mtd_user(&blktrans_notifier);
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Ingo Molnar48b19262006-03-31 02:29:41 -0800485 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 ret = register_blkdev(tr->major, tr->name);
488 if (ret) {
489 printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n",
490 tr->name, tr->major, ret);
Ingo Molnar48b19262006-03-31 02:29:41 -0800491 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return ret;
493 }
David Woodhouseeae9acd2008-08-05 18:08:25 +0100494
Richard Purdie19187672006-10-27 09:09:33 +0100495 tr->blkshift = ffs(tr->blksize) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 INIT_LIST_HEAD(&tr->devs);
498 list_add(&tr->list, &blktrans_majors);
499
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000500 mtd_for_each_device(mtd)
501 if (mtd->type != MTD_ABSENT)
502 tr->add_mtd(tr, mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Ingo Molnar48b19262006-03-31 02:29:41 -0800504 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return 0;
506}
507
508int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr)
509{
Chris Malley71a928c2008-05-19 20:11:50 +0100510 struct mtd_blktrans_dev *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Ingo Molnar48b19262006-03-31 02:29:41 -0800512 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /* Remove it from the list of active majors */
515 list_del(&tr->list);
516
Chris Malley71a928c2008-05-19 20:11:50 +0100517 list_for_each_entry_safe(dev, next, &tr->devs, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 tr->remove_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 unregister_blkdev(tr->major, tr->name);
Ingo Molnar48b19262006-03-31 02:29:41 -0800521 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Eric Sesterhenn373ebfb2006-03-26 18:15:12 +0200523 BUG_ON(!list_empty(&tr->devs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return 0;
525}
526
527static void __exit mtd_blktrans_exit(void)
528{
529 /* No race here -- if someone's currently in register_mtd_blktrans
530 we're screwed anyway. */
531 if (blktrans_notifier.list.next)
532 unregister_mtd_user(&blktrans_notifier);
533}
534
535module_exit(mtd_blktrans_exit);
536
537EXPORT_SYMBOL_GPL(register_mtd_blktrans);
538EXPORT_SYMBOL_GPL(deregister_mtd_blktrans);
539EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev);
540EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev);
541
542MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
543MODULE_LICENSE("GPL");
544MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'");