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>
9 #include <linux/module.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>
17 #include <linux/mutex.h>
18 #include <linux/mount.h>
19 #include <linux/slab.h>
21 #define ERROR(fmt, args...) printk(KERN_ERR "block2mtd: " fmt "\n" , ## args)
22 #define INFO(fmt, args...) printk(KERN_INFO "block2mtd: " fmt "\n" , ## args)
25 /* Info for the block device */
26 struct block2mtd_dev
{
27 struct list_head list
;
28 struct block_device
*blkdev
;
30 struct mutex write_mutex
;
34 /* Static info about the MTD, used in cleanup_module */
35 static LIST_HEAD(blkmtd_device_list
);
38 static struct page
*page_read(struct address_space
*mapping
, int index
)
40 return read_mapping_page(mapping
, index
, NULL
);
43 /* erase a specified part of the device */
44 static int _block2mtd_erase(struct block2mtd_dev
*dev
, loff_t to
, size_t len
)
46 struct address_space
*mapping
= dev
->blkdev
->bd_inode
->i_mapping
;
48 int index
= to
>> PAGE_SHIFT
; // page index
49 int pages
= len
>> PAGE_SHIFT
;
54 page
= page_read(mapping
, index
);
58 max
= page_address(page
) + PAGE_SIZE
;
59 for (p
=page_address(page
); p
<max
; p
++)
62 memset(page_address(page
), 0xff, PAGE_SIZE
);
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 ERROR("erase failed err = %d", 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
);
156 page_cache_release(page
);
169 static int block2mtd_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
170 size_t *retlen
, const u_char
*buf
)
172 struct block2mtd_dev
*dev
= mtd
->priv
;
175 mutex_lock(&dev
->write_mutex
);
176 err
= _block2mtd_write(dev
, buf
, to
, len
, retlen
);
177 mutex_unlock(&dev
->write_mutex
);
184 /* sync the device - wait until the write queue is empty */
185 static void block2mtd_sync(struct mtd_info
*mtd
)
187 struct block2mtd_dev
*dev
= mtd
->priv
;
188 sync_blockdev(dev
->blkdev
);
193 static void block2mtd_free_device(struct block2mtd_dev
*dev
)
198 kfree(dev
->mtd
.name
);
201 invalidate_mapping_pages(dev
->blkdev
->bd_inode
->i_mapping
,
203 blkdev_put(dev
->blkdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
210 /* FIXME: ensure that mtd->size % erase_size == 0 */
211 static struct block2mtd_dev
*add_device(char *devname
, int erase_size
)
213 const fmode_t mode
= FMODE_READ
| FMODE_WRITE
| FMODE_EXCL
;
214 struct block_device
*bdev
;
215 struct block2mtd_dev
*dev
;
221 dev
= kzalloc(sizeof(struct block2mtd_dev
), GFP_KERNEL
);
225 /* Get a handle on the device */
226 bdev
= blkdev_get_by_path(devname
, mode
, dev
);
230 /* We might not have rootfs mounted at this point. Try
231 to resolve the device name by other means. */
233 dev_t devt
= name_to_dev_t(devname
);
235 bdev
= blkdev_get_by_dev(devt
, mode
, dev
);
240 ERROR("error: cannot open device %s", devname
);
245 if (MAJOR(bdev
->bd_dev
) == MTD_BLOCK_MAJOR
) {
246 ERROR("attempting to use an MTD device as a block device");
250 mutex_init(&dev
->write_mutex
);
252 /* Setup the MTD structure */
253 /* make the name contain the block device in */
254 name
= kasprintf(GFP_KERNEL
, "block2mtd: %s", devname
);
258 dev
->mtd
.name
= name
;
260 dev
->mtd
.size
= dev
->blkdev
->bd_inode
->i_size
& PAGE_MASK
;
261 dev
->mtd
.erasesize
= erase_size
;
262 dev
->mtd
.writesize
= 1;
263 dev
->mtd
.writebufsize
= PAGE_SIZE
;
264 dev
->mtd
.type
= MTD_RAM
;
265 dev
->mtd
.flags
= MTD_CAP_RAM
;
266 dev
->mtd
._erase
= block2mtd_erase
;
267 dev
->mtd
._write
= block2mtd_write
;
268 dev
->mtd
._sync
= block2mtd_sync
;
269 dev
->mtd
._read
= block2mtd_read
;
271 dev
->mtd
.owner
= THIS_MODULE
;
273 if (mtd_device_register(&dev
->mtd
, NULL
, 0)) {
274 /* Device didn't get added, so free the entry */
277 list_add(&dev
->list
, &blkmtd_device_list
);
278 INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev
->mtd
.index
,
279 dev
->mtd
.name
+ strlen("block2mtd: "),
280 dev
->mtd
.erasesize
>> 10, dev
->mtd
.erasesize
);
284 block2mtd_free_device(dev
);
289 /* 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
295 static int ustrtoul(const char *cp
, char **endp
, unsigned int base
)
297 unsigned long result
= simple_strtoul(cp
, endp
, base
);
306 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
307 if ((*endp
)[1] == 'i') {
308 if ((*endp
)[2] == 'B')
318 static int parse_num(size_t *num
, const char *token
)
323 n
= (size_t) ustrtoul(token
, &endp
, 0);
332 static inline void kill_final_newline(char *str
)
334 char *newline
= strrchr(str
, '\n');
335 if (newline
&& !newline
[1])
340 #define parse_err(fmt, args...) do { \
341 ERROR(fmt, ## args); \
346 static int block2mtd_init_called
= 0;
347 static char block2mtd_paramline
[80 + 12]; /* 80 for device, 12 for erase size */
351 static int block2mtd_setup2(const char *val
)
353 char buf
[80 + 12]; /* 80 for device, 12 for erase size */
357 size_t erase_size
= PAGE_SIZE
;
360 if (strnlen(val
, sizeof(buf
)) >= sizeof(buf
))
361 parse_err("parameter too long");
364 kill_final_newline(str
);
366 for (i
= 0; i
< 2; i
++)
367 token
[i
] = strsep(&str
, ",");
370 parse_err("too many arguments");
373 parse_err("no argument");
376 if (strlen(name
) + 1 > 80)
377 parse_err("device name too long");
380 ret
= parse_num(&erase_size
, token
[1]);
382 parse_err("illegal erase size");
386 add_device(name
, erase_size
);
392 static int block2mtd_setup(const char *val
, struct kernel_param
*kp
)
395 return block2mtd_setup2(val
);
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. */
402 if (block2mtd_init_called
)
403 return block2mtd_setup2(val
);
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(). */
412 strlcpy(block2mtd_paramline
, val
, sizeof(block2mtd_paramline
));
419 module_param_call(block2mtd
, block2mtd_setup
, NULL
, NULL
, 0200);
420 MODULE_PARM_DESC(block2mtd
, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
422 static int __init
block2mtd_init(void)
427 if (strlen(block2mtd_paramline
))
428 ret
= block2mtd_setup2(block2mtd_paramline
);
429 block2mtd_init_called
= 1;
436 static void __devexit
block2mtd_exit(void)
438 struct list_head
*pos
, *next
;
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
);
444 mtd_device_unregister(&dev
->mtd
);
445 INFO("mtd%d: [%s] removed", dev
->mtd
.index
,
446 dev
->mtd
.name
+ strlen("block2mtd: "));
447 list_del(&dev
->list
);
448 block2mtd_free_device(dev
);
453 module_init(block2mtd_init
);
454 module_exit(block2mtd_exit
);
456 MODULE_LICENSE("GPL");
457 MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
458 MODULE_DESCRIPTION("Emulate an MTD using a block device");