Merge tag 'for-linus-3.3' of git://git.infradead.org/mtd-2.6
[linux-2.6/btrfs-unstable.git] / drivers / mtd / devices / block2mtd.c
blobe7e46d1e74631b6f31cbde50a0590077d8ee0da6
1 /*
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>
7 * Licence: GPL
8 */
9 #include <linux/module.h>
10 #include <linux/fs.h>
11 #include <linux/blkdev.h>
12 #include <linux/bio.h>
13 #include <linux/pagemap.h>
14 #include <linux/list.h>
15 #include <linux/init.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/mutex.h>
18 #include <linux/mount.h>
19 #include <linux/slab.h>
21 #define ERROR(fmt, args...) printk(KERN_ERR "block2mtd: " fmt "\n" , ## args)
22 #define INFO(fmt, args...) printk(KERN_INFO "block2mtd: " fmt "\n" , ## args)
25 /* Info for the block device */
26 struct block2mtd_dev {
27 struct list_head list;
28 struct block_device *blkdev;
29 struct mtd_info mtd;
30 struct mutex write_mutex;
34 /* Static info about the MTD, used in cleanup_module */
35 static LIST_HEAD(blkmtd_device_list);
38 static struct page *page_read(struct address_space *mapping, int index)
40 return read_mapping_page(mapping, index, NULL);
43 /* erase a specified part of the device */
44 static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
46 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
47 struct page *page;
48 int index = to >> PAGE_SHIFT; // page index
49 int pages = len >> PAGE_SHIFT;
50 u_long *p;
51 u_long *max;
53 while (pages) {
54 page = page_read(mapping, index);
55 if (!page)
56 return -ENOMEM;
57 if (IS_ERR(page))
58 return PTR_ERR(page);
60 max = page_address(page) + PAGE_SIZE;
61 for (p=page_address(page); p<max; p++)
62 if (*p != -1UL) {
63 lock_page(page);
64 memset(page_address(page), 0xff, PAGE_SIZE);
65 set_page_dirty(page);
66 unlock_page(page);
67 break;
70 page_cache_release(page);
71 pages--;
72 index++;
74 return 0;
76 static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
78 struct block2mtd_dev *dev = mtd->priv;
79 size_t from = instr->addr;
80 size_t len = instr->len;
81 int err;
83 instr->state = MTD_ERASING;
84 mutex_lock(&dev->write_mutex);
85 err = _block2mtd_erase(dev, from, len);
86 mutex_unlock(&dev->write_mutex);
87 if (err) {
88 ERROR("erase failed err = %d", err);
89 instr->state = MTD_ERASE_FAILED;
90 } else
91 instr->state = MTD_ERASE_DONE;
93 mtd_erase_callback(instr);
94 return err;
98 static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
99 size_t *retlen, u_char *buf)
101 struct block2mtd_dev *dev = mtd->priv;
102 struct page *page;
103 int index = from >> PAGE_SHIFT;
104 int offset = from & (PAGE_SIZE-1);
105 int cpylen;
107 if (from > mtd->size)
108 return -EINVAL;
109 if (from + len > mtd->size)
110 len = mtd->size - from;
112 if (retlen)
113 *retlen = 0;
115 while (len) {
116 if ((offset + len) > PAGE_SIZE)
117 cpylen = PAGE_SIZE - offset; // multiple pages
118 else
119 cpylen = len; // this page
120 len = len - cpylen;
122 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
123 if (!page)
124 return -ENOMEM;
125 if (IS_ERR(page))
126 return PTR_ERR(page);
128 memcpy(buf, page_address(page) + offset, cpylen);
129 page_cache_release(page);
131 if (retlen)
132 *retlen += cpylen;
133 buf += cpylen;
134 offset = 0;
135 index++;
137 return 0;
141 /* write data to the underlying device */
142 static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
143 loff_t to, size_t len, size_t *retlen)
145 struct page *page;
146 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
147 int index = to >> PAGE_SHIFT; // page index
148 int offset = to & ~PAGE_MASK; // page offset
149 int cpylen;
151 if (retlen)
152 *retlen = 0;
153 while (len) {
154 if ((offset+len) > PAGE_SIZE)
155 cpylen = PAGE_SIZE - offset; // multiple pages
156 else
157 cpylen = len; // this page
158 len = len - cpylen;
160 page = page_read(mapping, index);
161 if (!page)
162 return -ENOMEM;
163 if (IS_ERR(page))
164 return PTR_ERR(page);
166 if (memcmp(page_address(page)+offset, buf, cpylen)) {
167 lock_page(page);
168 memcpy(page_address(page) + offset, buf, cpylen);
169 set_page_dirty(page);
170 unlock_page(page);
172 page_cache_release(page);
174 if (retlen)
175 *retlen += cpylen;
177 buf += cpylen;
178 offset = 0;
179 index++;
181 return 0;
185 static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
186 size_t *retlen, const u_char *buf)
188 struct block2mtd_dev *dev = mtd->priv;
189 int err;
191 if (!len)
192 return 0;
193 if (to >= mtd->size)
194 return -ENOSPC;
195 if (to + len > mtd->size)
196 len = mtd->size - to;
198 mutex_lock(&dev->write_mutex);
199 err = _block2mtd_write(dev, buf, to, len, retlen);
200 mutex_unlock(&dev->write_mutex);
201 if (err > 0)
202 err = 0;
203 return err;
207 /* sync the device - wait until the write queue is empty */
208 static void block2mtd_sync(struct mtd_info *mtd)
210 struct block2mtd_dev *dev = mtd->priv;
211 sync_blockdev(dev->blkdev);
212 return;
216 static void block2mtd_free_device(struct block2mtd_dev *dev)
218 if (!dev)
219 return;
221 kfree(dev->mtd.name);
223 if (dev->blkdev) {
224 invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
225 0, -1);
226 blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
229 kfree(dev);
233 /* FIXME: ensure that mtd->size % erase_size == 0 */
234 static struct block2mtd_dev *add_device(char *devname, int erase_size)
236 const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
237 struct block_device *bdev;
238 struct block2mtd_dev *dev;
239 char *name;
241 if (!devname)
242 return NULL;
244 dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
245 if (!dev)
246 return NULL;
248 /* Get a handle on the device */
249 bdev = blkdev_get_by_path(devname, mode, dev);
250 #ifndef MODULE
251 if (IS_ERR(bdev)) {
253 /* We might not have rootfs mounted at this point. Try
254 to resolve the device name by other means. */
256 dev_t devt = name_to_dev_t(devname);
257 if (devt)
258 bdev = blkdev_get_by_dev(devt, mode, dev);
260 #endif
262 if (IS_ERR(bdev)) {
263 ERROR("error: cannot open device %s", devname);
264 goto devinit_err;
266 dev->blkdev = bdev;
268 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
269 ERROR("attempting to use an MTD device as a block device");
270 goto devinit_err;
273 mutex_init(&dev->write_mutex);
275 /* Setup the MTD structure */
276 /* make the name contain the block device in */
277 name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
278 if (!name)
279 goto devinit_err;
281 dev->mtd.name = name;
283 dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
284 dev->mtd.erasesize = erase_size;
285 dev->mtd.writesize = 1;
286 dev->mtd.type = MTD_RAM;
287 dev->mtd.flags = MTD_CAP_RAM;
288 dev->mtd.erase = block2mtd_erase;
289 dev->mtd.write = block2mtd_write;
290 dev->mtd.writev = mtd_writev;
291 dev->mtd.sync = block2mtd_sync;
292 dev->mtd.read = block2mtd_read;
293 dev->mtd.priv = dev;
294 dev->mtd.owner = THIS_MODULE;
296 if (mtd_device_register(&dev->mtd, NULL, 0)) {
297 /* Device didn't get added, so free the entry */
298 goto devinit_err;
300 list_add(&dev->list, &blkmtd_device_list);
301 INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev->mtd.index,
302 dev->mtd.name + strlen("block2mtd: "),
303 dev->mtd.erasesize >> 10, dev->mtd.erasesize);
304 return dev;
306 devinit_err:
307 block2mtd_free_device(dev);
308 return NULL;
312 /* This function works similar to reguler strtoul. In addition, it
313 * allows some suffixes for a more human-readable number format:
314 * ki, Ki, kiB, KiB - multiply result with 1024
315 * Mi, MiB - multiply result with 1024^2
316 * Gi, GiB - multiply result with 1024^3
318 static int ustrtoul(const char *cp, char **endp, unsigned int base)
320 unsigned long result = simple_strtoul(cp, endp, base);
321 switch (**endp) {
322 case 'G' :
323 result *= 1024;
324 case 'M':
325 result *= 1024;
326 case 'K':
327 case 'k':
328 result *= 1024;
329 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
330 if ((*endp)[1] == 'i') {
331 if ((*endp)[2] == 'B')
332 (*endp) += 3;
333 else
334 (*endp) += 2;
337 return result;
341 static int parse_num(size_t *num, const char *token)
343 char *endp;
344 size_t n;
346 n = (size_t) ustrtoul(token, &endp, 0);
347 if (*endp)
348 return -EINVAL;
350 *num = n;
351 return 0;
355 static inline void kill_final_newline(char *str)
357 char *newline = strrchr(str, '\n');
358 if (newline && !newline[1])
359 *newline = 0;
363 #define parse_err(fmt, args...) do { \
364 ERROR(fmt, ## args); \
365 return 0; \
366 } while (0)
368 #ifndef MODULE
369 static int block2mtd_init_called = 0;
370 static char block2mtd_paramline[80 + 12]; /* 80 for device, 12 for erase size */
371 #endif
374 static int block2mtd_setup2(const char *val)
376 char buf[80 + 12]; /* 80 for device, 12 for erase size */
377 char *str = buf;
378 char *token[2];
379 char *name;
380 size_t erase_size = PAGE_SIZE;
381 int i, ret;
383 if (strnlen(val, sizeof(buf)) >= sizeof(buf))
384 parse_err("parameter too long");
386 strcpy(str, val);
387 kill_final_newline(str);
389 for (i = 0; i < 2; i++)
390 token[i] = strsep(&str, ",");
392 if (str)
393 parse_err("too many arguments");
395 if (!token[0])
396 parse_err("no argument");
398 name = token[0];
399 if (strlen(name) + 1 > 80)
400 parse_err("device name too long");
402 if (token[1]) {
403 ret = parse_num(&erase_size, token[1]);
404 if (ret) {
405 parse_err("illegal erase size");
409 add_device(name, erase_size);
411 return 0;
415 static int block2mtd_setup(const char *val, struct kernel_param *kp)
417 #ifdef MODULE
418 return block2mtd_setup2(val);
419 #else
420 /* If more parameters are later passed in via
421 /sys/module/block2mtd/parameters/block2mtd
422 and block2mtd_init() has already been called,
423 we can parse the argument now. */
425 if (block2mtd_init_called)
426 return block2mtd_setup2(val);
428 /* During early boot stage, we only save the parameters
429 here. We must parse them later: if the param passed
430 from kernel boot command line, block2mtd_setup() is
431 called so early that it is not possible to resolve
432 the device (even kmalloc() fails). Deter that work to
433 block2mtd_setup2(). */
435 strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
437 return 0;
438 #endif
442 module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
443 MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
445 static int __init block2mtd_init(void)
447 int ret = 0;
449 #ifndef MODULE
450 if (strlen(block2mtd_paramline))
451 ret = block2mtd_setup2(block2mtd_paramline);
452 block2mtd_init_called = 1;
453 #endif
455 return ret;
459 static void __devexit block2mtd_exit(void)
461 struct list_head *pos, *next;
463 /* Remove the MTD devices */
464 list_for_each_safe(pos, next, &blkmtd_device_list) {
465 struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
466 block2mtd_sync(&dev->mtd);
467 mtd_device_unregister(&dev->mtd);
468 INFO("mtd%d: [%s] removed", dev->mtd.index,
469 dev->mtd.name + strlen("block2mtd: "));
470 list_del(&dev->list);
471 block2mtd_free_device(dev);
476 module_init(block2mtd_init);
477 module_exit(block2mtd_exit);
479 MODULE_LICENSE("GPL");
480 MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
481 MODULE_DESCRIPTION("Emulate an MTD using a block device");