blob: e0b5f644217145aa7428fc9499cf97fa2aec4d0d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
David Woodhousea1452a32010-08-08 20:58:20 +01002 * Interface to Linux block layer for MTD 'translation layers'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
David Woodhousea1452a32010-08-08 20:58:20 +01004 * Copyright © 2003-2010 David Woodhouse <dwmw2@infradead.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 *
20 */
21
22#include <linux/kernel.h>
23#include <linux/slab.h>
24#include <linux/module.h>
25#include <linux/list.h>
26#include <linux/fs.h>
27#include <linux/mtd/blktrans.h>
28#include <linux/mtd/mtd.h>
29#include <linux/blkdev.h>
30#include <linux/blkpg.h>
31#include <linux/spinlock.h>
32#include <linux/hdreg.h>
33#include <linux/init.h>
Ingo Molnar48b19262006-03-31 02:29:41 -080034#include <linux/mutex.h>
Eric W. Biederman99f9b242007-04-19 01:58:33 -060035#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Ben Dooks356d70f2007-05-28 20:28:34 +010038#include "mtdcore.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Ben Dooks356d70f2007-05-28 20:28:34 +010040static LIST_HEAD(blktrans_majors);
Maxim Levitsky048d8712010-02-22 20:39:30 +020041static DEFINE_MUTEX(blktrans_ref_mutex);
42
H Hartley Sweeten7f53f122011-01-11 18:46:10 -060043static void blktrans_dev_release(struct kref *kref)
Maxim Levitsky048d8712010-02-22 20:39:30 +020044{
45 struct mtd_blktrans_dev *dev =
46 container_of(kref, struct mtd_blktrans_dev, ref);
47
48 dev->disk->private_data = NULL;
Maxim Levitskye4d64ca2010-02-27 02:31:51 +020049 blk_cleanup_queue(dev->rq);
Maxim Levitsky048d8712010-02-22 20:39:30 +020050 put_disk(dev->disk);
51 list_del(&dev->list);
52 kfree(dev);
53}
54
55static struct mtd_blktrans_dev *blktrans_dev_get(struct gendisk *disk)
56{
57 struct mtd_blktrans_dev *dev;
58
59 mutex_lock(&blktrans_ref_mutex);
60 dev = disk->private_data;
61
62 if (!dev)
63 goto unlock;
64 kref_get(&dev->ref);
65unlock:
66 mutex_unlock(&blktrans_ref_mutex);
67 return dev;
68}
69
H Hartley Sweeten7f53f122011-01-11 18:46:10 -060070static void blktrans_dev_put(struct mtd_blktrans_dev *dev)
Maxim Levitsky048d8712010-02-22 20:39:30 +020071{
72 mutex_lock(&blktrans_ref_mutex);
73 kref_put(&dev->ref, blktrans_dev_release);
74 mutex_unlock(&blktrans_ref_mutex);
75}
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78static int do_blktrans_request(struct mtd_blktrans_ops *tr,
79 struct mtd_blktrans_dev *dev,
80 struct request *req)
81{
82 unsigned long block, nsect;
83 char *buf;
84
Tejun Heo83096eb2009-05-07 22:24:39 +090085 block = blk_rq_pos(req) << 9 >> tr->blkshift;
Tejun Heo1011c1b2009-05-07 22:24:45 +090086 nsect = blk_rq_cur_bytes(req) >> tr->blkshift;
Richard Purdie19187672006-10-27 09:09:33 +010087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 buf = req->buffer;
89
Christoph Hellwig33659eb2010-08-07 18:17:56 +020090 if (req->cmd_type != REQ_TYPE_FS)
Tejun Heof06d9a22009-04-23 11:05:19 +090091 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Tejun Heo83096eb2009-05-07 22:24:39 +090093 if (blk_rq_pos(req) + blk_rq_cur_sectors(req) >
94 get_capacity(req->rq_disk))
Tejun Heof06d9a22009-04-23 11:05:19 +090095 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Christoph Hellwig33659eb2010-08-07 18:17:56 +020097 if (req->cmd_flags & REQ_DISCARD)
Christoph Hellwig1122a262009-09-30 13:52:12 +020098 return tr->discard(dev, block, nsect);
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 switch(rq_data_dir(req)) {
101 case READ:
Richard Purdie19187672006-10-27 09:09:33 +0100102 for (; nsect > 0; nsect--, block++, buf += tr->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (tr->readsect(dev, block, buf))
Tejun Heof06d9a22009-04-23 11:05:19 +0900104 return -EIO;
Ilya Loginov2d4dc892009-11-26 09:16:19 +0100105 rq_flush_dcache_pages(req);
Tejun Heof06d9a22009-04-23 11:05:19 +0900106 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 case WRITE:
108 if (!tr->writesect)
Tejun Heof06d9a22009-04-23 11:05:19 +0900109 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Ilya Loginov2d4dc892009-11-26 09:16:19 +0100111 rq_flush_dcache_pages(req);
Richard Purdie19187672006-10-27 09:09:33 +0100112 for (; nsect > 0; nsect--, block++, buf += tr->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (tr->writesect(dev, block, buf))
Tejun Heof06d9a22009-04-23 11:05:19 +0900114 return -EIO;
115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 default:
Jeff Garzik9a292302006-10-01 12:16:00 -0400117 printk(KERN_NOTICE "Unknown request %u\n", rq_data_dir(req));
Tejun Heof06d9a22009-04-23 11:05:19 +0900118 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120}
121
Jarkko Lavinenc7519db2011-02-14 16:16:09 +0200122int mtd_blktrans_cease_background(struct mtd_blktrans_dev *dev)
123{
124 if (kthread_should_stop())
125 return 1;
126
127 return !elv_queue_empty(dev->rq);
128}
129EXPORT_SYMBOL_GPL(mtd_blktrans_cease_background);
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static int mtd_blktrans_thread(void *arg)
132{
Maxim Levitskya8638622010-02-22 20:39:29 +0200133 struct mtd_blktrans_dev *dev = arg;
Jarkko Lavinenc7519db2011-02-14 16:16:09 +0200134 struct mtd_blktrans_ops *tr = dev->tr;
Maxim Levitskya8638622010-02-22 20:39:29 +0200135 struct request_queue *rq = dev->rq;
Tejun Heo1498ada2009-05-08 11:54:11 +0900136 struct request *req = NULL;
Jarkko Lavinenc7519db2011-02-14 16:16:09 +0200137 int background_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 spin_lock_irq(rq->queue_lock);
Tejun Heo1498ada2009-05-08 11:54:11 +0900140
Christoph Hellwig3e67fe42007-04-22 20:40:57 +0100141 while (!kthread_should_stop()) {
Tejun Heof06d9a22009-04-23 11:05:19 +0900142 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Tejun Heo9934c8c2009-05-08 11:54:16 +0900144 if (!req && !(req = blk_fetch_request(rq))) {
Jarkko Lavinenc7519db2011-02-14 16:16:09 +0200145 if (tr->background && !background_done) {
146 spin_unlock_irq(rq->queue_lock);
147 mutex_lock(&dev->lock);
148 tr->background(dev);
149 mutex_unlock(&dev->lock);
150 spin_lock_irq(rq->queue_lock);
151 /*
152 * Do background processing just once per idle
153 * period.
154 */
155 background_done = 1;
156 continue;
157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 set_current_state(TASK_INTERRUPTIBLE);
Maxim Levitsky12aebf32010-10-15 17:20:45 +0200159
160 if (kthread_should_stop())
161 set_current_state(TASK_RUNNING);
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 spin_unlock_irq(rq->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 spin_lock_irq(rq->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 continue;
167 }
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 spin_unlock_irq(rq->queue_lock);
170
Ingo Molnar48b19262006-03-31 02:29:41 -0800171 mutex_lock(&dev->lock);
Maxim Levitskya8638622010-02-22 20:39:29 +0200172 res = do_blktrans_request(dev->tr, dev, req);
Ingo Molnar48b19262006-03-31 02:29:41 -0800173 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 spin_lock_irq(rq->queue_lock);
176
Tejun Heo1498ada2009-05-08 11:54:11 +0900177 if (!__blk_end_request_cur(req, res))
178 req = NULL;
Jarkko Lavinenc7519db2011-02-14 16:16:09 +0200179
180 background_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
Tejun Heo1498ada2009-05-08 11:54:11 +0900182
183 if (req)
184 __blk_end_request_all(req, -EIO);
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 spin_unlock_irq(rq->queue_lock);
187
Christoph Hellwig3e67fe42007-04-22 20:40:57 +0100188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191static void mtd_blktrans_request(struct request_queue *rq)
192{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200193 struct mtd_blktrans_dev *dev;
194 struct request *req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Maxim Levitsky048d8712010-02-22 20:39:30 +0200196 dev = rq->queuedata;
197
198 if (!dev)
199 while ((req = blk_fetch_request(rq)) != NULL)
200 __blk_end_request_all(req, -ENODEV);
201 else
202 wake_up_process(dev->thread);
203}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Al Viroaf0e2a02008-03-02 10:35:06 -0500205static int blktrans_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200207 struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
Maxim Levitsky008c7512010-10-15 17:20:43 +0200208 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Maxim Levitsky048d8712010-02-22 20:39:30 +0200210 if (!dev)
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200211 return -ERESTARTSYS; /* FIXME: busy loop! -arnd*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Maxim Levitsky048d8712010-02-22 20:39:30 +0200213 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Maxim Levitsky008c7512010-10-15 17:20:43 +0200215 if (dev->open++)
Maxim Levitsky048d8712010-02-22 20:39:30 +0200216 goto unlock;
Maxim Levitsky008c7512010-10-15 17:20:43 +0200217
218 kref_get(&dev->ref);
219 __module_get(dev->tr->owner);
220
221 if (dev->mtd) {
222 ret = dev->tr->open ? dev->tr->open(dev) : 0;
223 __get_mtd_device(dev->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Maxim Levitsky048d8712010-02-22 20:39:30 +0200225
Maxim Levitsky048d8712010-02-22 20:39:30 +0200226unlock:
227 mutex_unlock(&dev->lock);
228 blktrans_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return ret;
230}
231
Al Viroaf0e2a02008-03-02 10:35:06 -0500232static int blktrans_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200234 struct mtd_blktrans_dev *dev = blktrans_dev_get(disk);
Maxim Levitsky008c7512010-10-15 17:20:43 +0200235 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Maxim Levitsky048d8712010-02-22 20:39:30 +0200237 if (!dev)
238 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Maxim Levitsky048d8712010-02-22 20:39:30 +0200240 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Maxim Levitsky008c7512010-10-15 17:20:43 +0200242 if (--dev->open)
Maxim Levitsky048d8712010-02-22 20:39:30 +0200243 goto unlock;
244
Maxim Levitsky008c7512010-10-15 17:20:43 +0200245 kref_put(&dev->ref, blktrans_dev_release);
246 module_put(dev->tr->owner);
247
248 if (dev->mtd) {
249 ret = dev->tr->release ? dev->tr->release(dev) : 0;
250 __put_mtd_device(dev->mtd);
251 }
Maxim Levitsky048d8712010-02-22 20:39:30 +0200252unlock:
253 mutex_unlock(&dev->lock);
254 blktrans_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return ret;
256}
257
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800258static int blktrans_getgeo(struct block_device *bdev, struct hd_geometry *geo)
259{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200260 struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
261 int ret = -ENXIO;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800262
Maxim Levitsky048d8712010-02-22 20:39:30 +0200263 if (!dev)
264 return ret;
265
266 mutex_lock(&dev->lock);
267
268 if (!dev->mtd)
269 goto unlock;
270
271 ret = dev->tr->getgeo ? dev->tr->getgeo(dev, geo) : 0;
272unlock:
273 mutex_unlock(&dev->lock);
274 blktrans_dev_put(dev);
275 return ret;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800276}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Al Viroaf0e2a02008-03-02 10:35:06 -0500278static int blktrans_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 unsigned int cmd, unsigned long arg)
280{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200281 struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
282 int ret = -ENXIO;
283
284 if (!dev)
285 return ret;
286
287 mutex_lock(&dev->lock);
288
289 if (!dev->mtd)
290 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 switch (cmd) {
293 case BLKFLSBUF:
Maxim Levitsky048d8712010-02-22 20:39:30 +0200294 ret = dev->tr->flush ? dev->tr->flush(dev) : 0;
Dan Carpenter007c2d82010-05-31 16:03:38 +0200295 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 default:
Maxim Levitsky048d8712010-02-22 20:39:30 +0200297 ret = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
Maxim Levitsky048d8712010-02-22 20:39:30 +0200299unlock:
300 mutex_unlock(&dev->lock);
301 blktrans_dev_put(dev);
302 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700305static const struct block_device_operations mtd_blktrans_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 .owner = THIS_MODULE,
Al Viroaf0e2a02008-03-02 10:35:06 -0500307 .open = blktrans_open,
308 .release = blktrans_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200309 .ioctl = blktrans_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800310 .getgeo = blktrans_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311};
312
313int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
314{
315 struct mtd_blktrans_ops *tr = new->tr;
Chris Malley71a928c2008-05-19 20:11:50 +0100316 struct mtd_blktrans_dev *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 int last_devnum = -1;
318 struct gendisk *gd;
Maxim Levitskya8638622010-02-22 20:39:29 +0200319 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Jean Delvareb3561ea2007-05-08 00:30:46 -0700321 if (mutex_trylock(&mtd_table_mutex)) {
Ingo Molnar48b19262006-03-31 02:29:41 -0800322 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 BUG();
324 }
325
Maxim Levitsky048d8712010-02-22 20:39:30 +0200326 mutex_lock(&blktrans_ref_mutex);
Chris Malley71a928c2008-05-19 20:11:50 +0100327 list_for_each_entry(d, &tr->devs, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (new->devnum == -1) {
329 /* Use first free number */
330 if (d->devnum != last_devnum+1) {
331 /* Found a free devnum. Plug it in here */
332 new->devnum = last_devnum+1;
333 list_add_tail(&new->list, &d->list);
334 goto added;
335 }
336 } else if (d->devnum == new->devnum) {
337 /* Required number taken */
Maxim Levitsky048d8712010-02-22 20:39:30 +0200338 mutex_unlock(&blktrans_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return -EBUSY;
340 } else if (d->devnum > new->devnum) {
341 /* Required number was free */
342 list_add_tail(&new->list, &d->list);
343 goto added;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 last_devnum = d->devnum;
346 }
Maxim Levitskya8638622010-02-22 20:39:29 +0200347
348 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (new->devnum == -1)
350 new->devnum = last_devnum+1;
351
Ben Hutchings4d3a8532010-01-29 20:59:53 +0000352 /* Check that the device and any partitions will get valid
353 * minor numbers and that the disk naming code below can cope
354 * with this number. */
355 if (new->devnum > (MINORMASK >> tr->part_bits) ||
Maxim Levitsky048d8712010-02-22 20:39:30 +0200356 (tr->part_bits && new->devnum >= 27 * 26)) {
357 mutex_unlock(&blktrans_ref_mutex);
Maxim Levitskya8638622010-02-22 20:39:29 +0200358 goto error1;
Maxim Levitsky048d8712010-02-22 20:39:30 +0200359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 list_add_tail(&new->list, &tr->devs);
362 added:
Maxim Levitsky048d8712010-02-22 20:39:30 +0200363 mutex_unlock(&blktrans_ref_mutex);
364
David Woodhousece37ab42007-12-03 12:46:12 +0000365 mutex_init(&new->lock);
Maxim Levitsky048d8712010-02-22 20:39:30 +0200366 kref_init(&new->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (!tr->writesect)
368 new->readonly = 1;
369
Maxim Levitskya8638622010-02-22 20:39:29 +0200370 /* Create gendisk */
371 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 gd = alloc_disk(1 << tr->part_bits);
Maxim Levitskya8638622010-02-22 20:39:29 +0200373
374 if (!gd)
375 goto error2;
376
377 new->disk = gd;
378 gd->private_data = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 gd->major = tr->major;
380 gd->first_minor = (new->devnum) << tr->part_bits;
381 gd->fops = &mtd_blktrans_ops;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000382
Todd Poynor65a8de32005-07-29 20:42:07 +0100383 if (tr->part_bits)
384 if (new->devnum < 26)
385 snprintf(gd->disk_name, sizeof(gd->disk_name),
386 "%s%c", tr->name, 'a' + new->devnum);
387 else
388 snprintf(gd->disk_name, sizeof(gd->disk_name),
389 "%s%c%c", tr->name,
390 'a' - 1 + new->devnum / 26,
391 'a' + new->devnum % 26);
392 else
393 snprintf(gd->disk_name, sizeof(gd->disk_name),
394 "%s%d", tr->name, new->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Richard Purdie19187672006-10-27 09:09:33 +0100396 set_capacity(gd, (new->size * tr->blksize) >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Maxim Levitskya8638622010-02-22 20:39:29 +0200398 /* Create the request queue */
399 spin_lock_init(&new->queue_lock);
400 new->rq = blk_init_queue(mtd_blktrans_request, &new->queue_lock);
401
402 if (!new->rq)
403 goto error3;
404
405 new->rq->queuedata = new;
406 blk_queue_logical_block_size(new->rq, tr->blksize);
407
408 if (tr->discard)
409 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
410 new->rq);
411
412 gd->queue = new->rq;
413
414 /* Create processing thread */
415 /* TODO: workqueue ? */
416 new->thread = kthread_run(mtd_blktrans_thread, new,
417 "%s%d", tr->name, new->mtd->index);
418 if (IS_ERR(new->thread)) {
419 ret = PTR_ERR(new->thread);
420 goto error4;
421 }
David Woodhoused6948462009-04-05 07:38:33 -0700422 gd->driverfs_dev = &new->mtd->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 if (new->readonly)
425 set_disk_ro(gd, 1);
426
427 add_disk(gd);
Maxim Levitsky026ec572010-02-22 20:39:33 +0200428
Maxim Levitsky133fa8c72010-02-26 22:08:40 +0200429 if (new->disk_attributes) {
430 ret = sysfs_create_group(&disk_to_dev(gd)->kobj,
Maxim Levitsky026ec572010-02-22 20:39:33 +0200431 new->disk_attributes);
Maxim Levitsky133fa8c72010-02-26 22:08:40 +0200432 WARN_ON(ret);
433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return 0;
Maxim Levitskya8638622010-02-22 20:39:29 +0200435error4:
436 blk_cleanup_queue(new->rq);
437error3:
438 put_disk(new->disk);
439error2:
440 list_del(&new->list);
441error1:
Maxim Levitskya8638622010-02-22 20:39:29 +0200442 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
446{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200447 unsigned long flags;
448
Jean Delvareb3561ea2007-05-08 00:30:46 -0700449 if (mutex_trylock(&mtd_table_mutex)) {
Ingo Molnar48b19262006-03-31 02:29:41 -0800450 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 BUG();
452 }
453
Maxim Levitsky026ec572010-02-22 20:39:33 +0200454 if (old->disk_attributes)
455 sysfs_remove_group(&disk_to_dev(old->disk)->kobj,
456 old->disk_attributes);
457
Maxim Levitskydba76c02010-07-28 18:53:16 +0300458 /* Stop new requests to arrive */
459 del_gendisk(old->disk);
460
461
Maxim Levitskya8638622010-02-22 20:39:29 +0200462 /* Stop the thread */
463 kthread_stop(old->thread);
464
Maxim Levitsky048d8712010-02-22 20:39:30 +0200465 /* Kill current requests */
466 spin_lock_irqsave(&old->queue_lock, flags);
467 old->rq->queuedata = NULL;
468 blk_start_queue(old->rq);
469 spin_unlock_irqrestore(&old->queue_lock, flags);
Maxim Levitsky048d8712010-02-22 20:39:30 +0200470
Maxim Levitsky008c7512010-10-15 17:20:43 +0200471 /* If the device is currently open, tell trans driver to close it,
472 then put mtd device, and don't touch it again */
Maxim Levitsky048d8712010-02-22 20:39:30 +0200473 mutex_lock(&old->lock);
Maxim Levitsky008c7512010-10-15 17:20:43 +0200474 if (old->open) {
475 if (old->tr->release)
476 old->tr->release(old);
477 __put_mtd_device(old->mtd);
Maxim Levitsky048d8712010-02-22 20:39:30 +0200478 }
479
Maxim Levitsky048d8712010-02-22 20:39:30 +0200480 old->mtd = NULL;
481
482 mutex_unlock(&old->lock);
483 blktrans_dev_put(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return 0;
485}
486
487static void blktrans_notify_remove(struct mtd_info *mtd)
488{
Chris Malley71a928c2008-05-19 20:11:50 +0100489 struct mtd_blktrans_ops *tr;
490 struct mtd_blktrans_dev *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Chris Malley71a928c2008-05-19 20:11:50 +0100492 list_for_each_entry(tr, &blktrans_majors, list)
493 list_for_each_entry_safe(dev, next, &tr->devs, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (dev->mtd == mtd)
495 tr->remove_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498static void blktrans_notify_add(struct mtd_info *mtd)
499{
Chris Malley71a928c2008-05-19 20:11:50 +0100500 struct mtd_blktrans_ops *tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 if (mtd->type == MTD_ABSENT)
503 return;
504
Chris Malley71a928c2008-05-19 20:11:50 +0100505 list_for_each_entry(tr, &blktrans_majors, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 tr->add_mtd(tr, mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
509static struct mtd_notifier blktrans_notifier = {
510 .add = blktrans_notify_add,
511 .remove = blktrans_notify_remove,
512};
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
515{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000516 struct mtd_info *mtd;
517 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000519 /* Register the notifier if/when the first device type is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 registered, to prevent the link/init ordering from fucking
521 us over. */
522 if (!blktrans_notifier.list.next)
523 register_mtd_user(&blktrans_notifier);
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Ingo Molnar48b19262006-03-31 02:29:41 -0800526 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 ret = register_blkdev(tr->major, tr->name);
Frank Li6fe4c592010-10-26 11:02:19 +0800529 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n",
531 tr->name, tr->major, ret);
Ingo Molnar48b19262006-03-31 02:29:41 -0800532 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return ret;
534 }
David Woodhouseeae9acd2008-08-05 18:08:25 +0100535
Frank Li6fe4c592010-10-26 11:02:19 +0800536 if (ret)
537 tr->major = ret;
538
Richard Purdie19187672006-10-27 09:09:33 +0100539 tr->blkshift = ffs(tr->blksize) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 INIT_LIST_HEAD(&tr->devs);
542 list_add(&tr->list, &blktrans_majors);
543
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000544 mtd_for_each_device(mtd)
545 if (mtd->type != MTD_ABSENT)
546 tr->add_mtd(tr, mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Ingo Molnar48b19262006-03-31 02:29:41 -0800548 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return 0;
550}
551
552int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr)
553{
Chris Malley71a928c2008-05-19 20:11:50 +0100554 struct mtd_blktrans_dev *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Ingo Molnar48b19262006-03-31 02:29:41 -0800556 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /* Remove it from the list of active majors */
559 list_del(&tr->list);
560
Chris Malley71a928c2008-05-19 20:11:50 +0100561 list_for_each_entry_safe(dev, next, &tr->devs, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 tr->remove_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 unregister_blkdev(tr->major, tr->name);
Ingo Molnar48b19262006-03-31 02:29:41 -0800565 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Eric Sesterhenn373ebfb2006-03-26 18:15:12 +0200567 BUG_ON(!list_empty(&tr->devs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return 0;
569}
570
571static void __exit mtd_blktrans_exit(void)
572{
573 /* No race here -- if someone's currently in register_mtd_blktrans
574 we're screwed anyway. */
575 if (blktrans_notifier.list.next)
576 unregister_mtd_user(&blktrans_notifier);
577}
578
579module_exit(mtd_blktrans_exit);
580
581EXPORT_SYMBOL_GPL(register_mtd_blktrans);
582EXPORT_SYMBOL_GPL(deregister_mtd_blktrans);
583EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev);
584EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev);
585
586MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
587MODULE_LICENSE("GPL");
588MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'");