1
2
3
4
5
6
7
8
9#include <linux/module.h>
10#include <linux/fs.h>
11#include <linux/blkdev.h>
12#include <linux/bio.h>
13#include <linux/pagemap.h>
14#include <linux/list.h>
15#include <linux/init.h>
16#include <linux/mtd/mtd.h>
17#include <linux/mutex.h>
18#include <linux/mount.h>
19#include <linux/slab.h>
20
21#define ERROR(fmt, args...) printk(KERN_ERR "block2mtd: " fmt "\n" , ## args)
22#define INFO(fmt, args...) printk(KERN_INFO "block2mtd: " fmt "\n" , ## args)
23
24
25
26struct block2mtd_dev {
27 struct list_head list;
28 struct block_device *blkdev;
29 struct mtd_info mtd;
30 struct mutex write_mutex;
31};
32
33
34
35static LIST_HEAD(blkmtd_device_list);
36
37
38static struct page *page_read(struct address_space *mapping, int index)
39{
40 return read_mapping_page(mapping, index, NULL);
41}
42
43
44static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
45{
46 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
47 struct page *page;
48 int index = to >> PAGE_SHIFT;
49 int pages = len >> PAGE_SHIFT;
50 u_long *p;
51 u_long *max;
52
53 while (pages) {
54 page = page_read(mapping, index);
55 if (IS_ERR(page))
56 return PTR_ERR(page);
57
58 max = page_address(page) + PAGE_SIZE;
59 for (p=page_address(page); p<max; p++)
60 if (*p != -1UL) {
61 lock_page(page);
62 memset(page_address(page), 0xff, PAGE_SIZE);
63 set_page_dirty(page);
64 unlock_page(page);
65 balance_dirty_pages_ratelimited(mapping);
66 break;
67 }
68
69 page_cache_release(page);
70 pages--;
71 index++;
72 }
73 return 0;
74}
75static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
76{
77 struct block2mtd_dev *dev = mtd->priv;
78 size_t from = instr->addr;
79 size_t len = instr->len;
80 int err;
81
82 instr->state = MTD_ERASING;
83 mutex_lock(&dev->write_mutex);
84 err = _block2mtd_erase(dev, from, len);
85 mutex_unlock(&dev->write_mutex);
86 if (err) {
87 ERROR("erase failed err = %d", err);
88 instr->state = MTD_ERASE_FAILED;
89 } else
90 instr->state = MTD_ERASE_DONE;
91
92 mtd_erase_callback(instr);
93 return err;
94}
95
96
97static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
98 size_t *retlen, u_char *buf)
99{
100 struct block2mtd_dev *dev = mtd->priv;
101 struct page *page;
102 int index = from >> PAGE_SHIFT;
103 int offset = from & (PAGE_SIZE-1);
104 int cpylen;
105
106 while (len) {
107 if ((offset + len) > PAGE_SIZE)
108 cpylen = PAGE_SIZE - offset;
109 else
110 cpylen = len;
111 len = len - cpylen;
112
113 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
114 if (IS_ERR(page))
115 return PTR_ERR(page);
116
117 memcpy(buf, page_address(page) + offset, cpylen);
118 page_cache_release(page);
119
120 if (retlen)
121 *retlen += cpylen;
122 buf += cpylen;
123 offset = 0;
124 index++;
125 }
126 return 0;
127}
128
129
130
131static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
132 loff_t to, size_t len, size_t *retlen)
133{
134 struct page *page;
135 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
136 int index = to >> PAGE_SHIFT;
137 int offset = to & ~PAGE_MASK;
138 int cpylen;
139
140 while (len) {
141 if ((offset+len) > PAGE_SIZE)
142 cpylen = PAGE_SIZE - offset;
143 else
144 cpylen = len;
145 len = len - cpylen;
146
147 page = page_read(mapping, index);
148 if (IS_ERR(page))
149 return PTR_ERR(page);
150
151 if (memcmp(page_address(page)+offset, buf, cpylen)) {
152 lock_page(page);
153 memcpy(page_address(page) + offset, buf, cpylen);
154 set_page_dirty(page);
155 unlock_page(page);
156 balance_dirty_pages_ratelimited(mapping);
157 }
158 page_cache_release(page);
159
160 if (retlen)
161 *retlen += cpylen;
162
163 buf += cpylen;
164 offset = 0;
165 index++;
166 }
167 return 0;
168}
169
170
171static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
172 size_t *retlen, const u_char *buf)
173{
174 struct block2mtd_dev *dev = mtd->priv;
175 int err;
176
177 mutex_lock(&dev->write_mutex);
178 err = _block2mtd_write(dev, buf, to, len, retlen);
179 mutex_unlock(&dev->write_mutex);
180 if (err > 0)
181 err = 0;
182 return err;
183}
184
185
186
187static void block2mtd_sync(struct mtd_info *mtd)
188{
189 struct block2mtd_dev *dev = mtd->priv;
190 sync_blockdev(dev->blkdev);
191 return;
192}
193
194
195static void block2mtd_free_device(struct block2mtd_dev *dev)
196{
197 if (!dev)
198 return;
199
200 kfree(dev->mtd.name);
201
202 if (dev->blkdev) {
203 invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
204 0, -1);
205 blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
206 }
207
208 kfree(dev);
209}
210
211
212
213static struct block2mtd_dev *add_device(char *devname, int erase_size)
214{
215 const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
216 struct block_device *bdev;
217 struct block2mtd_dev *dev;
218 char *name;
219
220 if (!devname)
221 return NULL;
222
223 dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
224 if (!dev)
225 return NULL;
226
227
228 bdev = blkdev_get_by_path(devname, mode, dev);
229#ifndef MODULE
230 if (IS_ERR(bdev)) {
231
232
233
234
235 dev_t devt = name_to_dev_t(devname);
236 if (devt)
237 bdev = blkdev_get_by_dev(devt, mode, dev);
238 }
239#endif
240
241 if (IS_ERR(bdev)) {
242 ERROR("error: cannot open device %s", devname);
243 goto devinit_err;
244 }
245 dev->blkdev = bdev;
246
247 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
248 ERROR("attempting to use an MTD device as a block device");
249 goto devinit_err;
250 }
251
252 mutex_init(&dev->write_mutex);
253
254
255
256 name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
257 if (!name)
258 goto devinit_err;
259
260 dev->mtd.name = name;
261
262 dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
263 dev->mtd.erasesize = erase_size;
264 dev->mtd.writesize = 1;
265 dev->mtd.writebufsize = PAGE_SIZE;
266 dev->mtd.type = MTD_RAM;
267 dev->mtd.flags = MTD_CAP_RAM;
268 dev->mtd._erase = block2mtd_erase;
269 dev->mtd._write = block2mtd_write;
270 dev->mtd._sync = block2mtd_sync;
271 dev->mtd._read = block2mtd_read;
272 dev->mtd.priv = dev;
273 dev->mtd.owner = THIS_MODULE;
274
275 if (mtd_device_register(&dev->mtd, NULL, 0)) {
276
277 goto devinit_err;
278 }
279 list_add(&dev->list, &blkmtd_device_list);
280 INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev->mtd.index,
281 dev->mtd.name + strlen("block2mtd: "),
282 dev->mtd.erasesize >> 10, dev->mtd.erasesize);
283 return dev;
284
285devinit_err:
286 block2mtd_free_device(dev);
287 return NULL;
288}
289
290
291
292
293
294
295
296
297static int ustrtoul(const char *cp, char **endp, unsigned int base)
298{
299 unsigned long result = simple_strtoul(cp, endp, base);
300 switch (**endp) {
301 case 'G' :
302 result *= 1024;
303 case 'M':
304 result *= 1024;
305 case 'K':
306 case 'k':
307 result *= 1024;
308
309 if ((*endp)[1] == 'i') {
310 if ((*endp)[2] == 'B')
311 (*endp) += 3;
312 else
313 (*endp) += 2;
314 }
315 }
316 return result;
317}
318
319
320static int parse_num(size_t *num, const char *token)
321{
322 char *endp;
323 size_t n;
324
325 n = (size_t) ustrtoul(token, &endp, 0);
326 if (*endp)
327 return -EINVAL;
328
329 *num = n;
330 return 0;
331}
332
333
334static inline void kill_final_newline(char *str)
335{
336 char *newline = strrchr(str, '\n');
337 if (newline && !newline[1])
338 *newline = 0;
339}
340
341
342#define parse_err(fmt, args...) do { \
343 ERROR(fmt, ## args); \
344 return 0; \
345} while (0)
346
347#ifndef MODULE
348static int block2mtd_init_called = 0;
349static char block2mtd_paramline[80 + 12];
350#endif
351
352
353static int block2mtd_setup2(const char *val)
354{
355 char buf[80 + 12];
356 char *str = buf;
357 char *token[2];
358 char *name;
359 size_t erase_size = PAGE_SIZE;
360 int i, ret;
361
362 if (strnlen(val, sizeof(buf)) >= sizeof(buf))
363 parse_err("parameter too long");
364
365 strcpy(str, val);
366 kill_final_newline(str);
367
368 for (i = 0; i < 2; i++)
369 token[i] = strsep(&str, ",");
370
371 if (str)
372 parse_err("too many arguments");
373
374 if (!token[0])
375 parse_err("no argument");
376
377 name = token[0];
378 if (strlen(name) + 1 > 80)
379 parse_err("device name too long");
380
381 if (token[1]) {
382 ret = parse_num(&erase_size, token[1]);
383 if (ret) {
384 parse_err("illegal erase size");
385 }
386 }
387
388 add_device(name, erase_size);
389
390 return 0;
391}
392
393
394static int block2mtd_setup(const char *val, struct kernel_param *kp)
395{
396#ifdef MODULE
397 return block2mtd_setup2(val);
398#else
399
400
401
402
403
404 if (block2mtd_init_called)
405 return block2mtd_setup2(val);
406
407
408
409
410
411
412
413
414 strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
415
416 return 0;
417#endif
418}
419
420
421module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
422MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
423
424static int __init block2mtd_init(void)
425{
426 int ret = 0;
427
428#ifndef MODULE
429 if (strlen(block2mtd_paramline))
430 ret = block2mtd_setup2(block2mtd_paramline);
431 block2mtd_init_called = 1;
432#endif
433
434 return ret;
435}
436
437
438static void block2mtd_exit(void)
439{
440 struct list_head *pos, *next;
441
442
443 list_for_each_safe(pos, next, &blkmtd_device_list) {
444 struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
445 block2mtd_sync(&dev->mtd);
446 mtd_device_unregister(&dev->mtd);
447 INFO("mtd%d: [%s] removed", dev->mtd.index,
448 dev->mtd.name + strlen("block2mtd: "));
449 list_del(&dev->list);
450 block2mtd_free_device(dev);
451 }
452}
453
454
455module_init(block2mtd_init);
456module_exit(block2mtd_exit);
457
458MODULE_LICENSE("GPL");
459MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
460MODULE_DESCRIPTION("Emulate an MTD using a block device");
461