USB: usb-storage: unusual_devs update for Super TOP SATA bridge
[linux/fpc-iii.git] / drivers / mtd / devices / block2mtd.c
blob7d7000d2fff3305fef95e0ca0d26a3f51d27ba1a
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 while (len) {
108 if ((offset + len) > PAGE_SIZE)
109 cpylen = PAGE_SIZE - offset; // multiple pages
110 else
111 cpylen = len; // this page
112 len = len - cpylen;
114 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
115 if (!page)
116 return -ENOMEM;
117 if (IS_ERR(page))
118 return PTR_ERR(page);
120 memcpy(buf, page_address(page) + offset, cpylen);
121 page_cache_release(page);
123 if (retlen)
124 *retlen += cpylen;
125 buf += cpylen;
126 offset = 0;
127 index++;
129 return 0;
133 /* write data to the underlying device */
134 static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
135 loff_t to, size_t len, size_t *retlen)
137 struct page *page;
138 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
139 int index = to >> PAGE_SHIFT; // page index
140 int offset = to & ~PAGE_MASK; // page offset
141 int cpylen;
143 while (len) {
144 if ((offset+len) > PAGE_SIZE)
145 cpylen = PAGE_SIZE - offset; // multiple pages
146 else
147 cpylen = len; // this page
148 len = len - cpylen;
150 page = page_read(mapping, index);
151 if (!page)
152 return -ENOMEM;
153 if (IS_ERR(page))
154 return PTR_ERR(page);
156 if (memcmp(page_address(page)+offset, buf, cpylen)) {
157 lock_page(page);
158 memcpy(page_address(page) + offset, buf, cpylen);
159 set_page_dirty(page);
160 unlock_page(page);
162 page_cache_release(page);
164 if (retlen)
165 *retlen += cpylen;
167 buf += cpylen;
168 offset = 0;
169 index++;
171 return 0;
175 static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
176 size_t *retlen, const u_char *buf)
178 struct block2mtd_dev *dev = mtd->priv;
179 int err;
181 mutex_lock(&dev->write_mutex);
182 err = _block2mtd_write(dev, buf, to, len, retlen);
183 mutex_unlock(&dev->write_mutex);
184 if (err > 0)
185 err = 0;
186 return err;
190 /* sync the device - wait until the write queue is empty */
191 static void block2mtd_sync(struct mtd_info *mtd)
193 struct block2mtd_dev *dev = mtd->priv;
194 sync_blockdev(dev->blkdev);
195 return;
199 static void block2mtd_free_device(struct block2mtd_dev *dev)
201 if (!dev)
202 return;
204 kfree(dev->mtd.name);
206 if (dev->blkdev) {
207 invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
208 0, -1);
209 blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
212 kfree(dev);
216 /* FIXME: ensure that mtd->size % erase_size == 0 */
217 static struct block2mtd_dev *add_device(char *devname, int erase_size)
219 const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
220 struct block_device *bdev;
221 struct block2mtd_dev *dev;
222 char *name;
224 if (!devname)
225 return NULL;
227 dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
228 if (!dev)
229 return NULL;
231 /* Get a handle on the device */
232 bdev = blkdev_get_by_path(devname, mode, dev);
233 #ifndef MODULE
234 if (IS_ERR(bdev)) {
236 /* We might not have rootfs mounted at this point. Try
237 to resolve the device name by other means. */
239 dev_t devt = name_to_dev_t(devname);
240 if (devt)
241 bdev = blkdev_get_by_dev(devt, mode, dev);
243 #endif
245 if (IS_ERR(bdev)) {
246 ERROR("error: cannot open device %s", devname);
247 goto devinit_err;
249 dev->blkdev = bdev;
251 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
252 ERROR("attempting to use an MTD device as a block device");
253 goto devinit_err;
256 mutex_init(&dev->write_mutex);
258 /* Setup the MTD structure */
259 /* make the name contain the block device in */
260 name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
261 if (!name)
262 goto devinit_err;
264 dev->mtd.name = name;
266 dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
267 dev->mtd.erasesize = erase_size;
268 dev->mtd.writesize = 1;
269 dev->mtd.writebufsize = PAGE_SIZE;
270 dev->mtd.type = MTD_RAM;
271 dev->mtd.flags = MTD_CAP_RAM;
272 dev->mtd._erase = block2mtd_erase;
273 dev->mtd._write = block2mtd_write;
274 dev->mtd._sync = block2mtd_sync;
275 dev->mtd._read = block2mtd_read;
276 dev->mtd.priv = dev;
277 dev->mtd.owner = THIS_MODULE;
279 if (mtd_device_register(&dev->mtd, NULL, 0)) {
280 /* Device didn't get added, so free the entry */
281 goto devinit_err;
283 list_add(&dev->list, &blkmtd_device_list);
284 INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev->mtd.index,
285 dev->mtd.name + strlen("block2mtd: "),
286 dev->mtd.erasesize >> 10, dev->mtd.erasesize);
287 return dev;
289 devinit_err:
290 block2mtd_free_device(dev);
291 return NULL;
295 /* This function works similar to reguler strtoul. In addition, it
296 * allows some suffixes for a more human-readable number format:
297 * ki, Ki, kiB, KiB - multiply result with 1024
298 * Mi, MiB - multiply result with 1024^2
299 * Gi, GiB - multiply result with 1024^3
301 static int ustrtoul(const char *cp, char **endp, unsigned int base)
303 unsigned long result = simple_strtoul(cp, endp, base);
304 switch (**endp) {
305 case 'G' :
306 result *= 1024;
307 case 'M':
308 result *= 1024;
309 case 'K':
310 case 'k':
311 result *= 1024;
312 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
313 if ((*endp)[1] == 'i') {
314 if ((*endp)[2] == 'B')
315 (*endp) += 3;
316 else
317 (*endp) += 2;
320 return result;
324 static int parse_num(size_t *num, const char *token)
326 char *endp;
327 size_t n;
329 n = (size_t) ustrtoul(token, &endp, 0);
330 if (*endp)
331 return -EINVAL;
333 *num = n;
334 return 0;
338 static inline void kill_final_newline(char *str)
340 char *newline = strrchr(str, '\n');
341 if (newline && !newline[1])
342 *newline = 0;
346 #define parse_err(fmt, args...) do { \
347 ERROR(fmt, ## args); \
348 return 0; \
349 } while (0)
351 #ifndef MODULE
352 static int block2mtd_init_called = 0;
353 static char block2mtd_paramline[80 + 12]; /* 80 for device, 12 for erase size */
354 #endif
357 static int block2mtd_setup2(const char *val)
359 char buf[80 + 12]; /* 80 for device, 12 for erase size */
360 char *str = buf;
361 char *token[2];
362 char *name;
363 size_t erase_size = PAGE_SIZE;
364 int i, ret;
366 if (strnlen(val, sizeof(buf)) >= sizeof(buf))
367 parse_err("parameter too long");
369 strcpy(str, val);
370 kill_final_newline(str);
372 for (i = 0; i < 2; i++)
373 token[i] = strsep(&str, ",");
375 if (str)
376 parse_err("too many arguments");
378 if (!token[0])
379 parse_err("no argument");
381 name = token[0];
382 if (strlen(name) + 1 > 80)
383 parse_err("device name too long");
385 if (token[1]) {
386 ret = parse_num(&erase_size, token[1]);
387 if (ret) {
388 parse_err("illegal erase size");
392 add_device(name, erase_size);
394 return 0;
398 static int block2mtd_setup(const char *val, struct kernel_param *kp)
400 #ifdef MODULE
401 return block2mtd_setup2(val);
402 #else
403 /* If more parameters are later passed in via
404 /sys/module/block2mtd/parameters/block2mtd
405 and block2mtd_init() has already been called,
406 we can parse the argument now. */
408 if (block2mtd_init_called)
409 return block2mtd_setup2(val);
411 /* During early boot stage, we only save the parameters
412 here. We must parse them later: if the param passed
413 from kernel boot command line, block2mtd_setup() is
414 called so early that it is not possible to resolve
415 the device (even kmalloc() fails). Deter that work to
416 block2mtd_setup2(). */
418 strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
420 return 0;
421 #endif
425 module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
426 MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
428 static int __init block2mtd_init(void)
430 int ret = 0;
432 #ifndef MODULE
433 if (strlen(block2mtd_paramline))
434 ret = block2mtd_setup2(block2mtd_paramline);
435 block2mtd_init_called = 1;
436 #endif
438 return ret;
442 static void __devexit block2mtd_exit(void)
444 struct list_head *pos, *next;
446 /* Remove the MTD devices */
447 list_for_each_safe(pos, next, &blkmtd_device_list) {
448 struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
449 block2mtd_sync(&dev->mtd);
450 mtd_device_unregister(&dev->mtd);
451 INFO("mtd%d: [%s] removed", dev->mtd.index,
452 dev->mtd.name + strlen("block2mtd: "));
453 list_del(&dev->list);
454 block2mtd_free_device(dev);
459 module_init(block2mtd_init);
460 module_exit(block2mtd_exit);
462 MODULE_LICENSE("GPL");
463 MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
464 MODULE_DESCRIPTION("Emulate an MTD using a block device");