Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/devfs-2.6
[linux-2.6] / drivers / mtd / devices / block2mtd.c
1 /*
2  * $Id: block2mtd.c,v 1.30 2005/11/29 14:48:32 gleixner Exp $
3  *
4  * block2mtd.c - create an mtd from a block device
5  *
6  * Copyright (C) 2001,2002      Simon Evans <spse@secret.org.uk>
7  * Copyright (C) 2004-2006      Jörn Engel <joern@wh.fh-wedel.de>
8  *
9  * Licence: GPL
10  */
11 #include <linux/config.h>
12 #include <linux/module.h>
13 #include <linux/fs.h>
14 #include <linux/blkdev.h>
15 #include <linux/bio.h>
16 #include <linux/pagemap.h>
17 #include <linux/list.h>
18 #include <linux/init.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/buffer_head.h>
21 #include <linux/mutex.h>
22
23 #define VERSION "$Revision: 1.30 $"
24
25
26 #define ERROR(fmt, args...) printk(KERN_ERR "block2mtd: " fmt "\n" , ## args)
27 #define INFO(fmt, args...) printk(KERN_INFO "block2mtd: " fmt "\n" , ## args)
28
29
30 /* Info for the block device */
31 struct block2mtd_dev {
32         struct list_head list;
33         struct block_device *blkdev;
34         struct mtd_info mtd;
35         struct mutex write_mutex;
36 };
37
38
39 /* Static info about the MTD, used in cleanup_module */
40 static LIST_HEAD(blkmtd_device_list);
41
42
43 #define PAGE_READAHEAD 64
44 static void cache_readahead(struct address_space *mapping, int index)
45 {
46         filler_t *filler = (filler_t*)mapping->a_ops->readpage;
47         int i, pagei;
48         unsigned ret = 0;
49         unsigned long end_index;
50         struct page *page;
51         LIST_HEAD(page_pool);
52         struct inode *inode = mapping->host;
53         loff_t isize = i_size_read(inode);
54
55         if (!isize) {
56                 INFO("iSize=0 in cache_readahead\n");
57                 return;
58         }
59
60         end_index = ((isize - 1) >> PAGE_CACHE_SHIFT);
61
62         read_lock_irq(&mapping->tree_lock);
63         for (i = 0; i < PAGE_READAHEAD; i++) {
64                 pagei = index + i;
65                 if (pagei > end_index) {
66                         INFO("Overrun end of disk in cache readahead\n");
67                         break;
68                 }
69                 page = radix_tree_lookup(&mapping->page_tree, pagei);
70                 if (page && (!i))
71                         break;
72                 if (page)
73                         continue;
74                 read_unlock_irq(&mapping->tree_lock);
75                 page = page_cache_alloc_cold(mapping);
76                 read_lock_irq(&mapping->tree_lock);
77                 if (!page)
78                         break;
79                 page->index = pagei;
80                 list_add(&page->lru, &page_pool);
81                 ret++;
82         }
83         read_unlock_irq(&mapping->tree_lock);
84         if (ret)
85                 read_cache_pages(mapping, &page_pool, filler, NULL);
86 }
87
88
89 static struct page* page_readahead(struct address_space *mapping, int index)
90 {
91         filler_t *filler = (filler_t*)mapping->a_ops->readpage;
92         cache_readahead(mapping, index);
93         return read_cache_page(mapping, index, filler, NULL);
94 }
95
96
97 /* erase a specified part of the device */
98 static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
99 {
100         struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
101         struct page *page;
102         int index = to >> PAGE_SHIFT;   // page index
103         int pages = len >> PAGE_SHIFT;
104         u_long *p;
105         u_long *max;
106
107         while (pages) {
108                 page = page_readahead(mapping, index);
109                 if (!page)
110                         return -ENOMEM;
111                 if (IS_ERR(page))
112                         return PTR_ERR(page);
113
114                 max = (u_long*)page_address(page) + PAGE_SIZE;
115                 for (p=(u_long*)page_address(page); p<max; p++)
116                         if (*p != -1UL) {
117                                 lock_page(page);
118                                 memset(page_address(page), 0xff, PAGE_SIZE);
119                                 set_page_dirty(page);
120                                 unlock_page(page);
121                                 break;
122                         }
123
124                 page_cache_release(page);
125                 pages--;
126                 index++;
127         }
128         return 0;
129 }
130 static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
131 {
132         struct block2mtd_dev *dev = mtd->priv;
133         size_t from = instr->addr;
134         size_t len = instr->len;
135         int err;
136
137         instr->state = MTD_ERASING;
138         mutex_lock(&dev->write_mutex);
139         err = _block2mtd_erase(dev, from, len);
140         mutex_unlock(&dev->write_mutex);
141         if (err) {
142                 ERROR("erase failed err = %d", err);
143                 instr->state = MTD_ERASE_FAILED;
144         } else
145                 instr->state = MTD_ERASE_DONE;
146
147         instr->state = MTD_ERASE_DONE;
148         mtd_erase_callback(instr);
149         return err;
150 }
151
152
153 static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
154                 size_t *retlen, u_char *buf)
155 {
156         struct block2mtd_dev *dev = mtd->priv;
157         struct page *page;
158         int index = from >> PAGE_SHIFT;
159         int offset = from & (PAGE_SIZE-1);
160         int cpylen;
161
162         if (from > mtd->size)
163                 return -EINVAL;
164         if (from + len > mtd->size)
165                 len = mtd->size - from;
166
167         if (retlen)
168                 *retlen = 0;
169
170         while (len) {
171                 if ((offset + len) > PAGE_SIZE)
172                         cpylen = PAGE_SIZE - offset;    // multiple pages
173                 else
174                         cpylen = len;   // this page
175                 len = len - cpylen;
176
177                 //      Get page
178                 page = page_readahead(dev->blkdev->bd_inode->i_mapping, index);
179                 if (!page)
180                         return -ENOMEM;
181                 if (IS_ERR(page))
182                         return PTR_ERR(page);
183
184                 memcpy(buf, page_address(page) + offset, cpylen);
185                 page_cache_release(page);
186
187                 if (retlen)
188                         *retlen += cpylen;
189                 buf += cpylen;
190                 offset = 0;
191                 index++;
192         }
193         return 0;
194 }
195
196
197 /* write data to the underlying device */
198 static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
199                 loff_t to, size_t len, size_t *retlen)
200 {
201         struct page *page;
202         struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
203         int index = to >> PAGE_SHIFT;   // page index
204         int offset = to & ~PAGE_MASK;   // page offset
205         int cpylen;
206
207         if (retlen)
208                 *retlen = 0;
209         while (len) {
210                 if ((offset+len) > PAGE_SIZE)
211                         cpylen = PAGE_SIZE - offset;    // multiple pages
212                 else
213                         cpylen = len;                   // this page
214                 len = len - cpylen;
215
216                 //      Get page
217                 page = page_readahead(mapping, index);
218                 if (!page)
219                         return -ENOMEM;
220                 if (IS_ERR(page))
221                         return PTR_ERR(page);
222
223                 if (memcmp(page_address(page)+offset, buf, cpylen)) {
224                         lock_page(page);
225                         memcpy(page_address(page) + offset, buf, cpylen);
226                         set_page_dirty(page);
227                         unlock_page(page);
228                 }
229                 page_cache_release(page);
230
231                 if (retlen)
232                         *retlen += cpylen;
233
234                 buf += cpylen;
235                 offset = 0;
236                 index++;
237         }
238         return 0;
239 }
240 static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
241                 size_t *retlen, const u_char *buf)
242 {
243         struct block2mtd_dev *dev = mtd->priv;
244         int err;
245
246         if (!len)
247                 return 0;
248         if (to >= mtd->size)
249                 return -ENOSPC;
250         if (to + len > mtd->size)
251                 len = mtd->size - to;
252
253         mutex_lock(&dev->write_mutex);
254         err = _block2mtd_write(dev, buf, to, len, retlen);
255         mutex_unlock(&dev->write_mutex);
256         if (err > 0)
257                 err = 0;
258         return err;
259 }
260
261
262 /* sync the device - wait until the write queue is empty */
263 static void block2mtd_sync(struct mtd_info *mtd)
264 {
265         struct block2mtd_dev *dev = mtd->priv;
266         sync_blockdev(dev->blkdev);
267         return;
268 }
269
270
271 static void block2mtd_free_device(struct block2mtd_dev *dev)
272 {
273         if (!dev)
274                 return;
275
276         kfree(dev->mtd.name);
277
278         if (dev->blkdev) {
279                 invalidate_inode_pages(dev->blkdev->bd_inode->i_mapping);
280                 close_bdev_excl(dev->blkdev);
281         }
282
283         kfree(dev);
284 }
285
286
287 /* FIXME: ensure that mtd->size % erase_size == 0 */
288 static struct block2mtd_dev *add_device(char *devname, int erase_size)
289 {
290         struct block_device *bdev;
291         struct block2mtd_dev *dev;
292
293         if (!devname)
294                 return NULL;
295
296         dev = kmalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
297         if (!dev)
298                 return NULL;
299         memset(dev, 0, sizeof(*dev));
300
301         /* Get a handle on the device */
302         bdev = open_bdev_excl(devname, O_RDWR, NULL);
303         if (IS_ERR(bdev)) {
304                 ERROR("error: cannot open device %s", devname);
305                 goto devinit_err;
306         }
307         dev->blkdev = bdev;
308
309         if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
310                 ERROR("attempting to use an MTD device as a block device");
311                 goto devinit_err;
312         }
313
314         mutex_init(&dev->write_mutex);
315
316         /* Setup the MTD structure */
317         /* make the name contain the block device in */
318         dev->mtd.name = kmalloc(sizeof("block2mtd: ") + strlen(devname),
319                         GFP_KERNEL);
320         if (!dev->mtd.name)
321                 goto devinit_err;
322
323         sprintf(dev->mtd.name, "block2mtd: %s", devname);
324
325         dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
326         dev->mtd.erasesize = erase_size;
327         dev->mtd.writesize = 1;
328         dev->mtd.type = MTD_RAM;
329         dev->mtd.flags = MTD_CAP_RAM;
330         dev->mtd.erase = block2mtd_erase;
331         dev->mtd.write = block2mtd_write;
332         dev->mtd.writev = default_mtd_writev;
333         dev->mtd.sync = block2mtd_sync;
334         dev->mtd.read = block2mtd_read;
335         dev->mtd.priv = dev;
336         dev->mtd.owner = THIS_MODULE;
337
338         if (add_mtd_device(&dev->mtd)) {
339                 /* Device didnt get added, so free the entry */
340                 goto devinit_err;
341         }
342         list_add(&dev->list, &blkmtd_device_list);
343         INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev->mtd.index,
344                         dev->mtd.name + strlen("blkmtd: "),
345                         dev->mtd.erasesize >> 10, dev->mtd.erasesize);
346         return dev;
347
348 devinit_err:
349         block2mtd_free_device(dev);
350         return NULL;
351 }
352
353
354 /* This function works similar to reguler strtoul.  In addition, it
355  * allows some suffixes for a more human-readable number format:
356  * ki, Ki, kiB, KiB     - multiply result with 1024
357  * Mi, MiB              - multiply result with 1024^2
358  * Gi, GiB              - multiply result with 1024^3
359  */
360 static int ustrtoul(const char *cp, char **endp, unsigned int base)
361 {
362         unsigned long result = simple_strtoul(cp, endp, base);
363         switch (**endp) {
364         case 'G' :
365                 result *= 1024;
366         case 'M':
367                 result *= 1024;
368         case 'K':
369         case 'k':
370                 result *= 1024;
371         /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
372                 if ((*endp)[1] == 'i') {
373                         if ((*endp)[2] == 'B')
374                                 (*endp) += 3;
375                         else
376                                 (*endp) += 2;
377                 }
378         }
379         return result;
380 }
381
382
383 static int parse_num(size_t *num, const char *token)
384 {
385         char *endp;
386         size_t n;
387
388         n = (size_t) ustrtoul(token, &endp, 0);
389         if (*endp)
390                 return -EINVAL;
391
392         *num = n;
393         return 0;
394 }
395
396
397 static int parse_name(char **pname, const char *token, size_t limit)
398 {
399         size_t len;
400         char *name;
401
402         len = strlen(token) + 1;
403         if (len > limit)
404                 return -ENOSPC;
405
406         name = kmalloc(len, GFP_KERNEL);
407         if (!name)
408                 return -ENOMEM;
409
410         strcpy(name, token);
411
412         *pname = name;
413         return 0;
414 }
415
416
417 static inline void kill_final_newline(char *str)
418 {
419         char *newline = strrchr(str, '\n');
420         if (newline && !newline[1])
421                 *newline = 0;
422 }
423
424
425 #define parse_err(fmt, args...) do {            \
426         ERROR("block2mtd: " fmt "\n", ## args); \
427         return 0;                               \
428 } while (0)
429
430 static int block2mtd_setup(const char *val, struct kernel_param *kp)
431 {
432         char buf[80+12]; /* 80 for device, 12 for erase size */
433         char *str = buf;
434         char *token[2];
435         char *name;
436         size_t erase_size = PAGE_SIZE;
437         int i, ret;
438
439         if (strnlen(val, sizeof(buf)) >= sizeof(buf))
440                 parse_err("parameter too long");
441
442         strcpy(str, val);
443         kill_final_newline(str);
444
445         for (i = 0; i < 2; i++)
446                 token[i] = strsep(&str, ",");
447
448         if (str)
449                 parse_err("too many arguments");
450
451         if (!token[0])
452                 parse_err("no argument");
453
454         ret = parse_name(&name, token[0], 80);
455         if (ret == -ENOMEM)
456                 parse_err("out of memory");
457         if (ret == -ENOSPC)
458                 parse_err("name too long");
459         if (ret)
460                 return 0;
461
462         if (token[1]) {
463                 ret = parse_num(&erase_size, token[1]);
464                 if (ret) {
465                         kfree(name);
466                         parse_err("illegal erase size");
467                 }
468         }
469
470         add_device(name, erase_size);
471
472         return 0;
473 }
474
475
476 module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
477 MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
478
479 static int __init block2mtd_init(void)
480 {
481         INFO("version " VERSION);
482         return 0;
483 }
484
485
486 static void __devexit block2mtd_exit(void)
487 {
488         struct list_head *pos, *next;
489
490         /* Remove the MTD devices */
491         list_for_each_safe(pos, next, &blkmtd_device_list) {
492                 struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
493                 block2mtd_sync(&dev->mtd);
494                 del_mtd_device(&dev->mtd);
495                 INFO("mtd%d: [%s] removed", dev->mtd.index,
496                                 dev->mtd.name + strlen("blkmtd: "));
497                 list_del(&dev->list);
498                 block2mtd_free_device(dev);
499         }
500 }
501
502
503 module_init(block2mtd_init);
504 module_exit(block2mtd_exit);
505
506 MODULE_LICENSE("GPL");
507 MODULE_AUTHOR("Simon Evans <spse@secret.org.uk> and others");
508 MODULE_DESCRIPTION("Emulate an MTD using a block device");