blob: 6a41dfa3c36baa4ce2c4806bbe66c49f42ce5881 [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>
Ingo Molnar48b19262006-03-31 02:29:41 -080033#include <linux/mutex.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080034#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Ben Dooks356d70f2007-05-28 20:28:34 +010036#include "mtdcore.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Ben Dooks356d70f2007-05-28 20:28:34 +010038static LIST_HEAD(blktrans_majors);
Maxim Levitsky048d8712010-02-22 20:39:30 +020039static DEFINE_MUTEX(blktrans_ref_mutex);
40
H Hartley Sweeten7f53f122011-01-11 18:46:10 -060041static void blktrans_dev_release(struct kref *kref)
Maxim Levitsky048d8712010-02-22 20:39:30 +020042{
43 struct mtd_blktrans_dev *dev =
44 container_of(kref, struct mtd_blktrans_dev, ref);
45
46 dev->disk->private_data = NULL;
Maxim Levitskye4d64ca2010-02-27 02:31:51 +020047 blk_cleanup_queue(dev->rq);
Maxim Levitsky048d8712010-02-22 20:39:30 +020048 put_disk(dev->disk);
49 list_del(&dev->list);
50 kfree(dev);
51}
52
53static struct mtd_blktrans_dev *blktrans_dev_get(struct gendisk *disk)
54{
55 struct mtd_blktrans_dev *dev;
56
57 mutex_lock(&blktrans_ref_mutex);
58 dev = disk->private_data;
59
60 if (!dev)
61 goto unlock;
62 kref_get(&dev->ref);
63unlock:
64 mutex_unlock(&blktrans_ref_mutex);
65 return dev;
66}
67
H Hartley Sweeten7f53f122011-01-11 18:46:10 -060068static void blktrans_dev_put(struct mtd_blktrans_dev *dev)
Maxim Levitsky048d8712010-02-22 20:39:30 +020069{
70 mutex_lock(&blktrans_ref_mutex);
71 kref_put(&dev->ref, blktrans_dev_release);
72 mutex_unlock(&blktrans_ref_mutex);
73}
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020076static blk_status_t do_blktrans_request(struct mtd_blktrans_ops *tr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 struct mtd_blktrans_dev *dev,
78 struct request *req)
79{
80 unsigned long block, nsect;
81 char *buf;
82
Tejun Heo83096eb2009-05-07 22:24:39 +090083 block = blk_rq_pos(req) << 9 >> tr->blkshift;
Tejun Heo1011c1b2009-05-07 22:24:45 +090084 nsect = blk_rq_cur_bytes(req) >> tr->blkshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020086 if (req_op(req) == REQ_OP_FLUSH) {
87 if (tr->flush(dev))
88 return BLK_STS_IOERR;
89 return BLK_STS_OK;
90 }
Roman Peniaev566c0d6a2014-03-08 21:59:14 +090091
Tejun Heo83096eb2009-05-07 22:24:39 +090092 if (blk_rq_pos(req) + blk_rq_cur_sectors(req) >
93 get_capacity(req->rq_disk))
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020094 return BLK_STS_IOERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Christoph Hellwigaebf5262017-01-31 16:57:31 +010096 switch (req_op(req)) {
97 case REQ_OP_DISCARD:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020098 if (tr->discard(dev, block, nsect))
99 return BLK_STS_IOERR;
100 return BLK_STS_OK;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100101 case REQ_OP_READ:
Christoph Hellwig34ab96e2018-05-09 15:59:45 +0200102 buf = kmap(bio_page(req->bio)) + bio_offset(req->bio);
103 for (; nsect > 0; nsect--, block++, buf += tr->blksize) {
104 if (tr->readsect(dev, block, buf)) {
105 kunmap(bio_page(req->bio));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200106 return BLK_STS_IOERR;
Christoph Hellwig34ab96e2018-05-09 15:59:45 +0200107 }
108 }
109 kunmap(bio_page(req->bio));
Ilya Loginov2d4dc892009-11-26 09:16:19 +0100110 rq_flush_dcache_pages(req);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200111 return BLK_STS_OK;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100112 case REQ_OP_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (!tr->writesect)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200114 return BLK_STS_IOERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Ilya Loginov2d4dc892009-11-26 09:16:19 +0100116 rq_flush_dcache_pages(req);
Christoph Hellwig34ab96e2018-05-09 15:59:45 +0200117 buf = kmap(bio_page(req->bio)) + bio_offset(req->bio);
118 for (; nsect > 0; nsect--, block++, buf += tr->blksize) {
119 if (tr->writesect(dev, block, buf)) {
120 kunmap(bio_page(req->bio));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200121 return BLK_STS_IOERR;
Christoph Hellwig34ab96e2018-05-09 15:59:45 +0200122 }
123 }
124 kunmap(bio_page(req->bio));
Abhishek Sahu9a515442017-08-02 18:03:05 +0530125 return BLK_STS_OK;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100126 default:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200127 return BLK_STS_IOERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129}
130
Jarkko Lavinenc7519db2011-02-14 16:16:09 +0200131int mtd_blktrans_cease_background(struct mtd_blktrans_dev *dev)
132{
Artem Bityutskiy7bf7e372011-03-25 17:41:20 +0200133 return dev->bg_stop;
Jarkko Lavinenc7519db2011-02-14 16:16:09 +0200134}
135EXPORT_SYMBOL_GPL(mtd_blktrans_cease_background);
136
Ezequiel Garcia22a85782012-11-10 13:08:20 -0300137static void mtd_blktrans_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Ezequiel Garcia22a85782012-11-10 13:08:20 -0300139 struct mtd_blktrans_dev *dev =
140 container_of(work, struct mtd_blktrans_dev, work);
Jarkko Lavinenc7519db2011-02-14 16:16:09 +0200141 struct mtd_blktrans_ops *tr = dev->tr;
Maxim Levitskya8638622010-02-22 20:39:29 +0200142 struct request_queue *rq = dev->rq;
Tejun Heo1498ada2009-05-08 11:54:11 +0900143 struct request *req = NULL;
Jarkko Lavinenc7519db2011-02-14 16:16:09 +0200144 int background_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 spin_lock_irq(rq->queue_lock);
Tejun Heo1498ada2009-05-08 11:54:11 +0900147
Ezequiel Garcia22a85782012-11-10 13:08:20 -0300148 while (1) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200149 blk_status_t res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Artem Bityutskiy7bf7e372011-03-25 17:41:20 +0200151 dev->bg_stop = false;
Tejun Heo9934c8c2009-05-08 11:54:16 +0900152 if (!req && !(req = blk_fetch_request(rq))) {
Jarkko Lavinenc7519db2011-02-14 16:16:09 +0200153 if (tr->background && !background_done) {
154 spin_unlock_irq(rq->queue_lock);
155 mutex_lock(&dev->lock);
156 tr->background(dev);
157 mutex_unlock(&dev->lock);
158 spin_lock_irq(rq->queue_lock);
159 /*
160 * Do background processing just once per idle
161 * period.
162 */
Artem Bityutskiy7bf7e372011-03-25 17:41:20 +0200163 background_done = !dev->bg_stop;
Jarkko Lavinenc7519db2011-02-14 16:16:09 +0200164 continue;
165 }
Ezequiel Garcia22a85782012-11-10 13:08:20 -0300166 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 spin_unlock_irq(rq->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186static void mtd_blktrans_request(struct request_queue *rq)
187{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200188 struct mtd_blktrans_dev *dev;
189 struct request *req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Maxim Levitsky048d8712010-02-22 20:39:30 +0200191 dev = rq->queuedata;
192
193 if (!dev)
194 while ((req = blk_fetch_request(rq)) != NULL)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200195 __blk_end_request_all(req, BLK_STS_IOERR);
Ezequiel Garcia22a85782012-11-10 13:08:20 -0300196 else
197 queue_work(dev->wq, &dev->work);
Maxim Levitsky048d8712010-02-22 20:39:30 +0200198}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Al Viroaf0e2a02008-03-02 10:35:06 -0500200static int blktrans_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200202 struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
Maxim Levitsky008c7512010-10-15 17:20:43 +0200203 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Maxim Levitsky048d8712010-02-22 20:39:30 +0200205 if (!dev)
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200206 return -ERESTARTSYS; /* FIXME: busy loop! -arnd*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Brian Norris073db4a2015-05-07 17:55:16 -0700208 mutex_lock(&mtd_table_mutex);
Brian Norrisf3c63792015-10-26 10:20:23 -0700209 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Brian Norris342ff282011-11-07 15:51:05 -0800211 if (dev->open)
Maxim Levitsky048d8712010-02-22 20:39:30 +0200212 goto unlock;
Maxim Levitsky008c7512010-10-15 17:20:43 +0200213
214 kref_get(&dev->ref);
215 __module_get(dev->tr->owner);
216
Artem Bityutskiy94735ec2011-04-18 07:50:37 +0300217 if (!dev->mtd)
218 goto unlock;
219
220 if (dev->tr->open) {
221 ret = dev->tr->open(dev);
222 if (ret)
223 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Maxim Levitsky048d8712010-02-22 20:39:30 +0200225
Artem Bityutskiy94735ec2011-04-18 07:50:37 +0300226 ret = __get_mtd_device(dev->mtd);
227 if (ret)
228 goto error_release;
Alexander Stein70d50982012-01-10 13:26:58 +0100229 dev->file_mode = mode;
Artem Bityutskiy94735ec2011-04-18 07:50:37 +0300230
Maxim Levitsky048d8712010-02-22 20:39:30 +0200231unlock:
Brian Norris342ff282011-11-07 15:51:05 -0800232 dev->open++;
Maxim Levitsky048d8712010-02-22 20:39:30 +0200233 mutex_unlock(&dev->lock);
Brian Norrisf3c63792015-10-26 10:20:23 -0700234 mutex_unlock(&mtd_table_mutex);
Maxim Levitsky048d8712010-02-22 20:39:30 +0200235 blktrans_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return ret;
Artem Bityutskiy94735ec2011-04-18 07:50:37 +0300237
238error_release:
239 if (dev->tr->release)
240 dev->tr->release(dev);
241error_put:
242 module_put(dev->tr->owner);
243 kref_put(&dev->ref, blktrans_dev_release);
244 mutex_unlock(&dev->lock);
Brian Norrisf3c63792015-10-26 10:20:23 -0700245 mutex_unlock(&mtd_table_mutex);
Artem Bityutskiy94735ec2011-04-18 07:50:37 +0300246 blktrans_dev_put(dev);
247 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Al Virodb2a1442013-05-05 21:52:57 -0400250static void blktrans_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200252 struct mtd_blktrans_dev *dev = blktrans_dev_get(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Maxim Levitsky048d8712010-02-22 20:39:30 +0200254 if (!dev)
Al Virodb2a1442013-05-05 21:52:57 -0400255 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Brian Norris073db4a2015-05-07 17:55:16 -0700257 mutex_lock(&mtd_table_mutex);
Brian Norrisf3c63792015-10-26 10:20:23 -0700258 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Maxim Levitsky008c7512010-10-15 17:20:43 +0200260 if (--dev->open)
Maxim Levitsky048d8712010-02-22 20:39:30 +0200261 goto unlock;
262
Maxim Levitsky008c7512010-10-15 17:20:43 +0200263 kref_put(&dev->ref, blktrans_dev_release);
264 module_put(dev->tr->owner);
265
266 if (dev->mtd) {
Al Viroa8ca8892013-05-05 21:31:22 -0400267 if (dev->tr->release)
268 dev->tr->release(dev);
Maxim Levitsky008c7512010-10-15 17:20:43 +0200269 __put_mtd_device(dev->mtd);
270 }
Maxim Levitsky048d8712010-02-22 20:39:30 +0200271unlock:
272 mutex_unlock(&dev->lock);
Brian Norrisf3c63792015-10-26 10:20:23 -0700273 mutex_unlock(&mtd_table_mutex);
Maxim Levitsky048d8712010-02-22 20:39:30 +0200274 blktrans_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800277static int blktrans_getgeo(struct block_device *bdev, struct hd_geometry *geo)
278{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200279 struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
280 int ret = -ENXIO;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800281
Maxim Levitsky048d8712010-02-22 20:39:30 +0200282 if (!dev)
283 return ret;
284
285 mutex_lock(&dev->lock);
286
287 if (!dev->mtd)
288 goto unlock;
289
Brian Norrisc4a3f13c2015-05-21 10:44:32 -0700290 ret = dev->tr->getgeo ? dev->tr->getgeo(dev, geo) : -ENOTTY;
Maxim Levitsky048d8712010-02-22 20:39:30 +0200291unlock:
292 mutex_unlock(&dev->lock);
293 blktrans_dev_put(dev);
294 return ret;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800295}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Al Viroaf0e2a02008-03-02 10:35:06 -0500297static int blktrans_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 unsigned int cmd, unsigned long arg)
299{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200300 struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
301 int ret = -ENXIO;
302
303 if (!dev)
304 return ret;
305
306 mutex_lock(&dev->lock);
307
308 if (!dev->mtd)
309 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 switch (cmd) {
312 case BLKFLSBUF:
Maxim Levitsky048d8712010-02-22 20:39:30 +0200313 ret = dev->tr->flush ? dev->tr->flush(dev) : 0;
Dan Carpenter007c2d82010-05-31 16:03:38 +0200314 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 default:
Maxim Levitsky048d8712010-02-22 20:39:30 +0200316 ret = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
Maxim Levitsky048d8712010-02-22 20:39:30 +0200318unlock:
319 mutex_unlock(&dev->lock);
320 blktrans_dev_put(dev);
321 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Ezequiel Garcia9329c5e2012-11-09 12:36:35 -0300324static const struct block_device_operations mtd_block_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 .owner = THIS_MODULE,
Al Viroaf0e2a02008-03-02 10:35:06 -0500326 .open = blktrans_open,
327 .release = blktrans_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200328 .ioctl = blktrans_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800329 .getgeo = blktrans_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330};
331
332int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
333{
334 struct mtd_blktrans_ops *tr = new->tr;
Chris Malley71a928c2008-05-19 20:11:50 +0100335 struct mtd_blktrans_dev *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 int last_devnum = -1;
337 struct gendisk *gd;
Maxim Levitskya8638622010-02-22 20:39:29 +0200338 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Jean Delvareb3561ea2007-05-08 00:30:46 -0700340 if (mutex_trylock(&mtd_table_mutex)) {
Ingo Molnar48b19262006-03-31 02:29:41 -0800341 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 BUG();
343 }
344
Maxim Levitsky048d8712010-02-22 20:39:30 +0200345 mutex_lock(&blktrans_ref_mutex);
Chris Malley71a928c2008-05-19 20:11:50 +0100346 list_for_each_entry(d, &tr->devs, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (new->devnum == -1) {
348 /* Use first free number */
349 if (d->devnum != last_devnum+1) {
350 /* Found a free devnum. Plug it in here */
351 new->devnum = last_devnum+1;
352 list_add_tail(&new->list, &d->list);
353 goto added;
354 }
355 } else if (d->devnum == new->devnum) {
356 /* Required number taken */
Maxim Levitsky048d8712010-02-22 20:39:30 +0200357 mutex_unlock(&blktrans_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return -EBUSY;
359 } else if (d->devnum > new->devnum) {
360 /* Required number was free */
361 list_add_tail(&new->list, &d->list);
362 goto added;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 last_devnum = d->devnum;
365 }
Maxim Levitskya8638622010-02-22 20:39:29 +0200366
367 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (new->devnum == -1)
369 new->devnum = last_devnum+1;
370
Ben Hutchings4d3a8532010-01-29 20:59:53 +0000371 /* Check that the device and any partitions will get valid
372 * minor numbers and that the disk naming code below can cope
373 * with this number. */
374 if (new->devnum > (MINORMASK >> tr->part_bits) ||
Maxim Levitsky048d8712010-02-22 20:39:30 +0200375 (tr->part_bits && new->devnum >= 27 * 26)) {
376 mutex_unlock(&blktrans_ref_mutex);
Maxim Levitskya8638622010-02-22 20:39:29 +0200377 goto error1;
Maxim Levitsky048d8712010-02-22 20:39:30 +0200378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 list_add_tail(&new->list, &tr->devs);
381 added:
Maxim Levitsky048d8712010-02-22 20:39:30 +0200382 mutex_unlock(&blktrans_ref_mutex);
383
David Woodhousece37ab42007-12-03 12:46:12 +0000384 mutex_init(&new->lock);
Maxim Levitsky048d8712010-02-22 20:39:30 +0200385 kref_init(&new->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (!tr->writesect)
387 new->readonly = 1;
388
Maxim Levitskya8638622010-02-22 20:39:29 +0200389 /* Create gendisk */
390 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 gd = alloc_disk(1 << tr->part_bits);
Maxim Levitskya8638622010-02-22 20:39:29 +0200392
393 if (!gd)
394 goto error2;
395
396 new->disk = gd;
397 gd->private_data = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 gd->major = tr->major;
399 gd->first_minor = (new->devnum) << tr->part_bits;
Ezequiel Garcia9329c5e2012-11-09 12:36:35 -0300400 gd->fops = &mtd_block_ops;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000401
Todd Poynor65a8de32005-07-29 20:42:07 +0100402 if (tr->part_bits)
403 if (new->devnum < 26)
404 snprintf(gd->disk_name, sizeof(gd->disk_name),
405 "%s%c", tr->name, 'a' + new->devnum);
406 else
407 snprintf(gd->disk_name, sizeof(gd->disk_name),
408 "%s%c%c", tr->name,
409 'a' - 1 + new->devnum / 26,
410 'a' + new->devnum % 26);
411 else
412 snprintf(gd->disk_name, sizeof(gd->disk_name),
413 "%s%d", tr->name, new->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Peng Fan2ce401d2015-09-11 21:41:47 +0800415 set_capacity(gd, ((u64)new->size * tr->blksize) >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Maxim Levitskya8638622010-02-22 20:39:29 +0200417 /* Create the request queue */
418 spin_lock_init(&new->queue_lock);
419 new->rq = blk_init_queue(mtd_blktrans_request, &new->queue_lock);
420
421 if (!new->rq)
422 goto error3;
423
Roman Peniaev566c0d6a2014-03-08 21:59:14 +0900424 if (tr->flush)
Jens Axboefec3ff5d2016-03-30 10:17:47 -0600425 blk_queue_write_cache(new->rq, true, false);
Roman Peniaev566c0d6a2014-03-08 21:59:14 +0900426
Maxim Levitskya8638622010-02-22 20:39:29 +0200427 new->rq->queuedata = new;
428 blk_queue_logical_block_size(new->rq, tr->blksize);
429
Bart Van Assche8b904b52018-03-07 17:10:10 -0800430 blk_queue_flag_set(QUEUE_FLAG_NONROT, new->rq);
431 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, new->rq);
Dan McGee16f7eca2011-09-28 00:21:42 -0500432
Jarkko Lavinen115ee882011-02-14 16:16:10 +0200433 if (tr->discard) {
Bart Van Assche8b904b52018-03-07 17:10:10 -0800434 blk_queue_flag_set(QUEUE_FLAG_DISCARD, new->rq);
Jens Axboe2bb4cd52015-07-14 08:15:12 -0600435 blk_queue_max_discard_sectors(new->rq, UINT_MAX);
Jarkko Lavinen115ee882011-02-14 16:16:10 +0200436 }
Maxim Levitskya8638622010-02-22 20:39:29 +0200437
438 gd->queue = new->rq;
439
Ezequiel Garcia22a85782012-11-10 13:08:20 -0300440 /* Create processing workqueue */
441 new->wq = alloc_workqueue("%s%d", 0, 0,
442 tr->name, new->mtd->index);
443 if (!new->wq)
Maxim Levitskya8638622010-02-22 20:39:29 +0200444 goto error4;
Ezequiel Garcia22a85782012-11-10 13:08:20 -0300445 INIT_WORK(&new->work, mtd_blktrans_work);
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (new->readonly)
448 set_disk_ro(gd, 1);
449
Hannes Reineckefef912b2018-09-28 08:17:19 +0200450 device_add_disk(&new->mtd->dev, gd, NULL);
Maxim Levitsky026ec572010-02-22 20:39:33 +0200451
Maxim Levitsky133fa8c72010-02-26 22:08:40 +0200452 if (new->disk_attributes) {
453 ret = sysfs_create_group(&disk_to_dev(gd)->kobj,
Maxim Levitsky026ec572010-02-22 20:39:33 +0200454 new->disk_attributes);
Maxim Levitsky133fa8c72010-02-26 22:08:40 +0200455 WARN_ON(ret);
456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 0;
Maxim Levitskya8638622010-02-22 20:39:29 +0200458error4:
459 blk_cleanup_queue(new->rq);
460error3:
461 put_disk(new->disk);
462error2:
463 list_del(&new->list);
464error1:
Maxim Levitskya8638622010-02-22 20:39:29 +0200465 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
468int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
469{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200470 unsigned long flags;
471
Jean Delvareb3561ea2007-05-08 00:30:46 -0700472 if (mutex_trylock(&mtd_table_mutex)) {
Ingo Molnar48b19262006-03-31 02:29:41 -0800473 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 BUG();
475 }
476
Maxim Levitsky026ec572010-02-22 20:39:33 +0200477 if (old->disk_attributes)
478 sysfs_remove_group(&disk_to_dev(old->disk)->kobj,
479 old->disk_attributes);
480
Maxim Levitskydba76c02010-07-28 18:53:16 +0300481 /* Stop new requests to arrive */
482 del_gendisk(old->disk);
483
Ezequiel Garcia22a85782012-11-10 13:08:20 -0300484 /* Stop workqueue. This will perform any pending request. */
485 destroy_workqueue(old->wq);
Maxim Levitskya8638622010-02-22 20:39:29 +0200486
Maxim Levitsky048d8712010-02-22 20:39:30 +0200487 /* Kill current requests */
488 spin_lock_irqsave(&old->queue_lock, flags);
489 old->rq->queuedata = NULL;
490 blk_start_queue(old->rq);
491 spin_unlock_irqrestore(&old->queue_lock, flags);
Maxim Levitsky048d8712010-02-22 20:39:30 +0200492
Maxim Levitsky008c7512010-10-15 17:20:43 +0200493 /* If the device is currently open, tell trans driver to close it,
494 then put mtd device, and don't touch it again */
Maxim Levitsky048d8712010-02-22 20:39:30 +0200495 mutex_lock(&old->lock);
Maxim Levitsky008c7512010-10-15 17:20:43 +0200496 if (old->open) {
497 if (old->tr->release)
498 old->tr->release(old);
499 __put_mtd_device(old->mtd);
Maxim Levitsky048d8712010-02-22 20:39:30 +0200500 }
501
Maxim Levitsky048d8712010-02-22 20:39:30 +0200502 old->mtd = NULL;
503
504 mutex_unlock(&old->lock);
505 blktrans_dev_put(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return 0;
507}
508
509static void blktrans_notify_remove(struct mtd_info *mtd)
510{
Chris Malley71a928c2008-05-19 20:11:50 +0100511 struct mtd_blktrans_ops *tr;
512 struct mtd_blktrans_dev *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Chris Malley71a928c2008-05-19 20:11:50 +0100514 list_for_each_entry(tr, &blktrans_majors, list)
515 list_for_each_entry_safe(dev, next, &tr->devs, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (dev->mtd == mtd)
517 tr->remove_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
520static void blktrans_notify_add(struct mtd_info *mtd)
521{
Chris Malley71a928c2008-05-19 20:11:50 +0100522 struct mtd_blktrans_ops *tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 if (mtd->type == MTD_ABSENT)
525 return;
526
Chris Malley71a928c2008-05-19 20:11:50 +0100527 list_for_each_entry(tr, &blktrans_majors, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 tr->add_mtd(tr, mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531static struct mtd_notifier blktrans_notifier = {
532 .add = blktrans_notify_add,
533 .remove = blktrans_notify_remove,
534};
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
537{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000538 struct mtd_info *mtd;
539 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000541 /* Register the notifier if/when the first device type is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 registered, to prevent the link/init ordering from fucking
543 us over. */
544 if (!blktrans_notifier.list.next)
545 register_mtd_user(&blktrans_notifier);
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Ingo Molnar48b19262006-03-31 02:29:41 -0800548 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 ret = register_blkdev(tr->major, tr->name);
Frank Li6fe4c592010-10-26 11:02:19 +0800551 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n",
553 tr->name, tr->major, ret);
Ingo Molnar48b19262006-03-31 02:29:41 -0800554 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return ret;
556 }
David Woodhouseeae9acd2008-08-05 18:08:25 +0100557
Frank Li6fe4c592010-10-26 11:02:19 +0800558 if (ret)
559 tr->major = ret;
560
Richard Purdie19187672006-10-27 09:09:33 +0100561 tr->blkshift = ffs(tr->blksize) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 INIT_LIST_HEAD(&tr->devs);
564 list_add(&tr->list, &blktrans_majors);
565
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000566 mtd_for_each_device(mtd)
567 if (mtd->type != MTD_ABSENT)
568 tr->add_mtd(tr, mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Ingo Molnar48b19262006-03-31 02:29:41 -0800570 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return 0;
572}
573
574int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr)
575{
Chris Malley71a928c2008-05-19 20:11:50 +0100576 struct mtd_blktrans_dev *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Ingo Molnar48b19262006-03-31 02:29:41 -0800578 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /* Remove it from the list of active majors */
581 list_del(&tr->list);
582
Chris Malley71a928c2008-05-19 20:11:50 +0100583 list_for_each_entry_safe(dev, next, &tr->devs, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 tr->remove_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 unregister_blkdev(tr->major, tr->name);
Ingo Molnar48b19262006-03-31 02:29:41 -0800587 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Eric Sesterhenn373ebfb2006-03-26 18:15:12 +0200589 BUG_ON(!list_empty(&tr->devs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return 0;
591}
592
593static void __exit mtd_blktrans_exit(void)
594{
595 /* No race here -- if someone's currently in register_mtd_blktrans
596 we're screwed anyway. */
597 if (blktrans_notifier.list.next)
598 unregister_mtd_user(&blktrans_notifier);
599}
600
601module_exit(mtd_blktrans_exit);
602
603EXPORT_SYMBOL_GPL(register_mtd_blktrans);
604EXPORT_SYMBOL_GPL(deregister_mtd_blktrans);
605EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev);
606EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev);
607
608MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
609MODULE_LICENSE("GPL");
610MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'");