blob: 3e12234b756595efc929f5d9741a2b3ef7a7fdb1 [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 */
Joe Perchesa1c06602013-04-19 10:59:35 -07009
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/fs.h>
14#include <linux/blkdev.h>
15#include <linux/bio.h>
16#include <linux/pagemap.h>
17#include <linux/list.h>
18#include <linux/init.h>
19#include <linux/mtd/mtd.h>
Ingo Molnar48b19262006-03-31 02:29:41 -080020#include <linux/mutex.h>
Ville Hervac4e7fb32006-07-14 00:31:16 +030021#include <linux/mount.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Ezequiel Garciaf83c3832013-10-13 18:05:23 -030023#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/* 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);
NeilBrown031da732012-12-12 19:32:15 +110065 balance_dirty_pages_ratelimited(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 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;
Ingo Molnar48b19262006-03-31 02:29:41 -080083 mutex_lock(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 err = _block2mtd_erase(dev, from, len);
Ingo Molnar48b19262006-03-31 02:29:41 -080085 mutex_unlock(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (err) {
Joe Perchesa1c06602013-04-19 10:59:35 -070087 pr_err("erase failed err = %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 instr->state = MTD_ERASE_FAILED;
89 } else
90 instr->state = MTD_ERASE_DONE;
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 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;
Joern Engel711c11b2005-03-07 20:29:09 +0000103 int offset = from & (PAGE_SIZE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 int cpylen;
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 while (len) {
107 if ((offset + len) > PAGE_SIZE)
108 cpylen = PAGE_SIZE - offset; // multiple pages
109 else
110 cpylen = len; // this page
111 len = len - cpylen;
112
Joern Engel21d31f12007-02-20 20:22:22 +0100113 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 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/* write data to the underlying device */
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; // page index
137 int offset = to & ~PAGE_MASK; // page offset
138 int cpylen;
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 while (len) {
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000141 if ((offset+len) > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 cpylen = PAGE_SIZE - offset; // multiple pages
143 else
144 cpylen = len; // this page
145 len = len - cpylen;
146
Joern Engel21d31f12007-02-20 20:22:22 +0100147 page = page_read(mapping, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 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);
NeilBrown031da732012-12-12 19:32:15 +1100156 balance_dirty_pages_ratelimited(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
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}
Ville Hervac4e7fb32006-07-14 00:31:16 +0300169
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static 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
Ingo Molnar48b19262006-03-31 02:29:41 -0800177 mutex_lock(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 err = _block2mtd_write(dev, buf, to, len, retlen);
Ingo Molnar48b19262006-03-31 02:29:41 -0800179 mutex_unlock(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (err > 0)
181 err = 0;
182 return err;
183}
184
185
186/* sync the device - wait until the write queue is empty */
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) {
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800203 invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
204 0, -1);
Tejun Heoe525fd82010-11-13 11:55:17 +0100205 blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207
208 kfree(dev);
209}
210
211
212/* FIXME: ensure that mtd->size % erase_size == 0 */
213static struct block2mtd_dev *add_device(char *devname, int erase_size)
214{
Tejun Heoe525fd82010-11-13 11:55:17 +0100215 const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 struct block_device *bdev;
217 struct block2mtd_dev *dev;
Greg Kroah-Hartmaneadcf0d2008-07-02 12:46:22 -0700218 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 if (!devname)
221 return NULL;
222
Burman Yan95b93a02006-11-15 21:10:29 +0200223 dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (!dev)
225 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* Get a handle on the device */
Tejun Heod4d77622010-11-13 11:55:18 +0100228 bdev = blkdev_get_by_path(devname, mode, dev);
Ville Hervac4e7fb32006-07-14 00:31:16 +0300229#ifndef MODULE
230 if (IS_ERR(bdev)) {
231
232 /* We might not have rootfs mounted at this point. Try
233 to resolve the device name by other means. */
234
Joern Engel88705302007-02-20 20:21:41 +0100235 dev_t devt = name_to_dev_t(devname);
Tejun Heoe525fd82010-11-13 11:55:17 +0100236 if (devt)
Tejun Heod4d77622010-11-13 11:55:18 +0100237 bdev = blkdev_get_by_dev(devt, mode, dev);
Ville Hervac4e7fb32006-07-14 00:31:16 +0300238 }
239#endif
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (IS_ERR(bdev)) {
Joe Perchesa1c06602013-04-19 10:59:35 -0700242 pr_err("error: cannot open device %s\n", devname);
Fabian Frederick4bed2072014-01-25 10:45:08 +0800243 goto err_free_block2mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245 dev->blkdev = bdev;
246
247 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
Joe Perchesa1c06602013-04-19 10:59:35 -0700248 pr_err("attempting to use an MTD device as a block device\n");
Fabian Frederick4bed2072014-01-25 10:45:08 +0800249 goto err_free_block2mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251
Ingo Molnar48b19262006-03-31 02:29:41 -0800252 mutex_init(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 /* Setup the MTD structure */
255 /* make the name contain the block device in */
Julia Lawall4d682422010-03-10 22:15:19 +0100256 name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
Greg Kroah-Hartmaneadcf0d2008-07-02 12:46:22 -0700257 if (!name)
Fabian Frederick4bed2072014-01-25 10:45:08 +0800258 goto err_destroy_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Greg Kroah-Hartmaneadcf0d2008-07-02 12:46:22 -0700260 dev->mtd.name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
263 dev->mtd.erasesize = erase_size;
Artem B. Bityutskiy17ffc7b2006-06-22 18:15:48 +0400264 dev->mtd.writesize = 1;
Artem Bityutskiyb6043872012-02-03 09:32:44 +0200265 dev->mtd.writebufsize = PAGE_SIZE;
David Woodhouse21c8db92006-06-14 21:39:48 +0100266 dev->mtd.type = MTD_RAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 dev->mtd.flags = MTD_CAP_RAM;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200268 dev->mtd._erase = block2mtd_erase;
269 dev->mtd._write = block2mtd_write;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200270 dev->mtd._sync = block2mtd_sync;
271 dev->mtd._read = block2mtd_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 dev->mtd.priv = dev;
273 dev->mtd.owner = THIS_MODULE;
274
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100275 if (mtd_device_register(&dev->mtd, NULL, 0)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300276 /* Device didn't get added, so free the entry */
Fabian Frederick4bed2072014-01-25 10:45:08 +0800277 goto err_destroy_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279 list_add(&dev->list, &blkmtd_device_list);
Joe Perchesa1c06602013-04-19 10:59:35 -0700280 pr_info("mtd%d: [%s] erase_size = %dKiB [%d]\n",
281 dev->mtd.index,
282 dev->mtd.name + strlen("block2mtd: "),
283 dev->mtd.erasesize >> 10, dev->mtd.erasesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return dev;
285
Fabian Frederick4bed2072014-01-25 10:45:08 +0800286err_destroy_mutex:
287 mutex_destroy(&dev->write_mutex);
288err_free_block2mtd:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 block2mtd_free_device(dev);
290 return NULL;
291}
292
293
Joern Engel954c2422006-04-18 21:03:08 -0700294/* This function works similar to reguler strtoul. In addition, it
295 * allows some suffixes for a more human-readable number format:
296 * ki, Ki, kiB, KiB - multiply result with 1024
297 * Mi, MiB - multiply result with 1024^2
298 * Gi, GiB - multiply result with 1024^3
299 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300static int ustrtoul(const char *cp, char **endp, unsigned int base)
301{
302 unsigned long result = simple_strtoul(cp, endp, base);
303 switch (**endp) {
304 case 'G' :
305 result *= 1024;
306 case 'M':
307 result *= 1024;
Joern Engel954c2422006-04-18 21:03:08 -0700308 case 'K':
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 case 'k':
310 result *= 1024;
311 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
Joern Engel954c2422006-04-18 21:03:08 -0700312 if ((*endp)[1] == 'i') {
313 if ((*endp)[2] == 'B')
314 (*endp) += 3;
315 else
316 (*endp) += 2;
317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319 return result;
320}
321
322
Thomas Gleixnercc712292005-03-19 22:40:47 +0000323static int parse_num(size_t *num, const char *token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 char *endp;
Thomas Gleixnercc712292005-03-19 22:40:47 +0000326 size_t n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Thomas Gleixnercc712292005-03-19 22:40:47 +0000328 n = (size_t) ustrtoul(token, &endp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (*endp)
330 return -EINVAL;
331
Thomas Gleixnercc712292005-03-19 22:40:47 +0000332 *num = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return 0;
334}
335
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337static inline void kill_final_newline(char *str)
338{
339 char *newline = strrchr(str, '\n');
340 if (newline && !newline[1])
341 *newline = 0;
342}
343
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
Ville Hervac4e7fb32006-07-14 00:31:16 +0300350static int block2mtd_setup2(const char *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Ville Hervac4e7fb32006-07-14 00:31:16 +0300352 char buf[80 + 12]; /* 80 for device, 12 for erase size */
Jesper Juhla6550e52006-05-14 01:42:25 +0200353 char *str = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 char *token[2];
355 char *name;
Thomas Gleixnercc712292005-03-19 22:40:47 +0000356 size_t erase_size = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 int i, ret;
358
Joe Perchesa1c06602013-04-19 10:59:35 -0700359 if (strnlen(val, sizeof(buf)) >= sizeof(buf)) {
360 pr_err("parameter too long\n");
361 return 0;
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 strcpy(str, val);
365 kill_final_newline(str);
366
Jesper Juhla6550e52006-05-14 01:42:25 +0200367 for (i = 0; i < 2; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 token[i] = strsep(&str, ",");
369
Joe Perchesa1c06602013-04-19 10:59:35 -0700370 if (str) {
371 pr_err("too many arguments\n");
372 return 0;
373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Joe Perchesa1c06602013-04-19 10:59:35 -0700375 if (!token[0]) {
376 pr_err("no argument\n");
377 return 0;
378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Ville Hervac4e7fb32006-07-14 00:31:16 +0300380 name = token[0];
Joe Perchesa1c06602013-04-19 10:59:35 -0700381 if (strlen(name) + 1 > 80) {
382 pr_err("device name too long\n");
383 return 0;
384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 if (token[1]) {
Thomas Gleixnercc712292005-03-19 22:40:47 +0000387 ret = parse_num(&erase_size, token[1]);
Jesper Juhla6550e52006-05-14 01:42:25 +0200388 if (ret) {
Joe Perchesa1c06602013-04-19 10:59:35 -0700389 pr_err("illegal erase size\n");
390 return 0;
Jesper Juhla6550e52006-05-14 01:42:25 +0200391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
394 add_device(name, erase_size);
395
396 return 0;
397}
398
399
Ville Hervac4e7fb32006-07-14 00:31:16 +0300400static int block2mtd_setup(const char *val, struct kernel_param *kp)
401{
402#ifdef MODULE
403 return block2mtd_setup2(val);
404#else
405 /* If more parameters are later passed in via
406 /sys/module/block2mtd/parameters/block2mtd
407 and block2mtd_init() has already been called,
408 we can parse the argument now. */
409
410 if (block2mtd_init_called)
411 return block2mtd_setup2(val);
412
413 /* During early boot stage, we only save the parameters
414 here. We must parse them later: if the param passed
415 from kernel boot command line, block2mtd_setup() is
416 called so early that it is not possible to resolve
417 the device (even kmalloc() fails). Deter that work to
418 block2mtd_setup2(). */
419
420 strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
421
422 return 0;
423#endif
424}
425
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
428MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
429
430static int __init block2mtd_init(void)
431{
Ville Hervac4e7fb32006-07-14 00:31:16 +0300432 int ret = 0;
Ville Hervac4e7fb32006-07-14 00:31:16 +0300433
434#ifndef MODULE
435 if (strlen(block2mtd_paramline))
436 ret = block2mtd_setup2(block2mtd_paramline);
437 block2mtd_init_called = 1;
438#endif
439
440 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
443
Bill Pemberton810b7e02012-11-19 13:26:04 -0500444static void block2mtd_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 struct list_head *pos, *next;
447
448 /* Remove the MTD devices */
449 list_for_each_safe(pos, next, &blkmtd_device_list) {
450 struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
451 block2mtd_sync(&dev->mtd);
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100452 mtd_device_unregister(&dev->mtd);
Fabian Frederick4bed2072014-01-25 10:45:08 +0800453 mutex_destroy(&dev->write_mutex);
Joe Perchesa1c06602013-04-19 10:59:35 -0700454 pr_info("mtd%d: [%s] removed\n",
455 dev->mtd.index,
456 dev->mtd.name + strlen("block2mtd: "));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 list_del(&dev->list);
458 block2mtd_free_device(dev);
459 }
460}
461
462
463module_init(block2mtd_init);
464module_exit(block2mtd_exit);
465
466MODULE_LICENSE("GPL");
Joern Engel2b54aae2008-02-06 01:38:02 -0800467MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468MODULE_DESCRIPTION("Emulate an MTD using a block device");