block: move down direct IO plugging
[linux/fpc-iii.git] / drivers / mtd / devices / block2mtd.c
blob681e2ee0f2d6287a1c0e2170358a5e4cc9b11276
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 (IS_ERR(page))
56 return PTR_ERR(page);
58 max = page_address(page) + PAGE_SIZE;
59 for (p=page_address(page); p<max; p++)
60 if (*p != -1UL) {
61 lock_page(page);
62 memset(page_address(page), 0xff, PAGE_SIZE);
63 set_page_dirty(page);
64 unlock_page(page);
65 break;
68 page_cache_release(page);
69 pages--;
70 index++;
72 return 0;
74 static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
76 struct block2mtd_dev *dev = mtd->priv;
77 size_t from = instr->addr;
78 size_t len = instr->len;
79 int err;
81 instr->state = MTD_ERASING;
82 mutex_lock(&dev->write_mutex);
83 err = _block2mtd_erase(dev, from, len);
84 mutex_unlock(&dev->write_mutex);
85 if (err) {
86 ERROR("erase failed err = %d", err);
87 instr->state = MTD_ERASE_FAILED;
88 } else
89 instr->state = MTD_ERASE_DONE;
91 mtd_erase_callback(instr);
92 return err;
96 static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
97 size_t *retlen, u_char *buf)
99 struct block2mtd_dev *dev = mtd->priv;
100 struct page *page;
101 int index = from >> PAGE_SHIFT;
102 int offset = from & (PAGE_SIZE-1);
103 int cpylen;
105 while (len) {
106 if ((offset + len) > PAGE_SIZE)
107 cpylen = PAGE_SIZE - offset; // multiple pages
108 else
109 cpylen = len; // this page
110 len = len - cpylen;
112 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
113 if (IS_ERR(page))
114 return PTR_ERR(page);
116 memcpy(buf, page_address(page) + offset, cpylen);
117 page_cache_release(page);
119 if (retlen)
120 *retlen += cpylen;
121 buf += cpylen;
122 offset = 0;
123 index++;
125 return 0;
129 /* write data to the underlying device */
130 static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
131 loff_t to, size_t len, size_t *retlen)
133 struct page *page;
134 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
135 int index = to >> PAGE_SHIFT; // page index
136 int offset = to & ~PAGE_MASK; // page offset
137 int cpylen;
139 while (len) {
140 if ((offset+len) > PAGE_SIZE)
141 cpylen = PAGE_SIZE - offset; // multiple pages
142 else
143 cpylen = len; // this page
144 len = len - cpylen;
146 page = page_read(mapping, index);
147 if (IS_ERR(page))
148 return PTR_ERR(page);
150 if (memcmp(page_address(page)+offset, buf, cpylen)) {
151 lock_page(page);
152 memcpy(page_address(page) + offset, buf, cpylen);
153 set_page_dirty(page);
154 unlock_page(page);
156 page_cache_release(page);
158 if (retlen)
159 *retlen += cpylen;
161 buf += cpylen;
162 offset = 0;
163 index++;
165 return 0;
169 static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
170 size_t *retlen, const u_char *buf)
172 struct block2mtd_dev *dev = mtd->priv;
173 int err;
175 mutex_lock(&dev->write_mutex);
176 err = _block2mtd_write(dev, buf, to, len, retlen);
177 mutex_unlock(&dev->write_mutex);
178 if (err > 0)
179 err = 0;
180 return err;
184 /* sync the device - wait until the write queue is empty */
185 static void block2mtd_sync(struct mtd_info *mtd)
187 struct block2mtd_dev *dev = mtd->priv;
188 sync_blockdev(dev->blkdev);
189 return;
193 static void block2mtd_free_device(struct block2mtd_dev *dev)
195 if (!dev)
196 return;
198 kfree(dev->mtd.name);
200 if (dev->blkdev) {
201 invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
202 0, -1);
203 blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
206 kfree(dev);
210 /* FIXME: ensure that mtd->size % erase_size == 0 */
211 static struct block2mtd_dev *add_device(char *devname, int erase_size)
213 const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
214 struct block_device *bdev;
215 struct block2mtd_dev *dev;
216 char *name;
218 if (!devname)
219 return NULL;
221 dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
222 if (!dev)
223 return NULL;
225 /* Get a handle on the device */
226 bdev = blkdev_get_by_path(devname, mode, dev);
227 #ifndef MODULE
228 if (IS_ERR(bdev)) {
230 /* We might not have rootfs mounted at this point. Try
231 to resolve the device name by other means. */
233 dev_t devt = name_to_dev_t(devname);
234 if (devt)
235 bdev = blkdev_get_by_dev(devt, mode, dev);
237 #endif
239 if (IS_ERR(bdev)) {
240 ERROR("error: cannot open device %s", devname);
241 goto devinit_err;
243 dev->blkdev = bdev;
245 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
246 ERROR("attempting to use an MTD device as a block device");
247 goto devinit_err;
250 mutex_init(&dev->write_mutex);
252 /* Setup the MTD structure */
253 /* make the name contain the block device in */
254 name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
255 if (!name)
256 goto devinit_err;
258 dev->mtd.name = name;
260 dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
261 dev->mtd.erasesize = erase_size;
262 dev->mtd.writesize = 1;
263 dev->mtd.writebufsize = PAGE_SIZE;
264 dev->mtd.type = MTD_RAM;
265 dev->mtd.flags = MTD_CAP_RAM;
266 dev->mtd._erase = block2mtd_erase;
267 dev->mtd._write = block2mtd_write;
268 dev->mtd._sync = block2mtd_sync;
269 dev->mtd._read = block2mtd_read;
270 dev->mtd.priv = dev;
271 dev->mtd.owner = THIS_MODULE;
273 if (mtd_device_register(&dev->mtd, NULL, 0)) {
274 /* Device didn't get added, so free the entry */
275 goto devinit_err;
277 list_add(&dev->list, &blkmtd_device_list);
278 INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev->mtd.index,
279 dev->mtd.name + strlen("block2mtd: "),
280 dev->mtd.erasesize >> 10, dev->mtd.erasesize);
281 return dev;
283 devinit_err:
284 block2mtd_free_device(dev);
285 return NULL;
289 /* This function works similar to reguler strtoul. In addition, it
290 * allows some suffixes for a more human-readable number format:
291 * ki, Ki, kiB, KiB - multiply result with 1024
292 * Mi, MiB - multiply result with 1024^2
293 * Gi, GiB - multiply result with 1024^3
295 static int ustrtoul(const char *cp, char **endp, unsigned int base)
297 unsigned long result = simple_strtoul(cp, endp, base);
298 switch (**endp) {
299 case 'G' :
300 result *= 1024;
301 case 'M':
302 result *= 1024;
303 case 'K':
304 case 'k':
305 result *= 1024;
306 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
307 if ((*endp)[1] == 'i') {
308 if ((*endp)[2] == 'B')
309 (*endp) += 3;
310 else
311 (*endp) += 2;
314 return result;
318 static int parse_num(size_t *num, const char *token)
320 char *endp;
321 size_t n;
323 n = (size_t) ustrtoul(token, &endp, 0);
324 if (*endp)
325 return -EINVAL;
327 *num = n;
328 return 0;
332 static inline void kill_final_newline(char *str)
334 char *newline = strrchr(str, '\n');
335 if (newline && !newline[1])
336 *newline = 0;
340 #define parse_err(fmt, args...) do { \
341 ERROR(fmt, ## args); \
342 return 0; \
343 } while (0)
345 #ifndef MODULE
346 static int block2mtd_init_called = 0;
347 static char block2mtd_paramline[80 + 12]; /* 80 for device, 12 for erase size */
348 #endif
351 static int block2mtd_setup2(const char *val)
353 char buf[80 + 12]; /* 80 for device, 12 for erase size */
354 char *str = buf;
355 char *token[2];
356 char *name;
357 size_t erase_size = PAGE_SIZE;
358 int i, ret;
360 if (strnlen(val, sizeof(buf)) >= sizeof(buf))
361 parse_err("parameter too long");
363 strcpy(str, val);
364 kill_final_newline(str);
366 for (i = 0; i < 2; i++)
367 token[i] = strsep(&str, ",");
369 if (str)
370 parse_err("too many arguments");
372 if (!token[0])
373 parse_err("no argument");
375 name = token[0];
376 if (strlen(name) + 1 > 80)
377 parse_err("device name too long");
379 if (token[1]) {
380 ret = parse_num(&erase_size, token[1]);
381 if (ret) {
382 parse_err("illegal erase size");
386 add_device(name, erase_size);
388 return 0;
392 static int block2mtd_setup(const char *val, struct kernel_param *kp)
394 #ifdef MODULE
395 return block2mtd_setup2(val);
396 #else
397 /* If more parameters are later passed in via
398 /sys/module/block2mtd/parameters/block2mtd
399 and block2mtd_init() has already been called,
400 we can parse the argument now. */
402 if (block2mtd_init_called)
403 return block2mtd_setup2(val);
405 /* During early boot stage, we only save the parameters
406 here. We must parse them later: if the param passed
407 from kernel boot command line, block2mtd_setup() is
408 called so early that it is not possible to resolve
409 the device (even kmalloc() fails). Deter that work to
410 block2mtd_setup2(). */
412 strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
414 return 0;
415 #endif
419 module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
420 MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
422 static int __init block2mtd_init(void)
424 int ret = 0;
426 #ifndef MODULE
427 if (strlen(block2mtd_paramline))
428 ret = block2mtd_setup2(block2mtd_paramline);
429 block2mtd_init_called = 1;
430 #endif
432 return ret;
436 static void __devexit block2mtd_exit(void)
438 struct list_head *pos, *next;
440 /* Remove the MTD devices */
441 list_for_each_safe(pos, next, &blkmtd_device_list) {
442 struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
443 block2mtd_sync(&dev->mtd);
444 mtd_device_unregister(&dev->mtd);
445 INFO("mtd%d: [%s] removed", dev->mtd.index,
446 dev->mtd.name + strlen("block2mtd: "));
447 list_del(&dev->list);
448 block2mtd_free_device(dev);
453 module_init(block2mtd_init);
454 module_exit(block2mtd_exit);
456 MODULE_LICENSE("GPL");
457 MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
458 MODULE_DESCRIPTION("Emulate an MTD using a block device");