2 * Direct MTD block device access
4 * $Id: mtdblock.c,v 1.51 2001/11/20 11:42:33 dwmw2 Exp $
6 * 02-nov-2000 Nicolas Pitre Added read-modify-write with cache
9 #include <linux/config.h>
10 #include <linux/types.h>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/mtd/mtd.h>
15 #include <linux/mtd/compatmac.h>
17 #define MAJOR_NR MTD_BLOCK_MAJOR
18 #define DEVICE_NAME "mtdblock"
19 #define DEVICE_REQUEST mtdblock_request
20 #define DEVICE_NR(device) (device)
21 #define DEVICE_ON(device)
22 #define DEVICE_OFF(device)
23 #define DEVICE_NO_RANDOM
24 #include <linux/blk.h>
25 /* for old kernels... */
27 #define QUEUE_EMPTY (!CURRENT)
29 #if LINUX_VERSION_CODE < 0x20300
30 #define QUEUE_PLUGGED (blk_dev[MAJOR_NR].plug_tq.sync)
32 #define QUEUE_PLUGGED (blk_dev[MAJOR_NR].request_queue.plugged)
35 #ifdef CONFIG_DEVFS_FS
36 #include <linux/devfs_fs_kernel.h>
37 static void mtd_notify_add(struct mtd_info* mtd);
38 static void mtd_notify_remove(struct mtd_info* mtd);
39 static struct mtd_notifier notifier = {
44 static devfs_handle_t devfs_dir_handle = NULL;
45 static devfs_handle_t devfs_rw_handle[MAX_MTD_DEVICES];
48 static struct mtdblk_dev {
49 struct mtd_info *mtd; /* Locked */
51 struct semaphore cache_sem;
52 unsigned char *cache_data;
53 unsigned long cache_offset;
54 unsigned int cache_size;
55 enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state;
56 } *mtdblks[MAX_MTD_DEVICES];
58 static spinlock_t mtdblks_lock;
60 static int mtd_sizes[MAX_MTD_DEVICES];
61 static int mtd_blksizes[MAX_MTD_DEVICES];
63 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,14)
64 #define BLK_INC_USE_COUNT MOD_INC_USE_COUNT
65 #define BLK_DEC_USE_COUNT MOD_DEC_USE_COUNT
67 #define BLK_INC_USE_COUNT do {} while(0)
68 #define BLK_DEC_USE_COUNT do {} while(0)
74 * Since typical flash erasable sectors are much larger than what Linux's
75 * buffer cache can handle, we must implement read-modify-write on flash
76 * sectors for each block write requests. To avoid over-erasing flash sectors
77 * and to speed things up, we locally cache a whole flash sector while it is
78 * being written to until a different sector is required.
81 static void erase_callback(struct erase_info *done)
83 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
87 static int erase_write (struct mtd_info *mtd, unsigned long pos,
88 int len, const char *buf)
90 struct erase_info erase;
91 DECLARE_WAITQUEUE(wait, current);
92 wait_queue_head_t wait_q;
97 * First, let's erase the flash block.
100 init_waitqueue_head(&wait_q);
102 erase.callback = erase_callback;
105 erase.priv = (u_long)&wait_q;
107 set_current_state(TASK_INTERRUPTIBLE);
108 add_wait_queue(&wait_q, &wait);
110 ret = MTD_ERASE(mtd, &erase);
112 set_current_state(TASK_RUNNING);
113 remove_wait_queue(&wait_q, &wait);
114 printk (KERN_WARNING "mtdblock: erase of region [0x%lx, 0x%x] "
115 "on \"%s\" failed\n",
116 pos, len, mtd->name);
120 schedule(); /* Wait for erase to finish. */
121 remove_wait_queue(&wait_q, &wait);
124 * Next, writhe data to flash.
127 ret = MTD_WRITE (mtd, pos, len, &retlen, buf);
136 static int write_cached_data (struct mtdblk_dev *mtdblk)
138 struct mtd_info *mtd = mtdblk->mtd;
141 if (mtdblk->cache_state != STATE_DIRTY)
144 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: writing cached data for \"%s\" "
145 "at 0x%lx, size 0x%x\n", mtd->name,
146 mtdblk->cache_offset, mtdblk->cache_size);
148 ret = erase_write (mtd, mtdblk->cache_offset,
149 mtdblk->cache_size, mtdblk->cache_data);
154 * Here we could argably set the cache state to STATE_CLEAN.
155 * However this could lead to inconsistency since we will not
156 * be notified if this content is altered on the flash by other
157 * means. Let's declare it empty and leave buffering tasks to
158 * the buffer cache instead.
160 mtdblk->cache_state = STATE_EMPTY;
165 static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos,
166 int len, const char *buf)
168 struct mtd_info *mtd = mtdblk->mtd;
169 unsigned int sect_size = mtdblk->cache_size;
173 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: write on \"%s\" at 0x%lx, size 0x%x\n",
174 mtd->name, pos, len);
177 return MTD_WRITE (mtd, pos, len, &retlen, buf);
180 unsigned long sect_start = (pos/sect_size)*sect_size;
181 unsigned int offset = pos - sect_start;
182 unsigned int size = sect_size - offset;
186 if (size == sect_size) {
188 * We are covering a whole sector. Thus there is no
189 * need to bother with the cache while it may still be
190 * useful for other partial writes.
192 ret = erase_write (mtd, pos, size, buf);
196 /* Partial sector: need to use the cache */
198 if (mtdblk->cache_state == STATE_DIRTY &&
199 mtdblk->cache_offset != sect_start) {
200 ret = write_cached_data(mtdblk);
205 if (mtdblk->cache_state == STATE_EMPTY ||
206 mtdblk->cache_offset != sect_start) {
207 /* fill the cache with the current sector */
208 mtdblk->cache_state = STATE_EMPTY;
209 ret = MTD_READ(mtd, sect_start, sect_size, &retlen, mtdblk->cache_data);
212 if (retlen != sect_size)
215 mtdblk->cache_offset = sect_start;
216 mtdblk->cache_size = sect_size;
217 mtdblk->cache_state = STATE_CLEAN;
220 /* write data to our local cache */
221 memcpy (mtdblk->cache_data + offset, buf, size);
222 mtdblk->cache_state = STATE_DIRTY;
234 static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos,
237 struct mtd_info *mtd = mtdblk->mtd;
238 unsigned int sect_size = mtdblk->cache_size;
242 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: read on \"%s\" at 0x%lx, size 0x%x\n",
243 mtd->name, pos, len);
246 return MTD_READ (mtd, pos, len, &retlen, buf);
249 unsigned long sect_start = (pos/sect_size)*sect_size;
250 unsigned int offset = pos - sect_start;
251 unsigned int size = sect_size - offset;
256 * Check if the requested data is already cached
257 * Read the requested amount of data from our internal cache if it
258 * contains what we want, otherwise we read the data directly
261 if (mtdblk->cache_state != STATE_EMPTY &&
262 mtdblk->cache_offset == sect_start) {
263 memcpy (buf, mtdblk->cache_data + offset, size);
265 ret = MTD_READ (mtd, pos, size, &retlen, buf);
282 static int mtdblock_open(struct inode *inode, struct file *file)
284 struct mtdblk_dev *mtdblk;
285 struct mtd_info *mtd;
288 DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n");
293 dev = MINOR(inode->i_rdev);
294 if (dev >= MAX_MTD_DEVICES)
299 mtd = get_mtd_device(NULL, dev);
302 if (MTD_ABSENT == mtd->type) {
308 spin_lock(&mtdblks_lock);
310 /* If it's already open, no need to piss about. */
312 mtdblks[dev]->count++;
313 spin_unlock(&mtdblks_lock);
318 /* OK, it's not open. Try to find it */
320 /* First we have to drop the lock, because we have to
321 to things which might sleep.
323 spin_unlock(&mtdblks_lock);
325 mtdblk = kmalloc(sizeof(struct mtdblk_dev), GFP_KERNEL);
331 memset(mtdblk, 0, sizeof(*mtdblk));
335 init_MUTEX (&mtdblk->cache_sem);
336 mtdblk->cache_state = STATE_EMPTY;
337 if ((mtdblk->mtd->flags & MTD_CAP_RAM) != MTD_CAP_RAM &&
338 mtdblk->mtd->erasesize) {
339 mtdblk->cache_size = mtdblk->mtd->erasesize;
340 mtdblk->cache_data = vmalloc(mtdblk->mtd->erasesize);
341 if (!mtdblk->cache_data) {
342 put_mtd_device(mtdblk->mtd);
349 /* OK, we've created a new one. Add it to the list. */
351 spin_lock(&mtdblks_lock);
354 /* Another CPU made one at the same time as us. */
355 mtdblks[dev]->count++;
356 spin_unlock(&mtdblks_lock);
357 put_mtd_device(mtdblk->mtd);
358 vfree(mtdblk->cache_data);
363 mtdblks[dev] = mtdblk;
364 mtd_sizes[dev] = mtdblk->mtd->size/1024;
365 if (mtdblk->mtd->erasesize)
366 mtd_blksizes[dev] = mtdblk->mtd->erasesize;
367 if (mtd_blksizes[dev] > PAGE_SIZE)
368 mtd_blksizes[dev] = PAGE_SIZE;
369 set_device_ro (inode->i_rdev, !(mtdblk->mtd->flags & MTD_WRITEABLE));
371 spin_unlock(&mtdblks_lock);
373 DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
378 static release_t mtdblock_release(struct inode *inode, struct file *file)
381 struct mtdblk_dev *mtdblk;
382 DEBUG(MTD_DEBUG_LEVEL1, "mtdblock_release\n");
385 release_return(-ENODEV);
387 dev = MINOR(inode->i_rdev);
388 mtdblk = mtdblks[dev];
390 down(&mtdblk->cache_sem);
391 write_cached_data(mtdblk);
392 up(&mtdblk->cache_sem);
394 spin_lock(&mtdblks_lock);
395 if (!--mtdblk->count) {
396 /* It was the last usage. Free the device */
398 spin_unlock(&mtdblks_lock);
399 if (mtdblk->mtd->sync)
400 mtdblk->mtd->sync(mtdblk->mtd);
401 put_mtd_device(mtdblk->mtd);
402 vfree(mtdblk->cache_data);
405 spin_unlock(&mtdblks_lock);
408 DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
416 * This is a special request_fn because it is executed in a process context
417 * to be able to sleep independently of the caller. The io_request_lock
418 * is held upon entry and exit.
419 * The head of our request queue is considered active so there is no need
420 * to dequeue requests before we are done.
422 static void handle_mtdblock_request(void)
425 struct mtdblk_dev *mtdblk;
431 spin_unlock_irq(&io_request_lock);
432 mtdblk = mtdblks[MINOR(req->rq_dev)];
435 if (MINOR(req->rq_dev) >= MAX_MTD_DEVICES)
436 panic("%s: minor out of bounds", __FUNCTION__);
438 if ((req->sector + req->current_nr_sectors) > (mtdblk->mtd->size >> 9))
441 // Handle the request
447 down(&mtdblk->cache_sem);
448 err = do_cached_read (mtdblk, req->sector << 9,
449 req->current_nr_sectors << 9,
451 up(&mtdblk->cache_sem);
458 if ( !(mtdblk->mtd->flags & MTD_WRITEABLE) )
462 down(&mtdblk->cache_sem);
463 err = do_cached_write (mtdblk, req->sector << 9,
464 req->current_nr_sectors << 9,
466 up(&mtdblk->cache_sem);
473 spin_lock_irq(&io_request_lock);
478 static volatile int leaving = 0;
479 static DECLARE_MUTEX_LOCKED(thread_sem);
480 static DECLARE_WAIT_QUEUE_HEAD(thr_wq);
482 int mtdblock_thread(void *dummy)
484 struct task_struct *tsk = current;
485 DECLARE_WAITQUEUE(wait, tsk);
487 /* we might get involved when memory gets low, so use PF_MEMALLOC */
488 tsk->flags |= PF_MEMALLOC;
489 strcpy(tsk->comm, "mtdblockd");
490 spin_lock_irq(&tsk->sigmask_lock);
491 sigfillset(&tsk->blocked);
492 recalc_sigpending(tsk);
493 spin_unlock_irq(&tsk->sigmask_lock);
497 add_wait_queue(&thr_wq, &wait);
498 set_current_state(TASK_INTERRUPTIBLE);
499 spin_lock_irq(&io_request_lock);
500 if (QUEUE_EMPTY || QUEUE_PLUGGED) {
501 spin_unlock_irq(&io_request_lock);
503 remove_wait_queue(&thr_wq, &wait);
505 remove_wait_queue(&thr_wq, &wait);
506 set_current_state(TASK_RUNNING);
507 handle_mtdblock_request();
508 spin_unlock_irq(&io_request_lock);
516 #if LINUX_VERSION_CODE < 0x20300
517 #define RQFUNC_ARG void
519 #define RQFUNC_ARG request_queue_t *q
522 static void mtdblock_request(RQFUNC_ARG)
524 /* Don't do anything, except wake the thread if necessary */
529 static int mtdblock_ioctl(struct inode * inode, struct file * file,
530 unsigned int cmd, unsigned long arg)
532 struct mtdblk_dev *mtdblk;
534 mtdblk = mtdblks[MINOR(inode->i_rdev)];
542 case BLKGETSIZE: /* Return device size */
543 return put_user((mtdblk->mtd->size >> 9), (unsigned long *) arg);
547 return put_user((u64)mtdblk->mtd->size, (u64 *)arg);
551 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
552 if(!capable(CAP_SYS_ADMIN))
555 fsync_dev(inode->i_rdev);
556 invalidate_buffers(inode->i_rdev);
557 down(&mtdblk->cache_sem);
558 write_cached_data(mtdblk);
559 up(&mtdblk->cache_sem);
560 if (mtdblk->mtd->sync)
561 mtdblk->mtd->sync(mtdblk->mtd);
569 #if LINUX_VERSION_CODE < 0x20326
570 static struct file_operations mtd_fops =
573 ioctl: mtdblock_ioctl,
574 release: mtdblock_release,
579 static struct block_device_operations mtd_fops =
581 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,14)
585 release: mtdblock_release,
586 ioctl: mtdblock_ioctl
590 #ifdef CONFIG_DEVFS_FS
591 /* Notification that a new device has been added. Create the devfs entry for
594 static void mtd_notify_add(struct mtd_info* mtd)
598 if (!mtd || mtd->type == MTD_ABSENT)
601 sprintf(name, "%d", mtd->index);
602 devfs_rw_handle[mtd->index] = devfs_register(devfs_dir_handle, name,
603 DEVFS_FL_DEFAULT, MTD_BLOCK_MAJOR, mtd->index,
604 S_IFBLK | S_IRUGO | S_IWUGO,
608 static void mtd_notify_remove(struct mtd_info* mtd)
610 if (!mtd || mtd->type == MTD_ABSENT)
613 devfs_unregister(devfs_rw_handle[mtd->index]);
617 int __init init_mtdblock(void)
621 spin_lock_init(&mtdblks_lock);
622 #ifdef CONFIG_DEVFS_FS
623 if (devfs_register_blkdev(MTD_BLOCK_MAJOR, DEVICE_NAME, &mtd_fops))
625 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
630 devfs_dir_handle = devfs_mk_dir(NULL, DEVICE_NAME, NULL);
631 register_mtd_user(¬ifier);
633 if (register_blkdev(MAJOR_NR,DEVICE_NAME,&mtd_fops)) {
634 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
640 /* We fill it in at open() time. */
641 for (i=0; i< MAX_MTD_DEVICES; i++) {
643 mtd_blksizes[i] = BLOCK_SIZE;
645 init_waitqueue_head(&thr_wq);
646 /* Allow the block size to default to BLOCK_SIZE. */
647 blksize_size[MAJOR_NR] = mtd_blksizes;
648 blk_size[MAJOR_NR] = mtd_sizes;
650 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), &mtdblock_request);
651 kernel_thread (mtdblock_thread, NULL, CLONE_FS|CLONE_FILES|CLONE_SIGHAND);
655 static void __exit cleanup_mtdblock(void)
660 #ifdef CONFIG_DEVFS_FS
661 unregister_mtd_user(¬ifier);
662 devfs_unregister(devfs_dir_handle);
663 devfs_unregister_blkdev(MTD_BLOCK_MAJOR, DEVICE_NAME);
665 unregister_blkdev(MAJOR_NR,DEVICE_NAME);
667 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
668 blksize_size[MAJOR_NR] = NULL;
669 blk_size[MAJOR_NR] = NULL;
672 module_init(init_mtdblock);
673 module_exit(cleanup_mtdblock);
676 MODULE_LICENSE("GPL");
677 MODULE_AUTHOR("Nicolas Pitre <nico@cam.org> et al.");
678 MODULE_DESCRIPTION("Caching read/erase/writeback block device emulation access to MTD devices");