2 * Direct MTD block device access
4 * (C) 2000-2003 Nicolas Pitre <nico@fluxnic.net>
5 * (C) 1999-2003 David Woodhouse <dwmw2@infradead.org>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/types.h>
15 #include <linux/vmalloc.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/blktrans.h>
19 #include <linux/mutex.h>
23 struct mtd_blktrans_dev mbd
;
25 struct mutex cache_mutex
;
26 unsigned char *cache_data
;
27 unsigned long cache_offset
;
28 unsigned int cache_size
;
29 enum { STATE_EMPTY
, STATE_CLEAN
, STATE_DIRTY
} cache_state
;
32 static struct mutex mtdblks_lock
;
37 * Since typical flash erasable sectors are much larger than what Linux's
38 * buffer cache can handle, we must implement read-modify-write on flash
39 * sectors for each block write requests. To avoid over-erasing flash sectors
40 * and to speed things up, we locally cache a whole flash sector while it is
41 * being written to until a different sector is required.
44 static void erase_callback(struct erase_info
*done
)
46 wait_queue_head_t
*wait_q
= (wait_queue_head_t
*)done
->priv
;
50 static int erase_write (struct mtd_info
*mtd
, unsigned long pos
,
51 int len
, const char *buf
)
53 struct erase_info erase
;
54 DECLARE_WAITQUEUE(wait
, current
);
55 wait_queue_head_t wait_q
;
60 * First, let's erase the flash block.
63 init_waitqueue_head(&wait_q
);
65 erase
.callback
= erase_callback
;
68 erase
.priv
= (u_long
)&wait_q
;
70 set_current_state(TASK_INTERRUPTIBLE
);
71 add_wait_queue(&wait_q
, &wait
);
73 ret
= mtd
->erase(mtd
, &erase
);
75 set_current_state(TASK_RUNNING
);
76 remove_wait_queue(&wait_q
, &wait
);
77 printk (KERN_WARNING
"mtdblock: erase of region [0x%lx, 0x%x] "
83 schedule(); /* Wait for erase to finish. */
84 remove_wait_queue(&wait_q
, &wait
);
87 * Next, write the data to flash.
90 ret
= mtd
->write(mtd
, pos
, len
, &retlen
, buf
);
99 static int write_cached_data (struct mtdblk_dev
*mtdblk
)
101 struct mtd_info
*mtd
= mtdblk
->mbd
.mtd
;
104 if (mtdblk
->cache_state
!= STATE_DIRTY
)
107 DEBUG(MTD_DEBUG_LEVEL2
, "mtdblock: writing cached data for \"%s\" "
108 "at 0x%lx, size 0x%x\n", mtd
->name
,
109 mtdblk
->cache_offset
, mtdblk
->cache_size
);
111 ret
= erase_write (mtd
, mtdblk
->cache_offset
,
112 mtdblk
->cache_size
, mtdblk
->cache_data
);
117 * Here we could argubly set the cache state to STATE_CLEAN.
118 * However this could lead to inconsistency since we will not
119 * be notified if this content is altered on the flash by other
120 * means. Let's declare it empty and leave buffering tasks to
121 * the buffer cache instead.
123 mtdblk
->cache_state
= STATE_EMPTY
;
128 static int do_cached_write (struct mtdblk_dev
*mtdblk
, unsigned long pos
,
129 int len
, const char *buf
)
131 struct mtd_info
*mtd
= mtdblk
->mbd
.mtd
;
132 unsigned int sect_size
= mtdblk
->cache_size
;
136 DEBUG(MTD_DEBUG_LEVEL2
, "mtdblock: write on \"%s\" at 0x%lx, size 0x%x\n",
137 mtd
->name
, pos
, len
);
140 return mtd
->write(mtd
, pos
, len
, &retlen
, buf
);
143 unsigned long sect_start
= (pos
/sect_size
)*sect_size
;
144 unsigned int offset
= pos
- sect_start
;
145 unsigned int size
= sect_size
- offset
;
149 if (size
== sect_size
) {
151 * We are covering a whole sector. Thus there is no
152 * need to bother with the cache while it may still be
153 * useful for other partial writes.
155 ret
= erase_write (mtd
, pos
, size
, buf
);
159 /* Partial sector: need to use the cache */
161 if (mtdblk
->cache_state
== STATE_DIRTY
&&
162 mtdblk
->cache_offset
!= sect_start
) {
163 ret
= write_cached_data(mtdblk
);
168 if (mtdblk
->cache_state
== STATE_EMPTY
||
169 mtdblk
->cache_offset
!= sect_start
) {
170 /* fill the cache with the current sector */
171 mtdblk
->cache_state
= STATE_EMPTY
;
172 ret
= mtd
->read(mtd
, sect_start
, sect_size
,
173 &retlen
, mtdblk
->cache_data
);
176 if (retlen
!= sect_size
)
179 mtdblk
->cache_offset
= sect_start
;
180 mtdblk
->cache_size
= sect_size
;
181 mtdblk
->cache_state
= STATE_CLEAN
;
184 /* write data to our local cache */
185 memcpy (mtdblk
->cache_data
+ offset
, buf
, size
);
186 mtdblk
->cache_state
= STATE_DIRTY
;
198 static int do_cached_read (struct mtdblk_dev
*mtdblk
, unsigned long pos
,
201 struct mtd_info
*mtd
= mtdblk
->mbd
.mtd
;
202 unsigned int sect_size
= mtdblk
->cache_size
;
206 DEBUG(MTD_DEBUG_LEVEL2
, "mtdblock: read on \"%s\" at 0x%lx, size 0x%x\n",
207 mtd
->name
, pos
, len
);
210 return mtd
->read(mtd
, pos
, len
, &retlen
, buf
);
213 unsigned long sect_start
= (pos
/sect_size
)*sect_size
;
214 unsigned int offset
= pos
- sect_start
;
215 unsigned int size
= sect_size
- offset
;
220 * Check if the requested data is already cached
221 * Read the requested amount of data from our internal cache if it
222 * contains what we want, otherwise we read the data directly
225 if (mtdblk
->cache_state
!= STATE_EMPTY
&&
226 mtdblk
->cache_offset
== sect_start
) {
227 memcpy (buf
, mtdblk
->cache_data
+ offset
, size
);
229 ret
= mtd
->read(mtd
, pos
, size
, &retlen
, buf
);
244 static int mtdblock_readsect(struct mtd_blktrans_dev
*dev
,
245 unsigned long block
, char *buf
)
247 struct mtdblk_dev
*mtdblk
= container_of(dev
, struct mtdblk_dev
, mbd
);
248 return do_cached_read(mtdblk
, block
<<9, 512, buf
);
251 static int mtdblock_writesect(struct mtd_blktrans_dev
*dev
,
252 unsigned long block
, char *buf
)
254 struct mtdblk_dev
*mtdblk
= container_of(dev
, struct mtdblk_dev
, mbd
);
255 if (unlikely(!mtdblk
->cache_data
&& mtdblk
->cache_size
)) {
256 mtdblk
->cache_data
= vmalloc(mtdblk
->mbd
.mtd
->erasesize
);
257 if (!mtdblk
->cache_data
)
259 /* -EINTR is not really correct, but it is the best match
260 * documented in man 2 write for all cases. We could also
261 * return -EAGAIN sometimes, but why bother?
264 return do_cached_write(mtdblk
, block
<<9, 512, buf
);
267 static int mtdblock_open(struct mtd_blktrans_dev
*mbd
)
269 struct mtdblk_dev
*mtdblk
= container_of(mbd
, struct mtdblk_dev
, mbd
);
271 DEBUG(MTD_DEBUG_LEVEL1
,"mtdblock_open\n");
273 mutex_lock(&mtdblks_lock
);
276 mutex_unlock(&mtdblks_lock
);
280 /* OK, it's not open. Create cache info for it */
282 mutex_init(&mtdblk
->cache_mutex
);
283 mtdblk
->cache_state
= STATE_EMPTY
;
284 if (!(mbd
->mtd
->flags
& MTD_NO_ERASE
) && mbd
->mtd
->erasesize
) {
285 mtdblk
->cache_size
= mbd
->mtd
->erasesize
;
286 mtdblk
->cache_data
= NULL
;
289 mutex_unlock(&mtdblks_lock
);
291 DEBUG(MTD_DEBUG_LEVEL1
, "ok\n");
296 static int mtdblock_release(struct mtd_blktrans_dev
*mbd
)
298 struct mtdblk_dev
*mtdblk
= container_of(mbd
, struct mtdblk_dev
, mbd
);
300 DEBUG(MTD_DEBUG_LEVEL1
, "mtdblock_release\n");
302 mutex_lock(&mtdblks_lock
);
304 mutex_lock(&mtdblk
->cache_mutex
);
305 write_cached_data(mtdblk
);
306 mutex_unlock(&mtdblk
->cache_mutex
);
308 if (!--mtdblk
->count
) {
309 /* It was the last usage. Free the cache */
311 mbd
->mtd
->sync(mbd
->mtd
);
312 vfree(mtdblk
->cache_data
);
315 mutex_unlock(&mtdblks_lock
);
317 DEBUG(MTD_DEBUG_LEVEL1
, "ok\n");
322 static int mtdblock_flush(struct mtd_blktrans_dev
*dev
)
324 struct mtdblk_dev
*mtdblk
= container_of(dev
, struct mtdblk_dev
, mbd
);
326 mutex_lock(&mtdblk
->cache_mutex
);
327 write_cached_data(mtdblk
);
328 mutex_unlock(&mtdblk
->cache_mutex
);
331 dev
->mtd
->sync(dev
->mtd
);
335 static void mtdblock_add_mtd(struct mtd_blktrans_ops
*tr
, struct mtd_info
*mtd
)
337 struct mtdblk_dev
*dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
343 dev
->mbd
.devnum
= mtd
->index
;
345 dev
->mbd
.size
= mtd
->size
>> 9;
348 if (!(mtd
->flags
& MTD_WRITEABLE
))
349 dev
->mbd
.readonly
= 1;
351 if (add_mtd_blktrans_dev(&dev
->mbd
))
355 static void mtdblock_remove_dev(struct mtd_blktrans_dev
*dev
)
357 del_mtd_blktrans_dev(dev
);
360 static struct mtd_blktrans_ops mtdblock_tr
= {
365 .open
= mtdblock_open
,
366 .flush
= mtdblock_flush
,
367 .release
= mtdblock_release
,
368 .readsect
= mtdblock_readsect
,
369 .writesect
= mtdblock_writesect
,
370 .add_mtd
= mtdblock_add_mtd
,
371 .remove_dev
= mtdblock_remove_dev
,
372 .owner
= THIS_MODULE
,
375 static int __init
init_mtdblock(void)
377 mutex_init(&mtdblks_lock
);
379 return register_mtd_blktrans(&mtdblock_tr
);
382 static void __exit
cleanup_mtdblock(void)
384 deregister_mtd_blktrans(&mtdblock_tr
);
387 module_init(init_mtdblock
);
388 module_exit(cleanup_mtdblock
);
391 MODULE_LICENSE("GPL");
392 MODULE_AUTHOR("Nicolas Pitre <nico@fluxnic.net> et al.");
393 MODULE_DESCRIPTION("Caching read/erase/writeback block device emulation access to MTD devices");