blob: 8debac9efde986b16b37a5586feb75fff5bf9f26 [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>
Ingo Molnar48b19262006-03-31 02:29:41 -080017#include <linux/mutex.h>
Ville Hervac4e7fb32006-07-14 00:31:16 +030018#include <linux/mount.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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/* Info for the block device */
26struct block2mtd_dev {
27 struct list_head list;
28 struct block_device *blkdev;
29 struct mtd_info mtd;
Ingo Molnar48b19262006-03-31 02:29:41 -080030 struct mutex write_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031};
32
33
34/* Static info about the MTD, used in cleanup_module */
35static LIST_HEAD(blkmtd_device_list);
36
37
Nick Piggin6fe69002007-05-06 14:49:04 -070038static struct page *page_read(struct address_space *mapping, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Nick Piggin6fe69002007-05-06 14:49:04 -070040 return read_mapping_page(mapping, index, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* erase a specified part of the device */
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; // page index
49 int pages = len >> PAGE_SHIFT;
50 u_long *p;
51 u_long *max;
52
53 while (pages) {
Joern Engel21d31f12007-02-20 20:22:22 +010054 page = page_read(mapping, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (IS_ERR(page))
56 return PTR_ERR(page);
57
Joern Engel0ffb74c2007-02-20 20:20:58 +010058 max = page_address(page) + PAGE_SIZE;
59 for (p=page_address(page); p<max; p++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 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 break;
66 }
67
68 page_cache_release(page);
69 pages--;
70 index++;
71 }
72 return 0;
73}
74static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
75{
76 struct block2mtd_dev *dev = mtd->priv;
77 size_t from = instr->addr;
78 size_t len = instr->len;
79 int err;
80
81 instr->state = MTD_ERASING;
Ingo Molnar48b19262006-03-31 02:29:41 -080082 mutex_lock(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 err = _block2mtd_erase(dev, from, len);
Ingo Molnar48b19262006-03-31 02:29:41 -080084 mutex_unlock(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (err) {
86 ERROR("erase failed err = %d", err);
87 instr->state = MTD_ERASE_FAILED;
88 } else
89 instr->state = MTD_ERASE_DONE;
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 mtd_erase_callback(instr);
92 return err;
93}
94
95
96static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
97 size_t *retlen, u_char *buf)
98{
99 struct block2mtd_dev *dev = mtd->priv;
100 struct page *page;
101 int index = from >> PAGE_SHIFT;
Joern Engel711c11b2005-03-07 20:29:09 +0000102 int offset = from & (PAGE_SIZE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 int cpylen;
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 while (len) {
106 if ((offset + len) > PAGE_SIZE)
107 cpylen = PAGE_SIZE - offset; // multiple pages
108 else
109 cpylen = len; // this page
110 len = len - cpylen;
111
Joern Engel21d31f12007-02-20 20:22:22 +0100112 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (IS_ERR(page))
114 return PTR_ERR(page);
115
116 memcpy(buf, page_address(page) + offset, cpylen);
117 page_cache_release(page);
118
119 if (retlen)
120 *retlen += cpylen;
121 buf += cpylen;
122 offset = 0;
123 index++;
124 }
125 return 0;
126}
127
128
129/* write data to the underlying device */
130static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
131 loff_t to, size_t len, size_t *retlen)
132{
133 struct page *page;
134 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
135 int index = to >> PAGE_SHIFT; // page index
136 int offset = to & ~PAGE_MASK; // page offset
137 int cpylen;
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 while (len) {
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000140 if ((offset+len) > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 cpylen = PAGE_SIZE - offset; // multiple pages
142 else
143 cpylen = len; // this page
144 len = len - cpylen;
145
Joern Engel21d31f12007-02-20 20:22:22 +0100146 page = page_read(mapping, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (IS_ERR(page))
148 return PTR_ERR(page);
149
150 if (memcmp(page_address(page)+offset, buf, cpylen)) {
151 lock_page(page);
152 memcpy(page_address(page) + offset, buf, cpylen);
153 set_page_dirty(page);
154 unlock_page(page);
155 }
156 page_cache_release(page);
157
158 if (retlen)
159 *retlen += cpylen;
160
161 buf += cpylen;
162 offset = 0;
163 index++;
164 }
165 return 0;
166}
Ville Hervac4e7fb32006-07-14 00:31:16 +0300167
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
170 size_t *retlen, const u_char *buf)
171{
172 struct block2mtd_dev *dev = mtd->priv;
173 int err;
174
Ingo Molnar48b19262006-03-31 02:29:41 -0800175 mutex_lock(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 err = _block2mtd_write(dev, buf, to, len, retlen);
Ingo Molnar48b19262006-03-31 02:29:41 -0800177 mutex_unlock(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (err > 0)
179 err = 0;
180 return err;
181}
182
183
184/* sync the device - wait until the write queue is empty */
185static void block2mtd_sync(struct mtd_info *mtd)
186{
187 struct block2mtd_dev *dev = mtd->priv;
188 sync_blockdev(dev->blkdev);
189 return;
190}
191
192
193static void block2mtd_free_device(struct block2mtd_dev *dev)
194{
195 if (!dev)
196 return;
197
198 kfree(dev->mtd.name);
199
200 if (dev->blkdev) {
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800201 invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
202 0, -1);
Tejun Heoe525fd82010-11-13 11:55:17 +0100203 blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205
206 kfree(dev);
207}
208
209
210/* FIXME: ensure that mtd->size % erase_size == 0 */
211static struct block2mtd_dev *add_device(char *devname, int erase_size)
212{
Tejun Heoe525fd82010-11-13 11:55:17 +0100213 const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 struct block_device *bdev;
215 struct block2mtd_dev *dev;
Greg Kroah-Hartmaneadcf0d2008-07-02 12:46:22 -0700216 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 if (!devname)
219 return NULL;
220
Burman Yan95b93a02006-11-15 21:10:29 +0200221 dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (!dev)
223 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 /* Get a handle on the device */
Tejun Heod4d77622010-11-13 11:55:18 +0100226 bdev = blkdev_get_by_path(devname, mode, dev);
Ville Hervac4e7fb32006-07-14 00:31:16 +0300227#ifndef MODULE
228 if (IS_ERR(bdev)) {
229
230 /* We might not have rootfs mounted at this point. Try
231 to resolve the device name by other means. */
232
Joern Engel88705302007-02-20 20:21:41 +0100233 dev_t devt = name_to_dev_t(devname);
Tejun Heoe525fd82010-11-13 11:55:17 +0100234 if (devt)
Tejun Heod4d77622010-11-13 11:55:18 +0100235 bdev = blkdev_get_by_dev(devt, mode, dev);
Ville Hervac4e7fb32006-07-14 00:31:16 +0300236 }
237#endif
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (IS_ERR(bdev)) {
240 ERROR("error: cannot open device %s", devname);
241 goto devinit_err;
242 }
243 dev->blkdev = bdev;
244
245 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
246 ERROR("attempting to use an MTD device as a block device");
247 goto devinit_err;
248 }
249
Ingo Molnar48b19262006-03-31 02:29:41 -0800250 mutex_init(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /* Setup the MTD structure */
253 /* make the name contain the block device in */
Julia Lawall4d682422010-03-10 22:15:19 +0100254 name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
Greg Kroah-Hartmaneadcf0d2008-07-02 12:46:22 -0700255 if (!name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 goto devinit_err;
257
Greg Kroah-Hartmaneadcf0d2008-07-02 12:46:22 -0700258 dev->mtd.name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
261 dev->mtd.erasesize = erase_size;
Artem B. Bityutskiy17ffc7b2006-06-22 18:15:48 +0400262 dev->mtd.writesize = 1;
Artem Bityutskiyb6043872012-02-03 09:32:44 +0200263 dev->mtd.writebufsize = PAGE_SIZE;
David Woodhouse21c8db92006-06-14 21:39:48 +0100264 dev->mtd.type = MTD_RAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 dev->mtd.flags = MTD_CAP_RAM;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200266 dev->mtd._erase = block2mtd_erase;
267 dev->mtd._write = block2mtd_write;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200268 dev->mtd._sync = block2mtd_sync;
269 dev->mtd._read = block2mtd_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 dev->mtd.priv = dev;
271 dev->mtd.owner = THIS_MODULE;
272
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100273 if (mtd_device_register(&dev->mtd, NULL, 0)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300274 /* Device didn't get added, so free the entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 goto devinit_err;
276 }
277 list_add(&dev->list, &blkmtd_device_list);
278 INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev->mtd.index,
Stephane Chazelas0bc88c52008-04-18 13:44:15 -0700279 dev->mtd.name + strlen("block2mtd: "),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 dev->mtd.erasesize >> 10, dev->mtd.erasesize);
281 return dev;
282
283devinit_err:
284 block2mtd_free_device(dev);
285 return NULL;
286}
287
288
Joern Engel954c2422006-04-18 21:03:08 -0700289/* This function works similar to reguler strtoul. In addition, it
290 * allows some suffixes for a more human-readable number format:
291 * ki, Ki, kiB, KiB - multiply result with 1024
292 * Mi, MiB - multiply result with 1024^2
293 * Gi, GiB - multiply result with 1024^3
294 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295static int ustrtoul(const char *cp, char **endp, unsigned int base)
296{
297 unsigned long result = simple_strtoul(cp, endp, base);
298 switch (**endp) {
299 case 'G' :
300 result *= 1024;
301 case 'M':
302 result *= 1024;
Joern Engel954c2422006-04-18 21:03:08 -0700303 case 'K':
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 case 'k':
305 result *= 1024;
306 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
Joern Engel954c2422006-04-18 21:03:08 -0700307 if ((*endp)[1] == 'i') {
308 if ((*endp)[2] == 'B')
309 (*endp) += 3;
310 else
311 (*endp) += 2;
312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314 return result;
315}
316
317
Thomas Gleixnercc712292005-03-19 22:40:47 +0000318static int parse_num(size_t *num, const char *token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 char *endp;
Thomas Gleixnercc712292005-03-19 22:40:47 +0000321 size_t n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Thomas Gleixnercc712292005-03-19 22:40:47 +0000323 n = (size_t) ustrtoul(token, &endp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (*endp)
325 return -EINVAL;
326
Thomas Gleixnercc712292005-03-19 22:40:47 +0000327 *num = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return 0;
329}
330
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static inline void kill_final_newline(char *str)
333{
334 char *newline = strrchr(str, '\n');
335 if (newline && !newline[1])
336 *newline = 0;
337}
338
339
Stephane Chazelas0bc88c52008-04-18 13:44:15 -0700340#define parse_err(fmt, args...) do { \
341 ERROR(fmt, ## args); \
342 return 0; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343} while (0)
344
Ville Hervac4e7fb32006-07-14 00:31:16 +0300345#ifndef MODULE
346static int block2mtd_init_called = 0;
Adrian Bunk4839f042007-05-02 12:33:17 +0100347static char block2mtd_paramline[80 + 12]; /* 80 for device, 12 for erase size */
Ville Hervac4e7fb32006-07-14 00:31:16 +0300348#endif
349
350
351static int block2mtd_setup2(const char *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Ville Hervac4e7fb32006-07-14 00:31:16 +0300353 char buf[80 + 12]; /* 80 for device, 12 for erase size */
Jesper Juhla6550e52006-05-14 01:42:25 +0200354 char *str = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 char *token[2];
356 char *name;
Thomas Gleixnercc712292005-03-19 22:40:47 +0000357 size_t erase_size = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 int i, ret;
359
360 if (strnlen(val, sizeof(buf)) >= sizeof(buf))
361 parse_err("parameter too long");
362
363 strcpy(str, val);
364 kill_final_newline(str);
365
Jesper Juhla6550e52006-05-14 01:42:25 +0200366 for (i = 0; i < 2; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 token[i] = strsep(&str, ",");
368
369 if (str)
370 parse_err("too many arguments");
371
372 if (!token[0])
373 parse_err("no argument");
374
Ville Hervac4e7fb32006-07-14 00:31:16 +0300375 name = token[0];
376 if (strlen(name) + 1 > 80)
377 parse_err("device name too long");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 if (token[1]) {
Thomas Gleixnercc712292005-03-19 22:40:47 +0000380 ret = parse_num(&erase_size, token[1]);
Jesper Juhla6550e52006-05-14 01:42:25 +0200381 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 parse_err("illegal erase size");
Jesper Juhla6550e52006-05-14 01:42:25 +0200383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385
386 add_device(name, erase_size);
387
388 return 0;
389}
390
391
Ville Hervac4e7fb32006-07-14 00:31:16 +0300392static int block2mtd_setup(const char *val, struct kernel_param *kp)
393{
394#ifdef MODULE
395 return block2mtd_setup2(val);
396#else
397 /* If more parameters are later passed in via
398 /sys/module/block2mtd/parameters/block2mtd
399 and block2mtd_init() has already been called,
400 we can parse the argument now. */
401
402 if (block2mtd_init_called)
403 return block2mtd_setup2(val);
404
405 /* During early boot stage, we only save the parameters
406 here. We must parse them later: if the param passed
407 from kernel boot command line, block2mtd_setup() is
408 called so early that it is not possible to resolve
409 the device (even kmalloc() fails). Deter that work to
410 block2mtd_setup2(). */
411
412 strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
413
414 return 0;
415#endif
416}
417
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
420MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
421
422static int __init block2mtd_init(void)
423{
Ville Hervac4e7fb32006-07-14 00:31:16 +0300424 int ret = 0;
Ville Hervac4e7fb32006-07-14 00:31:16 +0300425
426#ifndef MODULE
427 if (strlen(block2mtd_paramline))
428 ret = block2mtd_setup2(block2mtd_paramline);
429 block2mtd_init_called = 1;
430#endif
431
432 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
435
Bill Pemberton810b7e02012-11-19 13:26:04 -0500436static void block2mtd_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 struct list_head *pos, *next;
439
440 /* Remove the MTD devices */
441 list_for_each_safe(pos, next, &blkmtd_device_list) {
442 struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
443 block2mtd_sync(&dev->mtd);
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100444 mtd_device_unregister(&dev->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 INFO("mtd%d: [%s] removed", dev->mtd.index,
Stephane Chazelas0bc88c52008-04-18 13:44:15 -0700446 dev->mtd.name + strlen("block2mtd: "));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 list_del(&dev->list);
448 block2mtd_free_device(dev);
449 }
450}
451
452
453module_init(block2mtd_init);
454module_exit(block2mtd_exit);
455
456MODULE_LICENSE("GPL");
Joern Engel2b54aae2008-02-06 01:38:02 -0800457MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458MODULE_DESCRIPTION("Emulate an MTD using a block device");