blob: 9ff007c4962c9f3c58868385729a915ba6b28222 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) 2003 David Woodhouse <dwmw2@infradead.org>
3 *
4 * Interface to Linux 2.5 block layer for MTD 'translation layers'.
5 *
6 */
7
8#include <linux/kernel.h>
9#include <linux/slab.h>
10#include <linux/module.h>
11#include <linux/list.h>
12#include <linux/fs.h>
13#include <linux/mtd/blktrans.h>
14#include <linux/mtd/mtd.h>
15#include <linux/blkdev.h>
16#include <linux/blkpg.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070017#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/spinlock.h>
19#include <linux/hdreg.h>
20#include <linux/init.h>
Ingo Molnar48b19262006-03-31 02:29:41 -080021#include <linux/mutex.h>
Eric W. Biederman99f9b242007-04-19 01:58:33 -060022#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Ben Dooks356d70f2007-05-28 20:28:34 +010025#include "mtdcore.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Ben Dooks356d70f2007-05-28 20:28:34 +010027static LIST_HEAD(blktrans_majors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29struct mtd_blkcore_priv {
Christoph Hellwig3e67fe42007-04-22 20:40:57 +010030 struct task_struct *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 struct request_queue *rq;
32 spinlock_t queue_lock;
33};
34
35static int do_blktrans_request(struct mtd_blktrans_ops *tr,
36 struct mtd_blktrans_dev *dev,
37 struct request *req)
38{
39 unsigned long block, nsect;
40 char *buf;
41
Richard Purdie19187672006-10-27 09:09:33 +010042 block = req->sector << 9 >> tr->blkshift;
43 nsect = req->current_nr_sectors << 9 >> tr->blkshift;
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 buf = req->buffer;
46
Jens Axboe4aff5e22006-08-10 08:44:47 +020047 if (!blk_fs_request(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 return 0;
49
Richard Purdie19187672006-10-27 09:09:33 +010050 if (req->sector + req->current_nr_sectors > get_capacity(req->rq_disk))
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 return 0;
52
53 switch(rq_data_dir(req)) {
54 case READ:
Richard Purdie19187672006-10-27 09:09:33 +010055 for (; nsect > 0; nsect--, block++, buf += tr->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 if (tr->readsect(dev, block, buf))
57 return 0;
58 return 1;
59
60 case WRITE:
61 if (!tr->writesect)
62 return 0;
63
Richard Purdie19187672006-10-27 09:09:33 +010064 for (; nsect > 0; nsect--, block++, buf += tr->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if (tr->writesect(dev, block, buf))
66 return 0;
67 return 1;
68
69 default:
Jeff Garzik9a292302006-10-01 12:16:00 -040070 printk(KERN_NOTICE "Unknown request %u\n", rq_data_dir(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return 0;
72 }
73}
74
75static int mtd_blktrans_thread(void *arg)
76{
77 struct mtd_blktrans_ops *tr = arg;
78 struct request_queue *rq = tr->blkcore_priv->rq;
79
80 /* we might get involved when memory gets low, so use PF_MEMALLOC */
Rafael J. Wysocki83144182007-07-17 04:03:35 -070081 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 spin_lock_irq(rq->queue_lock);
Christoph Hellwig3e67fe42007-04-22 20:40:57 +010084 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 struct request *req;
86 struct mtd_blktrans_dev *dev;
87 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 req = elv_next_request(rq);
90
91 if (!req) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 spin_unlock_irq(rq->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 spin_lock_irq(rq->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 continue;
97 }
98
99 dev = req->rq_disk->private_data;
100 tr = dev->tr;
101
102 spin_unlock_irq(rq->queue_lock);
103
Ingo Molnar48b19262006-03-31 02:29:41 -0800104 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 res = do_blktrans_request(tr, dev, req);
Ingo Molnar48b19262006-03-31 02:29:41 -0800106 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 spin_lock_irq(rq->queue_lock);
109
110 end_request(req, res);
111 }
112 spin_unlock_irq(rq->queue_lock);
113
Christoph Hellwig3e67fe42007-04-22 20:40:57 +0100114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117static void mtd_blktrans_request(struct request_queue *rq)
118{
119 struct mtd_blktrans_ops *tr = rq->queuedata;
Christoph Hellwig3e67fe42007-04-22 20:40:57 +0100120 wake_up_process(tr->blkcore_priv->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123
124static int blktrans_open(struct inode *i, struct file *f)
125{
126 struct mtd_blktrans_dev *dev;
127 struct mtd_blktrans_ops *tr;
128 int ret = -ENODEV;
129
130 dev = i->i_bdev->bd_disk->private_data;
131 tr = dev->tr;
132
133 if (!try_module_get(dev->mtd->owner))
134 goto out;
135
136 if (!try_module_get(tr->owner))
137 goto out_tr;
138
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000139 /* FIXME: Locking. A hot pluggable device can go away
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 (del_mtd_device can be called for it) without its module
141 being unloaded. */
142 dev->mtd->usecount++;
143
144 ret = 0;
145 if (tr->open && (ret = tr->open(dev))) {
146 dev->mtd->usecount--;
147 module_put(dev->mtd->owner);
148 out_tr:
149 module_put(tr->owner);
150 }
151 out:
152 return ret;
153}
154
155static int blktrans_release(struct inode *i, struct file *f)
156{
157 struct mtd_blktrans_dev *dev;
158 struct mtd_blktrans_ops *tr;
159 int ret = 0;
160
161 dev = i->i_bdev->bd_disk->private_data;
162 tr = dev->tr;
163
164 if (tr->release)
165 ret = tr->release(dev);
166
167 if (!ret) {
168 dev->mtd->usecount--;
169 module_put(dev->mtd->owner);
170 module_put(tr->owner);
171 }
172
173 return ret;
174}
175
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800176static int blktrans_getgeo(struct block_device *bdev, struct hd_geometry *geo)
177{
178 struct mtd_blktrans_dev *dev = bdev->bd_disk->private_data;
179
180 if (dev->tr->getgeo)
181 return dev->tr->getgeo(dev, geo);
182 return -ENOTTY;
183}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000185static int blktrans_ioctl(struct inode *inode, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 unsigned int cmd, unsigned long arg)
187{
188 struct mtd_blktrans_dev *dev = inode->i_bdev->bd_disk->private_data;
189 struct mtd_blktrans_ops *tr = dev->tr;
190
191 switch (cmd) {
192 case BLKFLSBUF:
193 if (tr->flush)
194 return tr->flush(dev);
195 /* The core code did the work, we had nothing to do. */
196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 default:
198 return -ENOTTY;
199 }
200}
201
Ben Dooks8f026f72007-05-28 19:51:59 +0100202static struct block_device_operations mtd_blktrans_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 .owner = THIS_MODULE,
204 .open = blktrans_open,
205 .release = blktrans_release,
206 .ioctl = blktrans_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800207 .getgeo = blktrans_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
210int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
211{
212 struct mtd_blktrans_ops *tr = new->tr;
Chris Malley71a928c2008-05-19 20:11:50 +0100213 struct mtd_blktrans_dev *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 int last_devnum = -1;
215 struct gendisk *gd;
216
Jean Delvareb3561ea2007-05-08 00:30:46 -0700217 if (mutex_trylock(&mtd_table_mutex)) {
Ingo Molnar48b19262006-03-31 02:29:41 -0800218 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 BUG();
220 }
221
Chris Malley71a928c2008-05-19 20:11:50 +0100222 list_for_each_entry(d, &tr->devs, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (new->devnum == -1) {
224 /* Use first free number */
225 if (d->devnum != last_devnum+1) {
226 /* Found a free devnum. Plug it in here */
227 new->devnum = last_devnum+1;
228 list_add_tail(&new->list, &d->list);
229 goto added;
230 }
231 } else if (d->devnum == new->devnum) {
232 /* Required number taken */
233 return -EBUSY;
234 } else if (d->devnum > new->devnum) {
235 /* Required number was free */
236 list_add_tail(&new->list, &d->list);
237 goto added;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 last_devnum = d->devnum;
240 }
241 if (new->devnum == -1)
242 new->devnum = last_devnum+1;
243
244 if ((new->devnum << tr->part_bits) > 256) {
245 return -EBUSY;
246 }
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 list_add_tail(&new->list, &tr->devs);
249 added:
David Woodhousece37ab42007-12-03 12:46:12 +0000250 mutex_init(&new->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (!tr->writesect)
252 new->readonly = 1;
253
254 gd = alloc_disk(1 << tr->part_bits);
255 if (!gd) {
256 list_del(&new->list);
257 return -ENOMEM;
258 }
259 gd->major = tr->major;
260 gd->first_minor = (new->devnum) << tr->part_bits;
261 gd->fops = &mtd_blktrans_ops;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000262
Todd Poynor65a8de32005-07-29 20:42:07 +0100263 if (tr->part_bits)
264 if (new->devnum < 26)
265 snprintf(gd->disk_name, sizeof(gd->disk_name),
266 "%s%c", tr->name, 'a' + new->devnum);
267 else
268 snprintf(gd->disk_name, sizeof(gd->disk_name),
269 "%s%c%c", tr->name,
270 'a' - 1 + new->devnum / 26,
271 'a' + new->devnum % 26);
272 else
273 snprintf(gd->disk_name, sizeof(gd->disk_name),
274 "%s%d", tr->name, new->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 /* 2.5 has capacity in units of 512 bytes while still
277 having BLOCK_SIZE_BITS set to 10. Just to keep us amused. */
Richard Purdie19187672006-10-27 09:09:33 +0100278 set_capacity(gd, (new->size * tr->blksize) >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 gd->private_data = new;
281 new->blkcore_priv = gd;
282 gd->queue = tr->blkcore_priv->rq;
283
284 if (new->readonly)
285 set_disk_ro(gd, 1);
286
287 add_disk(gd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return 0;
290}
291
292int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
293{
Jean Delvareb3561ea2007-05-08 00:30:46 -0700294 if (mutex_trylock(&mtd_table_mutex)) {
Ingo Molnar48b19262006-03-31 02:29:41 -0800295 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 BUG();
297 }
298
299 list_del(&old->list);
300
301 del_gendisk(old->blkcore_priv);
302 put_disk(old->blkcore_priv);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return 0;
305}
306
307static void blktrans_notify_remove(struct mtd_info *mtd)
308{
Chris Malley71a928c2008-05-19 20:11:50 +0100309 struct mtd_blktrans_ops *tr;
310 struct mtd_blktrans_dev *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Chris Malley71a928c2008-05-19 20:11:50 +0100312 list_for_each_entry(tr, &blktrans_majors, list)
313 list_for_each_entry_safe(dev, next, &tr->devs, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (dev->mtd == mtd)
315 tr->remove_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
318static void blktrans_notify_add(struct mtd_info *mtd)
319{
Chris Malley71a928c2008-05-19 20:11:50 +0100320 struct mtd_blktrans_ops *tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 if (mtd->type == MTD_ABSENT)
323 return;
324
Chris Malley71a928c2008-05-19 20:11:50 +0100325 list_for_each_entry(tr, &blktrans_majors, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 tr->add_mtd(tr, mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329static struct mtd_notifier blktrans_notifier = {
330 .add = blktrans_notify_add,
331 .remove = blktrans_notify_remove,
332};
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
335{
336 int ret, i;
337
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000338 /* Register the notifier if/when the first device type is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 registered, to prevent the link/init ordering from fucking
340 us over. */
341 if (!blktrans_notifier.list.next)
342 register_mtd_user(&blktrans_notifier);
343
Burman Yan95b93a02006-11-15 21:10:29 +0200344 tr->blkcore_priv = kzalloc(sizeof(*tr->blkcore_priv), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (!tr->blkcore_priv)
346 return -ENOMEM;
347
Ingo Molnar48b19262006-03-31 02:29:41 -0800348 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 ret = register_blkdev(tr->major, tr->name);
351 if (ret) {
352 printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n",
353 tr->name, tr->major, ret);
354 kfree(tr->blkcore_priv);
Ingo Molnar48b19262006-03-31 02:29:41 -0800355 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return ret;
357 }
358 spin_lock_init(&tr->blkcore_priv->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 tr->blkcore_priv->rq = blk_init_queue(mtd_blktrans_request, &tr->blkcore_priv->queue_lock);
361 if (!tr->blkcore_priv->rq) {
362 unregister_blkdev(tr->major, tr->name);
363 kfree(tr->blkcore_priv);
Ingo Molnar48b19262006-03-31 02:29:41 -0800364 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return -ENOMEM;
366 }
367
368 tr->blkcore_priv->rq->queuedata = tr;
Richard Purdie19187672006-10-27 09:09:33 +0100369 blk_queue_hardsect_size(tr->blkcore_priv->rq, tr->blksize);
370 tr->blkshift = ffs(tr->blksize) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Christoph Hellwig3e67fe42007-04-22 20:40:57 +0100372 tr->blkcore_priv->thread = kthread_run(mtd_blktrans_thread, tr,
373 "%sd", tr->name);
374 if (IS_ERR(tr->blkcore_priv->thread)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 blk_cleanup_queue(tr->blkcore_priv->rq);
376 unregister_blkdev(tr->major, tr->name);
377 kfree(tr->blkcore_priv);
Ingo Molnar48b19262006-03-31 02:29:41 -0800378 mutex_unlock(&mtd_table_mutex);
Christoph Hellwig3e67fe42007-04-22 20:40:57 +0100379 return PTR_ERR(tr->blkcore_priv->thread);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 INIT_LIST_HEAD(&tr->devs);
383 list_add(&tr->list, &blktrans_majors);
384
385 for (i=0; i<MAX_MTD_DEVICES; i++) {
386 if (mtd_table[i] && mtd_table[i]->type != MTD_ABSENT)
387 tr->add_mtd(tr, mtd_table[i]);
388 }
389
Ingo Molnar48b19262006-03-31 02:29:41 -0800390 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 return 0;
393}
394
395int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr)
396{
Chris Malley71a928c2008-05-19 20:11:50 +0100397 struct mtd_blktrans_dev *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Ingo Molnar48b19262006-03-31 02:29:41 -0800399 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /* Clean up the kernel thread */
Christoph Hellwig3e67fe42007-04-22 20:40:57 +0100402 kthread_stop(tr->blkcore_priv->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 /* Remove it from the list of active majors */
405 list_del(&tr->list);
406
Chris Malley71a928c2008-05-19 20:11:50 +0100407 list_for_each_entry_safe(dev, next, &tr->devs, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 tr->remove_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 blk_cleanup_queue(tr->blkcore_priv->rq);
411 unregister_blkdev(tr->major, tr->name);
412
Ingo Molnar48b19262006-03-31 02:29:41 -0800413 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 kfree(tr->blkcore_priv);
416
Eric Sesterhenn373ebfb2006-03-26 18:15:12 +0200417 BUG_ON(!list_empty(&tr->devs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return 0;
419}
420
421static void __exit mtd_blktrans_exit(void)
422{
423 /* No race here -- if someone's currently in register_mtd_blktrans
424 we're screwed anyway. */
425 if (blktrans_notifier.list.next)
426 unregister_mtd_user(&blktrans_notifier);
427}
428
429module_exit(mtd_blktrans_exit);
430
431EXPORT_SYMBOL_GPL(register_mtd_blktrans);
432EXPORT_SYMBOL_GPL(deregister_mtd_blktrans);
433EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev);
434EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev);
435
436MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
437MODULE_LICENSE("GPL");
438MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'");