blob: 03e19c1965cc031885e8a288ea092b10f3fea353 [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;
Maxim Levitskye4d64ca2010-02-27 02:31:51 +020035 blk_cleanup_queue(dev->rq);
Maxim Levitsky048d8712010-02-22 20:39:30 +020036 put_disk(dev->disk);
37 list_del(&dev->list);
38 kfree(dev);
39}
40
41static struct mtd_blktrans_dev *blktrans_dev_get(struct gendisk *disk)
42{
43 struct mtd_blktrans_dev *dev;
44
45 mutex_lock(&blktrans_ref_mutex);
46 dev = disk->private_data;
47
48 if (!dev)
49 goto unlock;
50 kref_get(&dev->ref);
51unlock:
52 mutex_unlock(&blktrans_ref_mutex);
53 return dev;
54}
55
56void blktrans_dev_put(struct mtd_blktrans_dev *dev)
57{
58 mutex_lock(&blktrans_ref_mutex);
59 kref_put(&dev->ref, blktrans_dev_release);
60 mutex_unlock(&blktrans_ref_mutex);
61}
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64static int do_blktrans_request(struct mtd_blktrans_ops *tr,
65 struct mtd_blktrans_dev *dev,
66 struct request *req)
67{
68 unsigned long block, nsect;
69 char *buf;
70
Tejun Heo83096eb2009-05-07 22:24:39 +090071 block = blk_rq_pos(req) << 9 >> tr->blkshift;
Tejun Heo1011c1b2009-05-07 22:24:45 +090072 nsect = blk_rq_cur_bytes(req) >> tr->blkshift;
Richard Purdie19187672006-10-27 09:09:33 +010073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 buf = req->buffer;
75
Jens Axboe4aff5e22006-08-10 08:44:47 +020076 if (!blk_fs_request(req))
Tejun Heof06d9a22009-04-23 11:05:19 +090077 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Tejun Heo83096eb2009-05-07 22:24:39 +090079 if (blk_rq_pos(req) + blk_rq_cur_sectors(req) >
80 get_capacity(req->rq_disk))
Tejun Heof06d9a22009-04-23 11:05:19 +090081 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Christoph Hellwig1122a262009-09-30 13:52:12 +020083 if (blk_discard_rq(req))
84 return tr->discard(dev, block, nsect);
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 switch(rq_data_dir(req)) {
87 case READ:
Richard Purdie19187672006-10-27 09:09:33 +010088 for (; nsect > 0; nsect--, block++, buf += tr->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 if (tr->readsect(dev, block, buf))
Tejun Heof06d9a22009-04-23 11:05:19 +090090 return -EIO;
Ilya Loginov2d4dc892009-11-26 09:16:19 +010091 rq_flush_dcache_pages(req);
Tejun Heof06d9a22009-04-23 11:05:19 +090092 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 case WRITE:
94 if (!tr->writesect)
Tejun Heof06d9a22009-04-23 11:05:19 +090095 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Ilya Loginov2d4dc892009-11-26 09:16:19 +010097 rq_flush_dcache_pages(req);
Richard Purdie19187672006-10-27 09:09:33 +010098 for (; nsect > 0; nsect--, block++, buf += tr->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (tr->writesect(dev, block, buf))
Tejun Heof06d9a22009-04-23 11:05:19 +0900100 return -EIO;
101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 default:
Jeff Garzik9a292302006-10-01 12:16:00 -0400103 printk(KERN_NOTICE "Unknown request %u\n", rq_data_dir(req));
Tejun Heof06d9a22009-04-23 11:05:19 +0900104 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106}
107
108static int mtd_blktrans_thread(void *arg)
109{
Maxim Levitskya8638622010-02-22 20:39:29 +0200110 struct mtd_blktrans_dev *dev = arg;
111 struct request_queue *rq = dev->rq;
Tejun Heo1498ada2009-05-08 11:54:11 +0900112 struct request *req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 spin_lock_irq(rq->queue_lock);
Tejun Heo1498ada2009-05-08 11:54:11 +0900115
Christoph Hellwig3e67fe42007-04-22 20:40:57 +0100116 while (!kthread_should_stop()) {
Tejun Heof06d9a22009-04-23 11:05:19 +0900117 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Tejun Heo9934c8c2009-05-08 11:54:16 +0900119 if (!req && !(req = blk_fetch_request(rq))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 spin_unlock_irq(rq->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 spin_lock_irq(rq->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 continue;
125 }
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 spin_unlock_irq(rq->queue_lock);
128
Ingo Molnar48b19262006-03-31 02:29:41 -0800129 mutex_lock(&dev->lock);
Maxim Levitskya8638622010-02-22 20:39:29 +0200130 res = do_blktrans_request(dev->tr, dev, req);
Ingo Molnar48b19262006-03-31 02:29:41 -0800131 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 spin_lock_irq(rq->queue_lock);
134
Tejun Heo1498ada2009-05-08 11:54:11 +0900135 if (!__blk_end_request_cur(req, res))
136 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
Tejun Heo1498ada2009-05-08 11:54:11 +0900138
139 if (req)
140 __blk_end_request_all(req, -EIO);
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 spin_unlock_irq(rq->queue_lock);
143
Christoph Hellwig3e67fe42007-04-22 20:40:57 +0100144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147static void mtd_blktrans_request(struct request_queue *rq)
148{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200149 struct mtd_blktrans_dev *dev;
150 struct request *req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Maxim Levitsky048d8712010-02-22 20:39:30 +0200152 dev = rq->queuedata;
153
154 if (!dev)
155 while ((req = blk_fetch_request(rq)) != NULL)
156 __blk_end_request_all(req, -ENODEV);
157 else
158 wake_up_process(dev->thread);
159}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Al Viroaf0e2a02008-03-02 10:35:06 -0500161static int blktrans_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200163 struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
164 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Maxim Levitsky048d8712010-02-22 20:39:30 +0200166 if (!dev)
167 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Maxim Levitsky048d8712010-02-22 20:39:30 +0200169 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Maxim Levitsky048d8712010-02-22 20:39:30 +0200171 if (!dev->mtd) {
172 ret = -ENXIO;
173 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
Maxim Levitsky048d8712010-02-22 20:39:30 +0200175
176 ret = !dev->open++ && dev->tr->open ? dev->tr->open(dev) : 0;
177
178 /* Take another reference on the device so it won't go away till
179 last release */
180 if (!ret)
181 kref_get(&dev->ref);
182unlock:
183 mutex_unlock(&dev->lock);
184 blktrans_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return ret;
186}
187
Al Viroaf0e2a02008-03-02 10:35:06 -0500188static int blktrans_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200190 struct mtd_blktrans_dev *dev = blktrans_dev_get(disk);
191 int ret = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Maxim Levitsky048d8712010-02-22 20:39:30 +0200193 if (!dev)
194 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Maxim Levitsky048d8712010-02-22 20:39:30 +0200196 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Maxim Levitsky048d8712010-02-22 20:39:30 +0200198 /* Release one reference, we sure its not the last one here*/
199 kref_put(&dev->ref, blktrans_dev_release);
200
201 if (!dev->mtd)
202 goto unlock;
203
204 ret = !--dev->open && dev->tr->release ? dev->tr->release(dev) : 0;
205unlock:
206 mutex_unlock(&dev->lock);
207 blktrans_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return ret;
209}
210
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800211static int blktrans_getgeo(struct block_device *bdev, struct hd_geometry *geo)
212{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200213 struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
214 int ret = -ENXIO;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800215
Maxim Levitsky048d8712010-02-22 20:39:30 +0200216 if (!dev)
217 return ret;
218
219 mutex_lock(&dev->lock);
220
221 if (!dev->mtd)
222 goto unlock;
223
224 ret = dev->tr->getgeo ? dev->tr->getgeo(dev, geo) : 0;
225unlock:
226 mutex_unlock(&dev->lock);
227 blktrans_dev_put(dev);
228 return ret;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800229}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Al Viroaf0e2a02008-03-02 10:35:06 -0500231static int blktrans_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 unsigned int cmd, unsigned long arg)
233{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200234 struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
235 int ret = -ENXIO;
236
237 if (!dev)
238 return ret;
239
240 mutex_lock(&dev->lock);
241
242 if (!dev->mtd)
243 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 switch (cmd) {
246 case BLKFLSBUF:
Maxim Levitsky048d8712010-02-22 20:39:30 +0200247 ret = dev->tr->flush ? dev->tr->flush(dev) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 default:
Maxim Levitsky048d8712010-02-22 20:39:30 +0200249 ret = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
Maxim Levitsky048d8712010-02-22 20:39:30 +0200251unlock:
252 mutex_unlock(&dev->lock);
253 blktrans_dev_put(dev);
254 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700257static const struct block_device_operations mtd_blktrans_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 .owner = THIS_MODULE,
Al Viroaf0e2a02008-03-02 10:35:06 -0500259 .open = blktrans_open,
260 .release = blktrans_release,
261 .locked_ioctl = blktrans_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800262 .getgeo = blktrans_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263};
264
265int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
266{
267 struct mtd_blktrans_ops *tr = new->tr;
Chris Malley71a928c2008-05-19 20:11:50 +0100268 struct mtd_blktrans_dev *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 int last_devnum = -1;
270 struct gendisk *gd;
Maxim Levitskya8638622010-02-22 20:39:29 +0200271 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Jean Delvareb3561ea2007-05-08 00:30:46 -0700273 if (mutex_trylock(&mtd_table_mutex)) {
Ingo Molnar48b19262006-03-31 02:29:41 -0800274 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 BUG();
276 }
277
Maxim Levitsky048d8712010-02-22 20:39:30 +0200278 mutex_lock(&blktrans_ref_mutex);
Chris Malley71a928c2008-05-19 20:11:50 +0100279 list_for_each_entry(d, &tr->devs, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (new->devnum == -1) {
281 /* Use first free number */
282 if (d->devnum != last_devnum+1) {
283 /* Found a free devnum. Plug it in here */
284 new->devnum = last_devnum+1;
285 list_add_tail(&new->list, &d->list);
286 goto added;
287 }
288 } else if (d->devnum == new->devnum) {
289 /* Required number taken */
Maxim Levitsky048d8712010-02-22 20:39:30 +0200290 mutex_unlock(&blktrans_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return -EBUSY;
292 } else if (d->devnum > new->devnum) {
293 /* Required number was free */
294 list_add_tail(&new->list, &d->list);
295 goto added;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 last_devnum = d->devnum;
298 }
Maxim Levitskya8638622010-02-22 20:39:29 +0200299
300 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (new->devnum == -1)
302 new->devnum = last_devnum+1;
303
Ben Hutchings4d3a8532010-01-29 20:59:53 +0000304 /* Check that the device and any partitions will get valid
305 * minor numbers and that the disk naming code below can cope
306 * with this number. */
307 if (new->devnum > (MINORMASK >> tr->part_bits) ||
Maxim Levitsky048d8712010-02-22 20:39:30 +0200308 (tr->part_bits && new->devnum >= 27 * 26)) {
309 mutex_unlock(&blktrans_ref_mutex);
Maxim Levitskya8638622010-02-22 20:39:29 +0200310 goto error1;
Maxim Levitsky048d8712010-02-22 20:39:30 +0200311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 list_add_tail(&new->list, &tr->devs);
314 added:
Maxim Levitsky048d8712010-02-22 20:39:30 +0200315 mutex_unlock(&blktrans_ref_mutex);
316
David Woodhousece37ab42007-12-03 12:46:12 +0000317 mutex_init(&new->lock);
Maxim Levitsky048d8712010-02-22 20:39:30 +0200318 kref_init(&new->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (!tr->writesect)
320 new->readonly = 1;
321
Maxim Levitskya8638622010-02-22 20:39:29 +0200322 /* Create gendisk */
323 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 gd = alloc_disk(1 << tr->part_bits);
Maxim Levitskya8638622010-02-22 20:39:29 +0200325
326 if (!gd)
327 goto error2;
328
329 new->disk = gd;
330 gd->private_data = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 gd->major = tr->major;
332 gd->first_minor = (new->devnum) << tr->part_bits;
333 gd->fops = &mtd_blktrans_ops;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000334
Todd Poynor65a8de32005-07-29 20:42:07 +0100335 if (tr->part_bits)
336 if (new->devnum < 26)
337 snprintf(gd->disk_name, sizeof(gd->disk_name),
338 "%s%c", tr->name, 'a' + new->devnum);
339 else
340 snprintf(gd->disk_name, sizeof(gd->disk_name),
341 "%s%c%c", tr->name,
342 'a' - 1 + new->devnum / 26,
343 'a' + new->devnum % 26);
344 else
345 snprintf(gd->disk_name, sizeof(gd->disk_name),
346 "%s%d", tr->name, new->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Richard Purdie19187672006-10-27 09:09:33 +0100348 set_capacity(gd, (new->size * tr->blksize) >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Maxim Levitskya8638622010-02-22 20:39:29 +0200350 /* Create the request queue */
351 spin_lock_init(&new->queue_lock);
352 new->rq = blk_init_queue(mtd_blktrans_request, &new->queue_lock);
353
354 if (!new->rq)
355 goto error3;
356
357 new->rq->queuedata = new;
358 blk_queue_logical_block_size(new->rq, tr->blksize);
359
360 if (tr->discard)
361 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
362 new->rq);
363
364 gd->queue = new->rq;
365
Maxim Levitsky048d8712010-02-22 20:39:30 +0200366 __get_mtd_device(new->mtd);
367 __module_get(tr->owner);
368
Maxim Levitskya8638622010-02-22 20:39:29 +0200369 /* Create processing thread */
370 /* TODO: workqueue ? */
371 new->thread = kthread_run(mtd_blktrans_thread, new,
372 "%s%d", tr->name, new->mtd->index);
373 if (IS_ERR(new->thread)) {
374 ret = PTR_ERR(new->thread);
375 goto error4;
376 }
David Woodhoused6948462009-04-05 07:38:33 -0700377 gd->driverfs_dev = &new->mtd->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 if (new->readonly)
380 set_disk_ro(gd, 1);
381
382 add_disk(gd);
Maxim Levitsky026ec572010-02-22 20:39:33 +0200383
Maxim Levitsky133fa8c2010-02-26 22:08:40 +0200384 if (new->disk_attributes) {
385 ret = sysfs_create_group(&disk_to_dev(gd)->kobj,
Maxim Levitsky026ec572010-02-22 20:39:33 +0200386 new->disk_attributes);
Maxim Levitsky133fa8c2010-02-26 22:08:40 +0200387 WARN_ON(ret);
388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return 0;
Maxim Levitskya8638622010-02-22 20:39:29 +0200390error4:
Maxim Levitsky048d8712010-02-22 20:39:30 +0200391 module_put(tr->owner);
392 __put_mtd_device(new->mtd);
Maxim Levitskya8638622010-02-22 20:39:29 +0200393 blk_cleanup_queue(new->rq);
394error3:
395 put_disk(new->disk);
396error2:
397 list_del(&new->list);
398error1:
399 kfree(new);
400 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
403int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
404{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200405 unsigned long flags;
406
Jean Delvareb3561ea2007-05-08 00:30:46 -0700407 if (mutex_trylock(&mtd_table_mutex)) {
Ingo Molnar48b19262006-03-31 02:29:41 -0800408 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 BUG();
410 }
411
Maxim Levitsky048d8712010-02-22 20:39:30 +0200412 /* Stop new requests to arrive */
Maxim Levitskya8638622010-02-22 20:39:29 +0200413 del_gendisk(old->disk);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000414
Maxim Levitsky026ec572010-02-22 20:39:33 +0200415 if (old->disk_attributes)
416 sysfs_remove_group(&disk_to_dev(old->disk)->kobj,
417 old->disk_attributes);
418
Maxim Levitskya8638622010-02-22 20:39:29 +0200419 /* Stop the thread */
420 kthread_stop(old->thread);
421
Maxim Levitsky048d8712010-02-22 20:39:30 +0200422 /* Kill current requests */
423 spin_lock_irqsave(&old->queue_lock, flags);
424 old->rq->queuedata = NULL;
425 blk_start_queue(old->rq);
426 spin_unlock_irqrestore(&old->queue_lock, flags);
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'");