[MTD] Use single flag to mark writeable devices.
[deliverable/linux.git] / drivers / mtd / mtdblock.c
CommitLineData
97894cda 1/*
1da177e4
LT
2 * Direct MTD block device access
3 *
97894cda 4 * $Id: mtdblock.c,v 1.68 2005/11/07 11:14:20 gleixner Exp $
1da177e4
LT
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>
1da177e4
LT
11#include <linux/fs.h>
12#include <linux/init.h>
15fdc52f
TG
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/sched.h>
1da177e4 16#include <linux/slab.h>
15fdc52f 17#include <linux/types.h>
1da177e4 18#include <linux/vmalloc.h>
15fdc52f 19
1da177e4
LT
20#include <linux/mtd/mtd.h>
21#include <linux/mtd/blktrans.h>
48b19268
IM
22#include <linux/mutex.h>
23
1da177e4
LT
24
25static struct mtdblk_dev {
26 struct mtd_info *mtd;
27 int count;
48b19268 28 struct mutex cache_mutex;
1da177e4
LT
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];
34
35/*
36 * Cache stuff...
97894cda 37 *
1da177e4
LT
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.
43 */
44
45static void erase_callback(struct erase_info *done)
46{
47 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
48 wake_up(wait_q);
49}
50
97894cda 51static int erase_write (struct mtd_info *mtd, unsigned long pos,
1da177e4
LT
52 int len, const char *buf)
53{
54 struct erase_info erase;
55 DECLARE_WAITQUEUE(wait, current);
56 wait_queue_head_t wait_q;
57 size_t retlen;
58 int ret;
59
60 /*
61 * First, let's erase the flash block.
62 */
63
64 init_waitqueue_head(&wait_q);
65 erase.mtd = mtd;
66 erase.callback = erase_callback;
67 erase.addr = pos;
68 erase.len = len;
69 erase.priv = (u_long)&wait_q;
70
71 set_current_state(TASK_INTERRUPTIBLE);
72 add_wait_queue(&wait_q, &wait);
73
74 ret = MTD_ERASE(mtd, &erase);
75 if (ret) {
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] "
79 "on \"%s\" failed\n",
80 pos, len, mtd->name);
81 return ret;
82 }
83
84 schedule(); /* Wait for erase to finish. */
85 remove_wait_queue(&wait_q, &wait);
86
87 /*
88 * Next, writhe data to flash.
89 */
90
91 ret = MTD_WRITE (mtd, pos, len, &retlen, buf);
92 if (ret)
93 return ret;
94 if (retlen != len)
95 return -EIO;
96 return 0;
97}
98
99
100static int write_cached_data (struct mtdblk_dev *mtdblk)
101{
102 struct mtd_info *mtd = mtdblk->mtd;
103 int ret;
104
105 if (mtdblk->cache_state != STATE_DIRTY)
106 return 0;
107
108 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: writing cached data for \"%s\" "
97894cda 109 "at 0x%lx, size 0x%x\n", mtd->name,
1da177e4 110 mtdblk->cache_offset, mtdblk->cache_size);
97894cda
TG
111
112 ret = erase_write (mtd, mtdblk->cache_offset,
1da177e4
LT
113 mtdblk->cache_size, mtdblk->cache_data);
114 if (ret)
115 return ret;
116
117 /*
118 * Here we could argubly set the cache state to STATE_CLEAN.
97894cda
TG
119 * However this could lead to inconsistency since we will not
120 * be notified if this content is altered on the flash by other
1da177e4
LT
121 * means. Let's declare it empty and leave buffering tasks to
122 * the buffer cache instead.
123 */
124 mtdblk->cache_state = STATE_EMPTY;
125 return 0;
126}
127
128
97894cda 129static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos,
1da177e4
LT
130 int len, const char *buf)
131{
132 struct mtd_info *mtd = mtdblk->mtd;
133 unsigned int sect_size = mtdblk->cache_size;
134 size_t retlen;
135 int ret;
136
137 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: write on \"%s\" at 0x%lx, size 0x%x\n",
138 mtd->name, pos, len);
97894cda 139
1da177e4
LT
140 if (!sect_size)
141 return MTD_WRITE (mtd, pos, len, &retlen, buf);
142
143 while (len > 0) {
144 unsigned long sect_start = (pos/sect_size)*sect_size;
145 unsigned int offset = pos - sect_start;
146 unsigned int size = sect_size - offset;
97894cda 147 if( size > len )
1da177e4
LT
148 size = len;
149
150 if (size == sect_size) {
97894cda 151 /*
1da177e4
LT
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.
155 */
156 ret = erase_write (mtd, pos, size, buf);
157 if (ret)
158 return ret;
159 } else {
160 /* Partial sector: need to use the cache */
161
162 if (mtdblk->cache_state == STATE_DIRTY &&
163 mtdblk->cache_offset != sect_start) {
164 ret = write_cached_data(mtdblk);
97894cda 165 if (ret)
1da177e4
LT
166 return ret;
167 }
168
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, &retlen, mtdblk->cache_data);
174 if (ret)
175 return ret;
176 if (retlen != sect_size)
177 return -EIO;
178
179 mtdblk->cache_offset = sect_start;
180 mtdblk->cache_size = sect_size;
181 mtdblk->cache_state = STATE_CLEAN;
182 }
183
184 /* write data to our local cache */
185 memcpy (mtdblk->cache_data + offset, buf, size);
186 mtdblk->cache_state = STATE_DIRTY;
187 }
188
189 buf += size;
190 pos += size;
191 len -= size;
192 }
193
194 return 0;
195}
196
197
97894cda 198static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos,
1da177e4
LT
199 int len, char *buf)
200{
201 struct mtd_info *mtd = mtdblk->mtd;
202 unsigned int sect_size = mtdblk->cache_size;
203 size_t retlen;
204 int ret;
205
97894cda 206 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: read on \"%s\" at 0x%lx, size 0x%x\n",
1da177e4 207 mtd->name, pos, len);
97894cda 208
1da177e4
LT
209 if (!sect_size)
210 return MTD_READ (mtd, pos, len, &retlen, buf);
211
212 while (len > 0) {
213 unsigned long sect_start = (pos/sect_size)*sect_size;
214 unsigned int offset = pos - sect_start;
215 unsigned int size = sect_size - offset;
97894cda 216 if (size > len)
1da177e4
LT
217 size = len;
218
219 /*
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
223 * from flash.
224 */
225 if (mtdblk->cache_state != STATE_EMPTY &&
226 mtdblk->cache_offset == sect_start) {
227 memcpy (buf, mtdblk->cache_data + offset, size);
228 } else {
229 ret = MTD_READ (mtd, pos, size, &retlen, buf);
230 if (ret)
231 return ret;
232 if (retlen != size)
233 return -EIO;
234 }
235
236 buf += size;
237 pos += size;
238 len -= size;
239 }
240
241 return 0;
242}
243
244static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
245 unsigned long block, char *buf)
246{
247 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
248 return do_cached_read(mtdblk, block<<9, 512, buf);
249}
250
251static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
252 unsigned long block, char *buf)
253{
254 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
255 if (unlikely(!mtdblk->cache_data && mtdblk->cache_size)) {
256 mtdblk->cache_data = vmalloc(mtdblk->mtd->erasesize);
257 if (!mtdblk->cache_data)
258 return -EINTR;
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?
262 */
263 }
264 return do_cached_write(mtdblk, block<<9, 512, buf);
265}
266
267static int mtdblock_open(struct mtd_blktrans_dev *mbd)
268{
269 struct mtdblk_dev *mtdblk;
270 struct mtd_info *mtd = mbd->mtd;
271 int dev = mbd->devnum;
272
273 DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n");
97894cda 274
1da177e4
LT
275 if (mtdblks[dev]) {
276 mtdblks[dev]->count++;
277 return 0;
278 }
97894cda 279
1da177e4
LT
280 /* OK, it's not open. Create cache info for it */
281 mtdblk = kmalloc(sizeof(struct mtdblk_dev), GFP_KERNEL);
282 if (!mtdblk)
283 return -ENOMEM;
284
285 memset(mtdblk, 0, sizeof(*mtdblk));
286 mtdblk->count = 1;
287 mtdblk->mtd = mtd;
288
48b19268 289 mutex_init(&mtdblk->cache_mutex);
1da177e4 290 mtdblk->cache_state = STATE_EMPTY;
7f7c08df 291 if (mtdblk->mtd->type != MTD_RAM && mtdblk->mtd->erasesize) {
1da177e4
LT
292 mtdblk->cache_size = mtdblk->mtd->erasesize;
293 mtdblk->cache_data = NULL;
294 }
295
296 mtdblks[dev] = mtdblk;
97894cda 297
1da177e4
LT
298 DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
299
300 return 0;
301}
302
303static int mtdblock_release(struct mtd_blktrans_dev *mbd)
304{
305 int dev = mbd->devnum;
306 struct mtdblk_dev *mtdblk = mtdblks[dev];
307
308 DEBUG(MTD_DEBUG_LEVEL1, "mtdblock_release\n");
309
48b19268 310 mutex_lock(&mtdblk->cache_mutex);
1da177e4 311 write_cached_data(mtdblk);
48b19268 312 mutex_unlock(&mtdblk->cache_mutex);
1da177e4
LT
313
314 if (!--mtdblk->count) {
315 /* It was the last usage. Free the device */
316 mtdblks[dev] = NULL;
317 if (mtdblk->mtd->sync)
318 mtdblk->mtd->sync(mtdblk->mtd);
319 vfree(mtdblk->cache_data);
320 kfree(mtdblk);
321 }
322 DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
323
324 return 0;
97894cda 325}
1da177e4
LT
326
327static int mtdblock_flush(struct mtd_blktrans_dev *dev)
328{
329 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
330
48b19268 331 mutex_lock(&mtdblk->cache_mutex);
1da177e4 332 write_cached_data(mtdblk);
48b19268 333 mutex_unlock(&mtdblk->cache_mutex);
1da177e4
LT
334
335 if (mtdblk->mtd->sync)
336 mtdblk->mtd->sync(mtdblk->mtd);
337 return 0;
338}
339
340static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
341{
342 struct mtd_blktrans_dev *dev = kmalloc(sizeof(*dev), GFP_KERNEL);
343
344 if (!dev)
345 return;
346
347 memset(dev, 0, sizeof(*dev));
348
349 dev->mtd = mtd;
350 dev->devnum = mtd->index;
351 dev->blksize = 512;
352 dev->size = mtd->size >> 9;
353 dev->tr = tr;
354
355 if (!(mtd->flags & MTD_WRITEABLE))
356 dev->readonly = 1;
357
358 add_mtd_blktrans_dev(dev);
359}
360
361static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
362{
363 del_mtd_blktrans_dev(dev);
364 kfree(dev);
365}
366
367static struct mtd_blktrans_ops mtdblock_tr = {
368 .name = "mtdblock",
369 .major = 31,
370 .part_bits = 0,
371 .open = mtdblock_open,
372 .flush = mtdblock_flush,
373 .release = mtdblock_release,
374 .readsect = mtdblock_readsect,
375 .writesect = mtdblock_writesect,
376 .add_mtd = mtdblock_add_mtd,
377 .remove_dev = mtdblock_remove_dev,
378 .owner = THIS_MODULE,
379};
380
381static int __init init_mtdblock(void)
382{
383 return register_mtd_blktrans(&mtdblock_tr);
384}
385
386static void __exit cleanup_mtdblock(void)
387{
388 deregister_mtd_blktrans(&mtdblock_tr);
389}
390
391module_init(init_mtdblock);
392module_exit(cleanup_mtdblock);
393
394
395MODULE_LICENSE("GPL");
396MODULE_AUTHOR("Nicolas Pitre <nico@cam.org> et al.");
397MODULE_DESCRIPTION("Caching read/erase/writeback block device emulation access to MTD devices");
This page took 0.146367 seconds and 5 git commands to generate.