2 * block2mtd.c - create an mtd from a block device
4 * Copyright (C) 2001,2002 Simon Evans <spse@secret.org.uk>
5 * Copyright (C) 2004-2006 Joern Engel <joern@wh.fh-wedel.de>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/module.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>
20 #include <linux/mutex.h>
21 #include <linux/mount.h>
22 #include <linux/slab.h>
24 /* Info for the block device */
25 struct block2mtd_dev
{
26 struct list_head list
;
27 struct block_device
*blkdev
;
29 struct mutex write_mutex
;
33 /* Static info about the MTD, used in cleanup_module */
34 static LIST_HEAD(blkmtd_device_list
);
37 static struct page
*page_read(struct address_space
*mapping
, int index
)
39 return read_mapping_page(mapping
, index
, NULL
);
42 /* erase a specified part of the device */
43 static int _block2mtd_erase(struct block2mtd_dev
*dev
, loff_t to
, size_t len
)
45 struct address_space
*mapping
= dev
->blkdev
->bd_inode
->i_mapping
;
47 int index
= to
>> PAGE_SHIFT
; // page index
48 int pages
= len
>> PAGE_SHIFT
;
53 page
= page_read(mapping
, index
);
57 max
= page_address(page
) + PAGE_SIZE
;
58 for (p
=page_address(page
); p
<max
; p
++)
61 memset(page_address(page
), 0xff, PAGE_SIZE
);
64 balance_dirty_pages_ratelimited(mapping
);
68 page_cache_release(page
);
74 static int block2mtd_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
76 struct block2mtd_dev
*dev
= mtd
->priv
;
77 size_t from
= instr
->addr
;
78 size_t len
= instr
->len
;
81 instr
->state
= MTD_ERASING
;
82 mutex_lock(&dev
->write_mutex
);
83 err
= _block2mtd_erase(dev
, from
, len
);
84 mutex_unlock(&dev
->write_mutex
);
86 pr_err("erase failed err = %d\n", err
);
87 instr
->state
= MTD_ERASE_FAILED
;
89 instr
->state
= MTD_ERASE_DONE
;
91 mtd_erase_callback(instr
);
96 static int block2mtd_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
97 size_t *retlen
, u_char
*buf
)
99 struct block2mtd_dev
*dev
= mtd
->priv
;
101 int index
= from
>> PAGE_SHIFT
;
102 int offset
= from
& (PAGE_SIZE
-1);
106 if ((offset
+ len
) > PAGE_SIZE
)
107 cpylen
= PAGE_SIZE
- offset
; // multiple pages
109 cpylen
= len
; // this page
112 page
= page_read(dev
->blkdev
->bd_inode
->i_mapping
, index
);
114 return PTR_ERR(page
);
116 memcpy(buf
, page_address(page
) + offset
, cpylen
);
117 page_cache_release(page
);
129 /* write data to the underlying device */
130 static int _block2mtd_write(struct block2mtd_dev
*dev
, const u_char
*buf
,
131 loff_t to
, size_t len
, size_t *retlen
)
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
140 if ((offset
+len
) > PAGE_SIZE
)
141 cpylen
= PAGE_SIZE
- offset
; // multiple pages
143 cpylen
= len
; // this page
146 page
= page_read(mapping
, index
);
148 return PTR_ERR(page
);
150 if (memcmp(page_address(page
)+offset
, buf
, cpylen
)) {
152 memcpy(page_address(page
) + offset
, buf
, cpylen
);
153 set_page_dirty(page
);
155 balance_dirty_pages_ratelimited(mapping
);
157 page_cache_release(page
);
170 static int block2mtd_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
171 size_t *retlen
, const u_char
*buf
)
173 struct block2mtd_dev
*dev
= mtd
->priv
;
176 mutex_lock(&dev
->write_mutex
);
177 err
= _block2mtd_write(dev
, buf
, to
, len
, retlen
);
178 mutex_unlock(&dev
->write_mutex
);
185 /* sync the device - wait until the write queue is empty */
186 static void block2mtd_sync(struct mtd_info
*mtd
)
188 struct block2mtd_dev
*dev
= mtd
->priv
;
189 sync_blockdev(dev
->blkdev
);
194 static void block2mtd_free_device(struct block2mtd_dev
*dev
)
199 kfree(dev
->mtd
.name
);
202 invalidate_mapping_pages(dev
->blkdev
->bd_inode
->i_mapping
,
204 blkdev_put(dev
->blkdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
211 /* FIXME: ensure that mtd->size % erase_size == 0 */
212 static struct block2mtd_dev
*add_device(char *devname
, int erase_size
)
214 const fmode_t mode
= FMODE_READ
| FMODE_WRITE
| FMODE_EXCL
;
215 struct block_device
*bdev
;
216 struct block2mtd_dev
*dev
;
222 dev
= kzalloc(sizeof(struct block2mtd_dev
), GFP_KERNEL
);
226 /* Get a handle on the device */
227 bdev
= blkdev_get_by_path(devname
, mode
, dev
);
231 /* We might not have rootfs mounted at this point. Try
232 to resolve the device name by other means. */
234 dev_t devt
= name_to_dev_t(devname
);
236 bdev
= blkdev_get_by_dev(devt
, mode
, dev
);
241 pr_err("error: cannot open device %s\n", devname
);
246 if (MAJOR(bdev
->bd_dev
) == MTD_BLOCK_MAJOR
) {
247 pr_err("attempting to use an MTD device as a block device\n");
251 mutex_init(&dev
->write_mutex
);
253 /* Setup the MTD structure */
254 /* make the name contain the block device in */
255 name
= kasprintf(GFP_KERNEL
, "block2mtd: %s", devname
);
259 dev
->mtd
.name
= name
;
261 dev
->mtd
.size
= dev
->blkdev
->bd_inode
->i_size
& PAGE_MASK
;
262 dev
->mtd
.erasesize
= erase_size
;
263 dev
->mtd
.writesize
= 1;
264 dev
->mtd
.writebufsize
= PAGE_SIZE
;
265 dev
->mtd
.type
= MTD_RAM
;
266 dev
->mtd
.flags
= MTD_CAP_RAM
;
267 dev
->mtd
._erase
= block2mtd_erase
;
268 dev
->mtd
._write
= block2mtd_write
;
269 dev
->mtd
._sync
= block2mtd_sync
;
270 dev
->mtd
._read
= block2mtd_read
;
272 dev
->mtd
.owner
= THIS_MODULE
;
274 if (mtd_device_register(&dev
->mtd
, NULL
, 0)) {
275 /* Device didn't get added, so free the entry */
278 list_add(&dev
->list
, &blkmtd_device_list
);
279 pr_info("mtd%d: [%s] erase_size = %dKiB [%d]\n",
281 dev
->mtd
.name
+ strlen("block2mtd: "),
282 dev
->mtd
.erasesize
>> 10, dev
->mtd
.erasesize
);
286 block2mtd_free_device(dev
);
291 /* 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
297 static int ustrtoul(const char *cp
, char **endp
, unsigned int base
)
299 unsigned long result
= simple_strtoul(cp
, endp
, base
);
308 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
309 if ((*endp
)[1] == 'i') {
310 if ((*endp
)[2] == 'B')
320 static int parse_num(size_t *num
, const char *token
)
325 n
= (size_t) ustrtoul(token
, &endp
, 0);
334 static inline void kill_final_newline(char *str
)
336 char *newline
= strrchr(str
, '\n');
337 if (newline
&& !newline
[1])
343 static int block2mtd_init_called
= 0;
344 static char block2mtd_paramline
[80 + 12]; /* 80 for device, 12 for erase size */
347 static int block2mtd_setup2(const char *val
)
349 char buf
[80 + 12]; /* 80 for device, 12 for erase size */
353 size_t erase_size
= PAGE_SIZE
;
356 if (strnlen(val
, sizeof(buf
)) >= sizeof(buf
)) {
357 pr_err("parameter too long\n");
362 kill_final_newline(str
);
364 for (i
= 0; i
< 2; i
++)
365 token
[i
] = strsep(&str
, ",");
368 pr_err("too many arguments\n");
373 pr_err("no argument\n");
378 if (strlen(name
) + 1 > 80) {
379 pr_err("device name too long\n");
384 ret
= parse_num(&erase_size
, token
[1]);
386 pr_err("illegal erase size\n");
391 add_device(name
, erase_size
);
397 static int block2mtd_setup(const char *val
, struct kernel_param
*kp
)
400 return block2mtd_setup2(val
);
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. */
407 if (block2mtd_init_called
)
408 return block2mtd_setup2(val
);
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(). */
417 strlcpy(block2mtd_paramline
, val
, sizeof(block2mtd_paramline
));
424 module_param_call(block2mtd
, block2mtd_setup
, NULL
, NULL
, 0200);
425 MODULE_PARM_DESC(block2mtd
, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
427 static int __init
block2mtd_init(void)
432 if (strlen(block2mtd_paramline
))
433 ret
= block2mtd_setup2(block2mtd_paramline
);
434 block2mtd_init_called
= 1;
441 static void block2mtd_exit(void)
443 struct list_head
*pos
, *next
;
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
);
449 mtd_device_unregister(&dev
->mtd
);
450 pr_info("mtd%d: [%s] removed\n",
452 dev
->mtd
.name
+ strlen("block2mtd: "));
453 list_del(&dev
->list
);
454 block2mtd_free_device(dev
);
459 module_init(block2mtd_init
);
460 module_exit(block2mtd_exit
);
462 MODULE_LICENSE("GPL");
463 MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
464 MODULE_DESCRIPTION("Emulate an MTD using a block device");