blob: 5cb4c04726b2e0eb58dd6452b293602f82fa1945 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/* Info for the block device */
25struct block2mtd_dev {
26 struct list_head list;
27 struct block_device *blkdev;
28 struct mtd_info mtd;
Ingo Molnar48b19262006-03-31 02:29:41 -080029 struct mutex write_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030};
31
32
33/* Static info about the MTD, used in cleanup_module */
34static LIST_HEAD(blkmtd_device_list);
35
36
Nick Piggin6fe69002007-05-06 14:49:04 -070037static struct page *page_read(struct address_space *mapping, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Nick Piggin6fe69002007-05-06 14:49:04 -070039 return read_mapping_page(mapping, index, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/* erase a specified part of the device */
43static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
44{
45 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
46 struct page *page;
47 int index = to >> PAGE_SHIFT; // page index
48 int pages = len >> PAGE_SHIFT;
49 u_long *p;
50 u_long *max;
51
52 while (pages) {
Joern Engel21d31f12007-02-20 20:22:22 +010053 page = page_read(mapping, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (IS_ERR(page))
55 return PTR_ERR(page);
56
Joern Engel0ffb74c2007-02-20 20:20:58 +010057 max = page_address(page) + PAGE_SIZE;
58 for (p=page_address(page); p<max; p++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (*p != -1UL) {
60 lock_page(page);
61 memset(page_address(page), 0xff, PAGE_SIZE);
62 set_page_dirty(page);
63 unlock_page(page);
NeilBrown031da732012-12-12 19:32:15 +110064 balance_dirty_pages_ratelimited(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 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) {
Joe Perchesa1c06602013-04-19 10:59:35 -070086 pr_err("erase failed err = %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 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);
NeilBrown031da732012-12-12 19:32:15 +1100155 balance_dirty_pages_ratelimited(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157 page_cache_release(page);
158
159 if (retlen)
160 *retlen += cpylen;
161
162 buf += cpylen;
163 offset = 0;
164 index++;
165 }
166 return 0;
167}
Ville Hervac4e7fb32006-07-14 00:31:16 +0300168
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
171 size_t *retlen, const u_char *buf)
172{
173 struct block2mtd_dev *dev = mtd->priv;
174 int err;
175
Ingo Molnar48b19262006-03-31 02:29:41 -0800176 mutex_lock(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 err = _block2mtd_write(dev, buf, to, len, retlen);
Ingo Molnar48b19262006-03-31 02:29:41 -0800178 mutex_unlock(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (err > 0)
180 err = 0;
181 return err;
182}
183
184
185/* sync the device - wait until the write queue is empty */
186static void block2mtd_sync(struct mtd_info *mtd)
187{
188 struct block2mtd_dev *dev = mtd->priv;
189 sync_blockdev(dev->blkdev);
190 return;
191}
192
193
194static void block2mtd_free_device(struct block2mtd_dev *dev)
195{
196 if (!dev)
197 return;
198
199 kfree(dev->mtd.name);
200
201 if (dev->blkdev) {
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800202 invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
203 0, -1);
Tejun Heoe525fd82010-11-13 11:55:17 +0100204 blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206
207 kfree(dev);
208}
209
210
211/* FIXME: ensure that mtd->size % erase_size == 0 */
212static struct block2mtd_dev *add_device(char *devname, int erase_size)
213{
Tejun Heoe525fd82010-11-13 11:55:17 +0100214 const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 struct block_device *bdev;
216 struct block2mtd_dev *dev;
Greg Kroah-Hartmaneadcf0d2008-07-02 12:46:22 -0700217 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 if (!devname)
220 return NULL;
221
Burman Yan95b93a02006-11-15 21:10:29 +0200222 dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (!dev)
224 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 /* Get a handle on the device */
Tejun Heod4d77622010-11-13 11:55:18 +0100227 bdev = blkdev_get_by_path(devname, mode, dev);
Ville Hervac4e7fb32006-07-14 00:31:16 +0300228#ifndef MODULE
229 if (IS_ERR(bdev)) {
230
231 /* We might not have rootfs mounted at this point. Try
232 to resolve the device name by other means. */
233
Joern Engel88705302007-02-20 20:21:41 +0100234 dev_t devt = name_to_dev_t(devname);
Tejun Heoe525fd82010-11-13 11:55:17 +0100235 if (devt)
Tejun Heod4d77622010-11-13 11:55:18 +0100236 bdev = blkdev_get_by_dev(devt, mode, dev);
Ville Hervac4e7fb32006-07-14 00:31:16 +0300237 }
238#endif
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (IS_ERR(bdev)) {
Joe Perchesa1c06602013-04-19 10:59:35 -0700241 pr_err("error: cannot open device %s\n", devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 goto devinit_err;
243 }
244 dev->blkdev = bdev;
245
246 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
Joe Perchesa1c06602013-04-19 10:59:35 -0700247 pr_err("attempting to use an MTD device as a block device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 goto devinit_err;
249 }
250
Ingo Molnar48b19262006-03-31 02:29:41 -0800251 mutex_init(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /* Setup the MTD structure */
254 /* make the name contain the block device in */
Julia Lawall4d682422010-03-10 22:15:19 +0100255 name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
Greg Kroah-Hartmaneadcf0d2008-07-02 12:46:22 -0700256 if (!name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 goto devinit_err;
258
Greg Kroah-Hartmaneadcf0d2008-07-02 12:46:22 -0700259 dev->mtd.name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
262 dev->mtd.erasesize = erase_size;
Artem B. Bityutskiy17ffc7b2006-06-22 18:15:48 +0400263 dev->mtd.writesize = 1;
Artem Bityutskiyb6043872012-02-03 09:32:44 +0200264 dev->mtd.writebufsize = PAGE_SIZE;
David Woodhouse21c8db92006-06-14 21:39:48 +0100265 dev->mtd.type = MTD_RAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 dev->mtd.flags = MTD_CAP_RAM;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200267 dev->mtd._erase = block2mtd_erase;
268 dev->mtd._write = block2mtd_write;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200269 dev->mtd._sync = block2mtd_sync;
270 dev->mtd._read = block2mtd_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 dev->mtd.priv = dev;
272 dev->mtd.owner = THIS_MODULE;
273
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100274 if (mtd_device_register(&dev->mtd, NULL, 0)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300275 /* Device didn't get added, so free the entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 goto devinit_err;
277 }
278 list_add(&dev->list, &blkmtd_device_list);
Joe Perchesa1c06602013-04-19 10:59:35 -0700279 pr_info("mtd%d: [%s] erase_size = %dKiB [%d]\n",
280 dev->mtd.index,
281 dev->mtd.name + strlen("block2mtd: "),
282 dev->mtd.erasesize >> 10, dev->mtd.erasesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return dev;
284
285devinit_err:
286 block2mtd_free_device(dev);
287 return NULL;
288}
289
290
Joern Engel954c2422006-04-18 21:03:08 -0700291/* This function works similar to reguler strtoul. In addition, it
292 * allows some suffixes for a more human-readable number format:
293 * ki, Ki, kiB, KiB - multiply result with 1024
294 * Mi, MiB - multiply result with 1024^2
295 * Gi, GiB - multiply result with 1024^3
296 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297static 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;
Joern Engel954c2422006-04-18 21:03:08 -0700305 case 'K':
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 case 'k':
307 result *= 1024;
308 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
Joern Engel954c2422006-04-18 21:03:08 -0700309 if ((*endp)[1] == 'i') {
310 if ((*endp)[2] == 'B')
311 (*endp) += 3;
312 else
313 (*endp) += 2;
314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316 return result;
317}
318
319
Thomas Gleixnercc712292005-03-19 22:40:47 +0000320static int parse_num(size_t *num, const char *token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 char *endp;
Thomas Gleixnercc712292005-03-19 22:40:47 +0000323 size_t n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Thomas Gleixnercc712292005-03-19 22:40:47 +0000325 n = (size_t) ustrtoul(token, &endp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (*endp)
327 return -EINVAL;
328
Thomas Gleixnercc712292005-03-19 22:40:47 +0000329 *num = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return 0;
331}
332
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334static 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
Ville Hervac4e7fb32006-07-14 00:31:16 +0300342#ifndef MODULE
343static int block2mtd_init_called = 0;
Adrian Bunk4839f042007-05-02 12:33:17 +0100344static char block2mtd_paramline[80 + 12]; /* 80 for device, 12 for erase size */
Ville Hervac4e7fb32006-07-14 00:31:16 +0300345#endif
346
Ville Hervac4e7fb32006-07-14 00:31:16 +0300347static int block2mtd_setup2(const char *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Ville Hervac4e7fb32006-07-14 00:31:16 +0300349 char buf[80 + 12]; /* 80 for device, 12 for erase size */
Jesper Juhla6550e52006-05-14 01:42:25 +0200350 char *str = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 char *token[2];
352 char *name;
Thomas Gleixnercc712292005-03-19 22:40:47 +0000353 size_t erase_size = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 int i, ret;
355
Joe Perchesa1c06602013-04-19 10:59:35 -0700356 if (strnlen(val, sizeof(buf)) >= sizeof(buf)) {
357 pr_err("parameter too long\n");
358 return 0;
359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 strcpy(str, val);
362 kill_final_newline(str);
363
Jesper Juhla6550e52006-05-14 01:42:25 +0200364 for (i = 0; i < 2; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 token[i] = strsep(&str, ",");
366
Joe Perchesa1c06602013-04-19 10:59:35 -0700367 if (str) {
368 pr_err("too many arguments\n");
369 return 0;
370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Joe Perchesa1c06602013-04-19 10:59:35 -0700372 if (!token[0]) {
373 pr_err("no argument\n");
374 return 0;
375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Ville Hervac4e7fb32006-07-14 00:31:16 +0300377 name = token[0];
Joe Perchesa1c06602013-04-19 10:59:35 -0700378 if (strlen(name) + 1 > 80) {
379 pr_err("device name too long\n");
380 return 0;
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 if (token[1]) {
Thomas Gleixnercc712292005-03-19 22:40:47 +0000384 ret = parse_num(&erase_size, token[1]);
Jesper Juhla6550e52006-05-14 01:42:25 +0200385 if (ret) {
Joe Perchesa1c06602013-04-19 10:59:35 -0700386 pr_err("illegal erase size\n");
387 return 0;
Jesper Juhla6550e52006-05-14 01:42:25 +0200388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390
391 add_device(name, erase_size);
392
393 return 0;
394}
395
396
Ville Hervac4e7fb32006-07-14 00:31:16 +0300397static int block2mtd_setup(const char *val, struct kernel_param *kp)
398{
399#ifdef MODULE
400 return block2mtd_setup2(val);
401#else
402 /* If more parameters are later passed in via
403 /sys/module/block2mtd/parameters/block2mtd
404 and block2mtd_init() has already been called,
405 we can parse the argument now. */
406
407 if (block2mtd_init_called)
408 return block2mtd_setup2(val);
409
410 /* During early boot stage, we only save the parameters
411 here. We must parse them later: if the param passed
412 from kernel boot command line, block2mtd_setup() is
413 called so early that it is not possible to resolve
414 the device (even kmalloc() fails). Deter that work to
415 block2mtd_setup2(). */
416
417 strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
418
419 return 0;
420#endif
421}
422
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
425MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
426
427static int __init block2mtd_init(void)
428{
Ville Hervac4e7fb32006-07-14 00:31:16 +0300429 int ret = 0;
Ville Hervac4e7fb32006-07-14 00:31:16 +0300430
431#ifndef MODULE
432 if (strlen(block2mtd_paramline))
433 ret = block2mtd_setup2(block2mtd_paramline);
434 block2mtd_init_called = 1;
435#endif
436
437 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440
Bill Pemberton810b7e02012-11-19 13:26:04 -0500441static void block2mtd_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
443 struct list_head *pos, *next;
444
445 /* Remove the MTD devices */
446 list_for_each_safe(pos, next, &blkmtd_device_list) {
447 struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
448 block2mtd_sync(&dev->mtd);
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100449 mtd_device_unregister(&dev->mtd);
Joe Perchesa1c06602013-04-19 10:59:35 -0700450 pr_info("mtd%d: [%s] removed\n",
451 dev->mtd.index,
452 dev->mtd.name + strlen("block2mtd: "));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 list_del(&dev->list);
454 block2mtd_free_device(dev);
455 }
456}
457
458
459module_init(block2mtd_init);
460module_exit(block2mtd_exit);
461
462MODULE_LICENSE("GPL");
Joern Engel2b54aae2008-02-06 01:38:02 -0800463MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464MODULE_DESCRIPTION("Emulate an MTD using a block device");