blob: 93651865ddbe6892891ce685698fe86153925807 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * block2mtd.c - create an mtd from a block device
3 *
4 * Copyright (C) 2001,2002 Simon Evans <spse@secret.org.uk>
Joern Engel2b54aae2008-02-06 01:38:02 -08005 * Copyright (C) 2004-2006 Joern Engel <joern@wh.fh-wedel.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Licence: GPL
8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#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/buffer_head.h>
Ingo Molnar48b19262006-03-31 02:29:41 -080018#include <linux/mutex.h>
Ville Hervac4e7fb32006-07-14 00:31:16 +030019#include <linux/mount.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#define ERROR(fmt, args...) printk(KERN_ERR "block2mtd: " fmt "\n" , ## args)
23#define INFO(fmt, args...) printk(KERN_INFO "block2mtd: " fmt "\n" , ## args)
24
25
26/* Info for the block device */
27struct block2mtd_dev {
28 struct list_head list;
29 struct block_device *blkdev;
30 struct mtd_info mtd;
Ingo Molnar48b19262006-03-31 02:29:41 -080031 struct mutex write_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032};
33
34
35/* Static info about the MTD, used in cleanup_module */
36static LIST_HEAD(blkmtd_device_list);
37
38
Nick Piggin6fe69002007-05-06 14:49:04 -070039static struct page *page_read(struct address_space *mapping, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Nick Piggin6fe69002007-05-06 14:49:04 -070041 return read_mapping_page(mapping, index, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/* erase a specified part of the device */
45static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
46{
47 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
48 struct page *page;
49 int index = to >> PAGE_SHIFT; // page index
50 int pages = len >> PAGE_SHIFT;
51 u_long *p;
52 u_long *max;
53
54 while (pages) {
Joern Engel21d31f12007-02-20 20:22:22 +010055 page = page_read(mapping, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 if (!page)
57 return -ENOMEM;
58 if (IS_ERR(page))
59 return PTR_ERR(page);
60
Joern Engel0ffb74c2007-02-20 20:20:58 +010061 max = page_address(page) + PAGE_SIZE;
62 for (p=page_address(page); p<max; p++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (*p != -1UL) {
64 lock_page(page);
65 memset(page_address(page), 0xff, PAGE_SIZE);
66 set_page_dirty(page);
67 unlock_page(page);
68 break;
69 }
70
71 page_cache_release(page);
72 pages--;
73 index++;
74 }
75 return 0;
76}
77static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
78{
79 struct block2mtd_dev *dev = mtd->priv;
80 size_t from = instr->addr;
81 size_t len = instr->len;
82 int err;
83
84 instr->state = MTD_ERASING;
Ingo Molnar48b19262006-03-31 02:29:41 -080085 mutex_lock(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 err = _block2mtd_erase(dev, from, len);
Ingo Molnar48b19262006-03-31 02:29:41 -080087 mutex_unlock(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (err) {
89 ERROR("erase failed err = %d", err);
90 instr->state = MTD_ERASE_FAILED;
91 } else
92 instr->state = MTD_ERASE_DONE;
93
94 instr->state = MTD_ERASE_DONE;
95 mtd_erase_callback(instr);
96 return err;
97}
98
99
100static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
101 size_t *retlen, u_char *buf)
102{
103 struct block2mtd_dev *dev = mtd->priv;
104 struct page *page;
105 int index = from >> PAGE_SHIFT;
Joern Engel711c11b2005-03-07 20:29:09 +0000106 int offset = from & (PAGE_SIZE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 int cpylen;
108
109 if (from > mtd->size)
110 return -EINVAL;
111 if (from + len > mtd->size)
112 len = mtd->size - from;
113
114 if (retlen)
115 *retlen = 0;
116
117 while (len) {
118 if ((offset + len) > PAGE_SIZE)
119 cpylen = PAGE_SIZE - offset; // multiple pages
120 else
121 cpylen = len; // this page
122 len = len - cpylen;
123
Joern Engel21d31f12007-02-20 20:22:22 +0100124 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (!page)
126 return -ENOMEM;
127 if (IS_ERR(page))
128 return PTR_ERR(page);
129
130 memcpy(buf, page_address(page) + offset, cpylen);
131 page_cache_release(page);
132
133 if (retlen)
134 *retlen += cpylen;
135 buf += cpylen;
136 offset = 0;
137 index++;
138 }
139 return 0;
140}
141
142
143/* write data to the underlying device */
144static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
145 loff_t to, size_t len, size_t *retlen)
146{
147 struct page *page;
148 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
149 int index = to >> PAGE_SHIFT; // page index
150 int offset = to & ~PAGE_MASK; // page offset
151 int cpylen;
152
153 if (retlen)
154 *retlen = 0;
155 while (len) {
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000156 if ((offset+len) > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 cpylen = PAGE_SIZE - offset; // multiple pages
158 else
159 cpylen = len; // this page
160 len = len - cpylen;
161
Joern Engel21d31f12007-02-20 20:22:22 +0100162 page = page_read(mapping, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (!page)
164 return -ENOMEM;
165 if (IS_ERR(page))
166 return PTR_ERR(page);
167
168 if (memcmp(page_address(page)+offset, buf, cpylen)) {
169 lock_page(page);
170 memcpy(page_address(page) + offset, buf, cpylen);
171 set_page_dirty(page);
172 unlock_page(page);
173 }
174 page_cache_release(page);
175
176 if (retlen)
177 *retlen += cpylen;
178
179 buf += cpylen;
180 offset = 0;
181 index++;
182 }
183 return 0;
184}
Ville Hervac4e7fb32006-07-14 00:31:16 +0300185
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
188 size_t *retlen, const u_char *buf)
189{
190 struct block2mtd_dev *dev = mtd->priv;
191 int err;
192
193 if (!len)
194 return 0;
195 if (to >= mtd->size)
196 return -ENOSPC;
197 if (to + len > mtd->size)
198 len = mtd->size - to;
199
Ingo Molnar48b19262006-03-31 02:29:41 -0800200 mutex_lock(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 err = _block2mtd_write(dev, buf, to, len, retlen);
Ingo Molnar48b19262006-03-31 02:29:41 -0800202 mutex_unlock(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (err > 0)
204 err = 0;
205 return err;
206}
207
208
209/* sync the device - wait until the write queue is empty */
210static void block2mtd_sync(struct mtd_info *mtd)
211{
212 struct block2mtd_dev *dev = mtd->priv;
213 sync_blockdev(dev->blkdev);
214 return;
215}
216
217
218static void block2mtd_free_device(struct block2mtd_dev *dev)
219{
220 if (!dev)
221 return;
222
223 kfree(dev->mtd.name);
224
225 if (dev->blkdev) {
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800226 invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
227 0, -1);
Al Viro30c40d22008-02-22 19:50:45 -0500228 close_bdev_exclusive(dev->blkdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230
231 kfree(dev);
232}
233
234
235/* FIXME: ensure that mtd->size % erase_size == 0 */
236static struct block2mtd_dev *add_device(char *devname, int erase_size)
237{
238 struct block_device *bdev;
239 struct block2mtd_dev *dev;
Greg Kroah-Hartmaneadcf0d2008-07-02 12:46:22 -0700240 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 if (!devname)
243 return NULL;
244
Burman Yan95b93a02006-11-15 21:10:29 +0200245 dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (!dev)
247 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 /* Get a handle on the device */
Al Viro30c40d22008-02-22 19:50:45 -0500250 bdev = open_bdev_exclusive(devname, FMODE_READ|FMODE_WRITE, NULL);
Ville Hervac4e7fb32006-07-14 00:31:16 +0300251#ifndef MODULE
252 if (IS_ERR(bdev)) {
253
254 /* We might not have rootfs mounted at this point. Try
255 to resolve the device name by other means. */
256
Joern Engel88705302007-02-20 20:21:41 +0100257 dev_t devt = name_to_dev_t(devname);
258 if (devt) {
259 bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
Ville Hervac4e7fb32006-07-14 00:31:16 +0300260 }
261 }
262#endif
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (IS_ERR(bdev)) {
265 ERROR("error: cannot open device %s", devname);
266 goto devinit_err;
267 }
268 dev->blkdev = bdev;
269
270 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
271 ERROR("attempting to use an MTD device as a block device");
272 goto devinit_err;
273 }
274
Ingo Molnar48b19262006-03-31 02:29:41 -0800275 mutex_init(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 /* Setup the MTD structure */
278 /* make the name contain the block device in */
Julia Lawall4d682422010-03-10 22:15:19 +0100279 name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
Greg Kroah-Hartmaneadcf0d2008-07-02 12:46:22 -0700280 if (!name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 goto devinit_err;
282
Greg Kroah-Hartmaneadcf0d2008-07-02 12:46:22 -0700283 dev->mtd.name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
286 dev->mtd.erasesize = erase_size;
Artem B. Bityutskiy17ffc7b2006-06-22 18:15:48 +0400287 dev->mtd.writesize = 1;
David Woodhouse21c8db92006-06-14 21:39:48 +0100288 dev->mtd.type = MTD_RAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 dev->mtd.flags = MTD_CAP_RAM;
290 dev->mtd.erase = block2mtd_erase;
291 dev->mtd.write = block2mtd_write;
292 dev->mtd.writev = default_mtd_writev;
293 dev->mtd.sync = block2mtd_sync;
294 dev->mtd.read = block2mtd_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 dev->mtd.priv = dev;
296 dev->mtd.owner = THIS_MODULE;
297
298 if (add_mtd_device(&dev->mtd)) {
299 /* Device didnt get added, so free the entry */
300 goto devinit_err;
301 }
302 list_add(&dev->list, &blkmtd_device_list);
303 INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev->mtd.index,
Stephane Chazelas0bc88c52008-04-18 13:44:15 -0700304 dev->mtd.name + strlen("block2mtd: "),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 dev->mtd.erasesize >> 10, dev->mtd.erasesize);
306 return dev;
307
308devinit_err:
309 block2mtd_free_device(dev);
310 return NULL;
311}
312
313
Joern Engel954c2422006-04-18 21:03:08 -0700314/* This function works similar to reguler strtoul. In addition, it
315 * allows some suffixes for a more human-readable number format:
316 * ki, Ki, kiB, KiB - multiply result with 1024
317 * Mi, MiB - multiply result with 1024^2
318 * Gi, GiB - multiply result with 1024^3
319 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320static int ustrtoul(const char *cp, char **endp, unsigned int base)
321{
322 unsigned long result = simple_strtoul(cp, endp, base);
323 switch (**endp) {
324 case 'G' :
325 result *= 1024;
326 case 'M':
327 result *= 1024;
Joern Engel954c2422006-04-18 21:03:08 -0700328 case 'K':
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 case 'k':
330 result *= 1024;
331 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
Joern Engel954c2422006-04-18 21:03:08 -0700332 if ((*endp)[1] == 'i') {
333 if ((*endp)[2] == 'B')
334 (*endp) += 3;
335 else
336 (*endp) += 2;
337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339 return result;
340}
341
342
Thomas Gleixnercc712292005-03-19 22:40:47 +0000343static int parse_num(size_t *num, const char *token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 char *endp;
Thomas Gleixnercc712292005-03-19 22:40:47 +0000346 size_t n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Thomas Gleixnercc712292005-03-19 22:40:47 +0000348 n = (size_t) ustrtoul(token, &endp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (*endp)
350 return -EINVAL;
351
Thomas Gleixnercc712292005-03-19 22:40:47 +0000352 *num = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return 0;
354}
355
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357static inline void kill_final_newline(char *str)
358{
359 char *newline = strrchr(str, '\n');
360 if (newline && !newline[1])
361 *newline = 0;
362}
363
364
Stephane Chazelas0bc88c52008-04-18 13:44:15 -0700365#define parse_err(fmt, args...) do { \
366 ERROR(fmt, ## args); \
367 return 0; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368} while (0)
369
Ville Hervac4e7fb32006-07-14 00:31:16 +0300370#ifndef MODULE
371static int block2mtd_init_called = 0;
Adrian Bunk4839f042007-05-02 12:33:17 +0100372static char block2mtd_paramline[80 + 12]; /* 80 for device, 12 for erase size */
Ville Hervac4e7fb32006-07-14 00:31:16 +0300373#endif
374
375
376static int block2mtd_setup2(const char *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Ville Hervac4e7fb32006-07-14 00:31:16 +0300378 char buf[80 + 12]; /* 80 for device, 12 for erase size */
Jesper Juhla6550e52006-05-14 01:42:25 +0200379 char *str = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 char *token[2];
381 char *name;
Thomas Gleixnercc712292005-03-19 22:40:47 +0000382 size_t erase_size = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 int i, ret;
384
385 if (strnlen(val, sizeof(buf)) >= sizeof(buf))
386 parse_err("parameter too long");
387
388 strcpy(str, val);
389 kill_final_newline(str);
390
Jesper Juhla6550e52006-05-14 01:42:25 +0200391 for (i = 0; i < 2; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 token[i] = strsep(&str, ",");
393
394 if (str)
395 parse_err("too many arguments");
396
397 if (!token[0])
398 parse_err("no argument");
399
Ville Hervac4e7fb32006-07-14 00:31:16 +0300400 name = token[0];
401 if (strlen(name) + 1 > 80)
402 parse_err("device name too long");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 if (token[1]) {
Thomas Gleixnercc712292005-03-19 22:40:47 +0000405 ret = parse_num(&erase_size, token[1]);
Jesper Juhla6550e52006-05-14 01:42:25 +0200406 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 parse_err("illegal erase size");
Jesper Juhla6550e52006-05-14 01:42:25 +0200408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410
411 add_device(name, erase_size);
412
413 return 0;
414}
415
416
Ville Hervac4e7fb32006-07-14 00:31:16 +0300417static int block2mtd_setup(const char *val, struct kernel_param *kp)
418{
419#ifdef MODULE
420 return block2mtd_setup2(val);
421#else
422 /* If more parameters are later passed in via
423 /sys/module/block2mtd/parameters/block2mtd
424 and block2mtd_init() has already been called,
425 we can parse the argument now. */
426
427 if (block2mtd_init_called)
428 return block2mtd_setup2(val);
429
430 /* During early boot stage, we only save the parameters
431 here. We must parse them later: if the param passed
432 from kernel boot command line, block2mtd_setup() is
433 called so early that it is not possible to resolve
434 the device (even kmalloc() fails). Deter that work to
435 block2mtd_setup2(). */
436
437 strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
438
439 return 0;
440#endif
441}
442
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
445MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
446
447static int __init block2mtd_init(void)
448{
Ville Hervac4e7fb32006-07-14 00:31:16 +0300449 int ret = 0;
Ville Hervac4e7fb32006-07-14 00:31:16 +0300450
451#ifndef MODULE
452 if (strlen(block2mtd_paramline))
453 ret = block2mtd_setup2(block2mtd_paramline);
454 block2mtd_init_called = 1;
455#endif
456
457 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460
461static void __devexit block2mtd_exit(void)
462{
463 struct list_head *pos, *next;
464
465 /* Remove the MTD devices */
466 list_for_each_safe(pos, next, &blkmtd_device_list) {
467 struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
468 block2mtd_sync(&dev->mtd);
469 del_mtd_device(&dev->mtd);
470 INFO("mtd%d: [%s] removed", dev->mtd.index,
Stephane Chazelas0bc88c52008-04-18 13:44:15 -0700471 dev->mtd.name + strlen("block2mtd: "));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 list_del(&dev->list);
473 block2mtd_free_device(dev);
474 }
475}
476
477
478module_init(block2mtd_init);
479module_exit(block2mtd_exit);
480
481MODULE_LICENSE("GPL");
Joern Engel2b54aae2008-02-06 01:38:02 -0800482MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483MODULE_DESCRIPTION("Emulate an MTD using a block device");