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 return read_mapping_page(mapping
, index
, NULL
);
48 /* erase a specified part of the device */
49 static int _block2mtd_erase(struct block2mtd_dev
*dev
, loff_t to
, size_t len
)
51 struct address_space
*mapping
= dev
->blkdev
->bd_inode
->i_mapping
;
53 int index
= to
>> PAGE_SHIFT
; // page index
54 int pages
= len
>> PAGE_SHIFT
;
59 page
= page_read(mapping
, index
);
65 max
= page_address(page
) + PAGE_SIZE
;
66 for (p
=page_address(page
); p
<max
; p
++)
69 memset(page_address(page
), 0xff, PAGE_SIZE
);
75 page_cache_release(page
);
81 static int block2mtd_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
83 struct block2mtd_dev
*dev
= mtd
->priv
;
84 size_t from
= instr
->addr
;
85 size_t len
= instr
->len
;
88 instr
->state
= MTD_ERASING
;
89 mutex_lock(&dev
->write_mutex
);
90 err
= _block2mtd_erase(dev
, from
, len
);
91 mutex_unlock(&dev
->write_mutex
);
93 ERROR("erase failed err = %d", err
);
94 instr
->state
= MTD_ERASE_FAILED
;
96 instr
->state
= MTD_ERASE_DONE
;
98 instr
->state
= MTD_ERASE_DONE
;
99 mtd_erase_callback(instr
);
104 static int block2mtd_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
105 size_t *retlen
, u_char
*buf
)
107 struct block2mtd_dev
*dev
= mtd
->priv
;
109 int index
= from
>> PAGE_SHIFT
;
110 int offset
= from
& (PAGE_SIZE
-1);
113 if (from
> mtd
->size
)
115 if (from
+ len
> mtd
->size
)
116 len
= mtd
->size
- from
;
122 if ((offset
+ len
) > PAGE_SIZE
)
123 cpylen
= PAGE_SIZE
- offset
; // multiple pages
125 cpylen
= len
; // this page
128 page
= page_read(dev
->blkdev
->bd_inode
->i_mapping
, index
);
132 return PTR_ERR(page
);
134 memcpy(buf
, page_address(page
) + offset
, cpylen
);
135 page_cache_release(page
);
147 /* write data to the underlying device */
148 static int _block2mtd_write(struct block2mtd_dev
*dev
, const u_char
*buf
,
149 loff_t to
, size_t len
, size_t *retlen
)
152 struct address_space
*mapping
= dev
->blkdev
->bd_inode
->i_mapping
;
153 int index
= to
>> PAGE_SHIFT
; // page index
154 int offset
= to
& ~PAGE_MASK
; // page offset
160 if ((offset
+len
) > PAGE_SIZE
)
161 cpylen
= PAGE_SIZE
- offset
; // multiple pages
163 cpylen
= len
; // this page
166 page
= page_read(mapping
, index
);
170 return PTR_ERR(page
);
172 if (memcmp(page_address(page
)+offset
, buf
, cpylen
)) {
174 memcpy(page_address(page
) + offset
, buf
, cpylen
);
175 set_page_dirty(page
);
178 page_cache_release(page
);
191 static int block2mtd_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
192 size_t *retlen
, const u_char
*buf
)
194 struct block2mtd_dev
*dev
= mtd
->priv
;
201 if (to
+ len
> mtd
->size
)
202 len
= mtd
->size
- to
;
204 mutex_lock(&dev
->write_mutex
);
205 err
= _block2mtd_write(dev
, buf
, to
, len
, retlen
);
206 mutex_unlock(&dev
->write_mutex
);
213 /* sync the device - wait until the write queue is empty */
214 static void block2mtd_sync(struct mtd_info
*mtd
)
216 struct block2mtd_dev
*dev
= mtd
->priv
;
217 sync_blockdev(dev
->blkdev
);
222 static void block2mtd_free_device(struct block2mtd_dev
*dev
)
227 kfree(dev
->mtd
.name
);
230 invalidate_mapping_pages(dev
->blkdev
->bd_inode
->i_mapping
,
232 close_bdev_excl(dev
->blkdev
);
239 /* FIXME: ensure that mtd->size % erase_size == 0 */
240 static struct block2mtd_dev
*add_device(char *devname
, int erase_size
)
242 struct block_device
*bdev
;
243 struct block2mtd_dev
*dev
;
248 dev
= kzalloc(sizeof(struct block2mtd_dev
), GFP_KERNEL
);
252 /* Get a handle on the device */
253 bdev
= open_bdev_excl(devname
, O_RDWR
, NULL
);
257 /* We might not have rootfs mounted at this point. Try
258 to resolve the device name by other means. */
260 dev_t devt
= name_to_dev_t(devname
);
262 bdev
= open_by_devnum(devt
, FMODE_WRITE
| FMODE_READ
);
268 ERROR("error: cannot open device %s", devname
);
273 if (MAJOR(bdev
->bd_dev
) == MTD_BLOCK_MAJOR
) {
274 ERROR("attempting to use an MTD device as a block device");
278 mutex_init(&dev
->write_mutex
);
280 /* Setup the MTD structure */
281 /* make the name contain the block device in */
282 dev
->mtd
.name
= kmalloc(sizeof("block2mtd: ") + strlen(devname
),
287 sprintf(dev
->mtd
.name
, "block2mtd: %s", devname
);
289 dev
->mtd
.size
= dev
->blkdev
->bd_inode
->i_size
& PAGE_MASK
;
290 dev
->mtd
.erasesize
= erase_size
;
291 dev
->mtd
.writesize
= 1;
292 dev
->mtd
.type
= MTD_RAM
;
293 dev
->mtd
.flags
= MTD_CAP_RAM
;
294 dev
->mtd
.erase
= block2mtd_erase
;
295 dev
->mtd
.write
= block2mtd_write
;
296 dev
->mtd
.writev
= default_mtd_writev
;
297 dev
->mtd
.sync
= block2mtd_sync
;
298 dev
->mtd
.read
= block2mtd_read
;
300 dev
->mtd
.owner
= THIS_MODULE
;
302 if (add_mtd_device(&dev
->mtd
)) {
303 /* Device didnt get added, so free the entry */
306 list_add(&dev
->list
, &blkmtd_device_list
);
307 INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev
->mtd
.index
,
308 dev
->mtd
.name
+ strlen("blkmtd: "),
309 dev
->mtd
.erasesize
>> 10, dev
->mtd
.erasesize
);
313 block2mtd_free_device(dev
);
318 /* This function works similar to reguler strtoul. In addition, it
319 * allows some suffixes for a more human-readable number format:
320 * ki, Ki, kiB, KiB - multiply result with 1024
321 * Mi, MiB - multiply result with 1024^2
322 * Gi, GiB - multiply result with 1024^3
324 static int ustrtoul(const char *cp
, char **endp
, unsigned int base
)
326 unsigned long result
= simple_strtoul(cp
, endp
, base
);
335 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
336 if ((*endp
)[1] == 'i') {
337 if ((*endp
)[2] == 'B')
347 static int parse_num(size_t *num
, const char *token
)
352 n
= (size_t) ustrtoul(token
, &endp
, 0);
361 static inline void kill_final_newline(char *str
)
363 char *newline
= strrchr(str
, '\n');
364 if (newline
&& !newline
[1])
369 #define parse_err(fmt, args...) do { \
370 ERROR("block2mtd: " fmt "\n", ## args); \
375 static int block2mtd_init_called
= 0;
376 static char block2mtd_paramline
[80 + 12]; /* 80 for device, 12 for erase size */
380 static int block2mtd_setup2(const char *val
)
382 char buf
[80 + 12]; /* 80 for device, 12 for erase size */
386 size_t erase_size
= PAGE_SIZE
;
389 if (strnlen(val
, sizeof(buf
)) >= sizeof(buf
))
390 parse_err("parameter too long");
393 kill_final_newline(str
);
395 for (i
= 0; i
< 2; i
++)
396 token
[i
] = strsep(&str
, ",");
399 parse_err("too many arguments");
402 parse_err("no argument");
405 if (strlen(name
) + 1 > 80)
406 parse_err("device name too long");
409 ret
= parse_num(&erase_size
, token
[1]);
412 parse_err("illegal erase size");
416 add_device(name
, erase_size
);
422 static int block2mtd_setup(const char *val
, struct kernel_param
*kp
)
425 return block2mtd_setup2(val
);
427 /* If more parameters are later passed in via
428 /sys/module/block2mtd/parameters/block2mtd
429 and block2mtd_init() has already been called,
430 we can parse the argument now. */
432 if (block2mtd_init_called
)
433 return block2mtd_setup2(val
);
435 /* During early boot stage, we only save the parameters
436 here. We must parse them later: if the param passed
437 from kernel boot command line, block2mtd_setup() is
438 called so early that it is not possible to resolve
439 the device (even kmalloc() fails). Deter that work to
440 block2mtd_setup2(). */
442 strlcpy(block2mtd_paramline
, val
, sizeof(block2mtd_paramline
));
449 module_param_call(block2mtd
, block2mtd_setup
, NULL
, NULL
, 0200);
450 MODULE_PARM_DESC(block2mtd
, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
452 static int __init
block2mtd_init(void)
455 INFO("version " VERSION
);
458 if (strlen(block2mtd_paramline
))
459 ret
= block2mtd_setup2(block2mtd_paramline
);
460 block2mtd_init_called
= 1;
467 static void __devexit
block2mtd_exit(void)
469 struct list_head
*pos
, *next
;
471 /* Remove the MTD devices */
472 list_for_each_safe(pos
, next
, &blkmtd_device_list
) {
473 struct block2mtd_dev
*dev
= list_entry(pos
, typeof(*dev
), list
);
474 block2mtd_sync(&dev
->mtd
);
475 del_mtd_device(&dev
->mtd
);
476 INFO("mtd%d: [%s] removed", dev
->mtd
.index
,
477 dev
->mtd
.name
+ strlen("blkmtd: "));
478 list_del(&dev
->list
);
479 block2mtd_free_device(dev
);
484 module_init(block2mtd_init
);
485 module_exit(block2mtd_exit
);
487 MODULE_LICENSE("GPL");
488 MODULE_AUTHOR("Simon Evans <spse@secret.org.uk> and others");
489 MODULE_DESCRIPTION("Emulate an MTD using a block device");