2 * $Id: mtd_blkdevs.c,v 1.16 2003/06/23 13:34:43 dwmw2 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 <asm/semaphore.h>
23 #include <asm/uaccess.h>
24 #include <linux/devfs_fs_kernel.h>
26 static LIST_HEAD(blktrans_majors);
28 extern struct semaphore mtd_table_mutex;
29 extern struct mtd_info *mtd_table[];
31 struct mtd_blkcore_priv {
32 struct completion thread_dead;
34 wait_queue_head_t thread_wq;
35 struct request_queue rq;
36 spinlock_t queue_lock;
39 static int do_blktrans_request(struct mtd_blktrans_ops *tr,
40 struct mtd_blktrans_dev *dev,
43 unsigned long block, nsect;
47 nsect = req->current_nr_sectors;
50 if (!(req->flags & REQ_CMD))
53 if (block + nsect > get_capacity(req->rq_disk))
56 switch(rq_data_dir(req)) {
58 for (; nsect > 0; nsect--, block++, buf += 512)
59 if (tr->readsect(dev, block, buf))
67 for (; nsect > 0; nsect--, block++, buf += 512)
68 if (tr->writesect(dev, block, buf))
73 printk(KERN_NOTICE "Unknown request %ld\n", rq_data_dir(req));
78 static int mtd_blktrans_thread(void *arg)
80 struct mtd_blktrans_ops *tr = arg;
81 struct request_queue *rq = &tr->blkcore_priv->rq;
83 /* we might get involved when memory gets low, so use PF_MEMALLOC */
84 current->flags |= PF_MEMALLOC;
86 daemonize("%sd", tr->name);
88 /* daemonize() doesn't do this for us since some kernel threads
89 actually want to deal with signals. We can't just call
90 exit_sighand() since that'll cause an oops when we finally
92 spin_lock_irq(¤t->sighand->siglock);
93 sigfillset(¤t->blocked);
95 spin_unlock_irq(¤t->sighand->siglock);
97 spin_lock_irq(rq->queue_lock);
99 while (!tr->blkcore_priv->exiting) {
101 struct mtd_blktrans_dev *dev;
103 DECLARE_WAITQUEUE(wait, current);
105 req = elv_next_request(rq);
108 add_wait_queue(&tr->blkcore_priv->thread_wq, &wait);
109 set_current_state(TASK_INTERRUPTIBLE);
111 spin_unlock_irq(rq->queue_lock);
114 remove_wait_queue(&tr->blkcore_priv->thread_wq, &wait);
116 spin_lock_irq(rq->queue_lock);
121 dev = req->rq_disk->private_data;
124 spin_unlock_irq(rq->queue_lock);
127 res = do_blktrans_request(tr, dev, req);
130 spin_lock_irq(rq->queue_lock);
132 end_request(req, res);
134 complete_and_exit(&tr->blkcore_priv->thread_dead, 0);
137 static void mtd_blktrans_request(struct request_queue *rq)
139 struct mtd_blktrans_ops *tr = rq->queuedata;
140 wake_up(&tr->blkcore_priv->thread_wq);
144 int blktrans_open(struct inode *i, struct file *f)
146 struct mtd_blktrans_dev *dev;
147 struct mtd_blktrans_ops *tr;
150 dev = i->i_bdev->bd_disk->private_data;
153 if (!try_module_get(dev->mtd->owner))
156 if (!try_module_get(tr->owner))
159 /* FIXME: Locking. A hot pluggable device can go away
160 (del_mtd_device can be called for it) without its module
162 dev->mtd->usecount++;
165 if (tr->open && (ret = tr->open(dev))) {
166 dev->mtd->usecount--;
167 module_put(dev->mtd->owner);
169 module_put(tr->owner);
175 int blktrans_release(struct inode *i, struct file *f)
177 struct mtd_blktrans_dev *dev;
178 struct mtd_blktrans_ops *tr;
181 dev = i->i_bdev->bd_disk->private_data;
185 ret = tr->release(dev);
188 dev->mtd->usecount--;
189 module_put(dev->mtd->owner);
190 module_put(tr->owner);
197 static int blktrans_ioctl(struct inode *inode, struct file *file,
198 unsigned int cmd, unsigned long arg)
200 struct mtd_blktrans_dev *dev = inode->i_bdev->bd_disk->private_data;
201 struct mtd_blktrans_ops *tr = dev->tr;
206 return tr->flush(dev);
207 /* The core code did the work, we had nothing to do. */
212 struct hd_geometry g;
215 memset(&g, 0, sizeof(g));
216 ret = tr->getgeo(dev, &g);
220 g.start = get_start_sect(inode->i_bdev);
221 if (copy_to_user((void *)arg, &g, sizeof(g)))
230 struct block_device_operations mtd_blktrans_ops = {
231 .owner = THIS_MODULE,
232 .open = blktrans_open,
233 .release = blktrans_release,
234 .ioctl = blktrans_ioctl,
237 int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
239 struct mtd_blktrans_ops *tr = new->tr;
240 struct list_head *this;
241 int last_devnum = -1;
244 if (!down_trylock(&mtd_table_mutex)) {
245 up(&mtd_table_mutex);
249 list_for_each(this, &tr->devs) {
250 struct mtd_blktrans_dev *d = list_entry(this, struct mtd_blktrans_dev, list);
251 if (new->devnum == -1) {
252 /* Use first free number */
253 if (d->devnum != last_devnum+1) {
254 /* Found a free devnum. Plug it in here */
255 new->devnum = last_devnum+1;
256 list_add_tail(&new->list, &d->list);
259 } else if (d->devnum == new->devnum) {
260 /* Required number taken */
262 } else if (d->devnum > new->devnum) {
263 /* Required number was free */
264 list_add_tail(&new->list, &d->list);
267 last_devnum = d->devnum;
269 if (new->devnum == -1)
270 new->devnum = last_devnum+1;
272 if ((new->devnum << tr->part_bits) > 256) {
276 init_MUTEX(&new->sem);
277 list_add_tail(&new->list, &tr->devs);
282 gd = alloc_disk(1 << tr->part_bits);
284 list_del(&new->list);
287 gd->major = tr->major;
288 gd->first_minor = (new->devnum) << tr->part_bits;
289 gd->fops = &mtd_blktrans_ops;
291 snprintf(gd->disk_name, sizeof(gd->disk_name),
292 "%s%c", tr->name, (tr->part_bits?'a':'0') + new->devnum);
293 snprintf(gd->devfs_name, sizeof(gd->devfs_name),
294 "%s/%c", tr->name, (tr->part_bits?'a':'0') + new->devnum);
296 set_capacity(gd, new->size);
297 gd->private_data = new;
298 new->blkcore_priv = gd;
299 gd->queue = &tr->blkcore_priv->rq;
309 int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
311 if (!down_trylock(&mtd_table_mutex)) {
312 up(&mtd_table_mutex);
316 list_del(&old->list);
318 del_gendisk(old->blkcore_priv);
319 put_disk(old->blkcore_priv);
324 void blktrans_notify_remove(struct mtd_info *mtd)
326 struct list_head *this, *this2, *next;
328 list_for_each(this, &blktrans_majors) {
329 struct mtd_blktrans_ops *tr = list_entry(this, struct mtd_blktrans_ops, list);
331 list_for_each_safe(this2, next, &tr->devs) {
332 struct mtd_blktrans_dev *dev = list_entry(this2, struct mtd_blktrans_dev, list);
340 void blktrans_notify_add(struct mtd_info *mtd)
342 struct list_head *this;
344 if (mtd->type == MTD_ABSENT)
347 list_for_each(this, &blktrans_majors) {
348 struct mtd_blktrans_ops *tr = list_entry(this, struct mtd_blktrans_ops, list);
350 tr->add_mtd(tr, mtd);
355 static struct mtd_notifier blktrans_notifier = {
356 .add = blktrans_notify_add,
357 .remove = blktrans_notify_remove,
360 int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
364 /* Register the notifier if/when the first device type is
365 registered, to prevent the link/init ordering from fucking
367 if (!blktrans_notifier.list.next)
368 register_mtd_user(&blktrans_notifier);
370 tr->blkcore_priv = kmalloc(sizeof(*tr->blkcore_priv), GFP_KERNEL);
371 if (!tr->blkcore_priv)
374 memset(tr->blkcore_priv, 0, sizeof(*tr->blkcore_priv));
376 down(&mtd_table_mutex);
378 ret = register_blkdev(tr->major, tr->name);
380 printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n",
381 tr->name, tr->major, ret);
382 kfree(tr->blkcore_priv);
383 up(&mtd_table_mutex);
386 spin_lock_init(&tr->blkcore_priv->queue_lock);
387 init_completion(&tr->blkcore_priv->thread_dead);
388 init_waitqueue_head(&tr->blkcore_priv->thread_wq);
390 blk_init_queue(&tr->blkcore_priv->rq, mtd_blktrans_request,
391 &tr->blkcore_priv->queue_lock);
392 tr->blkcore_priv->rq.queuedata = tr;
394 ret = kernel_thread(mtd_blktrans_thread, tr,
395 CLONE_FS|CLONE_FILES|CLONE_SIGHAND);
397 blk_cleanup_queue(&tr->blkcore_priv->rq);
398 unregister_blkdev(tr->major, tr->name);
399 kfree(tr->blkcore_priv);
400 up(&mtd_table_mutex);
404 devfs_mk_dir(tr->name);
406 INIT_LIST_HEAD(&tr->devs);
407 list_add(&tr->list, &blktrans_majors);
409 for (i=0; i<MAX_MTD_DEVICES; i++) {
410 if (mtd_table[i] && mtd_table[i]->type != MTD_ABSENT)
411 tr->add_mtd(tr, mtd_table[i]);
414 up(&mtd_table_mutex);
419 int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr)
421 struct list_head *this, *next;
423 down(&mtd_table_mutex);
425 /* Clean up the kernel thread */
426 tr->blkcore_priv->exiting = 1;
427 wake_up(&tr->blkcore_priv->thread_wq);
428 wait_for_completion(&tr->blkcore_priv->thread_dead);
430 /* Remove it from the list of active majors */
433 list_for_each_safe(this, next, &tr->devs) {
434 struct mtd_blktrans_dev *dev = list_entry(this, struct mtd_blktrans_dev, list);
438 devfs_remove(tr->name);
439 blk_cleanup_queue(&tr->blkcore_priv->rq);
440 unregister_blkdev(tr->major, tr->name);
442 up(&mtd_table_mutex);
444 kfree(tr->blkcore_priv);
446 if (!list_empty(&tr->devs))
451 static void __exit mtd_blktrans_exit(void)
453 /* No race here -- if someone's currently in register_mtd_blktrans
454 we're screwed anyway. */
455 if (blktrans_notifier.list.next)
456 unregister_mtd_user(&blktrans_notifier);
459 module_exit(mtd_blktrans_exit);
461 EXPORT_SYMBOL_GPL(register_mtd_blktrans);
462 EXPORT_SYMBOL_GPL(deregister_mtd_blktrans);
463 EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev);
464 EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev);
466 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
467 MODULE_LICENSE("GPL");
468 MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'");