2 * $Id: block2mtd.c,v 1.30 2005/11/29 14:48:32 gleixner Exp $
4 * block2mtd.c - create an mtd from a block device
6 * Copyright (C) 2001,2002 Simon Evans <spse@secret.org.uk>
7 * Copyright (C) 2004-2006 Jörn Engel <joern@wh.fh-wedel.de>
11 #include <linux/module.h>
13 #include <linux/blkdev.h>
14 #include <linux/bio.h>
15 #include <linux/pagemap.h>
16 #include <linux/list.h>
17 #include <linux/init.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/buffer_head.h>
20 #include <linux/mutex.h>
21 #include <linux/mount.h>
23 #define VERSION "$Revision: 1.30 $"
26 #define ERROR(fmt, args...) printk(KERN_ERR "block2mtd: " fmt "\n" , ## args)
27 #define INFO(fmt, args...) printk(KERN_INFO "block2mtd: " fmt "\n" , ## args)
30 /* Info for the block device */
31 struct block2mtd_dev
{
32 struct list_head list
;
33 struct block_device
*blkdev
;
35 struct mutex write_mutex
;
39 /* Static info about the MTD, used in cleanup_module */
40 static LIST_HEAD(blkmtd_device_list
);
43 static struct page
* page_read(struct address_space
*mapping
, int index
)
45 filler_t
*filler
= (filler_t
*)mapping
->a_ops
->readpage
;
46 return read_cache_page(mapping
, index
, filler
, NULL
);
50 /* erase a specified part of the device */
51 static int _block2mtd_erase(struct block2mtd_dev
*dev
, loff_t to
, size_t len
)
53 struct address_space
*mapping
= dev
->blkdev
->bd_inode
->i_mapping
;
55 int index
= to
>> PAGE_SHIFT
; // page index
56 int pages
= len
>> PAGE_SHIFT
;
61 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
);
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 ERROR("erase failed err = %d", err
);
96 instr
->state
= MTD_ERASE_FAILED
;
98 instr
->state
= MTD_ERASE_DONE
;
100 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);
115 if (from
> mtd
->size
)
117 if (from
+ len
> mtd
->size
)
118 len
= mtd
->size
- from
;
124 if ((offset
+ len
) > PAGE_SIZE
)
125 cpylen
= PAGE_SIZE
- offset
; // multiple pages
127 cpylen
= len
; // this page
130 page
= page_read(dev
->blkdev
->bd_inode
->i_mapping
, index
);
134 return PTR_ERR(page
);
136 memcpy(buf
, page_address(page
) + offset
, cpylen
);
137 page_cache_release(page
);
149 /* write data to the underlying device */
150 static int _block2mtd_write(struct block2mtd_dev
*dev
, const u_char
*buf
,
151 loff_t to
, size_t len
, size_t *retlen
)
154 struct address_space
*mapping
= dev
->blkdev
->bd_inode
->i_mapping
;
155 int index
= to
>> PAGE_SHIFT
; // page index
156 int offset
= to
& ~PAGE_MASK
; // page offset
162 if ((offset
+len
) > PAGE_SIZE
)
163 cpylen
= PAGE_SIZE
- offset
; // multiple pages
165 cpylen
= len
; // this page
168 page
= page_read(mapping
, index
);
172 return PTR_ERR(page
);
174 if (memcmp(page_address(page
)+offset
, buf
, cpylen
)) {
176 memcpy(page_address(page
) + offset
, buf
, cpylen
);
177 set_page_dirty(page
);
180 page_cache_release(page
);
193 static int block2mtd_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
194 size_t *retlen
, const u_char
*buf
)
196 struct block2mtd_dev
*dev
= mtd
->priv
;
203 if (to
+ len
> mtd
->size
)
204 len
= mtd
->size
- to
;
206 mutex_lock(&dev
->write_mutex
);
207 err
= _block2mtd_write(dev
, buf
, to
, len
, retlen
);
208 mutex_unlock(&dev
->write_mutex
);
215 /* sync the device - wait until the write queue is empty */
216 static void block2mtd_sync(struct mtd_info
*mtd
)
218 struct block2mtd_dev
*dev
= mtd
->priv
;
219 sync_blockdev(dev
->blkdev
);
224 static void block2mtd_free_device(struct block2mtd_dev
*dev
)
229 kfree(dev
->mtd
.name
);
232 invalidate_mapping_pages(dev
->blkdev
->bd_inode
->i_mapping
,
234 close_bdev_excl(dev
->blkdev
);
241 /* FIXME: ensure that mtd->size % erase_size == 0 */
242 static struct block2mtd_dev
*add_device(char *devname
, int erase_size
)
244 struct block_device
*bdev
;
245 struct block2mtd_dev
*dev
;
250 dev
= kzalloc(sizeof(struct block2mtd_dev
), GFP_KERNEL
);
254 /* Get a handle on the device */
255 bdev
= open_bdev_excl(devname
, O_RDWR
, NULL
);
259 /* We might not have rootfs mounted at this point. Try
260 to resolve the device name by other means. */
262 dev_t devt
= name_to_dev_t(devname
);
264 bdev
= open_by_devnum(devt
, FMODE_WRITE
| FMODE_READ
);
270 ERROR("error: cannot open device %s", devname
);
275 if (MAJOR(bdev
->bd_dev
) == MTD_BLOCK_MAJOR
) {
276 ERROR("attempting to use an MTD device as a block device");
280 mutex_init(&dev
->write_mutex
);
282 /* Setup the MTD structure */
283 /* make the name contain the block device in */
284 dev
->mtd
.name
= kmalloc(sizeof("block2mtd: ") + strlen(devname
),
289 sprintf(dev
->mtd
.name
, "block2mtd: %s", devname
);
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
.type
= MTD_RAM
;
295 dev
->mtd
.flags
= MTD_CAP_RAM
;
296 dev
->mtd
.erase
= block2mtd_erase
;
297 dev
->mtd
.write
= block2mtd_write
;
298 dev
->mtd
.writev
= default_mtd_writev
;
299 dev
->mtd
.sync
= block2mtd_sync
;
300 dev
->mtd
.read
= block2mtd_read
;
302 dev
->mtd
.owner
= THIS_MODULE
;
304 if (add_mtd_device(&dev
->mtd
)) {
305 /* Device didnt get added, so free the entry */
308 list_add(&dev
->list
, &blkmtd_device_list
);
309 INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev
->mtd
.index
,
310 dev
->mtd
.name
+ strlen("blkmtd: "),
311 dev
->mtd
.erasesize
>> 10, dev
->mtd
.erasesize
);
315 block2mtd_free_device(dev
);
320 /* This function works similar to reguler strtoul. In addition, it
321 * allows some suffixes for a more human-readable number format:
322 * ki, Ki, kiB, KiB - multiply result with 1024
323 * Mi, MiB - multiply result with 1024^2
324 * Gi, GiB - multiply result with 1024^3
326 static int ustrtoul(const char *cp
, char **endp
, unsigned int base
)
328 unsigned long result
= simple_strtoul(cp
, endp
, base
);
337 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
338 if ((*endp
)[1] == 'i') {
339 if ((*endp
)[2] == 'B')
349 static int parse_num(size_t *num
, const char *token
)
354 n
= (size_t) ustrtoul(token
, &endp
, 0);
363 static inline void kill_final_newline(char *str
)
365 char *newline
= strrchr(str
, '\n');
366 if (newline
&& !newline
[1])
371 #define parse_err(fmt, args...) do { \
372 ERROR("block2mtd: " fmt "\n", ## args); \
377 static int block2mtd_init_called
= 0;
378 static __initdata
char block2mtd_paramline
[80 + 12]; /* 80 for device, 12 for erase size */
382 static int block2mtd_setup2(const char *val
)
384 char buf
[80 + 12]; /* 80 for device, 12 for erase size */
388 size_t erase_size
= PAGE_SIZE
;
391 if (strnlen(val
, sizeof(buf
)) >= sizeof(buf
))
392 parse_err("parameter too long");
395 kill_final_newline(str
);
397 for (i
= 0; i
< 2; i
++)
398 token
[i
] = strsep(&str
, ",");
401 parse_err("too many arguments");
404 parse_err("no argument");
407 if (strlen(name
) + 1 > 80)
408 parse_err("device name too long");
411 ret
= parse_num(&erase_size
, token
[1]);
414 parse_err("illegal erase size");
418 add_device(name
, erase_size
);
424 static int block2mtd_setup(const char *val
, struct kernel_param
*kp
)
427 return block2mtd_setup2(val
);
429 /* If more parameters are later passed in via
430 /sys/module/block2mtd/parameters/block2mtd
431 and block2mtd_init() has already been called,
432 we can parse the argument now. */
434 if (block2mtd_init_called
)
435 return block2mtd_setup2(val
);
437 /* During early boot stage, we only save the parameters
438 here. We must parse them later: if the param passed
439 from kernel boot command line, block2mtd_setup() is
440 called so early that it is not possible to resolve
441 the device (even kmalloc() fails). Deter that work to
442 block2mtd_setup2(). */
444 strlcpy(block2mtd_paramline
, val
, sizeof(block2mtd_paramline
));
451 module_param_call(block2mtd
, block2mtd_setup
, NULL
, NULL
, 0200);
452 MODULE_PARM_DESC(block2mtd
, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
454 static int __init
block2mtd_init(void)
457 INFO("version " VERSION
);
460 if (strlen(block2mtd_paramline
))
461 ret
= block2mtd_setup2(block2mtd_paramline
);
462 block2mtd_init_called
= 1;
469 static void __devexit
block2mtd_exit(void)
471 struct list_head
*pos
, *next
;
473 /* Remove the MTD devices */
474 list_for_each_safe(pos
, next
, &blkmtd_device_list
) {
475 struct block2mtd_dev
*dev
= list_entry(pos
, typeof(*dev
), list
);
476 block2mtd_sync(&dev
->mtd
);
477 del_mtd_device(&dev
->mtd
);
478 INFO("mtd%d: [%s] removed", dev
->mtd
.index
,
479 dev
->mtd
.name
+ strlen("blkmtd: "));
480 list_del(&dev
->list
);
481 block2mtd_free_device(dev
);
486 module_init(block2mtd_init
);
487 module_exit(block2mtd_exit
);
489 MODULE_LICENSE("GPL");
490 MODULE_AUTHOR("Simon Evans <spse@secret.org.uk> and others");
491 MODULE_DESCRIPTION("Emulate an MTD using a block device");