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/backing-dev.h>
24 #include <linux/bio.h>
25 #include <linux/pagemap.h>
26 #include <linux/list.h>
27 #include <linux/init.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/mutex.h>
30 #include <linux/mount.h>
31 #include <linux/slab.h>
32 #include <linux/major.h>
34 /* Info for the block device */
35 struct block2mtd_dev
{
36 struct list_head list
;
37 struct block_device
*blkdev
;
39 struct mutex write_mutex
;
43 /* Static info about the MTD, used in cleanup_module */
44 static LIST_HEAD(blkmtd_device_list
);
47 static struct page
*page_read(struct address_space
*mapping
, int index
)
49 return read_mapping_page(mapping
, index
, NULL
);
52 /* erase a specified part of the device */
53 static int _block2mtd_erase(struct block2mtd_dev
*dev
, loff_t to
, size_t len
)
55 struct address_space
*mapping
= dev
->blkdev
->bd_inode
->i_mapping
;
57 int index
= to
>> PAGE_SHIFT
; // page index
58 int pages
= len
>> PAGE_SHIFT
;
63 page
= page_read(mapping
, index
);
67 max
= page_address(page
) + PAGE_SIZE
;
68 for (p
=page_address(page
); p
<max
; p
++)
71 memset(page_address(page
), 0xff, PAGE_SIZE
);
74 balance_dirty_pages_ratelimited(mapping
);
78 page_cache_release(page
);
84 static int block2mtd_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
86 struct block2mtd_dev
*dev
= mtd
->priv
;
87 size_t from
= instr
->addr
;
88 size_t len
= instr
->len
;
91 instr
->state
= MTD_ERASING
;
92 mutex_lock(&dev
->write_mutex
);
93 err
= _block2mtd_erase(dev
, from
, len
);
94 mutex_unlock(&dev
->write_mutex
);
96 pr_err("erase failed err = %d\n", err
);
97 instr
->state
= MTD_ERASE_FAILED
;
99 instr
->state
= MTD_ERASE_DONE
;
101 mtd_erase_callback(instr
);
106 static int block2mtd_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
107 size_t *retlen
, u_char
*buf
)
109 struct block2mtd_dev
*dev
= mtd
->priv
;
111 int index
= from
>> PAGE_SHIFT
;
112 int offset
= from
& (PAGE_SIZE
-1);
116 if ((offset
+ len
) > PAGE_SIZE
)
117 cpylen
= PAGE_SIZE
- offset
; // multiple pages
119 cpylen
= len
; // this page
122 page
= page_read(dev
->blkdev
->bd_inode
->i_mapping
, index
);
124 return PTR_ERR(page
);
126 memcpy(buf
, page_address(page
) + offset
, cpylen
);
127 page_cache_release(page
);
139 /* write data to the underlying device */
140 static int _block2mtd_write(struct block2mtd_dev
*dev
, const u_char
*buf
,
141 loff_t to
, size_t len
, size_t *retlen
)
144 struct address_space
*mapping
= dev
->blkdev
->bd_inode
->i_mapping
;
145 int index
= to
>> PAGE_SHIFT
; // page index
146 int offset
= to
& ~PAGE_MASK
; // page offset
150 if ((offset
+len
) > PAGE_SIZE
)
151 cpylen
= PAGE_SIZE
- offset
; // multiple pages
153 cpylen
= len
; // this page
156 page
= page_read(mapping
, index
);
158 return PTR_ERR(page
);
160 if (memcmp(page_address(page
)+offset
, buf
, cpylen
)) {
162 memcpy(page_address(page
) + offset
, buf
, cpylen
);
163 set_page_dirty(page
);
165 balance_dirty_pages_ratelimited(mapping
);
167 page_cache_release(page
);
180 static int block2mtd_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
181 size_t *retlen
, const u_char
*buf
)
183 struct block2mtd_dev
*dev
= mtd
->priv
;
186 mutex_lock(&dev
->write_mutex
);
187 err
= _block2mtd_write(dev
, buf
, to
, len
, retlen
);
188 mutex_unlock(&dev
->write_mutex
);
195 /* sync the device - wait until the write queue is empty */
196 static void block2mtd_sync(struct mtd_info
*mtd
)
198 struct block2mtd_dev
*dev
= mtd
->priv
;
199 sync_blockdev(dev
->blkdev
);
204 static void block2mtd_free_device(struct block2mtd_dev
*dev
)
209 kfree(dev
->mtd
.name
);
212 invalidate_mapping_pages(dev
->blkdev
->bd_inode
->i_mapping
,
214 blkdev_put(dev
->blkdev
, FMODE_READ
|FMODE_WRITE
|FMODE_EXCL
);
221 static struct block2mtd_dev
*add_device(char *devname
, int erase_size
,
227 const fmode_t mode
= FMODE_READ
| FMODE_WRITE
| FMODE_EXCL
;
228 struct block_device
*bdev
= ERR_PTR(-ENODEV
);
229 struct block2mtd_dev
*dev
;
235 dev
= kzalloc(sizeof(struct block2mtd_dev
), GFP_KERNEL
);
239 /* Get a handle on the device */
240 bdev
= blkdev_get_by_path(devname
, mode
, dev
);
244 * We might not have the root device mounted at this point.
245 * Try to resolve the device name by other means.
247 for (i
= 0; IS_ERR(bdev
) && i
<= timeout
; i
++) {
252 * Calling wait_for_device_probe in the first loop
253 * was not enough, sleep for a bit in subsequent
257 wait_for_device_probe();
259 devt
= name_to_dev_t(devname
);
262 bdev
= blkdev_get_by_dev(devt
, mode
, dev
);
267 pr_err("error: cannot open device %s\n", devname
);
268 goto err_free_block2mtd
;
272 if (MAJOR(bdev
->bd_dev
) == MTD_BLOCK_MAJOR
) {
273 pr_err("attempting to use an MTD device as a block device\n");
274 goto err_free_block2mtd
;
277 if ((long)dev
->blkdev
->bd_inode
->i_size
% erase_size
) {
278 pr_err("erasesize must be a divisor of device size\n");
279 goto err_free_block2mtd
;
282 mutex_init(&dev
->write_mutex
);
284 /* Setup the MTD structure */
285 /* make the name contain the block device in */
286 name
= kasprintf(GFP_KERNEL
, "block2mtd: %s", devname
);
288 goto err_destroy_mutex
;
290 dev
->mtd
.name
= name
;
292 dev
->mtd
.size
= dev
->blkdev
->bd_inode
->i_size
& PAGE_MASK
;
293 dev
->mtd
.erasesize
= erase_size
;
294 dev
->mtd
.writesize
= 1;
295 dev
->mtd
.writebufsize
= PAGE_SIZE
;
296 dev
->mtd
.type
= MTD_RAM
;
297 dev
->mtd
.flags
= MTD_CAP_RAM
;
298 dev
->mtd
._erase
= block2mtd_erase
;
299 dev
->mtd
._write
= block2mtd_write
;
300 dev
->mtd
._sync
= block2mtd_sync
;
301 dev
->mtd
._read
= block2mtd_read
;
303 dev
->mtd
.owner
= THIS_MODULE
;
305 if (mtd_device_register(&dev
->mtd
, NULL
, 0)) {
306 /* Device didn't get added, so free the entry */
307 goto err_destroy_mutex
;
310 list_add(&dev
->list
, &blkmtd_device_list
);
311 pr_info("mtd%d: [%s] erase_size = %dKiB [%d]\n",
313 dev
->mtd
.name
+ strlen("block2mtd: "),
314 dev
->mtd
.erasesize
>> 10, dev
->mtd
.erasesize
);
318 mutex_destroy(&dev
->write_mutex
);
320 block2mtd_free_device(dev
);
325 /* This function works similar to reguler strtoul. In addition, it
326 * allows some suffixes for a more human-readable number format:
327 * ki, Ki, kiB, KiB - multiply result with 1024
328 * Mi, MiB - multiply result with 1024^2
329 * Gi, GiB - multiply result with 1024^3
331 static int ustrtoul(const char *cp
, char **endp
, unsigned int base
)
333 unsigned long result
= simple_strtoul(cp
, endp
, base
);
342 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
343 if ((*endp
)[1] == 'i') {
344 if ((*endp
)[2] == 'B')
354 static int parse_num(size_t *num
, const char *token
)
359 n
= (size_t) ustrtoul(token
, &endp
, 0);
368 static inline void kill_final_newline(char *str
)
370 char *newline
= strrchr(str
, '\n');
371 if (newline
&& !newline
[1])
377 static int block2mtd_init_called
= 0;
378 /* 80 for device, 12 for erase size */
379 static char block2mtd_paramline
[80 + 12];
382 static int block2mtd_setup2(const char *val
)
384 /* 80 for device, 12 for erase size, 80 for name, 8 for timeout */
385 char buf
[80 + 12 + 80 + 8];
389 size_t erase_size
= PAGE_SIZE
;
390 unsigned long timeout
= MTD_DEFAULT_TIMEOUT
;
393 if (strnlen(val
, sizeof(buf
)) >= sizeof(buf
)) {
394 pr_err("parameter too long\n");
399 kill_final_newline(str
);
401 for (i
= 0; i
< 2; i
++)
402 token
[i
] = strsep(&str
, ",");
405 pr_err("too many arguments\n");
410 pr_err("no argument\n");
415 if (strlen(name
) + 1 > 80) {
416 pr_err("device name too long\n");
421 ret
= parse_num(&erase_size
, token
[1]);
423 pr_err("illegal erase size\n");
428 add_device(name
, erase_size
, timeout
);
434 static int block2mtd_setup(const char *val
, struct kernel_param
*kp
)
437 return block2mtd_setup2(val
);
439 /* If more parameters are later passed in via
440 /sys/module/block2mtd/parameters/block2mtd
441 and block2mtd_init() has already been called,
442 we can parse the argument now. */
444 if (block2mtd_init_called
)
445 return block2mtd_setup2(val
);
447 /* During early boot stage, we only save the parameters
448 here. We must parse them later: if the param passed
449 from kernel boot command line, block2mtd_setup() is
450 called so early that it is not possible to resolve
451 the device (even kmalloc() fails). Deter that work to
452 block2mtd_setup2(). */
454 strlcpy(block2mtd_paramline
, val
, sizeof(block2mtd_paramline
));
461 module_param_call(block2mtd
, block2mtd_setup
, NULL
, NULL
, 0200);
462 MODULE_PARM_DESC(block2mtd
, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
464 static int __init
block2mtd_init(void)
469 if (strlen(block2mtd_paramline
))
470 ret
= block2mtd_setup2(block2mtd_paramline
);
471 block2mtd_init_called
= 1;
478 static void block2mtd_exit(void)
480 struct list_head
*pos
, *next
;
482 /* Remove the MTD devices */
483 list_for_each_safe(pos
, next
, &blkmtd_device_list
) {
484 struct block2mtd_dev
*dev
= list_entry(pos
, typeof(*dev
), list
);
485 block2mtd_sync(&dev
->mtd
);
486 mtd_device_unregister(&dev
->mtd
);
487 mutex_destroy(&dev
->write_mutex
);
488 pr_info("mtd%d: [%s] removed\n",
490 dev
->mtd
.name
+ strlen("block2mtd: "));
491 list_del(&dev
->list
);
492 block2mtd_free_device(dev
);
496 late_initcall(block2mtd_init
);
497 module_exit(block2mtd_exit
);
499 MODULE_LICENSE("GPL");
500 MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
501 MODULE_DESCRIPTION("Emulate an MTD using a block device");