blob: 243f28a3206b4d8b390d5360ece621ae9de544a0 [file] [log] [blame]
Thomas Gleixnerfd534e92019-05-23 11:14:39 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
David Woodhousea1452a32010-08-08 20:58:20 +01003 * Interface to Linux block layer for MTD 'translation layers'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
David Woodhousea1452a32010-08-08 20:58:20 +01005 * Copyright © 2003-2010 David Woodhouse <dwmw2@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
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>
Jens Axboe891b7c52018-10-16 08:09:58 -060016#include <linux/blk-mq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/blkpg.h>
18#include <linux/spinlock.h>
19#include <linux/hdreg.h>
Ingo Molnar48b19262006-03-31 02:29:41 -080020#include <linux/mutex.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080021#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Ben Dooks356d70f2007-05-28 20:28:34 +010023#include "mtdcore.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Ben Dooks356d70f2007-05-28 20:28:34 +010025static LIST_HEAD(blktrans_majors);
Maxim Levitsky048d8712010-02-22 20:39:30 +020026
H Hartley Sweeten7f53f122011-01-11 18:46:10 -060027static void blktrans_dev_release(struct kref *kref)
Maxim Levitsky048d8712010-02-22 20:39:30 +020028{
29 struct mtd_blktrans_dev *dev =
30 container_of(kref, struct mtd_blktrans_dev, ref);
31
Christoph Hellwig6966bb92021-06-02 09:53:24 +030032 blk_cleanup_disk(dev->disk);
Jens Axboe891b7c52018-10-16 08:09:58 -060033 blk_mq_free_tag_set(dev->tag_set);
34 kfree(dev->tag_set);
Maxim Levitsky048d8712010-02-22 20:39:30 +020035 list_del(&dev->list);
36 kfree(dev);
37}
38
H Hartley Sweeten7f53f122011-01-11 18:46:10 -060039static void blktrans_dev_put(struct mtd_blktrans_dev *dev)
Maxim Levitsky048d8712010-02-22 20:39:30 +020040{
Maxim Levitsky048d8712010-02-22 20:39:30 +020041 kref_put(&dev->ref, blktrans_dev_release);
Maxim Levitsky048d8712010-02-22 20:39:30 +020042}
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020045static blk_status_t do_blktrans_request(struct mtd_blktrans_ops *tr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 struct mtd_blktrans_dev *dev,
47 struct request *req)
48{
Christoph Hellwig786d4e02021-11-17 07:13:55 +010049 struct req_iterator iter;
50 struct bio_vec bvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 unsigned long block, nsect;
52 char *buf;
53
Tejun Heo83096eb2009-05-07 22:24:39 +090054 block = blk_rq_pos(req) << 9 >> tr->blkshift;
Tejun Heo1011c1b2009-05-07 22:24:45 +090055 nsect = blk_rq_cur_bytes(req) >> tr->blkshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Christoph Hellwig82baa322021-11-26 13:17:58 +010057 switch (req_op(req)) {
58 case REQ_OP_FLUSH:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020059 if (tr->flush(dev))
60 return BLK_STS_IOERR;
61 return BLK_STS_OK;
Christoph Hellwigaebf5262017-01-31 16:57:31 +010062 case REQ_OP_DISCARD:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020063 if (tr->discard(dev, block, nsect))
64 return BLK_STS_IOERR;
65 return BLK_STS_OK;
Christoph Hellwigaebf5262017-01-31 16:57:31 +010066 case REQ_OP_READ:
Christoph Hellwig34ab96e2018-05-09 15:59:45 +020067 buf = kmap(bio_page(req->bio)) + bio_offset(req->bio);
68 for (; nsect > 0; nsect--, block++, buf += tr->blksize) {
69 if (tr->readsect(dev, block, buf)) {
70 kunmap(bio_page(req->bio));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020071 return BLK_STS_IOERR;
Christoph Hellwig34ab96e2018-05-09 15:59:45 +020072 }
73 }
74 kunmap(bio_page(req->bio));
Christoph Hellwig786d4e02021-11-17 07:13:55 +010075
76 rq_for_each_segment(bvec, req, iter)
77 flush_dcache_page(bvec.bv_page);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020078 return BLK_STS_OK;
Christoph Hellwigaebf5262017-01-31 16:57:31 +010079 case REQ_OP_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (!tr->writesect)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020081 return BLK_STS_IOERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Christoph Hellwig786d4e02021-11-17 07:13:55 +010083 rq_for_each_segment(bvec, req, iter)
84 flush_dcache_page(bvec.bv_page);
85
Christoph Hellwig34ab96e2018-05-09 15:59:45 +020086 buf = kmap(bio_page(req->bio)) + bio_offset(req->bio);
87 for (; nsect > 0; nsect--, block++, buf += tr->blksize) {
88 if (tr->writesect(dev, block, buf)) {
89 kunmap(bio_page(req->bio));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020090 return BLK_STS_IOERR;
Christoph Hellwig34ab96e2018-05-09 15:59:45 +020091 }
92 }
93 kunmap(bio_page(req->bio));
Abhishek Sahu9a515442017-08-02 18:03:05 +053094 return BLK_STS_OK;
Christoph Hellwigaebf5262017-01-31 16:57:31 +010095 default:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020096 return BLK_STS_IOERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
98}
99
Jarkko Lavinenc7519db2011-02-14 16:16:09 +0200100int mtd_blktrans_cease_background(struct mtd_blktrans_dev *dev)
101{
Artem Bityutskiy7bf7e372011-03-25 17:41:20 +0200102 return dev->bg_stop;
Jarkko Lavinenc7519db2011-02-14 16:16:09 +0200103}
104EXPORT_SYMBOL_GPL(mtd_blktrans_cease_background);
105
Jens Axboe891b7c52018-10-16 08:09:58 -0600106static struct request *mtd_next_request(struct mtd_blktrans_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Jens Axboe891b7c52018-10-16 08:09:58 -0600108 struct request *rq;
109
110 rq = list_first_entry_or_null(&dev->rq_list, struct request, queuelist);
111 if (rq) {
112 list_del_init(&rq->queuelist);
113 blk_mq_start_request(rq);
114 return rq;
115 }
116
117 return NULL;
118}
119
120static void mtd_blktrans_work(struct mtd_blktrans_dev *dev)
121 __releases(&dev->queue_lock)
122 __acquires(&dev->queue_lock)
123{
Jarkko Lavinenc7519db2011-02-14 16:16:09 +0200124 struct mtd_blktrans_ops *tr = dev->tr;
Tejun Heo1498ada2009-05-08 11:54:11 +0900125 struct request *req = NULL;
Jarkko Lavinenc7519db2011-02-14 16:16:09 +0200126 int background_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Ezequiel Garcia22a85782012-11-10 13:08:20 -0300128 while (1) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200129 blk_status_t res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Artem Bityutskiy7bf7e372011-03-25 17:41:20 +0200131 dev->bg_stop = false;
Jens Axboe891b7c52018-10-16 08:09:58 -0600132 if (!req && !(req = mtd_next_request(dev))) {
Jarkko Lavinenc7519db2011-02-14 16:16:09 +0200133 if (tr->background && !background_done) {
Jens Axboe891b7c52018-10-16 08:09:58 -0600134 spin_unlock_irq(&dev->queue_lock);
Jarkko Lavinenc7519db2011-02-14 16:16:09 +0200135 mutex_lock(&dev->lock);
136 tr->background(dev);
137 mutex_unlock(&dev->lock);
Jens Axboe891b7c52018-10-16 08:09:58 -0600138 spin_lock_irq(&dev->queue_lock);
Jarkko Lavinenc7519db2011-02-14 16:16:09 +0200139 /*
140 * Do background processing just once per idle
141 * period.
142 */
Artem Bityutskiy7bf7e372011-03-25 17:41:20 +0200143 background_done = !dev->bg_stop;
Jarkko Lavinenc7519db2011-02-14 16:16:09 +0200144 continue;
145 }
Ezequiel Garcia22a85782012-11-10 13:08:20 -0300146 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148
Jens Axboe891b7c52018-10-16 08:09:58 -0600149 spin_unlock_irq(&dev->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Ingo Molnar48b19262006-03-31 02:29:41 -0800151 mutex_lock(&dev->lock);
Maxim Levitskya8638622010-02-22 20:39:29 +0200152 res = do_blktrans_request(dev->tr, dev, req);
Ingo Molnar48b19262006-03-31 02:29:41 -0800153 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Jens Axboe891b7c52018-10-16 08:09:58 -0600155 if (!blk_update_request(req, res, blk_rq_cur_bytes(req))) {
156 __blk_mq_end_request(req, res);
Tejun Heo1498ada2009-05-08 11:54:11 +0900157 req = NULL;
Jens Axboe891b7c52018-10-16 08:09:58 -0600158 }
Jarkko Lavinenc7519db2011-02-14 16:16:09 +0200159
160 background_done = 0;
Jens Axboe891b7c52018-10-16 08:09:58 -0600161 spin_lock_irq(&dev->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Jens Axboe891b7c52018-10-16 08:09:58 -0600165static blk_status_t mtd_queue_rq(struct blk_mq_hw_ctx *hctx,
166 const struct blk_mq_queue_data *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200168 struct mtd_blktrans_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Jens Axboe891b7c52018-10-16 08:09:58 -0600170 dev = hctx->queue->queuedata;
171 if (!dev) {
172 blk_mq_start_request(bd->rq);
173 return BLK_STS_IOERR;
174 }
Maxim Levitsky048d8712010-02-22 20:39:30 +0200175
Jens Axboe891b7c52018-10-16 08:09:58 -0600176 spin_lock_irq(&dev->queue_lock);
177 list_add_tail(&bd->rq->queuelist, &dev->rq_list);
178 mtd_blktrans_work(dev);
179 spin_unlock_irq(&dev->queue_lock);
180
181 return BLK_STS_OK;
Maxim Levitsky048d8712010-02-22 20:39:30 +0200182}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Al Viroaf0e2a02008-03-02 10:35:06 -0500184static int blktrans_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Christoph Hellwigee28b422021-08-23 09:33:59 +0200186 struct mtd_blktrans_dev *dev = bdev->bd_disk->private_data;
Maxim Levitsky008c7512010-10-15 17:20:43 +0200187 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Christoph Hellwigee28b422021-08-23 09:33:59 +0200189 kref_get(&dev->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Brian Norrisf3c63792015-10-26 10:20:23 -0700191 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Brian Norris342ff282011-11-07 15:51:05 -0800193 if (dev->open)
Maxim Levitsky048d8712010-02-22 20:39:30 +0200194 goto unlock;
Maxim Levitsky008c7512010-10-15 17:20:43 +0200195
Maxim Levitsky008c7512010-10-15 17:20:43 +0200196 __module_get(dev->tr->owner);
197
Artem Bityutskiy94735ec2011-04-18 07:50:37 +0300198 if (!dev->mtd)
199 goto unlock;
200
201 if (dev->tr->open) {
202 ret = dev->tr->open(dev);
203 if (ret)
204 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
Maxim Levitsky048d8712010-02-22 20:39:30 +0200206
Artem Bityutskiy94735ec2011-04-18 07:50:37 +0300207 ret = __get_mtd_device(dev->mtd);
208 if (ret)
209 goto error_release;
Alexander Stein70d50982012-01-10 13:26:58 +0100210 dev->file_mode = mode;
Artem Bityutskiy94735ec2011-04-18 07:50:37 +0300211
Maxim Levitsky048d8712010-02-22 20:39:30 +0200212unlock:
Brian Norris342ff282011-11-07 15:51:05 -0800213 dev->open++;
Maxim Levitsky048d8712010-02-22 20:39:30 +0200214 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return ret;
Artem Bityutskiy94735ec2011-04-18 07:50:37 +0300216
217error_release:
218 if (dev->tr->release)
219 dev->tr->release(dev);
220error_put:
221 module_put(dev->tr->owner);
Artem Bityutskiy94735ec2011-04-18 07:50:37 +0300222 mutex_unlock(&dev->lock);
223 blktrans_dev_put(dev);
224 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Al Virodb2a1442013-05-05 21:52:57 -0400227static void blktrans_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Christoph Hellwigee28b422021-08-23 09:33:59 +0200229 struct mtd_blktrans_dev *dev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Brian Norrisf3c63792015-10-26 10:20:23 -0700231 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Maxim Levitsky008c7512010-10-15 17:20:43 +0200233 if (--dev->open)
Maxim Levitsky048d8712010-02-22 20:39:30 +0200234 goto unlock;
235
Maxim Levitsky008c7512010-10-15 17:20:43 +0200236 module_put(dev->tr->owner);
237
238 if (dev->mtd) {
Al Viroa8ca8892013-05-05 21:31:22 -0400239 if (dev->tr->release)
240 dev->tr->release(dev);
Maxim Levitsky008c7512010-10-15 17:20:43 +0200241 __put_mtd_device(dev->mtd);
242 }
Maxim Levitsky048d8712010-02-22 20:39:30 +0200243unlock:
244 mutex_unlock(&dev->lock);
245 blktrans_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800248static int blktrans_getgeo(struct block_device *bdev, struct hd_geometry *geo)
249{
Christoph Hellwig37b143d2021-08-23 09:33:58 +0200250 struct mtd_blktrans_dev *dev = bdev->bd_disk->private_data;
Maxim Levitsky048d8712010-02-22 20:39:30 +0200251 int ret = -ENXIO;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800252
Maxim Levitsky048d8712010-02-22 20:39:30 +0200253 mutex_lock(&dev->lock);
254
255 if (!dev->mtd)
256 goto unlock;
257
Brian Norrisc4a3f13c2015-05-21 10:44:32 -0700258 ret = dev->tr->getgeo ? dev->tr->getgeo(dev, geo) : -ENOTTY;
Maxim Levitsky048d8712010-02-22 20:39:30 +0200259unlock:
260 mutex_unlock(&dev->lock);
Maxim Levitsky048d8712010-02-22 20:39:30 +0200261 return ret;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800262}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Ezequiel Garcia9329c5e2012-11-09 12:36:35 -0300264static const struct block_device_operations mtd_block_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 .owner = THIS_MODULE,
Al Viroaf0e2a02008-03-02 10:35:06 -0500266 .open = blktrans_open,
267 .release = blktrans_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800268 .getgeo = blktrans_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269};
270
Jens Axboe891b7c52018-10-16 08:09:58 -0600271static const struct blk_mq_ops mtd_mq_ops = {
272 .queue_rq = mtd_queue_rq,
273};
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
276{
277 struct mtd_blktrans_ops *tr = new->tr;
Chris Malley71a928c2008-05-19 20:11:50 +0100278 struct mtd_blktrans_dev *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 int last_devnum = -1;
280 struct gendisk *gd;
Maxim Levitskya8638622010-02-22 20:39:29 +0200281 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Christoph Hellwigf214eeb2021-08-23 09:33:53 +0200283 lockdep_assert_held(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Chris Malley71a928c2008-05-19 20:11:50 +0100285 list_for_each_entry(d, &tr->devs, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (new->devnum == -1) {
287 /* Use first free number */
288 if (d->devnum != last_devnum+1) {
289 /* Found a free devnum. Plug it in here */
290 new->devnum = last_devnum+1;
291 list_add_tail(&new->list, &d->list);
292 goto added;
293 }
294 } else if (d->devnum == new->devnum) {
295 /* Required number taken */
296 return -EBUSY;
297 } else if (d->devnum > new->devnum) {
298 /* Required number was free */
299 list_add_tail(&new->list, &d->list);
300 goto added;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 last_devnum = d->devnum;
303 }
Maxim Levitskya8638622010-02-22 20:39:29 +0200304
305 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (new->devnum == -1)
307 new->devnum = last_devnum+1;
308
Ben Hutchings4d3a8532010-01-29 20:59:53 +0000309 /* Check that the device and any partitions will get valid
310 * minor numbers and that the disk naming code below can cope
311 * with this number. */
312 if (new->devnum > (MINORMASK >> tr->part_bits) ||
Christoph Hellwig560a3912021-08-23 09:33:57 +0200313 (tr->part_bits && new->devnum >= 27 * 26))
Christoph Hellwig6966bb92021-06-02 09:53:24 +0300314 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 list_add_tail(&new->list, &tr->devs);
317 added:
Maxim Levitsky048d8712010-02-22 20:39:30 +0200318
David Woodhousece37ab42007-12-03 12:46:12 +0000319 mutex_init(&new->lock);
Maxim Levitsky048d8712010-02-22 20:39:30 +0200320 kref_init(&new->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (!tr->writesect)
322 new->readonly = 1;
323
Maxim Levitskya8638622010-02-22 20:39:29 +0200324 ret = -ENOMEM;
Christoph Hellwig6966bb92021-06-02 09:53:24 +0300325 new->tag_set = kzalloc(sizeof(*new->tag_set), GFP_KERNEL);
326 if (!new->tag_set)
327 goto out_list_del;
Maxim Levitskya8638622010-02-22 20:39:29 +0200328
Christoph Hellwig6966bb92021-06-02 09:53:24 +0300329 ret = blk_mq_alloc_sq_tag_set(new->tag_set, &mtd_mq_ops, 2,
330 BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING);
331 if (ret)
332 goto out_kfree_tag_set;
333
334 /* Create gendisk */
335 gd = blk_mq_alloc_disk(new->tag_set, new);
336 if (IS_ERR(gd)) {
337 ret = PTR_ERR(gd);
338 goto out_free_tag_set;
339 }
Maxim Levitskya8638622010-02-22 20:39:29 +0200340
341 new->disk = gd;
Christoph Hellwig07a719f2021-06-16 09:15:46 +0200342 new->rq = new->disk->queue;
Maxim Levitskya8638622010-02-22 20:39:29 +0200343 gd->private_data = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 gd->major = tr->major;
345 gd->first_minor = (new->devnum) << tr->part_bits;
Christoph Hellwig6966bb92021-06-02 09:53:24 +0300346 gd->minors = 1 << tr->part_bits;
Ezequiel Garcia9329c5e2012-11-09 12:36:35 -0300347 gd->fops = &mtd_block_ops;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000348
Christoph Hellwig17f81f92021-12-06 08:04:09 +0100349 if (tr->part_bits) {
Todd Poynor65a8de32005-07-29 20:42:07 +0100350 if (new->devnum < 26)
351 snprintf(gd->disk_name, sizeof(gd->disk_name),
352 "%s%c", tr->name, 'a' + new->devnum);
353 else
354 snprintf(gd->disk_name, sizeof(gd->disk_name),
355 "%s%c%c", tr->name,
356 'a' - 1 + new->devnum / 26,
357 'a' + new->devnum % 26);
Christoph Hellwig17f81f92021-12-06 08:04:09 +0100358 } else {
Todd Poynor65a8de32005-07-29 20:42:07 +0100359 snprintf(gd->disk_name, sizeof(gd->disk_name),
360 "%s%d", tr->name, new->devnum);
Christoph Hellwig17f81f92021-12-06 08:04:09 +0100361 gd->flags |= GENHD_FL_NO_PART;
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Peng Fan2ce401d2015-09-11 21:41:47 +0800364 set_capacity(gd, ((u64)new->size * tr->blksize) >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Maxim Levitskya8638622010-02-22 20:39:29 +0200366 /* Create the request queue */
367 spin_lock_init(&new->queue_lock);
Jens Axboe891b7c52018-10-16 08:09:58 -0600368 INIT_LIST_HEAD(&new->rq_list);
Maxim Levitskya8638622010-02-22 20:39:29 +0200369
Roman Peniaev566c0d6a2014-03-08 21:59:14 +0900370 if (tr->flush)
Jens Axboefec3ff5d2016-03-30 10:17:47 -0600371 blk_queue_write_cache(new->rq, true, false);
Roman Peniaev566c0d6a2014-03-08 21:59:14 +0900372
Maxim Levitskya8638622010-02-22 20:39:29 +0200373 blk_queue_logical_block_size(new->rq, tr->blksize);
374
Bart Van Assche8b904b52018-03-07 17:10:10 -0800375 blk_queue_flag_set(QUEUE_FLAG_NONROT, new->rq);
376 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, new->rq);
Dan McGee16f7eca2011-09-28 00:21:42 -0500377
Jarkko Lavinen115ee882011-02-14 16:16:10 +0200378 if (tr->discard) {
Bart Van Assche8b904b52018-03-07 17:10:10 -0800379 blk_queue_flag_set(QUEUE_FLAG_DISCARD, new->rq);
Jens Axboe2bb4cd52015-07-14 08:15:12 -0600380 blk_queue_max_discard_sectors(new->rq, UINT_MAX);
Zhihao Cheng2b6d2832021-06-15 17:39:05 +0800381 new->rq->limits.discard_granularity = tr->blksize;
Jarkko Lavinen115ee882011-02-14 16:16:10 +0200382 }
Maxim Levitskya8638622010-02-22 20:39:29 +0200383
384 gd->queue = new->rq;
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (new->readonly)
387 set_disk_ro(gd, 1);
388
Luis Chamberlain83b863f2021-10-15 16:30:28 -0700389 ret = device_add_disk(&new->mtd->dev, gd, NULL);
390 if (ret)
391 goto out_cleanup_disk;
Maxim Levitsky026ec572010-02-22 20:39:33 +0200392
Maxim Levitsky133fa8c72010-02-26 22:08:40 +0200393 if (new->disk_attributes) {
394 ret = sysfs_create_group(&disk_to_dev(gd)->kobj,
Maxim Levitsky026ec572010-02-22 20:39:33 +0200395 new->disk_attributes);
Maxim Levitsky133fa8c72010-02-26 22:08:40 +0200396 WARN_ON(ret);
397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return 0;
Christoph Hellwig6966bb92021-06-02 09:53:24 +0300399
Luis Chamberlain83b863f2021-10-15 16:30:28 -0700400out_cleanup_disk:
401 blk_cleanup_disk(new->disk);
Christoph Hellwig6966bb92021-06-02 09:53:24 +0300402out_free_tag_set:
403 blk_mq_free_tag_set(new->tag_set);
404out_kfree_tag_set:
Jens Axboe891b7c52018-10-16 08:09:58 -0600405 kfree(new->tag_set);
Christoph Hellwig6966bb92021-06-02 09:53:24 +0300406out_list_del:
Maxim Levitskya8638622010-02-22 20:39:29 +0200407 list_del(&new->list);
Maxim Levitskya8638622010-02-22 20:39:29 +0200408 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
411int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
412{
Maxim Levitsky048d8712010-02-22 20:39:30 +0200413 unsigned long flags;
414
Christoph Hellwigf214eeb2021-08-23 09:33:53 +0200415 lockdep_assert_held(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Maxim Levitsky026ec572010-02-22 20:39:33 +0200417 if (old->disk_attributes)
418 sysfs_remove_group(&disk_to_dev(old->disk)->kobj,
419 old->disk_attributes);
420
Maxim Levitskydba76c02010-07-28 18:53:16 +0300421 /* Stop new requests to arrive */
422 del_gendisk(old->disk);
423
Maxim Levitsky048d8712010-02-22 20:39:30 +0200424 /* Kill current requests */
425 spin_lock_irqsave(&old->queue_lock, flags);
426 old->rq->queuedata = NULL;
Maxim Levitsky048d8712010-02-22 20:39:30 +0200427 spin_unlock_irqrestore(&old->queue_lock, flags);
Maxim Levitsky048d8712010-02-22 20:39:30 +0200428
Jens Axboe891b7c52018-10-16 08:09:58 -0600429 /* freeze+quiesce queue to ensure all requests are flushed */
430 blk_mq_freeze_queue(old->rq);
431 blk_mq_quiesce_queue(old->rq);
432 blk_mq_unquiesce_queue(old->rq);
433 blk_mq_unfreeze_queue(old->rq);
434
Maxim Levitsky008c7512010-10-15 17:20:43 +0200435 /* If the device is currently open, tell trans driver to close it,
436 then put mtd device, and don't touch it again */
Maxim Levitsky048d8712010-02-22 20:39:30 +0200437 mutex_lock(&old->lock);
Maxim Levitsky008c7512010-10-15 17:20:43 +0200438 if (old->open) {
439 if (old->tr->release)
440 old->tr->release(old);
441 __put_mtd_device(old->mtd);
Maxim Levitsky048d8712010-02-22 20:39:30 +0200442 }
443
Maxim Levitsky048d8712010-02-22 20:39:30 +0200444 old->mtd = NULL;
445
446 mutex_unlock(&old->lock);
447 blktrans_dev_put(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return 0;
449}
450
451static void blktrans_notify_remove(struct mtd_info *mtd)
452{
Chris Malley71a928c2008-05-19 20:11:50 +0100453 struct mtd_blktrans_ops *tr;
454 struct mtd_blktrans_dev *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Chris Malley71a928c2008-05-19 20:11:50 +0100456 list_for_each_entry(tr, &blktrans_majors, list)
457 list_for_each_entry_safe(dev, next, &tr->devs, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (dev->mtd == mtd)
459 tr->remove_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462static void blktrans_notify_add(struct mtd_info *mtd)
463{
Chris Malley71a928c2008-05-19 20:11:50 +0100464 struct mtd_blktrans_ops *tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 if (mtd->type == MTD_ABSENT)
467 return;
468
Chris Malley71a928c2008-05-19 20:11:50 +0100469 list_for_each_entry(tr, &blktrans_majors, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 tr->add_mtd(tr, mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
473static struct mtd_notifier blktrans_notifier = {
474 .add = blktrans_notify_add,
475 .remove = blktrans_notify_remove,
476};
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
479{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000480 struct mtd_info *mtd;
481 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000483 /* Register the notifier if/when the first device type is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 registered, to prevent the link/init ordering from fucking
485 us over. */
486 if (!blktrans_notifier.list.next)
487 register_mtd_user(&blktrans_notifier);
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 ret = register_blkdev(tr->major, tr->name);
Frank Li6fe4c592010-10-26 11:02:19 +0800490 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n",
492 tr->name, tr->major, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return ret;
494 }
David Woodhouseeae9acd2008-08-05 18:08:25 +0100495
Frank Li6fe4c592010-10-26 11:02:19 +0800496 if (ret)
497 tr->major = ret;
498
Richard Purdie19187672006-10-27 09:09:33 +0100499 tr->blkshift = ffs(tr->blksize) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 INIT_LIST_HEAD(&tr->devs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Desmond Cheong Zhi Xi962bf782021-06-18 00:09:04 +0800503 mutex_lock(&mtd_table_mutex);
504 list_add(&tr->list, &blktrans_majors);
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000505 mtd_for_each_device(mtd)
506 if (mtd->type != MTD_ABSENT)
507 tr->add_mtd(tr, mtd);
Ingo Molnar48b19262006-03-31 02:29:41 -0800508 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return 0;
510}
511
512int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr)
513{
Chris Malley71a928c2008-05-19 20:11:50 +0100514 struct mtd_blktrans_dev *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Ingo Molnar48b19262006-03-31 02:29:41 -0800516 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /* Remove it from the list of active majors */
519 list_del(&tr->list);
520
Chris Malley71a928c2008-05-19 20:11:50 +0100521 list_for_each_entry_safe(dev, next, &tr->devs, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 tr->remove_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Ingo Molnar48b19262006-03-31 02:29:41 -0800524 mutex_unlock(&mtd_table_mutex);
Desmond Cheong Zhi Xib7abb052021-07-17 18:07:19 +0800525 unregister_blkdev(tr->major, tr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Eric Sesterhenn373ebfb2006-03-26 18:15:12 +0200527 BUG_ON(!list_empty(&tr->devs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return 0;
529}
530
531static void __exit mtd_blktrans_exit(void)
532{
533 /* No race here -- if someone's currently in register_mtd_blktrans
534 we're screwed anyway. */
535 if (blktrans_notifier.list.next)
536 unregister_mtd_user(&blktrans_notifier);
537}
538
539module_exit(mtd_blktrans_exit);
540
541EXPORT_SYMBOL_GPL(register_mtd_blktrans);
542EXPORT_SYMBOL_GPL(deregister_mtd_blktrans);
543EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev);
544EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev);
545
546MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
547MODULE_LICENSE("GPL");
548MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'");