blob: b7c32c242bc712ce6e0bd87e358dc33eb40a8a56 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Direct MTD block device access
3 *
4 * $Id: mtdblock.c,v 1.66 2004/11/25 13:52:52 joern Exp $
5 *
6 * (C) 2000-2003 Nicolas Pitre <nico@cam.org>
7 * (C) 1999-2003 David Woodhouse <dwmw2@infradead.org>
8 */
9
10#include <linux/config.h>
11#include <linux/types.h>
12#include <linux/module.h>
13#include <linux/kernel.h>
14#include <linux/fs.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>
20
21static struct mtdblk_dev {
22 struct mtd_info *mtd;
23 int count;
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];
30
31/*
32 * Cache stuff...
33 *
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.
39 */
40
41static void erase_callback(struct erase_info *done)
42{
43 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
44 wake_up(wait_q);
45}
46
47static int erase_write (struct mtd_info *mtd, unsigned long pos,
48 int len, const char *buf)
49{
50 struct erase_info erase;
51 DECLARE_WAITQUEUE(wait, current);
52 wait_queue_head_t wait_q;
53 size_t retlen;
54 int ret;
55
56 /*
57 * First, let's erase the flash block.
58 */
59
60 init_waitqueue_head(&wait_q);
61 erase.mtd = mtd;
62 erase.callback = erase_callback;
63 erase.addr = pos;
64 erase.len = len;
65 erase.priv = (u_long)&wait_q;
66
67 set_current_state(TASK_INTERRUPTIBLE);
68 add_wait_queue(&wait_q, &wait);
69
70 ret = MTD_ERASE(mtd, &erase);
71 if (ret) {
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] "
75 "on \"%s\" failed\n",
76 pos, len, mtd->name);
77 return ret;
78 }
79
80 schedule(); /* Wait for erase to finish. */
81 remove_wait_queue(&wait_q, &wait);
82
83 /*
84 * Next, writhe data to flash.
85 */
86
87 ret = MTD_WRITE (mtd, pos, len, &retlen, buf);
88 if (ret)
89 return ret;
90 if (retlen != len)
91 return -EIO;
92 return 0;
93}
94
95
96static int write_cached_data (struct mtdblk_dev *mtdblk)
97{
98 struct mtd_info *mtd = mtdblk->mtd;
99 int ret;
100
101 if (mtdblk->cache_state != STATE_DIRTY)
102 return 0;
103
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);
107
108 ret = erase_write (mtd, mtdblk->cache_offset,
109 mtdblk->cache_size, mtdblk->cache_data);
110 if (ret)
111 return ret;
112
113 /*
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.
119 */
120 mtdblk->cache_state = STATE_EMPTY;
121 return 0;
122}
123
124
125static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos,
126 int len, const char *buf)
127{
128 struct mtd_info *mtd = mtdblk->mtd;
129 unsigned int sect_size = mtdblk->cache_size;
130 size_t retlen;
131 int ret;
132
133 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: write on \"%s\" at 0x%lx, size 0x%x\n",
134 mtd->name, pos, len);
135
136 if (!sect_size)
137 return MTD_WRITE (mtd, pos, len, &retlen, buf);
138
139 while (len > 0) {
140 unsigned long sect_start = (pos/sect_size)*sect_size;
141 unsigned int offset = pos - sect_start;
142 unsigned int size = sect_size - offset;
143 if( size > len )
144 size = len;
145
146 if (size == sect_size) {
147 /*
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.
151 */
152 ret = erase_write (mtd, pos, size, buf);
153 if (ret)
154 return ret;
155 } else {
156 /* Partial sector: need to use the cache */
157
158 if (mtdblk->cache_state == STATE_DIRTY &&
159 mtdblk->cache_offset != sect_start) {
160 ret = write_cached_data(mtdblk);
161 if (ret)
162 return ret;
163 }
164
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);
170 if (ret)
171 return ret;
172 if (retlen != sect_size)
173 return -EIO;
174
175 mtdblk->cache_offset = sect_start;
176 mtdblk->cache_size = sect_size;
177 mtdblk->cache_state = STATE_CLEAN;
178 }
179
180 /* write data to our local cache */
181 memcpy (mtdblk->cache_data + offset, buf, size);
182 mtdblk->cache_state = STATE_DIRTY;
183 }
184
185 buf += size;
186 pos += size;
187 len -= size;
188 }
189
190 return 0;
191}
192
193
194static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos,
195 int len, char *buf)
196{
197 struct mtd_info *mtd = mtdblk->mtd;
198 unsigned int sect_size = mtdblk->cache_size;
199 size_t retlen;
200 int ret;
201
202 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: read on \"%s\" at 0x%lx, size 0x%x\n",
203 mtd->name, pos, len);
204
205 if (!sect_size)
206 return MTD_READ (mtd, pos, len, &retlen, buf);
207
208 while (len > 0) {
209 unsigned long sect_start = (pos/sect_size)*sect_size;
210 unsigned int offset = pos - sect_start;
211 unsigned int size = sect_size - offset;
212 if (size > len)
213 size = len;
214
215 /*
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
219 * from flash.
220 */
221 if (mtdblk->cache_state != STATE_EMPTY &&
222 mtdblk->cache_offset == sect_start) {
223 memcpy (buf, mtdblk->cache_data + offset, size);
224 } else {
225 ret = MTD_READ (mtd, pos, size, &retlen, buf);
226 if (ret)
227 return ret;
228 if (retlen != size)
229 return -EIO;
230 }
231
232 buf += size;
233 pos += size;
234 len -= size;
235 }
236
237 return 0;
238}
239
240static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
241 unsigned long block, char *buf)
242{
243 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
244 return do_cached_read(mtdblk, block<<9, 512, buf);
245}
246
247static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
248 unsigned long block, char *buf)
249{
250 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
251 if (unlikely(!mtdblk->cache_data && mtdblk->cache_size)) {
252 mtdblk->cache_data = vmalloc(mtdblk->mtd->erasesize);
253 if (!mtdblk->cache_data)
254 return -EINTR;
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?
258 */
259 }
260 return do_cached_write(mtdblk, block<<9, 512, buf);
261}
262
263static int mtdblock_open(struct mtd_blktrans_dev *mbd)
264{
265 struct mtdblk_dev *mtdblk;
266 struct mtd_info *mtd = mbd->mtd;
267 int dev = mbd->devnum;
268
269 DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n");
270
271 if (mtdblks[dev]) {
272 mtdblks[dev]->count++;
273 return 0;
274 }
275
276 /* OK, it's not open. Create cache info for it */
277 mtdblk = kmalloc(sizeof(struct mtdblk_dev), GFP_KERNEL);
278 if (!mtdblk)
279 return -ENOMEM;
280
281 memset(mtdblk, 0, sizeof(*mtdblk));
282 mtdblk->count = 1;
283 mtdblk->mtd = mtd;
284
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;
291 }
292
293 mtdblks[dev] = mtdblk;
294
295 DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
296
297 return 0;
298}
299
300static int mtdblock_release(struct mtd_blktrans_dev *mbd)
301{
302 int dev = mbd->devnum;
303 struct mtdblk_dev *mtdblk = mtdblks[dev];
304
305 DEBUG(MTD_DEBUG_LEVEL1, "mtdblock_release\n");
306
307 down(&mtdblk->cache_sem);
308 write_cached_data(mtdblk);
309 up(&mtdblk->cache_sem);
310
311 if (!--mtdblk->count) {
312 /* It was the last usage. Free the device */
313 mtdblks[dev] = NULL;
314 if (mtdblk->mtd->sync)
315 mtdblk->mtd->sync(mtdblk->mtd);
316 vfree(mtdblk->cache_data);
317 kfree(mtdblk);
318 }
319 DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
320
321 return 0;
322}
323
324static int mtdblock_flush(struct mtd_blktrans_dev *dev)
325{
326 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
327
328 down(&mtdblk->cache_sem);
329 write_cached_data(mtdblk);
330 up(&mtdblk->cache_sem);
331
332 if (mtdblk->mtd->sync)
333 mtdblk->mtd->sync(mtdblk->mtd);
334 return 0;
335}
336
337static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
338{
339 struct mtd_blktrans_dev *dev = kmalloc(sizeof(*dev), GFP_KERNEL);
340
341 if (!dev)
342 return;
343
344 memset(dev, 0, sizeof(*dev));
345
346 dev->mtd = mtd;
347 dev->devnum = mtd->index;
348 dev->blksize = 512;
349 dev->size = mtd->size >> 9;
350 dev->tr = tr;
351
352 if (!(mtd->flags & MTD_WRITEABLE))
353 dev->readonly = 1;
354
355 add_mtd_blktrans_dev(dev);
356}
357
358static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
359{
360 del_mtd_blktrans_dev(dev);
361 kfree(dev);
362}
363
364static struct mtd_blktrans_ops mtdblock_tr = {
365 .name = "mtdblock",
366 .major = 31,
367 .part_bits = 0,
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,
376};
377
378static int __init init_mtdblock(void)
379{
380 return register_mtd_blktrans(&mtdblock_tr);
381}
382
383static void __exit cleanup_mtdblock(void)
384{
385 deregister_mtd_blktrans(&mtdblock_tr);
386}
387
388module_init(init_mtdblock);
389module_exit(cleanup_mtdblock);
390
391
392MODULE_LICENSE("GPL");
393MODULE_AUTHOR("Nicolas Pitre <nico@cam.org> et al.");
394MODULE_DESCRIPTION("Caching read/erase/writeback block device emulation access to MTD devices");