blob: 0fccf149f5aa9126758ad1e3760b22b7a80088a9 [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 (!page)
56 return -ENOMEM;
57 if (IS_ERR(page))
58 return PTR_ERR(page);
59
Joern Engel0ffb74c2007-02-20 20:20:58 +010060 max = page_address(page) + PAGE_SIZE;
61 for (p=page_address(page); p<max; p++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 if (*p != -1UL) {
63 lock_page(page);
64 memset(page_address(page), 0xff, PAGE_SIZE);
65 set_page_dirty(page);
66 unlock_page(page);
67 break;
68 }
69
70 page_cache_release(page);
71 pages--;
72 index++;
73 }
74 return 0;
75}
76static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
77{
78 struct block2mtd_dev *dev = mtd->priv;
79 size_t from = instr->addr;
80 size_t len = instr->len;
81 int err;
82
83 instr->state = MTD_ERASING;
Ingo Molnar48b19262006-03-31 02:29:41 -080084 mutex_lock(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 err = _block2mtd_erase(dev, from, len);
Ingo Molnar48b19262006-03-31 02:29:41 -080086 mutex_unlock(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (err) {
88 ERROR("erase failed err = %d", err);
89 instr->state = MTD_ERASE_FAILED;
90 } else
91 instr->state = MTD_ERASE_DONE;
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 mtd_erase_callback(instr);
94 return err;
95}
96
97
98static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
99 size_t *retlen, u_char *buf)
100{
101 struct block2mtd_dev *dev = mtd->priv;
102 struct page *page;
103 int index = from >> PAGE_SHIFT;
Joern Engel711c11b2005-03-07 20:29:09 +0000104 int offset = from & (PAGE_SIZE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int cpylen;
106
107 if (from > mtd->size)
108 return -EINVAL;
109 if (from + len > mtd->size)
110 len = mtd->size - from;
111
112 if (retlen)
113 *retlen = 0;
114
115 while (len) {
116 if ((offset + len) > PAGE_SIZE)
117 cpylen = PAGE_SIZE - offset; // multiple pages
118 else
119 cpylen = len; // this page
120 len = len - cpylen;
121
Joern Engel21d31f12007-02-20 20:22:22 +0100122 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (!page)
124 return -ENOMEM;
125 if (IS_ERR(page))
126 return PTR_ERR(page);
127
128 memcpy(buf, page_address(page) + offset, cpylen);
129 page_cache_release(page);
130
131 if (retlen)
132 *retlen += cpylen;
133 buf += cpylen;
134 offset = 0;
135 index++;
136 }
137 return 0;
138}
139
140
141/* write data to the underlying device */
142static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
143 loff_t to, size_t len, size_t *retlen)
144{
145 struct page *page;
146 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
147 int index = to >> PAGE_SHIFT; // page index
148 int offset = to & ~PAGE_MASK; // page offset
149 int cpylen;
150
151 if (retlen)
152 *retlen = 0;
153 while (len) {
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000154 if ((offset+len) > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 cpylen = PAGE_SIZE - offset; // multiple pages
156 else
157 cpylen = len; // this page
158 len = len - cpylen;
159
Joern Engel21d31f12007-02-20 20:22:22 +0100160 page = page_read(mapping, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (!page)
162 return -ENOMEM;
163 if (IS_ERR(page))
164 return PTR_ERR(page);
165
166 if (memcmp(page_address(page)+offset, buf, cpylen)) {
167 lock_page(page);
168 memcpy(page_address(page) + offset, buf, cpylen);
169 set_page_dirty(page);
170 unlock_page(page);
171 }
172 page_cache_release(page);
173
174 if (retlen)
175 *retlen += cpylen;
176
177 buf += cpylen;
178 offset = 0;
179 index++;
180 }
181 return 0;
182}
Ville Hervac4e7fb32006-07-14 00:31:16 +0300183
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
186 size_t *retlen, const u_char *buf)
187{
188 struct block2mtd_dev *dev = mtd->priv;
189 int err;
190
191 if (!len)
192 return 0;
193 if (to >= mtd->size)
194 return -ENOSPC;
195 if (to + len > mtd->size)
196 len = mtd->size - to;
197
Ingo Molnar48b19262006-03-31 02:29:41 -0800198 mutex_lock(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 err = _block2mtd_write(dev, buf, to, len, retlen);
Ingo Molnar48b19262006-03-31 02:29:41 -0800200 mutex_unlock(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (err > 0)
202 err = 0;
203 return err;
204}
205
206
207/* sync the device - wait until the write queue is empty */
208static void block2mtd_sync(struct mtd_info *mtd)
209{
210 struct block2mtd_dev *dev = mtd->priv;
211 sync_blockdev(dev->blkdev);
212 return;
213}
214
215
216static void block2mtd_free_device(struct block2mtd_dev *dev)
217{
218 if (!dev)
219 return;
220
221 kfree(dev->mtd.name);
222
223 if (dev->blkdev) {
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800224 invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
225 0, -1);
Tejun Heoe525fd82010-11-13 11:55:17 +0100226 blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228
229 kfree(dev);
230}
231
232
233/* FIXME: ensure that mtd->size % erase_size == 0 */
234static struct block2mtd_dev *add_device(char *devname, int erase_size)
235{
Tejun Heoe525fd82010-11-13 11:55:17 +0100236 const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 struct block_device *bdev;
238 struct block2mtd_dev *dev;
Greg Kroah-Hartmaneadcf0d2008-07-02 12:46:22 -0700239 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 if (!devname)
242 return NULL;
243
Burman Yan95b93a02006-11-15 21:10:29 +0200244 dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (!dev)
246 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 /* Get a handle on the device */
Tejun Heod4d77622010-11-13 11:55:18 +0100249 bdev = blkdev_get_by_path(devname, mode, dev);
Ville Hervac4e7fb32006-07-14 00:31:16 +0300250#ifndef MODULE
251 if (IS_ERR(bdev)) {
252
253 /* We might not have rootfs mounted at this point. Try
254 to resolve the device name by other means. */
255
Joern Engel88705302007-02-20 20:21:41 +0100256 dev_t devt = name_to_dev_t(devname);
Tejun Heoe525fd82010-11-13 11:55:17 +0100257 if (devt)
Tejun Heod4d77622010-11-13 11:55:18 +0100258 bdev = blkdev_get_by_dev(devt, mode, dev);
Ville Hervac4e7fb32006-07-14 00:31:16 +0300259 }
260#endif
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (IS_ERR(bdev)) {
263 ERROR("error: cannot open device %s", devname);
264 goto devinit_err;
265 }
266 dev->blkdev = bdev;
267
268 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
269 ERROR("attempting to use an MTD device as a block device");
270 goto devinit_err;
271 }
272
Ingo Molnar48b19262006-03-31 02:29:41 -0800273 mutex_init(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /* Setup the MTD structure */
276 /* make the name contain the block device in */
Julia Lawall4d682422010-03-10 22:15:19 +0100277 name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
Greg Kroah-Hartmaneadcf0d2008-07-02 12:46:22 -0700278 if (!name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 goto devinit_err;
280
Greg Kroah-Hartmaneadcf0d2008-07-02 12:46:22 -0700281 dev->mtd.name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
284 dev->mtd.erasesize = erase_size;
Artem B. Bityutskiy17ffc7b2006-06-22 18:15:48 +0400285 dev->mtd.writesize = 1;
Artem Bityutskiyb6043872012-02-03 09:32:44 +0200286 dev->mtd.writebufsize = PAGE_SIZE;
David Woodhouse21c8db92006-06-14 21:39:48 +0100287 dev->mtd.type = MTD_RAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 dev->mtd.flags = MTD_CAP_RAM;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200289 dev->mtd._erase = block2mtd_erase;
290 dev->mtd._write = block2mtd_write;
291 dev->mtd._writev = mtd_writev;
292 dev->mtd._sync = block2mtd_sync;
293 dev->mtd._read = block2mtd_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 dev->mtd.priv = dev;
295 dev->mtd.owner = THIS_MODULE;
296
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100297 if (mtd_device_register(&dev->mtd, NULL, 0)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300298 /* Device didn't get added, so free the entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 goto devinit_err;
300 }
301 list_add(&dev->list, &blkmtd_device_list);
302 INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev->mtd.index,
Stephane Chazelas0bc88c52008-04-18 13:44:15 -0700303 dev->mtd.name + strlen("block2mtd: "),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 dev->mtd.erasesize >> 10, dev->mtd.erasesize);
305 return dev;
306
307devinit_err:
308 block2mtd_free_device(dev);
309 return NULL;
310}
311
312
Joern Engel954c2422006-04-18 21:03:08 -0700313/* This function works similar to reguler strtoul. In addition, it
314 * allows some suffixes for a more human-readable number format:
315 * ki, Ki, kiB, KiB - multiply result with 1024
316 * Mi, MiB - multiply result with 1024^2
317 * Gi, GiB - multiply result with 1024^3
318 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319static int ustrtoul(const char *cp, char **endp, unsigned int base)
320{
321 unsigned long result = simple_strtoul(cp, endp, base);
322 switch (**endp) {
323 case 'G' :
324 result *= 1024;
325 case 'M':
326 result *= 1024;
Joern Engel954c2422006-04-18 21:03:08 -0700327 case 'K':
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 case 'k':
329 result *= 1024;
330 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
Joern Engel954c2422006-04-18 21:03:08 -0700331 if ((*endp)[1] == 'i') {
332 if ((*endp)[2] == 'B')
333 (*endp) += 3;
334 else
335 (*endp) += 2;
336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338 return result;
339}
340
341
Thomas Gleixnercc712292005-03-19 22:40:47 +0000342static int parse_num(size_t *num, const char *token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 char *endp;
Thomas Gleixnercc712292005-03-19 22:40:47 +0000345 size_t n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Thomas Gleixnercc712292005-03-19 22:40:47 +0000347 n = (size_t) ustrtoul(token, &endp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (*endp)
349 return -EINVAL;
350
Thomas Gleixnercc712292005-03-19 22:40:47 +0000351 *num = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return 0;
353}
354
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356static inline void kill_final_newline(char *str)
357{
358 char *newline = strrchr(str, '\n');
359 if (newline && !newline[1])
360 *newline = 0;
361}
362
363
Stephane Chazelas0bc88c52008-04-18 13:44:15 -0700364#define parse_err(fmt, args...) do { \
365 ERROR(fmt, ## args); \
366 return 0; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367} while (0)
368
Ville Hervac4e7fb32006-07-14 00:31:16 +0300369#ifndef MODULE
370static int block2mtd_init_called = 0;
Adrian Bunk4839f042007-05-02 12:33:17 +0100371static char block2mtd_paramline[80 + 12]; /* 80 for device, 12 for erase size */
Ville Hervac4e7fb32006-07-14 00:31:16 +0300372#endif
373
374
375static int block2mtd_setup2(const char *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Ville Hervac4e7fb32006-07-14 00:31:16 +0300377 char buf[80 + 12]; /* 80 for device, 12 for erase size */
Jesper Juhla6550e52006-05-14 01:42:25 +0200378 char *str = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 char *token[2];
380 char *name;
Thomas Gleixnercc712292005-03-19 22:40:47 +0000381 size_t erase_size = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 int i, ret;
383
384 if (strnlen(val, sizeof(buf)) >= sizeof(buf))
385 parse_err("parameter too long");
386
387 strcpy(str, val);
388 kill_final_newline(str);
389
Jesper Juhla6550e52006-05-14 01:42:25 +0200390 for (i = 0; i < 2; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 token[i] = strsep(&str, ",");
392
393 if (str)
394 parse_err("too many arguments");
395
396 if (!token[0])
397 parse_err("no argument");
398
Ville Hervac4e7fb32006-07-14 00:31:16 +0300399 name = token[0];
400 if (strlen(name) + 1 > 80)
401 parse_err("device name too long");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 if (token[1]) {
Thomas Gleixnercc712292005-03-19 22:40:47 +0000404 ret = parse_num(&erase_size, token[1]);
Jesper Juhla6550e52006-05-14 01:42:25 +0200405 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 parse_err("illegal erase size");
Jesper Juhla6550e52006-05-14 01:42:25 +0200407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
410 add_device(name, erase_size);
411
412 return 0;
413}
414
415
Ville Hervac4e7fb32006-07-14 00:31:16 +0300416static int block2mtd_setup(const char *val, struct kernel_param *kp)
417{
418#ifdef MODULE
419 return block2mtd_setup2(val);
420#else
421 /* If more parameters are later passed in via
422 /sys/module/block2mtd/parameters/block2mtd
423 and block2mtd_init() has already been called,
424 we can parse the argument now. */
425
426 if (block2mtd_init_called)
427 return block2mtd_setup2(val);
428
429 /* During early boot stage, we only save the parameters
430 here. We must parse them later: if the param passed
431 from kernel boot command line, block2mtd_setup() is
432 called so early that it is not possible to resolve
433 the device (even kmalloc() fails). Deter that work to
434 block2mtd_setup2(). */
435
436 strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
437
438 return 0;
439#endif
440}
441
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
444MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
445
446static int __init block2mtd_init(void)
447{
Ville Hervac4e7fb32006-07-14 00:31:16 +0300448 int ret = 0;
Ville Hervac4e7fb32006-07-14 00:31:16 +0300449
450#ifndef MODULE
451 if (strlen(block2mtd_paramline))
452 ret = block2mtd_setup2(block2mtd_paramline);
453 block2mtd_init_called = 1;
454#endif
455
456 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459
460static void __devexit block2mtd_exit(void)
461{
462 struct list_head *pos, *next;
463
464 /* Remove the MTD devices */
465 list_for_each_safe(pos, next, &blkmtd_device_list) {
466 struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
467 block2mtd_sync(&dev->mtd);
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100468 mtd_device_unregister(&dev->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 INFO("mtd%d: [%s] removed", dev->mtd.index,
Stephane Chazelas0bc88c52008-04-18 13:44:15 -0700470 dev->mtd.name + strlen("block2mtd: "));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 list_del(&dev->list);
472 block2mtd_free_device(dev);
473 }
474}
475
476
477module_init(block2mtd_init);
478module_exit(block2mtd_exit);
479
480MODULE_LICENSE("GPL");
Joern Engel2b54aae2008-02-06 01:38:02 -0800481MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482MODULE_DESCRIPTION("Emulate an MTD using a block device");