[PATCH] W1: w1_netlink: New init/fini netlink callbacks.
[linux-2.6/verdex.git] / drivers / mtd / devices / block2mtd.c
blob4a7a805e7564c4d359f9049daf1ebc54a284c39b
1 /*
2 * $Id: block2mtd.c,v 1.28 2005/03/19 22:40:44 gleixner Exp $
4 * block2mtd.c - create an mtd from a block device
6 * Copyright (C) 2001,2002 Simon Evans <spse@secret.org.uk>
7 * Copyright (C) 2004,2005 Jörn Engel <joern@wh.fh-wedel.de>
9 * Licence: GPL
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>
22 #define VERSION "$Revision: 1.28 $"
25 #define ERROR(fmt, args...) printk(KERN_ERR "block2mtd: " fmt "\n" , ## args)
26 #define INFO(fmt, args...) printk(KERN_INFO "block2mtd: " fmt "\n" , ## args)
29 /* Info for the block device */
30 struct block2mtd_dev {
31 struct list_head list;
32 struct block_device *blkdev;
33 struct mtd_info mtd;
34 struct semaphore write_mutex;
38 /* Static info about the MTD, used in cleanup_module */
39 static LIST_HEAD(blkmtd_device_list);
42 #define PAGE_READAHEAD 64
43 void cache_readahead(struct address_space *mapping, int index)
45 filler_t *filler = (filler_t*)mapping->a_ops->readpage;
46 int i, pagei;
47 unsigned ret = 0;
48 unsigned long end_index;
49 struct page *page;
50 LIST_HEAD(page_pool);
51 struct inode *inode = mapping->host;
52 loff_t isize = i_size_read(inode);
54 if (!isize) {
55 INFO("iSize=0 in cache_readahead\n");
56 return;
59 end_index = ((isize - 1) >> PAGE_CACHE_SHIFT);
61 read_lock_irq(&mapping->tree_lock);
62 for (i = 0; i < PAGE_READAHEAD; i++) {
63 pagei = index + i;
64 if (pagei > end_index) {
65 INFO("Overrun end of disk in cache readahead\n");
66 break;
68 page = radix_tree_lookup(&mapping->page_tree, pagei);
69 if (page && (!i))
70 break;
71 if (page)
72 continue;
73 read_unlock_irq(&mapping->tree_lock);
74 page = page_cache_alloc_cold(mapping);
75 read_lock_irq(&mapping->tree_lock);
76 if (!page)
77 break;
78 page->index = pagei;
79 list_add(&page->lru, &page_pool);
80 ret++;
82 read_unlock_irq(&mapping->tree_lock);
83 if (ret)
84 read_cache_pages(mapping, &page_pool, filler, NULL);
88 static struct page* page_readahead(struct address_space *mapping, int index)
90 filler_t *filler = (filler_t*)mapping->a_ops->readpage;
91 cache_readahead(mapping, index);
92 return read_cache_page(mapping, index, filler, NULL);
96 /* erase a specified part of the device */
97 static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
99 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
100 struct page *page;
101 int index = to >> PAGE_SHIFT; // page index
102 int pages = len >> PAGE_SHIFT;
103 u_long *p;
104 u_long *max;
106 while (pages) {
107 page = page_readahead(mapping, index);
108 if (!page)
109 return -ENOMEM;
110 if (IS_ERR(page))
111 return PTR_ERR(page);
113 max = (u_long*)page_address(page) + PAGE_SIZE;
114 for (p=(u_long*)page_address(page); p<max; p++)
115 if (*p != -1UL) {
116 lock_page(page);
117 memset(page_address(page), 0xff, PAGE_SIZE);
118 set_page_dirty(page);
119 unlock_page(page);
120 break;
123 page_cache_release(page);
124 pages--;
125 index++;
127 return 0;
129 static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
131 struct block2mtd_dev *dev = mtd->priv;
132 size_t from = instr->addr;
133 size_t len = instr->len;
134 int err;
136 instr->state = MTD_ERASING;
137 down(&dev->write_mutex);
138 err = _block2mtd_erase(dev, from, len);
139 up(&dev->write_mutex);
140 if (err) {
141 ERROR("erase failed err = %d", err);
142 instr->state = MTD_ERASE_FAILED;
143 } else
144 instr->state = MTD_ERASE_DONE;
146 instr->state = MTD_ERASE_DONE;
147 mtd_erase_callback(instr);
148 return err;
152 static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
153 size_t *retlen, u_char *buf)
155 struct block2mtd_dev *dev = mtd->priv;
156 struct page *page;
157 int index = from >> PAGE_SHIFT;
158 int offset = from & (PAGE_SIZE-1);
159 int cpylen;
161 if (from > mtd->size)
162 return -EINVAL;
163 if (from + len > mtd->size)
164 len = mtd->size - from;
166 if (retlen)
167 *retlen = 0;
169 while (len) {
170 if ((offset + len) > PAGE_SIZE)
171 cpylen = PAGE_SIZE - offset; // multiple pages
172 else
173 cpylen = len; // this page
174 len = len - cpylen;
176 // Get page
177 page = page_readahead(dev->blkdev->bd_inode->i_mapping, index);
178 if (!page)
179 return -ENOMEM;
180 if (IS_ERR(page))
181 return PTR_ERR(page);
183 memcpy(buf, page_address(page) + offset, cpylen);
184 page_cache_release(page);
186 if (retlen)
187 *retlen += cpylen;
188 buf += cpylen;
189 offset = 0;
190 index++;
192 return 0;
196 /* write data to the underlying device */
197 static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
198 loff_t to, size_t len, size_t *retlen)
200 struct page *page;
201 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
202 int index = to >> PAGE_SHIFT; // page index
203 int offset = to & ~PAGE_MASK; // page offset
204 int cpylen;
206 if (retlen)
207 *retlen = 0;
208 while (len) {
209 if ((offset+len) > PAGE_SIZE)
210 cpylen = PAGE_SIZE - offset; // multiple pages
211 else
212 cpylen = len; // this page
213 len = len - cpylen;
215 // Get page
216 page = page_readahead(mapping, index);
217 if (!page)
218 return -ENOMEM;
219 if (IS_ERR(page))
220 return PTR_ERR(page);
222 if (memcmp(page_address(page)+offset, buf, cpylen)) {
223 lock_page(page);
224 memcpy(page_address(page) + offset, buf, cpylen);
225 set_page_dirty(page);
226 unlock_page(page);
228 page_cache_release(page);
230 if (retlen)
231 *retlen += cpylen;
233 buf += cpylen;
234 offset = 0;
235 index++;
237 return 0;
239 static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
240 size_t *retlen, const u_char *buf)
242 struct block2mtd_dev *dev = mtd->priv;
243 int err;
245 if (!len)
246 return 0;
247 if (to >= mtd->size)
248 return -ENOSPC;
249 if (to + len > mtd->size)
250 len = mtd->size - to;
252 down(&dev->write_mutex);
253 err = _block2mtd_write(dev, buf, to, len, retlen);
254 up(&dev->write_mutex);
255 if (err > 0)
256 err = 0;
257 return err;
261 /* sync the device - wait until the write queue is empty */
262 static void block2mtd_sync(struct mtd_info *mtd)
264 struct block2mtd_dev *dev = mtd->priv;
265 sync_blockdev(dev->blkdev);
266 return;
270 static void block2mtd_free_device(struct block2mtd_dev *dev)
272 if (!dev)
273 return;
275 kfree(dev->mtd.name);
277 if (dev->blkdev) {
278 invalidate_inode_pages(dev->blkdev->bd_inode->i_mapping);
279 close_bdev_excl(dev->blkdev);
282 kfree(dev);
286 /* FIXME: ensure that mtd->size % erase_size == 0 */
287 static struct block2mtd_dev *add_device(char *devname, int erase_size)
289 struct block_device *bdev;
290 struct block2mtd_dev *dev;
292 if (!devname)
293 return NULL;
295 dev = kmalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
296 if (!dev)
297 return NULL;
298 memset(dev, 0, sizeof(*dev));
300 /* Get a handle on the device */
301 bdev = open_bdev_excl(devname, O_RDWR, NULL);
302 if (IS_ERR(bdev)) {
303 ERROR("error: cannot open device %s", devname);
304 goto devinit_err;
306 dev->blkdev = bdev;
308 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
309 ERROR("attempting to use an MTD device as a block device");
310 goto devinit_err;
313 init_MUTEX(&dev->write_mutex);
315 /* Setup the MTD structure */
316 /* make the name contain the block device in */
317 dev->mtd.name = kmalloc(sizeof("block2mtd: ") + strlen(devname),
318 GFP_KERNEL);
319 if (!dev->mtd.name)
320 goto devinit_err;
322 sprintf(dev->mtd.name, "block2mtd: %s", devname);
324 dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
325 dev->mtd.erasesize = erase_size;
326 dev->mtd.type = MTD_RAM;
327 dev->mtd.flags = MTD_CAP_RAM;
328 dev->mtd.erase = block2mtd_erase;
329 dev->mtd.write = block2mtd_write;
330 dev->mtd.writev = default_mtd_writev;
331 dev->mtd.sync = block2mtd_sync;
332 dev->mtd.read = block2mtd_read;
333 dev->mtd.readv = default_mtd_readv;
334 dev->mtd.priv = dev;
335 dev->mtd.owner = THIS_MODULE;
337 if (add_mtd_device(&dev->mtd)) {
338 /* Device didnt get added, so free the entry */
339 goto devinit_err;
341 list_add(&dev->list, &blkmtd_device_list);
342 INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev->mtd.index,
343 dev->mtd.name + strlen("blkmtd: "),
344 dev->mtd.erasesize >> 10, dev->mtd.erasesize);
345 return dev;
347 devinit_err:
348 block2mtd_free_device(dev);
349 return NULL;
353 static int ustrtoul(const char *cp, char **endp, unsigned int base)
355 unsigned long result = simple_strtoul(cp, endp, base);
356 switch (**endp) {
357 case 'G' :
358 result *= 1024;
359 case 'M':
360 result *= 1024;
361 case 'k':
362 result *= 1024;
363 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
364 if ((*endp)[1] == 'i')
365 (*endp) += 2;
367 return result;
371 static int parse_num(size_t *num, const char *token)
373 char *endp;
374 size_t n;
376 n = (size_t) ustrtoul(token, &endp, 0);
377 if (*endp)
378 return -EINVAL;
380 *num = n;
381 return 0;
385 static int parse_name(char **pname, const char *token, size_t limit)
387 size_t len;
388 char *name;
390 len = strlen(token) + 1;
391 if (len > limit)
392 return -ENOSPC;
394 name = kmalloc(len, GFP_KERNEL);
395 if (!name)
396 return -ENOMEM;
398 strcpy(name, token);
400 *pname = name;
401 return 0;
405 static inline void kill_final_newline(char *str)
407 char *newline = strrchr(str, '\n');
408 if (newline && !newline[1])
409 *newline = 0;
413 #define parse_err(fmt, args...) do { \
414 ERROR("block2mtd: " fmt "\n", ## args); \
415 return 0; \
416 } while (0)
418 static int block2mtd_setup(const char *val, struct kernel_param *kp)
420 char buf[80+12], *str=buf; /* 80 for device, 12 for erase size */
421 char *token[2];
422 char *name;
423 size_t erase_size = PAGE_SIZE;
424 int i, ret;
426 if (strnlen(val, sizeof(buf)) >= sizeof(buf))
427 parse_err("parameter too long");
429 strcpy(str, val);
430 kill_final_newline(str);
432 for (i=0; i<2; i++)
433 token[i] = strsep(&str, ",");
435 if (str)
436 parse_err("too many arguments");
438 if (!token[0])
439 parse_err("no argument");
441 ret = parse_name(&name, token[0], 80);
442 if (ret == -ENOMEM)
443 parse_err("out of memory");
444 if (ret == -ENOSPC)
445 parse_err("name too long");
446 if (ret)
447 return 0;
449 if (token[1]) {
450 ret = parse_num(&erase_size, token[1]);
451 if (ret)
452 parse_err("illegal erase size");
455 add_device(name, erase_size);
457 return 0;
461 module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
462 MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
464 static int __init block2mtd_init(void)
466 INFO("version " VERSION);
467 return 0;
471 static void __devexit block2mtd_exit(void)
473 struct list_head *pos, *next;
475 /* Remove the MTD devices */
476 list_for_each_safe(pos, next, &blkmtd_device_list) {
477 struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
478 block2mtd_sync(&dev->mtd);
479 del_mtd_device(&dev->mtd);
480 INFO("mtd%d: [%s] removed", dev->mtd.index,
481 dev->mtd.name + strlen("blkmtd: "));
482 list_del(&dev->list);
483 block2mtd_free_device(dev);
488 module_init(block2mtd_init);
489 module_exit(block2mtd_exit);
491 MODULE_LICENSE("GPL");
492 MODULE_AUTHOR("Simon Evans <spse@secret.org.uk> and others");
493 MODULE_DESCRIPTION("Emulate an MTD using a block device");