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
13 * When the first attempt at device initialization fails, we may need to
14 * wait a little bit and retry. This timeout, by default 3 seconds, gives
15 * device time to start up. Required on BCM2708 and a few other chipsets.
17 #define MTD_DEFAULT_TIMEOUT 3
19 #include <linux/module.h>
20 #include <linux/delay.h>
22 #include <linux/blkdev.h>
23 #include <linux/bio.h>
24 #include <linux/pagemap.h>
25 #include <linux/list.h>
26 #include <linux/init.h>
27 #include <linux/mtd/mtd.h>
28 #include <linux/mutex.h>
29 #include <linux/mount.h>
30 #include <linux/slab.h>
31 #include <linux/major.h>
33 /* Info for the block device */
34 struct block2mtd_dev
{
35 struct list_head list
;
36 struct block_device
*blkdev
;
38 struct mutex write_mutex
;
42 /* Static info about the MTD, used in cleanup_module */
43 static LIST_HEAD(blkmtd_device_list
);
46 static struct page
*page_read(struct address_space
*mapping
, int index
)
48 return read_mapping_page(mapping
, index
, NULL
);
51 /* erase a specified part of the device */
52 static int _block2mtd_erase(struct block2mtd_dev
*dev
, loff_t to
, size_t len
)
54 struct address_space
*mapping
= dev
->blkdev
->bd_inode
->i_mapping
;
56 int index
= to
>> PAGE_SHIFT
; // page index
57 int pages
= len
>> PAGE_SHIFT
;
62 page
= page_read(mapping
, index
);
66 max
= page_address(page
) + PAGE_SIZE
;
67 for (p
=page_address(page
); p
<max
; p
++)
70 memset(page_address(page
), 0xff, PAGE_SIZE
);
73 balance_dirty_pages_ratelimited(mapping
);
77 page_cache_release(page
);
83 static int block2mtd_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
85 struct block2mtd_dev
*dev
= mtd
->priv
;
86 size_t from
= instr
->addr
;
87 size_t len
= instr
->len
;
90 instr
->state
= MTD_ERASING
;
91 mutex_lock(&dev
->write_mutex
);
92 err
= _block2mtd_erase(dev
, from
, len
);
93 mutex_unlock(&dev
->write_mutex
);
95 pr_err("erase failed err = %d\n", err
);
96 instr
->state
= MTD_ERASE_FAILED
;
98 instr
->state
= MTD_ERASE_DONE
;
100 mtd_erase_callback(instr
);
105 static int block2mtd_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
106 size_t *retlen
, u_char
*buf
)
108 struct block2mtd_dev
*dev
= mtd
->priv
;
110 int index
= from
>> PAGE_SHIFT
;
111 int offset
= from
& (PAGE_SIZE
-1);
115 if ((offset
+ len
) > PAGE_SIZE
)
116 cpylen
= PAGE_SIZE
- offset
; // multiple pages
118 cpylen
= len
; // this page
121 page
= page_read(dev
->blkdev
->bd_inode
->i_mapping
, index
);
123 return PTR_ERR(page
);
125 memcpy(buf
, page_address(page
) + offset
, cpylen
);
126 page_cache_release(page
);
138 /* write data to the underlying device */
139 static int _block2mtd_write(struct block2mtd_dev
*dev
, const u_char
*buf
,
140 loff_t to
, size_t len
, size_t *retlen
)
143 struct address_space
*mapping
= dev
->blkdev
->bd_inode
->i_mapping
;
144 int index
= to
>> PAGE_SHIFT
; // page index
145 int offset
= to
& ~PAGE_MASK
; // page offset
149 if ((offset
+len
) > PAGE_SIZE
)
150 cpylen
= PAGE_SIZE
- offset
; // multiple pages
152 cpylen
= len
; // this page
155 page
= page_read(mapping
, index
);
157 return PTR_ERR(page
);
159 if (memcmp(page_address(page
)+offset
, buf
, cpylen
)) {
161 memcpy(page_address(page
) + offset
, buf
, cpylen
);
162 set_page_dirty(page
);
164 balance_dirty_pages_ratelimited(mapping
);
166 page_cache_release(page
);
179 static int block2mtd_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
180 size_t *retlen
, const u_char
*buf
)
182 struct block2mtd_dev
*dev
= mtd
->priv
;
185 mutex_lock(&dev
->write_mutex
);
186 err
= _block2mtd_write(dev
, buf
, to
, len
, retlen
);
187 mutex_unlock(&dev
->write_mutex
);
194 /* sync the device - wait until the write queue is empty */
195 static void block2mtd_sync(struct mtd_info
*mtd
)
197 struct block2mtd_dev
*dev
= mtd
->priv
;
198 sync_blockdev(dev
->blkdev
);
203 static void block2mtd_free_device(struct block2mtd_dev
*dev
)
208 kfree(dev
->mtd
.name
);
211 invalidate_mapping_pages(dev
->blkdev
->bd_inode
->i_mapping
,
213 blkdev_put(dev
->blkdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
220 static struct block2mtd_dev
*add_device(char *devname
, int erase_size
,
226 const fmode_t mode
= FMODE_READ
| FMODE_WRITE
| FMODE_EXCL
;
227 struct block_device
*bdev
= ERR_PTR(-ENODEV
);
228 struct block2mtd_dev
*dev
;
234 dev
= kzalloc(sizeof(struct block2mtd_dev
), GFP_KERNEL
);
238 /* Get a handle on the device */
239 bdev
= blkdev_get_by_path(devname
, mode
, dev
);
243 * We might not have the root device mounted at this point.
244 * Try to resolve the device name by other means.
246 for (i
= 0; IS_ERR(bdev
) && i
<= timeout
; i
++) {
251 * Calling wait_for_device_probe in the first loop
252 * was not enough, sleep for a bit in subsequent
256 wait_for_device_probe();
258 devt
= name_to_dev_t(devname
);
261 bdev
= blkdev_get_by_dev(devt
, mode
, dev
);
266 pr_err("error: cannot open device %s\n", devname
);
267 goto err_free_block2mtd
;
271 if (MAJOR(bdev
->bd_dev
) == MTD_BLOCK_MAJOR
) {
272 pr_err("attempting to use an MTD device as a block device\n");
273 goto err_free_block2mtd
;
276 if ((long)dev
->blkdev
->bd_inode
->i_size
% erase_size
) {
277 pr_err("erasesize must be a divisor of device size\n");
278 goto err_free_block2mtd
;
281 mutex_init(&dev
->write_mutex
);
283 /* Setup the MTD structure */
284 /* make the name contain the block device in */
285 name
= kasprintf(GFP_KERNEL
, "block2mtd: %s", devname
);
287 goto err_destroy_mutex
;
289 dev
->mtd
.name
= name
;
291 dev
->mtd
.size
= dev
->blkdev
->bd_inode
->i_size
& PAGE_MASK
;
292 dev
->mtd
.erasesize
= erase_size
;
293 dev
->mtd
.writesize
= 1;
294 dev
->mtd
.writebufsize
= PAGE_SIZE
;
295 dev
->mtd
.type
= MTD_RAM
;
296 dev
->mtd
.flags
= MTD_CAP_RAM
;
297 dev
->mtd
._erase
= block2mtd_erase
;
298 dev
->mtd
._write
= block2mtd_write
;
299 dev
->mtd
._sync
= block2mtd_sync
;
300 dev
->mtd
._read
= block2mtd_read
;
302 dev
->mtd
.owner
= THIS_MODULE
;
304 if (mtd_device_register(&dev
->mtd
, NULL
, 0)) {
305 /* Device didn't get added, so free the entry */
306 goto err_destroy_mutex
;
309 list_add(&dev
->list
, &blkmtd_device_list
);
310 pr_info("mtd%d: [%s] erase_size = %dKiB [%d]\n",
312 dev
->mtd
.name
+ strlen("block2mtd: "),
313 dev
->mtd
.erasesize
>> 10, dev
->mtd
.erasesize
);
317 mutex_destroy(&dev
->write_mutex
);
319 block2mtd_free_device(dev
);
324 /* This function works similar to reguler strtoul. In addition, it
325 * allows some suffixes for a more human-readable number format:
326 * ki, Ki, kiB, KiB - multiply result with 1024
327 * Mi, MiB - multiply result with 1024^2
328 * Gi, GiB - multiply result with 1024^3
330 static int ustrtoul(const char *cp
, char **endp
, unsigned int base
)
332 unsigned long result
= simple_strtoul(cp
, endp
, base
);
341 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
342 if ((*endp
)[1] == 'i') {
343 if ((*endp
)[2] == 'B')
353 static int parse_num(size_t *num
, const char *token
)
358 n
= (size_t) ustrtoul(token
, &endp
, 0);
367 static inline void kill_final_newline(char *str
)
369 char *newline
= strrchr(str
, '\n');
370 if (newline
&& !newline
[1])
376 static int block2mtd_init_called
= 0;
377 /* 80 for device, 12 for erase size */
378 static char block2mtd_paramline
[80 + 12];
381 static int block2mtd_setup2(const char *val
)
383 /* 80 for device, 12 for erase size, 80 for name, 8 for timeout */
384 char buf
[80 + 12 + 80 + 8];
388 size_t erase_size
= PAGE_SIZE
;
389 unsigned long timeout
= MTD_DEFAULT_TIMEOUT
;
392 if (strnlen(val
, sizeof(buf
)) >= sizeof(buf
)) {
393 pr_err("parameter too long\n");
398 kill_final_newline(str
);
400 for (i
= 0; i
< 2; i
++)
401 token
[i
] = strsep(&str
, ",");
404 pr_err("too many arguments\n");
409 pr_err("no argument\n");
414 if (strlen(name
) + 1 > 80) {
415 pr_err("device name too long\n");
420 ret
= parse_num(&erase_size
, token
[1]);
422 pr_err("illegal erase size\n");
427 add_device(name
, erase_size
, timeout
);
433 static int block2mtd_setup(const char *val
, struct kernel_param
*kp
)
436 return block2mtd_setup2(val
);
438 /* If more parameters are later passed in via
439 /sys/module/block2mtd/parameters/block2mtd
440 and block2mtd_init() has already been called,
441 we can parse the argument now. */
443 if (block2mtd_init_called
)
444 return block2mtd_setup2(val
);
446 /* During early boot stage, we only save the parameters
447 here. We must parse them later: if the param passed
448 from kernel boot command line, block2mtd_setup() is
449 called so early that it is not possible to resolve
450 the device (even kmalloc() fails). Deter that work to
451 block2mtd_setup2(). */
453 strlcpy(block2mtd_paramline
, val
, sizeof(block2mtd_paramline
));
460 module_param_call(block2mtd
, block2mtd_setup
, NULL
, NULL
, 0200);
461 MODULE_PARM_DESC(block2mtd
, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
463 static int __init
block2mtd_init(void)
468 if (strlen(block2mtd_paramline
))
469 ret
= block2mtd_setup2(block2mtd_paramline
);
470 block2mtd_init_called
= 1;
477 static void block2mtd_exit(void)
479 struct list_head
*pos
, *next
;
481 /* Remove the MTD devices */
482 list_for_each_safe(pos
, next
, &blkmtd_device_list
) {
483 struct block2mtd_dev
*dev
= list_entry(pos
, typeof(*dev
), list
);
484 block2mtd_sync(&dev
->mtd
);
485 mtd_device_unregister(&dev
->mtd
);
486 mutex_destroy(&dev
->write_mutex
);
487 pr_info("mtd%d: [%s] removed\n",
489 dev
->mtd
.name
+ strlen("block2mtd: "));
490 list_del(&dev
->list
);
491 block2mtd_free_device(dev
);
495 late_initcall(block2mtd_init
);
496 module_exit(block2mtd_exit
);
498 MODULE_LICENSE("GPL");
499 MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
500 MODULE_DESCRIPTION("Emulate an MTD using a block device");