Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / drivers / mtd / devices / block2mtd.c
blobb78f23169d4e7dce23848eba02b7cf9b8f6cd43f
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/buffer_head.h>
18 #include <linux/mutex.h>
19 #include <linux/mount.h>
20 #include <linux/slab.h>
22 #define ERROR(fmt, args...) printk(KERN_ERR "block2mtd: " fmt "\n" , ## args)
23 #define INFO(fmt, args...) printk(KERN_INFO "block2mtd: " fmt "\n" , ## args)
26 /* Info for the block device */
27 struct block2mtd_dev {
28 struct list_head list;
29 struct block_device *blkdev;
30 struct mtd_info mtd;
31 struct mutex write_mutex;
35 /* Static info about the MTD, used in cleanup_module */
36 static LIST_HEAD(blkmtd_device_list);
39 static struct page *page_read(struct address_space *mapping, int index)
41 return read_mapping_page(mapping, index, NULL);
44 /* erase a specified part of the device */
45 static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
47 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
48 struct page *page;
49 int index = to >> PAGE_SHIFT; // page index
50 int pages = len >> PAGE_SHIFT;
51 u_long *p;
52 u_long *max;
54 while (pages) {
55 page = page_read(mapping, index);
56 if (!page)
57 return -ENOMEM;
58 if (IS_ERR(page))
59 return PTR_ERR(page);
61 max = page_address(page) + PAGE_SIZE;
62 for (p=page_address(page); p<max; p++)
63 if (*p != -1UL) {
64 lock_page(page);
65 memset(page_address(page), 0xff, PAGE_SIZE);
66 set_page_dirty(page);
67 unlock_page(page);
68 break;
71 page_cache_release(page);
72 pages--;
73 index++;
75 return 0;
77 static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
79 struct block2mtd_dev *dev = mtd->priv;
80 size_t from = instr->addr;
81 size_t len = instr->len;
82 int err;
84 instr->state = MTD_ERASING;
85 mutex_lock(&dev->write_mutex);
86 err = _block2mtd_erase(dev, from, len);
87 mutex_unlock(&dev->write_mutex);
88 if (err) {
89 ERROR("erase failed err = %d", err);
90 instr->state = MTD_ERASE_FAILED;
91 } else
92 instr->state = MTD_ERASE_DONE;
94 mtd_erase_callback(instr);
95 return err;
99 static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
100 size_t *retlen, u_char *buf)
102 struct block2mtd_dev *dev = mtd->priv;
103 struct page *page;
104 int index = from >> PAGE_SHIFT;
105 int offset = from & (PAGE_SIZE-1);
106 int cpylen;
108 if (from > mtd->size)
109 return -EINVAL;
110 if (from + len > mtd->size)
111 len = mtd->size - from;
113 if (retlen)
114 *retlen = 0;
116 while (len) {
117 if ((offset + len) > PAGE_SIZE)
118 cpylen = PAGE_SIZE - offset; // multiple pages
119 else
120 cpylen = len; // this page
121 len = len - cpylen;
123 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
124 if (!page)
125 return -ENOMEM;
126 if (IS_ERR(page))
127 return PTR_ERR(page);
129 memcpy(buf, page_address(page) + offset, cpylen);
130 page_cache_release(page);
132 if (retlen)
133 *retlen += cpylen;
134 buf += cpylen;
135 offset = 0;
136 index++;
138 return 0;
142 /* write data to the underlying device */
143 static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
144 loff_t to, size_t len, size_t *retlen)
146 struct page *page;
147 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
148 int index = to >> PAGE_SHIFT; // page index
149 int offset = to & ~PAGE_MASK; // page offset
150 int cpylen;
152 if (retlen)
153 *retlen = 0;
154 while (len) {
155 if ((offset+len) > PAGE_SIZE)
156 cpylen = PAGE_SIZE - offset; // multiple pages
157 else
158 cpylen = len; // this page
159 len = len - cpylen;
161 page = page_read(mapping, index);
162 if (!page)
163 return -ENOMEM;
164 if (IS_ERR(page))
165 return PTR_ERR(page);
167 if (memcmp(page_address(page)+offset, buf, cpylen)) {
168 lock_page(page);
169 memcpy(page_address(page) + offset, buf, cpylen);
170 set_page_dirty(page);
171 unlock_page(page);
173 page_cache_release(page);
175 if (retlen)
176 *retlen += cpylen;
178 buf += cpylen;
179 offset = 0;
180 index++;
182 return 0;
186 static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
187 size_t *retlen, const u_char *buf)
189 struct block2mtd_dev *dev = mtd->priv;
190 int err;
192 if (!len)
193 return 0;
194 if (to >= mtd->size)
195 return -ENOSPC;
196 if (to + len > mtd->size)
197 len = mtd->size - to;
199 mutex_lock(&dev->write_mutex);
200 err = _block2mtd_write(dev, buf, to, len, retlen);
201 mutex_unlock(&dev->write_mutex);
202 if (err > 0)
203 err = 0;
204 return err;
208 /* sync the device - wait until the write queue is empty */
209 static void block2mtd_sync(struct mtd_info *mtd)
211 struct block2mtd_dev *dev = mtd->priv;
212 sync_blockdev(dev->blkdev);
213 return;
217 static void block2mtd_free_device(struct block2mtd_dev *dev)
219 if (!dev)
220 return;
222 kfree(dev->mtd.name);
224 if (dev->blkdev) {
225 invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
226 0, -1);
227 blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
230 kfree(dev);
234 /* FIXME: ensure that mtd->size % erase_size == 0 */
235 static struct block2mtd_dev *add_device(char *devname, int erase_size)
237 const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
238 struct block_device *bdev;
239 struct block2mtd_dev *dev;
240 char *name;
242 if (!devname)
243 return NULL;
245 dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
246 if (!dev)
247 return NULL;
249 /* Get a handle on the device */
250 bdev = blkdev_get_by_path(devname, mode, dev);
251 #ifndef MODULE
252 if (IS_ERR(bdev)) {
254 /* We might not have rootfs mounted at this point. Try
255 to resolve the device name by other means. */
257 dev_t devt = name_to_dev_t(devname);
258 if (devt)
259 bdev = blkdev_get_by_dev(devt, mode, dev);
261 #endif
263 if (IS_ERR(bdev)) {
264 ERROR("error: cannot open device %s", devname);
265 goto devinit_err;
267 dev->blkdev = bdev;
269 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
270 ERROR("attempting to use an MTD device as a block device");
271 goto devinit_err;
274 mutex_init(&dev->write_mutex);
276 /* Setup the MTD structure */
277 /* make the name contain the block device in */
278 name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
279 if (!name)
280 goto devinit_err;
282 dev->mtd.name = name;
284 dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
285 dev->mtd.erasesize = erase_size;
286 dev->mtd.writesize = 1;
287 dev->mtd.type = MTD_RAM;
288 dev->mtd.flags = MTD_CAP_RAM;
289 dev->mtd.erase = block2mtd_erase;
290 dev->mtd.write = block2mtd_write;
291 dev->mtd.writev = default_mtd_writev;
292 dev->mtd.sync = block2mtd_sync;
293 dev->mtd.read = block2mtd_read;
294 dev->mtd.priv = dev;
295 dev->mtd.owner = THIS_MODULE;
297 if (mtd_device_register(&dev->mtd, NULL, 0)) {
298 /* Device didn't get added, so free the entry */
299 goto devinit_err;
301 list_add(&dev->list, &blkmtd_device_list);
302 INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev->mtd.index,
303 dev->mtd.name + strlen("block2mtd: "),
304 dev->mtd.erasesize >> 10, dev->mtd.erasesize);
305 return dev;
307 devinit_err:
308 block2mtd_free_device(dev);
309 return NULL;
313 /* This function works similar to reguler strtoul. In addition, it
314 * allows some suffixes for a more human-readable number format:
315 * ki, Ki, kiB, KiB - multiply result with 1024
316 * Mi, MiB - multiply result with 1024^2
317 * Gi, GiB - multiply result with 1024^3
319 static int ustrtoul(const char *cp, char **endp, unsigned int base)
321 unsigned long result = simple_strtoul(cp, endp, base);
322 switch (**endp) {
323 case 'G' :
324 result *= 1024;
325 case 'M':
326 result *= 1024;
327 case 'K':
328 case 'k':
329 result *= 1024;
330 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
331 if ((*endp)[1] == 'i') {
332 if ((*endp)[2] == 'B')
333 (*endp) += 3;
334 else
335 (*endp) += 2;
338 return result;
342 static int parse_num(size_t *num, const char *token)
344 char *endp;
345 size_t n;
347 n = (size_t) ustrtoul(token, &endp, 0);
348 if (*endp)
349 return -EINVAL;
351 *num = n;
352 return 0;
356 static inline void kill_final_newline(char *str)
358 char *newline = strrchr(str, '\n');
359 if (newline && !newline[1])
360 *newline = 0;
364 #define parse_err(fmt, args...) do { \
365 ERROR(fmt, ## args); \
366 return 0; \
367 } while (0)
369 #ifndef MODULE
370 static int block2mtd_init_called = 0;
371 static char block2mtd_paramline[80 + 12]; /* 80 for device, 12 for erase size */
372 #endif
375 static int block2mtd_setup2(const char *val)
377 char buf[80 + 12]; /* 80 for device, 12 for erase size */
378 char *str = buf;
379 char *token[2];
380 char *name;
381 size_t erase_size = PAGE_SIZE;
382 int i, ret;
384 if (strnlen(val, sizeof(buf)) >= sizeof(buf))
385 parse_err("parameter too long");
387 strcpy(str, val);
388 kill_final_newline(str);
390 for (i = 0; i < 2; i++)
391 token[i] = strsep(&str, ",");
393 if (str)
394 parse_err("too many arguments");
396 if (!token[0])
397 parse_err("no argument");
399 name = token[0];
400 if (strlen(name) + 1 > 80)
401 parse_err("device name too long");
403 if (token[1]) {
404 ret = parse_num(&erase_size, token[1]);
405 if (ret) {
406 parse_err("illegal erase size");
410 add_device(name, erase_size);
412 return 0;
416 static int block2mtd_setup(const char *val, struct kernel_param *kp)
418 #ifdef MODULE
419 return block2mtd_setup2(val);
420 #else
421 /* If more parameters are later passed in via
422 /sys/module/block2mtd/parameters/block2mtd
423 and block2mtd_init() has already been called,
424 we can parse the argument now. */
426 if (block2mtd_init_called)
427 return block2mtd_setup2(val);
429 /* During early boot stage, we only save the parameters
430 here. We must parse them later: if the param passed
431 from kernel boot command line, block2mtd_setup() is
432 called so early that it is not possible to resolve
433 the device (even kmalloc() fails). Deter that work to
434 block2mtd_setup2(). */
436 strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
438 return 0;
439 #endif
443 module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
444 MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
446 static int __init block2mtd_init(void)
448 int ret = 0;
450 #ifndef MODULE
451 if (strlen(block2mtd_paramline))
452 ret = block2mtd_setup2(block2mtd_paramline);
453 block2mtd_init_called = 1;
454 #endif
456 return ret;
460 static void __devexit block2mtd_exit(void)
462 struct list_head *pos, *next;
464 /* Remove the MTD devices */
465 list_for_each_safe(pos, next, &blkmtd_device_list) {
466 struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
467 block2mtd_sync(&dev->mtd);
468 mtd_device_unregister(&dev->mtd);
469 INFO("mtd%d: [%s] removed", dev->mtd.index,
470 dev->mtd.name + strlen("block2mtd: "));
471 list_del(&dev->list);
472 block2mtd_free_device(dev);
477 module_init(block2mtd_init);
478 module_exit(block2mtd_exit);
480 MODULE_LICENSE("GPL");
481 MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
482 MODULE_DESCRIPTION("Emulate an MTD using a block device");