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 <asm/semaphore.h>
23 #include <asm/uaccess.h>
25 static LIST_HEAD(blktrans_majors
);
27 extern struct semaphore mtd_table_mutex
;
28 extern struct mtd_info
*mtd_table
[];
30 struct mtd_blkcore_priv
{
31 struct completion thread_dead
;
33 wait_queue_head_t thread_wq
;
34 struct request_queue
*rq
;
35 spinlock_t queue_lock
;
38 static int do_blktrans_request(struct mtd_blktrans_ops
*tr
,
39 struct mtd_blktrans_dev
*dev
,
42 unsigned long block
, nsect
;
46 nsect
= req
->current_nr_sectors
;
49 if (!(req
->flags
& REQ_CMD
))
52 if (block
+ nsect
> get_capacity(req
->rq_disk
))
55 switch(rq_data_dir(req
)) {
57 for (; nsect
> 0; nsect
--, block
++, buf
+= 512)
58 if (tr
->readsect(dev
, block
, buf
))
66 for (; nsect
> 0; nsect
--, block
++, buf
+= 512)
67 if (tr
->writesect(dev
, block
, buf
))
72 printk(KERN_NOTICE
"Unknown request %ld\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 daemonize("%sd", tr
->name
);
87 /* daemonize() doesn't do this for us since some kernel threads
88 actually want to deal with signals. We can't just call
89 exit_sighand() since that'll cause an oops when we finally
91 spin_lock_irq(¤t
->sighand
->siglock
);
92 sigfillset(¤t
->blocked
);
94 spin_unlock_irq(¤t
->sighand
->siglock
);
96 spin_lock_irq(rq
->queue_lock
);
98 while (!tr
->blkcore_priv
->exiting
) {
100 struct mtd_blktrans_dev
*dev
;
102 DECLARE_WAITQUEUE(wait
, current
);
104 req
= elv_next_request(rq
);
107 add_wait_queue(&tr
->blkcore_priv
->thread_wq
, &wait
);
108 set_current_state(TASK_INTERRUPTIBLE
);
110 spin_unlock_irq(rq
->queue_lock
);
113 remove_wait_queue(&tr
->blkcore_priv
->thread_wq
, &wait
);
115 spin_lock_irq(rq
->queue_lock
);
120 dev
= req
->rq_disk
->private_data
;
123 spin_unlock_irq(rq
->queue_lock
);
126 res
= do_blktrans_request(tr
, dev
, req
);
129 spin_lock_irq(rq
->queue_lock
);
131 end_request(req
, res
);
133 spin_unlock_irq(rq
->queue_lock
);
135 complete_and_exit(&tr
->blkcore_priv
->thread_dead
, 0);
138 static void mtd_blktrans_request(struct request_queue
*rq
)
140 struct mtd_blktrans_ops
*tr
= rq
->queuedata
;
141 wake_up(&tr
->blkcore_priv
->thread_wq
);
145 static int blktrans_open(struct inode
*i
, struct file
*f
)
147 struct mtd_blktrans_dev
*dev
;
148 struct mtd_blktrans_ops
*tr
;
151 dev
= i
->i_bdev
->bd_disk
->private_data
;
154 if (!try_module_get(dev
->mtd
->owner
))
157 if (!try_module_get(tr
->owner
))
160 /* FIXME: Locking. A hot pluggable device can go away
161 (del_mtd_device can be called for it) without its module
163 dev
->mtd
->usecount
++;
166 if (tr
->open
&& (ret
= tr
->open(dev
))) {
167 dev
->mtd
->usecount
--;
168 module_put(dev
->mtd
->owner
);
170 module_put(tr
->owner
);
176 static int blktrans_release(struct inode
*i
, struct file
*f
)
178 struct mtd_blktrans_dev
*dev
;
179 struct mtd_blktrans_ops
*tr
;
182 dev
= i
->i_bdev
->bd_disk
->private_data
;
186 ret
= tr
->release(dev
);
189 dev
->mtd
->usecount
--;
190 module_put(dev
->mtd
->owner
);
191 module_put(tr
->owner
);
197 static int blktrans_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
199 struct mtd_blktrans_dev
*dev
= bdev
->bd_disk
->private_data
;
202 return dev
->tr
->getgeo(dev
, geo
);
206 static int blktrans_ioctl(struct inode
*inode
, struct file
*file
,
207 unsigned int cmd
, unsigned long arg
)
209 struct mtd_blktrans_dev
*dev
= inode
->i_bdev
->bd_disk
->private_data
;
210 struct mtd_blktrans_ops
*tr
= dev
->tr
;
215 return tr
->flush(dev
);
216 /* The core code did the work, we had nothing to do. */
223 struct block_device_operations mtd_blktrans_ops
= {
224 .owner
= THIS_MODULE
,
225 .open
= blktrans_open
,
226 .release
= blktrans_release
,
227 .ioctl
= blktrans_ioctl
,
228 .getgeo
= blktrans_getgeo
,
231 int add_mtd_blktrans_dev(struct mtd_blktrans_dev
*new)
233 struct mtd_blktrans_ops
*tr
= new->tr
;
234 struct list_head
*this;
235 int last_devnum
= -1;
238 if (!down_trylock(&mtd_table_mutex
)) {
239 up(&mtd_table_mutex
);
243 list_for_each(this, &tr
->devs
) {
244 struct mtd_blktrans_dev
*d
= list_entry(this, struct mtd_blktrans_dev
, list
);
245 if (new->devnum
== -1) {
246 /* Use first free number */
247 if (d
->devnum
!= last_devnum
+1) {
248 /* Found a free devnum. Plug it in here */
249 new->devnum
= last_devnum
+1;
250 list_add_tail(&new->list
, &d
->list
);
253 } else if (d
->devnum
== new->devnum
) {
254 /* Required number taken */
256 } else if (d
->devnum
> new->devnum
) {
257 /* Required number was free */
258 list_add_tail(&new->list
, &d
->list
);
261 last_devnum
= d
->devnum
;
263 if (new->devnum
== -1)
264 new->devnum
= last_devnum
+1;
266 if ((new->devnum
<< tr
->part_bits
) > 256) {
270 init_MUTEX(&new->sem
);
271 list_add_tail(&new->list
, &tr
->devs
);
276 gd
= alloc_disk(1 << tr
->part_bits
);
278 list_del(&new->list
);
281 gd
->major
= tr
->major
;
282 gd
->first_minor
= (new->devnum
) << tr
->part_bits
;
283 gd
->fops
= &mtd_blktrans_ops
;
286 if (new->devnum
< 26)
287 snprintf(gd
->disk_name
, sizeof(gd
->disk_name
),
288 "%s%c", tr
->name
, 'a' + new->devnum
);
290 snprintf(gd
->disk_name
, sizeof(gd
->disk_name
),
292 'a' - 1 + new->devnum
/ 26,
293 'a' + new->devnum
% 26);
295 snprintf(gd
->disk_name
, sizeof(gd
->disk_name
),
296 "%s%d", tr
->name
, new->devnum
);
298 /* 2.5 has capacity in units of 512 bytes while still
299 having BLOCK_SIZE_BITS set to 10. Just to keep us amused. */
300 set_capacity(gd
, (new->size
* new->blksize
) >> 9);
302 gd
->private_data
= new;
303 new->blkcore_priv
= gd
;
304 gd
->queue
= tr
->blkcore_priv
->rq
;
314 int del_mtd_blktrans_dev(struct mtd_blktrans_dev
*old
)
316 if (!down_trylock(&mtd_table_mutex
)) {
317 up(&mtd_table_mutex
);
321 list_del(&old
->list
);
323 del_gendisk(old
->blkcore_priv
);
324 put_disk(old
->blkcore_priv
);
329 static void blktrans_notify_remove(struct mtd_info
*mtd
)
331 struct list_head
*this, *this2
, *next
;
333 list_for_each(this, &blktrans_majors
) {
334 struct mtd_blktrans_ops
*tr
= list_entry(this, struct mtd_blktrans_ops
, list
);
336 list_for_each_safe(this2
, next
, &tr
->devs
) {
337 struct mtd_blktrans_dev
*dev
= list_entry(this2
, struct mtd_blktrans_dev
, list
);
345 static void blktrans_notify_add(struct mtd_info
*mtd
)
347 struct list_head
*this;
349 if (mtd
->type
== MTD_ABSENT
)
352 list_for_each(this, &blktrans_majors
) {
353 struct mtd_blktrans_ops
*tr
= list_entry(this, struct mtd_blktrans_ops
, list
);
355 tr
->add_mtd(tr
, mtd
);
360 static struct mtd_notifier blktrans_notifier
= {
361 .add
= blktrans_notify_add
,
362 .remove
= blktrans_notify_remove
,
365 int register_mtd_blktrans(struct mtd_blktrans_ops
*tr
)
369 /* Register the notifier if/when the first device type is
370 registered, to prevent the link/init ordering from fucking
372 if (!blktrans_notifier
.list
.next
)
373 register_mtd_user(&blktrans_notifier
);
375 tr
->blkcore_priv
= kmalloc(sizeof(*tr
->blkcore_priv
), GFP_KERNEL
);
376 if (!tr
->blkcore_priv
)
379 memset(tr
->blkcore_priv
, 0, sizeof(*tr
->blkcore_priv
));
381 down(&mtd_table_mutex
);
383 ret
= register_blkdev(tr
->major
, tr
->name
);
385 printk(KERN_WARNING
"Unable to register %s block device on major %d: %d\n",
386 tr
->name
, tr
->major
, ret
);
387 kfree(tr
->blkcore_priv
);
388 up(&mtd_table_mutex
);
391 spin_lock_init(&tr
->blkcore_priv
->queue_lock
);
392 init_completion(&tr
->blkcore_priv
->thread_dead
);
393 init_waitqueue_head(&tr
->blkcore_priv
->thread_wq
);
395 tr
->blkcore_priv
->rq
= blk_init_queue(mtd_blktrans_request
, &tr
->blkcore_priv
->queue_lock
);
396 if (!tr
->blkcore_priv
->rq
) {
397 unregister_blkdev(tr
->major
, tr
->name
);
398 kfree(tr
->blkcore_priv
);
399 up(&mtd_table_mutex
);
403 tr
->blkcore_priv
->rq
->queuedata
= tr
;
405 ret
= kernel_thread(mtd_blktrans_thread
, tr
, CLONE_KERNEL
);
407 blk_cleanup_queue(tr
->blkcore_priv
->rq
);
408 unregister_blkdev(tr
->major
, tr
->name
);
409 kfree(tr
->blkcore_priv
);
410 up(&mtd_table_mutex
);
414 INIT_LIST_HEAD(&tr
->devs
);
415 list_add(&tr
->list
, &blktrans_majors
);
417 for (i
=0; i
<MAX_MTD_DEVICES
; i
++) {
418 if (mtd_table
[i
] && mtd_table
[i
]->type
!= MTD_ABSENT
)
419 tr
->add_mtd(tr
, mtd_table
[i
]);
422 up(&mtd_table_mutex
);
427 int deregister_mtd_blktrans(struct mtd_blktrans_ops
*tr
)
429 struct list_head
*this, *next
;
431 down(&mtd_table_mutex
);
433 /* Clean up the kernel thread */
434 tr
->blkcore_priv
->exiting
= 1;
435 wake_up(&tr
->blkcore_priv
->thread_wq
);
436 wait_for_completion(&tr
->blkcore_priv
->thread_dead
);
438 /* Remove it from the list of active majors */
441 list_for_each_safe(this, next
, &tr
->devs
) {
442 struct mtd_blktrans_dev
*dev
= list_entry(this, struct mtd_blktrans_dev
, list
);
446 blk_cleanup_queue(tr
->blkcore_priv
->rq
);
447 unregister_blkdev(tr
->major
, tr
->name
);
449 up(&mtd_table_mutex
);
451 kfree(tr
->blkcore_priv
);
453 if (!list_empty(&tr
->devs
))
458 static void __exit
mtd_blktrans_exit(void)
460 /* No race here -- if someone's currently in register_mtd_blktrans
461 we're screwed anyway. */
462 if (blktrans_notifier
.list
.next
)
463 unregister_mtd_user(&blktrans_notifier
);
466 module_exit(mtd_blktrans_exit
);
468 EXPORT_SYMBOL_GPL(register_mtd_blktrans
);
469 EXPORT_SYMBOL_GPL(deregister_mtd_blktrans
);
470 EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev
);
471 EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev
);
473 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
474 MODULE_LICENSE("GPL");
475 MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'");