2 * $Id: mtd_blkdevs.c,v 1.27 2005/11/07 11:14:20 gleixner Exp $
4 * (C) 2003 David Woodhouse <dwmw2@infradead.org>
6 * Interface to Linux 2.5 block layer for MTD 'translation layers'.
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/list.h>
15 #include <linux/mtd/blktrans.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/blkdev.h>
18 #include <linux/blkpg.h>
19 #include <linux/spinlock.h>
20 #include <linux/hdreg.h>
21 #include <linux/init.h>
22 #include <linux/mutex.h>
23 #include <linux/kthread.h>
24 #include <asm/uaccess.h>
26 static LIST_HEAD(blktrans_majors);
28 extern struct mutex mtd_table_mutex;
29 extern struct mtd_info *mtd_table[];
31 struct mtd_blkcore_priv {
32 struct task_struct *thread;
33 struct request_queue *rq;
34 spinlock_t queue_lock;
37 static int do_blktrans_request(struct mtd_blktrans_ops *tr,
38 struct mtd_blktrans_dev *dev,
41 unsigned long block, nsect;
44 block = req->sector << 9 >> tr->blkshift;
45 nsect = req->current_nr_sectors << 9 >> tr->blkshift;
49 if (!blk_fs_request(req))
52 if (req->sector + req->current_nr_sectors > get_capacity(req->rq_disk))
55 switch(rq_data_dir(req)) {
57 for (; nsect > 0; nsect--, block++, buf += tr->blksize)
58 if (tr->readsect(dev, block, buf))
66 for (; nsect > 0; nsect--, block++, buf += tr->blksize)
67 if (tr->writesect(dev, block, buf))
72 printk(KERN_NOTICE "Unknown request %u\n", rq_data_dir(req));
77 static int mtd_blktrans_thread(void *arg)
79 struct mtd_blktrans_ops *tr = arg;
80 struct request_queue *rq = tr->blkcore_priv->rq;
82 /* we might get involved when memory gets low, so use PF_MEMALLOC */
83 current->flags |= PF_MEMALLOC | PF_NOFREEZE;
85 spin_lock_irq(rq->queue_lock);
86 while (!kthread_should_stop()) {
88 struct mtd_blktrans_dev *dev;
91 req = elv_next_request(rq);
94 set_current_state(TASK_INTERRUPTIBLE);
95 spin_unlock_irq(rq->queue_lock);
97 spin_lock_irq(rq->queue_lock);
101 dev = req->rq_disk->private_data;
104 spin_unlock_irq(rq->queue_lock);
106 mutex_lock(&dev->lock);
107 res = do_blktrans_request(tr, dev, req);
108 mutex_unlock(&dev->lock);
110 spin_lock_irq(rq->queue_lock);
112 end_request(req, res);
114 spin_unlock_irq(rq->queue_lock);
119 static void mtd_blktrans_request(struct request_queue *rq)
121 struct mtd_blktrans_ops *tr = rq->queuedata;
122 wake_up_process(tr->blkcore_priv->thread);
126 static int blktrans_open(struct inode *i, struct file *f)
128 struct mtd_blktrans_dev *dev;
129 struct mtd_blktrans_ops *tr;
132 dev = i->i_bdev->bd_disk->private_data;
135 if (!try_module_get(dev->mtd->owner))
138 if (!try_module_get(tr->owner))
141 /* FIXME: Locking. A hot pluggable device can go away
142 (del_mtd_device can be called for it) without its module
144 dev->mtd->usecount++;
147 if (tr->open && (ret = tr->open(dev))) {
148 dev->mtd->usecount--;
149 module_put(dev->mtd->owner);
151 module_put(tr->owner);
157 static int blktrans_release(struct inode *i, struct file *f)
159 struct mtd_blktrans_dev *dev;
160 struct mtd_blktrans_ops *tr;
163 dev = i->i_bdev->bd_disk->private_data;
167 ret = tr->release(dev);
170 dev->mtd->usecount--;
171 module_put(dev->mtd->owner);
172 module_put(tr->owner);
178 static int blktrans_getgeo(struct block_device *bdev, struct hd_geometry *geo)
180 struct mtd_blktrans_dev *dev = bdev->bd_disk->private_data;
183 return dev->tr->getgeo(dev, geo);
187 static int blktrans_ioctl(struct inode *inode, struct file *file,
188 unsigned int cmd, unsigned long arg)
190 struct mtd_blktrans_dev *dev = inode->i_bdev->bd_disk->private_data;
191 struct mtd_blktrans_ops *tr = dev->tr;
196 return tr->flush(dev);
197 /* The core code did the work, we had nothing to do. */
204 struct block_device_operations mtd_blktrans_ops = {
205 .owner = THIS_MODULE,
206 .open = blktrans_open,
207 .release = blktrans_release,
208 .ioctl = blktrans_ioctl,
209 .getgeo = blktrans_getgeo,
212 int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
214 struct mtd_blktrans_ops *tr = new->tr;
215 struct list_head *this;
216 int last_devnum = -1;
219 if (mutex_trylock(&mtd_table_mutex)) {
220 mutex_unlock(&mtd_table_mutex);
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);
234 } else if (d->devnum == new->devnum) {
235 /* Required number taken */
237 } else if (d->devnum > new->devnum) {
238 /* Required number was free */
239 list_add_tail(&new->list, &d->list);
242 last_devnum = d->devnum;
244 if (new->devnum == -1)
245 new->devnum = last_devnum+1;
247 if ((new->devnum << tr->part_bits) > 256) {
251 mutex_init(&new->lock);
252 list_add_tail(&new->list, &tr->devs);
257 gd = alloc_disk(1 << tr->part_bits);
259 list_del(&new->list);
262 gd->major = tr->major;
263 gd->first_minor = (new->devnum) << tr->part_bits;
264 gd->fops = &mtd_blktrans_ops;
267 if (new->devnum < 26)
268 snprintf(gd->disk_name, sizeof(gd->disk_name),
269 "%s%c", tr->name, 'a' + new->devnum);
271 snprintf(gd->disk_name, sizeof(gd->disk_name),
273 'a' - 1 + new->devnum / 26,
274 'a' + new->devnum % 26);
276 snprintf(gd->disk_name, sizeof(gd->disk_name),
277 "%s%d", tr->name, new->devnum);
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. */
281 set_capacity(gd, (new->size * tr->blksize) >> 9);
283 gd->private_data = new;
284 new->blkcore_priv = gd;
285 gd->queue = tr->blkcore_priv->rq;
295 int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
297 if (mutex_trylock(&mtd_table_mutex)) {
298 mutex_unlock(&mtd_table_mutex);
302 list_del(&old->list);
304 del_gendisk(old->blkcore_priv);
305 put_disk(old->blkcore_priv);
310 static void blktrans_notify_remove(struct mtd_info *mtd)
312 struct list_head *this, *this2, *next;
314 list_for_each(this, &blktrans_majors) {
315 struct mtd_blktrans_ops *tr = list_entry(this, struct mtd_blktrans_ops, list);
317 list_for_each_safe(this2, next, &tr->devs) {
318 struct mtd_blktrans_dev *dev = list_entry(this2, struct mtd_blktrans_dev, list);
326 static void blktrans_notify_add(struct mtd_info *mtd)
328 struct list_head *this;
330 if (mtd->type == MTD_ABSENT)
333 list_for_each(this, &blktrans_majors) {
334 struct mtd_blktrans_ops *tr = list_entry(this, struct mtd_blktrans_ops, list);
336 tr->add_mtd(tr, mtd);
341 static struct mtd_notifier blktrans_notifier = {
342 .add = blktrans_notify_add,
343 .remove = blktrans_notify_remove,
346 int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
350 /* Register the notifier if/when the first device type is
351 registered, to prevent the link/init ordering from fucking
353 if (!blktrans_notifier.list.next)
354 register_mtd_user(&blktrans_notifier);
356 tr->blkcore_priv = kzalloc(sizeof(*tr->blkcore_priv), GFP_KERNEL);
357 if (!tr->blkcore_priv)
360 mutex_lock(&mtd_table_mutex);
362 ret = register_blkdev(tr->major, tr->name);
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);
367 mutex_unlock(&mtd_table_mutex);
370 spin_lock_init(&tr->blkcore_priv->queue_lock);
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);
376 mutex_unlock(&mtd_table_mutex);
380 tr->blkcore_priv->rq->queuedata = tr;
381 blk_queue_hardsect_size(tr->blkcore_priv->rq, tr->blksize);
382 tr->blkshift = ffs(tr->blksize) - 1;
384 tr->blkcore_priv->thread = kthread_run(mtd_blktrans_thread, tr,
386 if (IS_ERR(tr->blkcore_priv->thread)) {
387 blk_cleanup_queue(tr->blkcore_priv->rq);
388 unregister_blkdev(tr->major, tr->name);
389 kfree(tr->blkcore_priv);
390 mutex_unlock(&mtd_table_mutex);
391 return PTR_ERR(tr->blkcore_priv->thread);
394 INIT_LIST_HEAD(&tr->devs);
395 list_add(&tr->list, &blktrans_majors);
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]);
402 mutex_unlock(&mtd_table_mutex);
407 int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr)
409 struct list_head *this, *next;
411 mutex_lock(&mtd_table_mutex);
413 /* Clean up the kernel thread */
414 kthread_stop(tr->blkcore_priv->thread);
416 /* Remove it from the list of active majors */
419 list_for_each_safe(this, next, &tr->devs) {
420 struct mtd_blktrans_dev *dev = list_entry(this, struct mtd_blktrans_dev, list);
424 blk_cleanup_queue(tr->blkcore_priv->rq);
425 unregister_blkdev(tr->major, tr->name);
427 mutex_unlock(&mtd_table_mutex);
429 kfree(tr->blkcore_priv);
431 BUG_ON(!list_empty(&tr->devs));
435 static void __exit mtd_blktrans_exit(void)
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);
443 module_exit(mtd_blktrans_exit);
445 EXPORT_SYMBOL_GPL(register_mtd_blktrans);
446 EXPORT_SYMBOL_GPL(deregister_mtd_blktrans);
447 EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev);
448 EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev);
450 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
451 MODULE_LICENSE("GPL");
452 MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'");