2 * Direct MTD block device access
4 * $Id: mtdblock.c,v 1.64 2003/10/04 17:14:14 dwmw2 Exp $
6 * (C) 2000-2003 Nicolas Pitre <nico@cam.org>
7 * (C) 1999-2003 David Woodhouse <dwmw2@infradead.org>
10 #include <linux/config.h>
11 #include <linux/types.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/vmalloc.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/blktrans.h>
21 static struct mtdblk_dev {
24 struct semaphore cache_sem;
25 unsigned char *cache_data;
26 unsigned long cache_offset;
27 unsigned int cache_size;
28 enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state;
29 } *mtdblks[MAX_MTD_DEVICES];
34 * Since typical flash erasable sectors are much larger than what Linux's
35 * buffer cache can handle, we must implement read-modify-write on flash
36 * sectors for each block write requests. To avoid over-erasing flash sectors
37 * and to speed things up, we locally cache a whole flash sector while it is
38 * being written to until a different sector is required.
41 static void erase_callback(struct erase_info *done)
43 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
47 static int erase_write (struct mtd_info *mtd, unsigned long pos,
48 int len, const char *buf)
50 struct erase_info erase;
51 DECLARE_WAITQUEUE(wait, current);
52 wait_queue_head_t wait_q;
57 * First, let's erase the flash block.
60 init_waitqueue_head(&wait_q);
62 erase.callback = erase_callback;
65 erase.priv = (u_long)&wait_q;
67 set_current_state(TASK_INTERRUPTIBLE);
68 add_wait_queue(&wait_q, &wait);
70 ret = MTD_ERASE(mtd, &erase);
72 set_current_state(TASK_RUNNING);
73 remove_wait_queue(&wait_q, &wait);
74 printk (KERN_WARNING "mtdblock: erase of region [0x%lx, 0x%x] "
80 schedule(); /* Wait for erase to finish. */
81 remove_wait_queue(&wait_q, &wait);
84 * Next, writhe data to flash.
87 ret = MTD_WRITE (mtd, pos, len, &retlen, buf);
96 static int write_cached_data (struct mtdblk_dev *mtdblk)
98 struct mtd_info *mtd = mtdblk->mtd;
101 if (mtdblk->cache_state != STATE_DIRTY)
104 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: writing cached data for \"%s\" "
105 "at 0x%lx, size 0x%x\n", mtd->name,
106 mtdblk->cache_offset, mtdblk->cache_size);
108 ret = erase_write (mtd, mtdblk->cache_offset,
109 mtdblk->cache_size, mtdblk->cache_data);
114 * Here we could argubly set the cache state to STATE_CLEAN.
115 * However this could lead to inconsistency since we will not
116 * be notified if this content is altered on the flash by other
117 * means. Let's declare it empty and leave buffering tasks to
118 * the buffer cache instead.
120 mtdblk->cache_state = STATE_EMPTY;
125 static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos,
126 int len, const char *buf)
128 struct mtd_info *mtd = mtdblk->mtd;
129 unsigned int sect_size = mtdblk->cache_size;
133 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: write on \"%s\" at 0x%lx, size 0x%x\n",
134 mtd->name, pos, len);
137 return MTD_WRITE (mtd, pos, len, &retlen, buf);
140 unsigned long sect_start = (pos/sect_size)*sect_size;
141 unsigned int offset = pos - sect_start;
142 unsigned int size = sect_size - offset;
146 if (size == sect_size) {
148 * We are covering a whole sector. Thus there is no
149 * need to bother with the cache while it may still be
150 * useful for other partial writes.
152 ret = erase_write (mtd, pos, size, buf);
156 /* Partial sector: need to use the cache */
158 if (mtdblk->cache_state == STATE_DIRTY &&
159 mtdblk->cache_offset != sect_start) {
160 ret = write_cached_data(mtdblk);
165 if (mtdblk->cache_state == STATE_EMPTY ||
166 mtdblk->cache_offset != sect_start) {
167 /* fill the cache with the current sector */
168 mtdblk->cache_state = STATE_EMPTY;
169 ret = MTD_READ(mtd, sect_start, sect_size, &retlen, mtdblk->cache_data);
172 if (retlen != sect_size)
175 mtdblk->cache_offset = sect_start;
176 mtdblk->cache_size = sect_size;
177 mtdblk->cache_state = STATE_CLEAN;
180 /* write data to our local cache */
181 memcpy (mtdblk->cache_data + offset, buf, size);
182 mtdblk->cache_state = STATE_DIRTY;
194 static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos,
197 struct mtd_info *mtd = mtdblk->mtd;
198 unsigned int sect_size = mtdblk->cache_size;
202 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: read on \"%s\" at 0x%lx, size 0x%x\n",
203 mtd->name, pos, len);
206 return MTD_READ (mtd, pos, len, &retlen, buf);
209 unsigned long sect_start = (pos/sect_size)*sect_size;
210 unsigned int offset = pos - sect_start;
211 unsigned int size = sect_size - offset;
216 * Check if the requested data is already cached
217 * Read the requested amount of data from our internal cache if it
218 * contains what we want, otherwise we read the data directly
221 if (mtdblk->cache_state != STATE_EMPTY &&
222 mtdblk->cache_offset == sect_start) {
223 memcpy (buf, mtdblk->cache_data + offset, size);
225 ret = MTD_READ (mtd, pos, size, &retlen, buf);
240 static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
241 unsigned long block, char *buf)
243 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
244 return do_cached_read(mtdblk, block<<9, 512, buf);
247 static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
248 unsigned long block, char *buf)
250 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
251 if (unlikely(!mtdblk->cache_data)) {
252 mtdblk->cache_data = vmalloc(mtdblk->mtd->erasesize);
253 if (!mtdblk->cache_data)
255 /* -EINTR is not really correct, but it is the best match
256 * documented in man 2 write for all cases. We could also
257 * return -EAGAIN sometimes, but why bother?
260 return do_cached_write(mtdblk, block<<9, 512, buf);
263 static int mtdblock_open(struct mtd_blktrans_dev *mbd)
265 struct mtdblk_dev *mtdblk;
266 struct mtd_info *mtd = mbd->mtd;
267 int dev = mbd->devnum;
269 DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n");
272 mtdblks[dev]->count++;
276 /* OK, it's not open. Create cache info for it */
277 mtdblk = kmalloc(sizeof(struct mtdblk_dev), GFP_KERNEL);
281 memset(mtdblk, 0, sizeof(*mtdblk));
285 init_MUTEX (&mtdblk->cache_sem);
286 mtdblk->cache_state = STATE_EMPTY;
287 if ((mtdblk->mtd->flags & MTD_CAP_RAM) != MTD_CAP_RAM &&
288 mtdblk->mtd->erasesize) {
289 mtdblk->cache_size = mtdblk->mtd->erasesize;
290 mtdblk->cache_data = NULL;
293 mtdblks[dev] = mtdblk;
295 DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
300 static int mtdblock_release(struct mtd_blktrans_dev *mbd)
302 int dev = mbd->devnum;
303 struct mtdblk_dev *mtdblk = mtdblks[dev];
305 DEBUG(MTD_DEBUG_LEVEL1, "mtdblock_release\n");
307 down(&mtdblk->cache_sem);
308 write_cached_data(mtdblk);
309 up(&mtdblk->cache_sem);
311 if (!--mtdblk->count) {
312 /* It was the last usage. Free the device */
314 if (mtdblk->mtd->sync)
315 mtdblk->mtd->sync(mtdblk->mtd);
316 vfree(mtdblk->cache_data);
319 DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
324 static int mtdblock_flush(struct mtd_blktrans_dev *dev)
326 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
328 down(&mtdblk->cache_sem);
329 write_cached_data(mtdblk);
330 up(&mtdblk->cache_sem);
332 if (mtdblk->mtd->sync)
333 mtdblk->mtd->sync(mtdblk->mtd);
337 static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
339 struct mtd_blktrans_dev *dev = kmalloc(sizeof(*dev), GFP_KERNEL);
344 memset(dev, 0, sizeof(*dev));
347 dev->devnum = mtd->index;
349 dev->size = mtd->size >> 9;
352 if (!(mtd->flags & MTD_WRITEABLE))
355 add_mtd_blktrans_dev(dev);
358 static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
360 del_mtd_blktrans_dev(dev);
364 struct mtd_blktrans_ops mtdblock_tr = {
368 .open = mtdblock_open,
369 .flush = mtdblock_flush,
370 .release = mtdblock_release,
371 .readsect = mtdblock_readsect,
372 .writesect = mtdblock_writesect,
373 .add_mtd = mtdblock_add_mtd,
374 .remove_dev = mtdblock_remove_dev,
375 .owner = THIS_MODULE,
378 int __init init_mtdblock(void)
380 return register_mtd_blktrans(&mtdblock_tr);
383 static void __exit cleanup_mtdblock(void)
385 deregister_mtd_blktrans(&mtdblock_tr);
388 module_init(init_mtdblock);
389 module_exit(cleanup_mtdblock);
392 MODULE_LICENSE("GPL");
393 MODULE_AUTHOR("Nicolas Pitre <nico@cam.org> et al.");
394 MODULE_DESCRIPTION("Caching read/erase/writeback block device emulation access to MTD devices");