blob: 74d9d30edabdc49c04f42b435b53e851f159c3c5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Thomas Gleixner97894cd2005-11-07 11:15:26 +00002 * $Id: mtd_blkdevs.c,v 1.27 2005/11/07 11:14:20 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * (C) 2003 David Woodhouse <dwmw2@infradead.org>
5 *
6 * Interface to Linux 2.5 block layer for MTD 'translation layers'.
7 *
8 */
9
10#include <linux/kernel.h>
11#include <linux/slab.h>
12#include <linux/module.h>
13#include <linux/list.h>
14#include <linux/fs.h>
15#include <linux/mtd/blktrans.h>
16#include <linux/mtd/mtd.h>
17#include <linux/blkdev.h>
18#include <linux/blkpg.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070019#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/spinlock.h>
21#include <linux/hdreg.h>
22#include <linux/init.h>
Ingo Molnar48b19262006-03-31 02:29:41 -080023#include <linux/mutex.h>
Eric W. Biederman99f9b242007-04-19 01:58:33 -060024#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Ben Dooks356d70f2007-05-28 20:28:34 +010027#include "mtdcore.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Ben Dooks356d70f2007-05-28 20:28:34 +010029static LIST_HEAD(blktrans_majors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31struct mtd_blkcore_priv {
Christoph Hellwig3e67fe42007-04-22 20:40:57 +010032 struct task_struct *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 struct request_queue *rq;
34 spinlock_t queue_lock;
35};
36
37static int do_blktrans_request(struct mtd_blktrans_ops *tr,
38 struct mtd_blktrans_dev *dev,
39 struct request *req)
40{
41 unsigned long block, nsect;
42 char *buf;
43
Richard Purdie19187672006-10-27 09:09:33 +010044 block = req->sector << 9 >> tr->blkshift;
45 nsect = req->current_nr_sectors << 9 >> tr->blkshift;
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 buf = req->buffer;
48
Jens Axboe4aff5e22006-08-10 08:44:47 +020049 if (!blk_fs_request(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 return 0;
51
Richard Purdie19187672006-10-27 09:09:33 +010052 if (req->sector + req->current_nr_sectors > get_capacity(req->rq_disk))
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 return 0;
54
55 switch(rq_data_dir(req)) {
56 case READ:
Richard Purdie19187672006-10-27 09:09:33 +010057 for (; nsect > 0; nsect--, block++, buf += tr->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 if (tr->readsect(dev, block, buf))
59 return 0;
60 return 1;
61
62 case WRITE:
63 if (!tr->writesect)
64 return 0;
65
Richard Purdie19187672006-10-27 09:09:33 +010066 for (; nsect > 0; nsect--, block++, buf += tr->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 if (tr->writesect(dev, block, buf))
68 return 0;
69 return 1;
70
71 default:
Jeff Garzik9a292302006-10-01 12:16:00 -040072 printk(KERN_NOTICE "Unknown request %u\n", rq_data_dir(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return 0;
74 }
75}
76
77static int mtd_blktrans_thread(void *arg)
78{
79 struct mtd_blktrans_ops *tr = arg;
80 struct request_queue *rq = tr->blkcore_priv->rq;
81
82 /* we might get involved when memory gets low, so use PF_MEMALLOC */
Rafael J. Wysocki83144182007-07-17 04:03:35 -070083 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 spin_lock_irq(rq->queue_lock);
Christoph Hellwig3e67fe42007-04-22 20:40:57 +010086 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 struct request *req;
88 struct mtd_blktrans_dev *dev;
89 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 req = elv_next_request(rq);
92
93 if (!req) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 spin_unlock_irq(rq->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 spin_lock_irq(rq->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 continue;
99 }
100
101 dev = req->rq_disk->private_data;
102 tr = dev->tr;
103
104 spin_unlock_irq(rq->queue_lock);
105
Ingo Molnar48b19262006-03-31 02:29:41 -0800106 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 res = do_blktrans_request(tr, dev, req);
Ingo Molnar48b19262006-03-31 02:29:41 -0800108 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 spin_lock_irq(rq->queue_lock);
111
112 end_request(req, res);
113 }
114 spin_unlock_irq(rq->queue_lock);
115
Christoph Hellwig3e67fe42007-04-22 20:40:57 +0100116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119static void mtd_blktrans_request(struct request_queue *rq)
120{
121 struct mtd_blktrans_ops *tr = rq->queuedata;
Christoph Hellwig3e67fe42007-04-22 20:40:57 +0100122 wake_up_process(tr->blkcore_priv->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125
126static int blktrans_open(struct inode *i, struct file *f)
127{
128 struct mtd_blktrans_dev *dev;
129 struct mtd_blktrans_ops *tr;
130 int ret = -ENODEV;
131
132 dev = i->i_bdev->bd_disk->private_data;
133 tr = dev->tr;
134
135 if (!try_module_get(dev->mtd->owner))
136 goto out;
137
138 if (!try_module_get(tr->owner))
139 goto out_tr;
140
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000141 /* FIXME: Locking. A hot pluggable device can go away
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 (del_mtd_device can be called for it) without its module
143 being unloaded. */
144 dev->mtd->usecount++;
145
146 ret = 0;
147 if (tr->open && (ret = tr->open(dev))) {
148 dev->mtd->usecount--;
149 module_put(dev->mtd->owner);
150 out_tr:
151 module_put(tr->owner);
152 }
153 out:
154 return ret;
155}
156
157static int blktrans_release(struct inode *i, struct file *f)
158{
159 struct mtd_blktrans_dev *dev;
160 struct mtd_blktrans_ops *tr;
161 int ret = 0;
162
163 dev = i->i_bdev->bd_disk->private_data;
164 tr = dev->tr;
165
166 if (tr->release)
167 ret = tr->release(dev);
168
169 if (!ret) {
170 dev->mtd->usecount--;
171 module_put(dev->mtd->owner);
172 module_put(tr->owner);
173 }
174
175 return ret;
176}
177
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800178static int blktrans_getgeo(struct block_device *bdev, struct hd_geometry *geo)
179{
180 struct mtd_blktrans_dev *dev = bdev->bd_disk->private_data;
181
182 if (dev->tr->getgeo)
183 return dev->tr->getgeo(dev, geo);
184 return -ENOTTY;
185}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000187static int blktrans_ioctl(struct inode *inode, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 unsigned int cmd, unsigned long arg)
189{
190 struct mtd_blktrans_dev *dev = inode->i_bdev->bd_disk->private_data;
191 struct mtd_blktrans_ops *tr = dev->tr;
192
193 switch (cmd) {
194 case BLKFLSBUF:
195 if (tr->flush)
196 return tr->flush(dev);
197 /* The core code did the work, we had nothing to do. */
198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 default:
200 return -ENOTTY;
201 }
202}
203
Ben Dooks8f026f72007-05-28 19:51:59 +0100204static struct block_device_operations mtd_blktrans_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 .owner = THIS_MODULE,
206 .open = blktrans_open,
207 .release = blktrans_release,
208 .ioctl = blktrans_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800209 .getgeo = blktrans_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210};
211
212int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
213{
214 struct mtd_blktrans_ops *tr = new->tr;
215 struct list_head *this;
216 int last_devnum = -1;
217 struct gendisk *gd;
218
Jean Delvareb3561ea2007-05-08 00:30:46 -0700219 if (mutex_trylock(&mtd_table_mutex)) {
Ingo Molnar48b19262006-03-31 02:29:41 -0800220 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 BUG();
222 }
223
224 list_for_each(this, &tr->devs) {
225 struct mtd_blktrans_dev *d = list_entry(this, struct mtd_blktrans_dev, list);
226 if (new->devnum == -1) {
227 /* Use first free number */
228 if (d->devnum != last_devnum+1) {
229 /* Found a free devnum. Plug it in here */
230 new->devnum = last_devnum+1;
231 list_add_tail(&new->list, &d->list);
232 goto added;
233 }
234 } else if (d->devnum == new->devnum) {
235 /* Required number taken */
236 return -EBUSY;
237 } else if (d->devnum > new->devnum) {
238 /* Required number was free */
239 list_add_tail(&new->list, &d->list);
240 goto added;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 last_devnum = d->devnum;
243 }
244 if (new->devnum == -1)
245 new->devnum = last_devnum+1;
246
247 if ((new->devnum << tr->part_bits) > 256) {
248 return -EBUSY;
249 }
250
Ingo Molnar48b19262006-03-31 02:29:41 -0800251 mutex_init(&new->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 list_add_tail(&new->list, &tr->devs);
253 added:
254 if (!tr->writesect)
255 new->readonly = 1;
256
257 gd = alloc_disk(1 << tr->part_bits);
258 if (!gd) {
259 list_del(&new->list);
260 return -ENOMEM;
261 }
262 gd->major = tr->major;
263 gd->first_minor = (new->devnum) << tr->part_bits;
264 gd->fops = &mtd_blktrans_ops;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000265
Todd Poynor65a8de32005-07-29 20:42:07 +0100266 if (tr->part_bits)
267 if (new->devnum < 26)
268 snprintf(gd->disk_name, sizeof(gd->disk_name),
269 "%s%c", tr->name, 'a' + new->devnum);
270 else
271 snprintf(gd->disk_name, sizeof(gd->disk_name),
272 "%s%c%c", tr->name,
273 'a' - 1 + new->devnum / 26,
274 'a' + new->devnum % 26);
275 else
276 snprintf(gd->disk_name, sizeof(gd->disk_name),
277 "%s%d", tr->name, new->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 /* 2.5 has capacity in units of 512 bytes while still
280 having BLOCK_SIZE_BITS set to 10. Just to keep us amused. */
Richard Purdie19187672006-10-27 09:09:33 +0100281 set_capacity(gd, (new->size * tr->blksize) >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 gd->private_data = new;
284 new->blkcore_priv = gd;
285 gd->queue = tr->blkcore_priv->rq;
286
287 if (new->readonly)
288 set_disk_ro(gd, 1);
289
290 add_disk(gd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return 0;
293}
294
295int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
296{
Jean Delvareb3561ea2007-05-08 00:30:46 -0700297 if (mutex_trylock(&mtd_table_mutex)) {
Ingo Molnar48b19262006-03-31 02:29:41 -0800298 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 BUG();
300 }
301
302 list_del(&old->list);
303
304 del_gendisk(old->blkcore_priv);
305 put_disk(old->blkcore_priv);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return 0;
308}
309
310static void blktrans_notify_remove(struct mtd_info *mtd)
311{
312 struct list_head *this, *this2, *next;
313
314 list_for_each(this, &blktrans_majors) {
315 struct mtd_blktrans_ops *tr = list_entry(this, struct mtd_blktrans_ops, list);
316
317 list_for_each_safe(this2, next, &tr->devs) {
318 struct mtd_blktrans_dev *dev = list_entry(this2, struct mtd_blktrans_dev, list);
319
320 if (dev->mtd == mtd)
321 tr->remove_dev(dev);
322 }
323 }
324}
325
326static void blktrans_notify_add(struct mtd_info *mtd)
327{
328 struct list_head *this;
329
330 if (mtd->type == MTD_ABSENT)
331 return;
332
333 list_for_each(this, &blktrans_majors) {
334 struct mtd_blktrans_ops *tr = list_entry(this, struct mtd_blktrans_ops, list);
335
336 tr->add_mtd(tr, mtd);
337 }
338
339}
340
341static struct mtd_notifier blktrans_notifier = {
342 .add = blktrans_notify_add,
343 .remove = blktrans_notify_remove,
344};
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
347{
348 int ret, i;
349
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000350 /* Register the notifier if/when the first device type is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 registered, to prevent the link/init ordering from fucking
352 us over. */
353 if (!blktrans_notifier.list.next)
354 register_mtd_user(&blktrans_notifier);
355
Burman Yan95b93a02006-11-15 21:10:29 +0200356 tr->blkcore_priv = kzalloc(sizeof(*tr->blkcore_priv), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (!tr->blkcore_priv)
358 return -ENOMEM;
359
Ingo Molnar48b19262006-03-31 02:29:41 -0800360 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 ret = register_blkdev(tr->major, tr->name);
363 if (ret) {
364 printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n",
365 tr->name, tr->major, ret);
366 kfree(tr->blkcore_priv);
Ingo Molnar48b19262006-03-31 02:29:41 -0800367 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return ret;
369 }
370 spin_lock_init(&tr->blkcore_priv->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 tr->blkcore_priv->rq = blk_init_queue(mtd_blktrans_request, &tr->blkcore_priv->queue_lock);
373 if (!tr->blkcore_priv->rq) {
374 unregister_blkdev(tr->major, tr->name);
375 kfree(tr->blkcore_priv);
Ingo Molnar48b19262006-03-31 02:29:41 -0800376 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return -ENOMEM;
378 }
379
380 tr->blkcore_priv->rq->queuedata = tr;
Richard Purdie19187672006-10-27 09:09:33 +0100381 blk_queue_hardsect_size(tr->blkcore_priv->rq, tr->blksize);
382 tr->blkshift = ffs(tr->blksize) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Christoph Hellwig3e67fe42007-04-22 20:40:57 +0100384 tr->blkcore_priv->thread = kthread_run(mtd_blktrans_thread, tr,
385 "%sd", tr->name);
386 if (IS_ERR(tr->blkcore_priv->thread)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 blk_cleanup_queue(tr->blkcore_priv->rq);
388 unregister_blkdev(tr->major, tr->name);
389 kfree(tr->blkcore_priv);
Ingo Molnar48b19262006-03-31 02:29:41 -0800390 mutex_unlock(&mtd_table_mutex);
Christoph Hellwig3e67fe42007-04-22 20:40:57 +0100391 return PTR_ERR(tr->blkcore_priv->thread);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 INIT_LIST_HEAD(&tr->devs);
395 list_add(&tr->list, &blktrans_majors);
396
397 for (i=0; i<MAX_MTD_DEVICES; i++) {
398 if (mtd_table[i] && mtd_table[i]->type != MTD_ABSENT)
399 tr->add_mtd(tr, mtd_table[i]);
400 }
401
Ingo Molnar48b19262006-03-31 02:29:41 -0800402 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 return 0;
405}
406
407int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr)
408{
409 struct list_head *this, *next;
410
Ingo Molnar48b19262006-03-31 02:29:41 -0800411 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 /* Clean up the kernel thread */
Christoph Hellwig3e67fe42007-04-22 20:40:57 +0100414 kthread_stop(tr->blkcore_priv->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /* Remove it from the list of active majors */
417 list_del(&tr->list);
418
419 list_for_each_safe(this, next, &tr->devs) {
420 struct mtd_blktrans_dev *dev = list_entry(this, struct mtd_blktrans_dev, list);
421 tr->remove_dev(dev);
422 }
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 blk_cleanup_queue(tr->blkcore_priv->rq);
425 unregister_blkdev(tr->major, tr->name);
426
Ingo Molnar48b19262006-03-31 02:29:41 -0800427 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 kfree(tr->blkcore_priv);
430
Eric Sesterhenn373ebfb2006-03-26 18:15:12 +0200431 BUG_ON(!list_empty(&tr->devs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return 0;
433}
434
435static void __exit mtd_blktrans_exit(void)
436{
437 /* No race here -- if someone's currently in register_mtd_blktrans
438 we're screwed anyway. */
439 if (blktrans_notifier.list.next)
440 unregister_mtd_user(&blktrans_notifier);
441}
442
443module_exit(mtd_blktrans_exit);
444
445EXPORT_SYMBOL_GPL(register_mtd_blktrans);
446EXPORT_SYMBOL_GPL(deregister_mtd_blktrans);
447EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev);
448EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev);
449
450MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
451MODULE_LICENSE("GPL");
452MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'");