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/mtd/partitions.h>
20 #include <linux/buffer_head.h>
21 #include <linux/mutex.h>
22 #include <linux/mount.h>
24 #define VERSION "$Revision: 1.30 $"
27 #define ERROR(fmt, args...) printk(KERN_ERR "block2mtd: " fmt "\n" , ## args)
28 #define INFO(fmt, args...) printk(KERN_INFO "block2mtd: " fmt "\n" , ## args)
31 /* Info for the block device */
32 struct block2mtd_dev
{
33 struct list_head list
;
34 struct block_device
*blkdev
;
36 struct mutex write_mutex
;
40 /* Static info about the MTD, used in cleanup_module */
41 static LIST_HEAD(blkmtd_device_list
);
44 static struct page
*page_read(struct address_space
*mapping
, int index
)
46 return read_mapping_page(mapping
, index
, NULL
);
49 /* erase a specified part of the device */
50 static int _block2mtd_erase(struct block2mtd_dev
*dev
, loff_t to
, size_t len
)
52 struct address_space
*mapping
= dev
->blkdev
->bd_inode
->i_mapping
;
54 int index
= to
>> PAGE_SHIFT
; // page index
55 int pages
= len
>> PAGE_SHIFT
;
60 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
);
76 page_cache_release(page
);
82 static int block2mtd_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
84 struct block2mtd_dev
*dev
= mtd
->priv
;
85 size_t from
= instr
->addr
;
86 size_t len
= instr
->len
;
89 instr
->state
= MTD_ERASING
;
90 mutex_lock(&dev
->write_mutex
);
91 err
= _block2mtd_erase(dev
, from
, len
);
92 mutex_unlock(&dev
->write_mutex
);
94 ERROR("erase failed err = %d", err
);
95 instr
->state
= MTD_ERASE_FAILED
;
97 instr
->state
= MTD_ERASE_DONE
;
99 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);
114 if (from
> mtd
->size
)
116 if (from
+ len
> mtd
->size
)
117 len
= mtd
->size
- from
;
123 if ((offset
+ len
) > PAGE_SIZE
)
124 cpylen
= PAGE_SIZE
- offset
; // multiple pages
126 cpylen
= len
; // this page
129 page
= page_read(dev
->blkdev
->bd_inode
->i_mapping
, index
);
133 return PTR_ERR(page
);
135 memcpy(buf
, page_address(page
) + offset
, cpylen
);
136 page_cache_release(page
);
148 /* write data to the underlying device */
149 static int _block2mtd_write(struct block2mtd_dev
*dev
, const u_char
*buf
,
150 loff_t to
, size_t len
, size_t *retlen
)
153 struct address_space
*mapping
= dev
->blkdev
->bd_inode
->i_mapping
;
154 int index
= to
>> PAGE_SHIFT
; // page index
155 int offset
= to
& ~PAGE_MASK
; // page offset
161 if ((offset
+len
) > PAGE_SIZE
)
162 cpylen
= PAGE_SIZE
- offset
; // multiple pages
164 cpylen
= len
; // this page
167 page
= page_read(mapping
, index
);
171 return PTR_ERR(page
);
173 if (memcmp(page_address(page
)+offset
, buf
, cpylen
)) {
175 memcpy(page_address(page
) + offset
, buf
, cpylen
);
176 set_page_dirty(page
);
179 page_cache_release(page
);
192 static int block2mtd_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
193 size_t *retlen
, const u_char
*buf
)
195 struct block2mtd_dev
*dev
= mtd
->priv
;
202 if (to
+ len
> mtd
->size
)
203 len
= mtd
->size
- to
;
205 mutex_lock(&dev
->write_mutex
);
206 err
= _block2mtd_write(dev
, buf
, to
, len
, retlen
);
207 mutex_unlock(&dev
->write_mutex
);
214 /* sync the device - wait until the write queue is empty */
215 static void block2mtd_sync(struct mtd_info
*mtd
)
217 struct block2mtd_dev
*dev
= mtd
->priv
;
218 sync_blockdev(dev
->blkdev
);
223 static void block2mtd_free_device(struct block2mtd_dev
*dev
)
228 kfree(dev
->mtd
.name
);
231 invalidate_mapping_pages(dev
->blkdev
->bd_inode
->i_mapping
,
233 close_bdev_excl(dev
->blkdev
);
240 /* FIXME: ensure that mtd->size % erase_size == 0 */
241 static struct block2mtd_dev
*add_device(char *devname
, int erase_size
, char *mtdname
)
243 struct block_device
*bdev
;
244 struct block2mtd_dev
*dev
;
245 struct mtd_partition
*part
;
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 */
288 dev
->mtd
.name
= kmalloc(strlen(mtdname
) + 1, GFP_KERNEL
);
293 strcpy(dev
->mtd
.name
, mtdname
);
295 dev
->mtd
.size
= dev
->blkdev
->bd_inode
->i_size
& PAGE_MASK
& ~(erase_size
- 1);
296 dev
->mtd
.erasesize
= erase_size
;
297 dev
->mtd
.writesize
= 1;
298 dev
->mtd
.type
= MTD_RAM
;
299 dev
->mtd
.flags
= MTD_CAP_RAM
;
300 dev
->mtd
.erase
= block2mtd_erase
;
301 dev
->mtd
.write
= block2mtd_write
;
302 dev
->mtd
.writev
= default_mtd_writev
;
303 dev
->mtd
.sync
= block2mtd_sync
;
304 dev
->mtd
.read
= block2mtd_read
;
306 dev
->mtd
.owner
= THIS_MODULE
;
308 part
= kzalloc(sizeof(struct mtd_partition
), GFP_KERNEL
);
309 part
->name
= dev
->mtd
.name
;
311 part
->size
= dev
->mtd
.size
;
312 if (add_mtd_partitions(&dev
->mtd
, part
, 1)) {
313 /* Device didnt get added, so free the entry */
316 list_add(&dev
->list
, &blkmtd_device_list
);
317 INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev
->mtd
.index
,
318 mtdname
, dev
->mtd
.erasesize
>> 10, dev
->mtd
.erasesize
);
322 block2mtd_free_device(dev
);
327 /* This function works similar to reguler strtoul. In addition, it
328 * allows some suffixes for a more human-readable number format:
329 * ki, Ki, kiB, KiB - multiply result with 1024
330 * Mi, MiB - multiply result with 1024^2
331 * Gi, GiB - multiply result with 1024^3
333 static int ustrtoul(const char *cp
, char **endp
, unsigned int base
)
335 unsigned long result
= simple_strtoul(cp
, endp
, base
);
344 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
345 if ((*endp
)[1] == 'i') {
346 if ((*endp
)[2] == 'B')
356 static int parse_num(size_t *num
, const char *token
)
361 n
= (size_t) ustrtoul(token
, &endp
, 0);
370 static inline void kill_final_newline(char *str
)
372 char *newline
= strrchr(str
, '\n');
373 if (newline
&& !newline
[1])
378 #define parse_err(fmt, args...) do { \
379 ERROR("block2mtd: " fmt "\n", ## args); \
384 static int block2mtd_init_called
= 0;
385 static char block2mtd_paramline
[80 + 12]; /* 80 for device, 12 for erase size */
389 static int block2mtd_setup2(const char *val
)
391 char buf
[80 + 12 + 80]; /* 80 for device, 12 for erase size, 80 for name */
395 size_t erase_size
= PAGE_SIZE
;
398 if (strnlen(val
, sizeof(buf
)) >= sizeof(buf
))
399 parse_err("parameter too long");
402 kill_final_newline(str
);
404 for (i
= 0; i
< 3; i
++)
405 token
[i
] = strsep(&str
, ",");
408 parse_err("too many arguments");
411 parse_err("no argument");
414 if (strlen(name
) + 1 > 80)
415 parse_err("device name too long");
418 ret
= parse_num(&erase_size
, token
[1]);
421 parse_err("illegal erase size");
424 if (token
[2] && (strlen(token
[2]) + 1 > 80))
425 parse_err("mtd device name too long");
427 add_device(name
, erase_size
, token
[2]);
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>[,<name>]]\"");
463 static int __init
block2mtd_init(void)
466 INFO("version " VERSION
);
469 if (strlen(block2mtd_paramline
))
470 ret
= block2mtd_setup2(block2mtd_paramline
);
471 block2mtd_init_called
= 1;
478 static void __devexit
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 del_mtd_device(&dev
->mtd
);
487 INFO("mtd%d: [%s] removed", dev
->mtd
.index
,
488 dev
->mtd
.name
+ strlen("blkmtd: "));
489 list_del(&dev
->list
);
490 block2mtd_free_device(dev
);
495 module_init(block2mtd_init
);
496 module_exit(block2mtd_exit
);
498 MODULE_LICENSE("GPL");
499 MODULE_AUTHOR("Simon Evans <spse@secret.org.uk> and others");
500 MODULE_DESCRIPTION("Emulate an MTD using a block device");