2 * (C) 2003 David Woodhouse <dwmw2@infradead.org>
4 * Interface to Linux 2.5 block layer for MTD 'translation layers'.
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/module.h>
11 #include <linux/list.h>
13 #include <linux/mtd/blktrans.h>
14 #include <linux/mtd/mtd.h>
15 #include <linux/blkdev.h>
16 #include <linux/blkpg.h>
17 #include <linux/spinlock.h>
18 #include <linux/hdreg.h>
19 #include <linux/init.h>
20 #include <linux/mutex.h>
21 #include <linux/kthread.h>
22 #include <asm/uaccess.h>
26 static LIST_HEAD(blktrans_majors
);
27 static DEFINE_MUTEX(blktrans_ref_mutex
);
29 void blktrans_dev_release(struct kref
*kref
)
31 struct mtd_blktrans_dev
*dev
=
32 container_of(kref
, struct mtd_blktrans_dev
, ref
);
34 dev
->disk
->private_data
= NULL
;
35 blk_cleanup_queue(dev
->rq
);
41 static struct mtd_blktrans_dev
*blktrans_dev_get(struct gendisk
*disk
)
43 struct mtd_blktrans_dev
*dev
;
45 mutex_lock(&blktrans_ref_mutex
);
46 dev
= disk
->private_data
;
52 mutex_unlock(&blktrans_ref_mutex
);
56 void blktrans_dev_put(struct mtd_blktrans_dev
*dev
)
58 mutex_lock(&blktrans_ref_mutex
);
59 kref_put(&dev
->ref
, blktrans_dev_release
);
60 mutex_unlock(&blktrans_ref_mutex
);
64 static int do_blktrans_request(struct mtd_blktrans_ops
*tr
,
65 struct mtd_blktrans_dev
*dev
,
68 unsigned long block
, nsect
;
71 block
= blk_rq_pos(req
) << 9 >> tr
->blkshift
;
72 nsect
= blk_rq_cur_bytes(req
) >> tr
->blkshift
;
76 if (!blk_fs_request(req
))
79 if (blk_rq_pos(req
) + blk_rq_cur_sectors(req
) >
80 get_capacity(req
->rq_disk
))
83 if (blk_discard_rq(req
))
84 return tr
->discard(dev
, block
, nsect
);
86 switch(rq_data_dir(req
)) {
88 for (; nsect
> 0; nsect
--, block
++, buf
+= tr
->blksize
)
89 if (tr
->readsect(dev
, block
, buf
))
91 rq_flush_dcache_pages(req
);
97 rq_flush_dcache_pages(req
);
98 for (; nsect
> 0; nsect
--, block
++, buf
+= tr
->blksize
)
99 if (tr
->writesect(dev
, block
, buf
))
103 printk(KERN_NOTICE
"Unknown request %u\n", rq_data_dir(req
));
108 static int mtd_blktrans_thread(void *arg
)
110 struct mtd_blktrans_dev
*dev
= arg
;
111 struct request_queue
*rq
= dev
->rq
;
112 struct request
*req
= NULL
;
114 spin_lock_irq(rq
->queue_lock
);
116 while (!kthread_should_stop()) {
119 if (!req
&& !(req
= blk_fetch_request(rq
))) {
120 set_current_state(TASK_INTERRUPTIBLE
);
121 spin_unlock_irq(rq
->queue_lock
);
123 spin_lock_irq(rq
->queue_lock
);
127 spin_unlock_irq(rq
->queue_lock
);
129 mutex_lock(&dev
->lock
);
130 res
= do_blktrans_request(dev
->tr
, dev
, req
);
131 mutex_unlock(&dev
->lock
);
133 spin_lock_irq(rq
->queue_lock
);
135 if (!__blk_end_request_cur(req
, res
))
140 __blk_end_request_all(req
, -EIO
);
142 spin_unlock_irq(rq
->queue_lock
);
147 static void mtd_blktrans_request(struct request_queue
*rq
)
149 struct mtd_blktrans_dev
*dev
;
150 struct request
*req
= NULL
;
155 while ((req
= blk_fetch_request(rq
)) != NULL
)
156 __blk_end_request_all(req
, -ENODEV
);
158 wake_up_process(dev
->thread
);
161 static int blktrans_open(struct block_device
*bdev
, fmode_t mode
)
163 struct mtd_blktrans_dev
*dev
= blktrans_dev_get(bdev
->bd_disk
);
169 mutex_lock(&dev
->lock
);
176 ret
= !dev
->open
++ && dev
->tr
->open
? dev
->tr
->open(dev
) : 0;
178 /* Take another reference on the device so it won't go away till
183 mutex_unlock(&dev
->lock
);
184 blktrans_dev_put(dev
);
188 static int blktrans_release(struct gendisk
*disk
, fmode_t mode
)
190 struct mtd_blktrans_dev
*dev
= blktrans_dev_get(disk
);
196 mutex_lock(&dev
->lock
);
198 /* Release one reference, we sure its not the last one here*/
199 kref_put(&dev
->ref
, blktrans_dev_release
);
204 ret
= !--dev
->open
&& dev
->tr
->release
? dev
->tr
->release(dev
) : 0;
206 mutex_unlock(&dev
->lock
);
207 blktrans_dev_put(dev
);
211 static int blktrans_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
213 struct mtd_blktrans_dev
*dev
= blktrans_dev_get(bdev
->bd_disk
);
219 mutex_lock(&dev
->lock
);
224 ret
= dev
->tr
->getgeo
? dev
->tr
->getgeo(dev
, geo
) : 0;
226 mutex_unlock(&dev
->lock
);
227 blktrans_dev_put(dev
);
231 static int blktrans_ioctl(struct block_device
*bdev
, fmode_t mode
,
232 unsigned int cmd
, unsigned long arg
)
234 struct mtd_blktrans_dev
*dev
= blktrans_dev_get(bdev
->bd_disk
);
240 mutex_lock(&dev
->lock
);
247 ret
= dev
->tr
->flush
? dev
->tr
->flush(dev
) : 0;
252 mutex_unlock(&dev
->lock
);
253 blktrans_dev_put(dev
);
257 static const struct block_device_operations mtd_blktrans_ops
= {
258 .owner
= THIS_MODULE
,
259 .open
= blktrans_open
,
260 .release
= blktrans_release
,
261 .locked_ioctl
= blktrans_ioctl
,
262 .getgeo
= blktrans_getgeo
,
265 int add_mtd_blktrans_dev(struct mtd_blktrans_dev
*new)
267 struct mtd_blktrans_ops
*tr
= new->tr
;
268 struct mtd_blktrans_dev
*d
;
269 int last_devnum
= -1;
273 if (mutex_trylock(&mtd_table_mutex
)) {
274 mutex_unlock(&mtd_table_mutex
);
278 mutex_lock(&blktrans_ref_mutex
);
279 list_for_each_entry(d
, &tr
->devs
, list
) {
280 if (new->devnum
== -1) {
281 /* Use first free number */
282 if (d
->devnum
!= last_devnum
+1) {
283 /* Found a free devnum. Plug it in here */
284 new->devnum
= last_devnum
+1;
285 list_add_tail(&new->list
, &d
->list
);
288 } else if (d
->devnum
== new->devnum
) {
289 /* Required number taken */
290 mutex_unlock(&blktrans_ref_mutex
);
292 } else if (d
->devnum
> new->devnum
) {
293 /* Required number was free */
294 list_add_tail(&new->list
, &d
->list
);
297 last_devnum
= d
->devnum
;
301 if (new->devnum
== -1)
302 new->devnum
= last_devnum
+1;
304 /* Check that the device and any partitions will get valid
305 * minor numbers and that the disk naming code below can cope
306 * with this number. */
307 if (new->devnum
> (MINORMASK
>> tr
->part_bits
) ||
308 (tr
->part_bits
&& new->devnum
>= 27 * 26)) {
309 mutex_unlock(&blktrans_ref_mutex
);
313 list_add_tail(&new->list
, &tr
->devs
);
315 mutex_unlock(&blktrans_ref_mutex
);
317 mutex_init(&new->lock
);
318 kref_init(&new->ref
);
324 gd
= alloc_disk(1 << tr
->part_bits
);
330 gd
->private_data
= new;
331 gd
->major
= tr
->major
;
332 gd
->first_minor
= (new->devnum
) << tr
->part_bits
;
333 gd
->fops
= &mtd_blktrans_ops
;
336 if (new->devnum
< 26)
337 snprintf(gd
->disk_name
, sizeof(gd
->disk_name
),
338 "%s%c", tr
->name
, 'a' + new->devnum
);
340 snprintf(gd
->disk_name
, sizeof(gd
->disk_name
),
342 'a' - 1 + new->devnum
/ 26,
343 'a' + new->devnum
% 26);
345 snprintf(gd
->disk_name
, sizeof(gd
->disk_name
),
346 "%s%d", tr
->name
, new->devnum
);
348 set_capacity(gd
, (new->size
* tr
->blksize
) >> 9);
350 /* Create the request queue */
351 spin_lock_init(&new->queue_lock
);
352 new->rq
= blk_init_queue(mtd_blktrans_request
, &new->queue_lock
);
357 new->rq
->queuedata
= new;
358 blk_queue_logical_block_size(new->rq
, tr
->blksize
);
361 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
,
366 __get_mtd_device(new->mtd
);
367 __module_get(tr
->owner
);
369 /* Create processing thread */
370 /* TODO: workqueue ? */
371 new->thread
= kthread_run(mtd_blktrans_thread
, new,
372 "%s%d", tr
->name
, new->mtd
->index
);
373 if (IS_ERR(new->thread
)) {
374 ret
= PTR_ERR(new->thread
);
377 gd
->driverfs_dev
= &new->mtd
->dev
;
384 if (new->disk_attributes
) {
385 ret
= sysfs_create_group(&disk_to_dev(gd
)->kobj
,
386 new->disk_attributes
);
391 module_put(tr
->owner
);
392 __put_mtd_device(new->mtd
);
393 blk_cleanup_queue(new->rq
);
397 list_del(&new->list
);
403 int del_mtd_blktrans_dev(struct mtd_blktrans_dev
*old
)
407 if (mutex_trylock(&mtd_table_mutex
)) {
408 mutex_unlock(&mtd_table_mutex
);
412 /* Stop new requests to arrive */
413 del_gendisk(old
->disk
);
415 if (old
->disk_attributes
)
416 sysfs_remove_group(&disk_to_dev(old
->disk
)->kobj
,
417 old
->disk_attributes
);
419 /* Stop the thread */
420 kthread_stop(old
->thread
);
422 /* Kill current requests */
423 spin_lock_irqsave(&old
->queue_lock
, flags
);
424 old
->rq
->queuedata
= NULL
;
425 blk_start_queue(old
->rq
);
426 spin_unlock_irqrestore(&old
->queue_lock
, flags
);
428 /* Ask trans driver for release to the mtd device */
429 mutex_lock(&old
->lock
);
430 if (old
->open
&& old
->tr
->release
) {
431 old
->tr
->release(old
);
435 __put_mtd_device(old
->mtd
);
436 module_put(old
->tr
->owner
);
438 /* At that point, we don't touch the mtd anymore */
441 mutex_unlock(&old
->lock
);
442 blktrans_dev_put(old
);
446 static void blktrans_notify_remove(struct mtd_info
*mtd
)
448 struct mtd_blktrans_ops
*tr
;
449 struct mtd_blktrans_dev
*dev
, *next
;
451 list_for_each_entry(tr
, &blktrans_majors
, list
)
452 list_for_each_entry_safe(dev
, next
, &tr
->devs
, list
)
457 static void blktrans_notify_add(struct mtd_info
*mtd
)
459 struct mtd_blktrans_ops
*tr
;
461 if (mtd
->type
== MTD_ABSENT
)
464 list_for_each_entry(tr
, &blktrans_majors
, list
)
465 tr
->add_mtd(tr
, mtd
);
468 static struct mtd_notifier blktrans_notifier
= {
469 .add
= blktrans_notify_add
,
470 .remove
= blktrans_notify_remove
,
473 int register_mtd_blktrans(struct mtd_blktrans_ops
*tr
)
475 struct mtd_info
*mtd
;
478 /* Register the notifier if/when the first device type is
479 registered, to prevent the link/init ordering from fucking
481 if (!blktrans_notifier
.list
.next
)
482 register_mtd_user(&blktrans_notifier
);
485 mutex_lock(&mtd_table_mutex
);
487 ret
= register_blkdev(tr
->major
, tr
->name
);
489 printk(KERN_WARNING
"Unable to register %s block device on major %d: %d\n",
490 tr
->name
, tr
->major
, ret
);
491 mutex_unlock(&mtd_table_mutex
);
495 tr
->blkshift
= ffs(tr
->blksize
) - 1;
497 INIT_LIST_HEAD(&tr
->devs
);
498 list_add(&tr
->list
, &blktrans_majors
);
500 mtd_for_each_device(mtd
)
501 if (mtd
->type
!= MTD_ABSENT
)
502 tr
->add_mtd(tr
, mtd
);
504 mutex_unlock(&mtd_table_mutex
);
508 int deregister_mtd_blktrans(struct mtd_blktrans_ops
*tr
)
510 struct mtd_blktrans_dev
*dev
, *next
;
512 mutex_lock(&mtd_table_mutex
);
514 /* Remove it from the list of active majors */
517 list_for_each_entry_safe(dev
, next
, &tr
->devs
, list
)
520 unregister_blkdev(tr
->major
, tr
->name
);
521 mutex_unlock(&mtd_table_mutex
);
523 BUG_ON(!list_empty(&tr
->devs
));
527 static void __exit
mtd_blktrans_exit(void)
529 /* No race here -- if someone's currently in register_mtd_blktrans
530 we're screwed anyway. */
531 if (blktrans_notifier
.list
.next
)
532 unregister_mtd_user(&blktrans_notifier
);
535 module_exit(mtd_blktrans_exit
);
537 EXPORT_SYMBOL_GPL(register_mtd_blktrans
);
538 EXPORT_SYMBOL_GPL(deregister_mtd_blktrans
);
539 EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev
);
540 EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev
);
542 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
543 MODULE_LICENSE("GPL");
544 MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'");