2 * Direct MTD block device access
4 * $Id: mtdblock.c,v 1.68 2005/11/07 11:14:20 gleixner Exp $
6 * (C) 2000-2003 Nicolas Pitre <nico@cam.org>
7 * (C) 1999-2003 David Woodhouse <dwmw2@infradead.org>
10 #include <linux/config.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/types.h>
18 #include <linux/vmalloc.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/mtd/blktrans.h>
22 #include <linux/mutex.h>
25 static struct mtdblk_dev {
28 struct mutex cache_mutex;
29 unsigned char *cache_data;
30 unsigned long cache_offset;
31 unsigned int cache_size;
32 enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state;
33 } *mtdblks[MAX_MTD_DEVICES];
38 * Since typical flash erasable sectors are much larger than what Linux's
39 * buffer cache can handle, we must implement read-modify-write on flash
40 * sectors for each block write requests. To avoid over-erasing flash sectors
41 * and to speed things up, we locally cache a whole flash sector while it is
42 * being written to until a different sector is required.
45 static void erase_callback(struct erase_info *done)
47 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
51 static int erase_write (struct mtd_info *mtd, unsigned long pos,
52 int len, const char *buf)
54 struct erase_info erase;
55 DECLARE_WAITQUEUE(wait, current);
56 wait_queue_head_t wait_q;
61 * First, let's erase the flash block.
64 init_waitqueue_head(&wait_q);
66 erase.callback = erase_callback;
69 erase.priv = (u_long)&wait_q;
71 set_current_state(TASK_INTERRUPTIBLE);
72 add_wait_queue(&wait_q, &wait);
74 ret = mtd->erase(mtd, &erase);
76 set_current_state(TASK_RUNNING);
77 remove_wait_queue(&wait_q, &wait);
78 printk (KERN_WARNING "mtdblock: erase of region [0x%lx, 0x%x] "
84 schedule(); /* Wait for erase to finish. */
85 remove_wait_queue(&wait_q, &wait);
88 * Next, writhe data to flash.
91 ret = mtd->write(mtd, pos, len, &retlen, buf);
100 static int write_cached_data (struct mtdblk_dev *mtdblk)
102 struct mtd_info *mtd = mtdblk->mtd;
105 if (mtdblk->cache_state != STATE_DIRTY)
108 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: writing cached data for \"%s\" "
109 "at 0x%lx, size 0x%x\n", mtd->name,
110 mtdblk->cache_offset, mtdblk->cache_size);
112 ret = erase_write (mtd, mtdblk->cache_offset,
113 mtdblk->cache_size, mtdblk->cache_data);
118 * Here we could argubly set the cache state to STATE_CLEAN.
119 * However this could lead to inconsistency since we will not
120 * be notified if this content is altered on the flash by other
121 * means. Let's declare it empty and leave buffering tasks to
122 * the buffer cache instead.
124 mtdblk->cache_state = STATE_EMPTY;
129 static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos,
130 int len, const char *buf)
132 struct mtd_info *mtd = mtdblk->mtd;
133 unsigned int sect_size = mtdblk->cache_size;
137 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: write on \"%s\" at 0x%lx, size 0x%x\n",
138 mtd->name, pos, len);
141 return mtd->write(mtd, pos, len, &retlen, buf);
144 unsigned long sect_start = (pos/sect_size)*sect_size;
145 unsigned int offset = pos - sect_start;
146 unsigned int size = sect_size - offset;
150 if (size == sect_size) {
152 * We are covering a whole sector. Thus there is no
153 * need to bother with the cache while it may still be
154 * useful for other partial writes.
156 ret = erase_write (mtd, pos, size, buf);
160 /* Partial sector: need to use the cache */
162 if (mtdblk->cache_state == STATE_DIRTY &&
163 mtdblk->cache_offset != sect_start) {
164 ret = write_cached_data(mtdblk);
169 if (mtdblk->cache_state == STATE_EMPTY ||
170 mtdblk->cache_offset != sect_start) {
171 /* fill the cache with the current sector */
172 mtdblk->cache_state = STATE_EMPTY;
173 ret = mtd->read(mtd, sect_start, sect_size,
174 &retlen, mtdblk->cache_data);
177 if (retlen != sect_size)
180 mtdblk->cache_offset = sect_start;
181 mtdblk->cache_size = sect_size;
182 mtdblk->cache_state = STATE_CLEAN;
185 /* write data to our local cache */
186 memcpy (mtdblk->cache_data + offset, buf, size);
187 mtdblk->cache_state = STATE_DIRTY;
199 static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos,
202 struct mtd_info *mtd = mtdblk->mtd;
203 unsigned int sect_size = mtdblk->cache_size;
207 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: read on \"%s\" at 0x%lx, size 0x%x\n",
208 mtd->name, pos, len);
211 return mtd->read(mtd, pos, len, &retlen, buf);
214 unsigned long sect_start = (pos/sect_size)*sect_size;
215 unsigned int offset = pos - sect_start;
216 unsigned int size = sect_size - offset;
221 * Check if the requested data is already cached
222 * Read the requested amount of data from our internal cache if it
223 * contains what we want, otherwise we read the data directly
226 if (mtdblk->cache_state != STATE_EMPTY &&
227 mtdblk->cache_offset == sect_start) {
228 memcpy (buf, mtdblk->cache_data + offset, size);
230 ret = mtd->read(mtd, pos, size, &retlen, buf);
245 static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
246 unsigned long block, char *buf)
248 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
249 return do_cached_read(mtdblk, block<<9, 512, buf);
252 static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
253 unsigned long block, char *buf)
255 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
256 if (unlikely(!mtdblk->cache_data && mtdblk->cache_size)) {
257 mtdblk->cache_data = vmalloc(mtdblk->mtd->erasesize);
258 if (!mtdblk->cache_data)
260 /* -EINTR is not really correct, but it is the best match
261 * documented in man 2 write for all cases. We could also
262 * return -EAGAIN sometimes, but why bother?
265 return do_cached_write(mtdblk, block<<9, 512, buf);
268 static int mtdblock_open(struct mtd_blktrans_dev *mbd)
270 struct mtdblk_dev *mtdblk;
271 struct mtd_info *mtd = mbd->mtd;
272 int dev = mbd->devnum;
274 DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n");
277 mtdblks[dev]->count++;
281 /* OK, it's not open. Create cache info for it */
282 mtdblk = kmalloc(sizeof(struct mtdblk_dev), GFP_KERNEL);
286 memset(mtdblk, 0, sizeof(*mtdblk));
290 mutex_init(&mtdblk->cache_mutex);
291 mtdblk->cache_state = STATE_EMPTY;
292 if ( !(mtdblk->mtd->flags & MTD_NO_ERASE) && mtdblk->mtd->erasesize) {
293 mtdblk->cache_size = mtdblk->mtd->erasesize;
294 mtdblk->cache_data = NULL;
297 mtdblks[dev] = mtdblk;
299 DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
304 static int mtdblock_release(struct mtd_blktrans_dev *mbd)
306 int dev = mbd->devnum;
307 struct mtdblk_dev *mtdblk = mtdblks[dev];
309 DEBUG(MTD_DEBUG_LEVEL1, "mtdblock_release\n");
311 mutex_lock(&mtdblk->cache_mutex);
312 write_cached_data(mtdblk);
313 mutex_unlock(&mtdblk->cache_mutex);
315 if (!--mtdblk->count) {
316 /* It was the last usage. Free the device */
318 if (mtdblk->mtd->sync)
319 mtdblk->mtd->sync(mtdblk->mtd);
320 vfree(mtdblk->cache_data);
323 DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
328 static int mtdblock_flush(struct mtd_blktrans_dev *dev)
330 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
332 mutex_lock(&mtdblk->cache_mutex);
333 write_cached_data(mtdblk);
334 mutex_unlock(&mtdblk->cache_mutex);
336 if (mtdblk->mtd->sync)
337 mtdblk->mtd->sync(mtdblk->mtd);
341 static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
343 struct mtd_blktrans_dev *dev = kmalloc(sizeof(*dev), GFP_KERNEL);
348 memset(dev, 0, sizeof(*dev));
351 dev->devnum = mtd->index;
353 dev->size = mtd->size >> 9;
356 if (!(mtd->flags & MTD_WRITEABLE))
359 add_mtd_blktrans_dev(dev);
362 static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
364 del_mtd_blktrans_dev(dev);
368 static struct mtd_blktrans_ops mtdblock_tr = {
372 .open = mtdblock_open,
373 .flush = mtdblock_flush,
374 .release = mtdblock_release,
375 .readsect = mtdblock_readsect,
376 .writesect = mtdblock_writesect,
377 .add_mtd = mtdblock_add_mtd,
378 .remove_dev = mtdblock_remove_dev,
379 .owner = THIS_MODULE,
382 static int __init init_mtdblock(void)
384 return register_mtd_blktrans(&mtdblock_tr);
387 static void __exit cleanup_mtdblock(void)
389 deregister_mtd_blktrans(&mtdblock_tr);
392 module_init(init_mtdblock);
393 module_exit(cleanup_mtdblock);
396 MODULE_LICENSE("GPL");
397 MODULE_AUTHOR("Nicolas Pitre <nico@cam.org> et al.");
398 MODULE_DESCRIPTION("Caching read/erase/writeback block device emulation access to MTD devices");